By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,154 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,154 IT Pros & Developers. It's quick & easy.

2D array in Java

P: 2
Hi guys I question, as I am new to Java I have some problem to create relatonship between classes can you help me please, I have coursework from my module so I need to solve the problem. I dont ask you to do it for me I just ask if someone can guide to me like explain what kind of relationship need to be between Classes, here is question:

************************************************** **************************Hi guys , I have the same kind of coursework, i need some help how design it,
sorry guys i am not asking you to do for me i need just the right direction how to do it, i have drawn relationship diagram but i dunno where to put right methods and how create relationship ************************************************** **************************


You are required to write a 'level' editor for a simple game called CharWorld. A level in CharWorld is a room made up from 10 x 10 squares. Each square in the room is referred to by an X and a Y coordinate. Hence the '@' in the room shown below is placed at square (1,3):

0 1 2 3 4 5 6 7 8 9
0 . . . . . . . . . . . .
1 . . . . . . . . . . . .
2 . . . . . . . . . . . .
3 . @ . . . . . . . . .
4 . . . . . . . . . . . .
5 . . . . . . . . . . . .
6 . . . . . . . . . . . .
7 . . . . . . . . . . . .
8 . . . . . . . . . . . .
9 . . . . . . . . . . . .

A room in CharWorld can be populated by two types of entities: inanimate objects and living things. There are two kinds of inanimate objects: obstacles which are represented by 'O' (capital 'o') and treasure chests, which are represented by a '*'. Treasure chests can contain up to 100 gold coins.

Each living thing in CharWorld has a measure of its health, represented using an integer in the range 0 to 100. Living things are divided in to monsters and humans. Monsters are represented in the display by a '#' character whilst humans are depicted by an '@' character. The amount of damage that a particular monster can cause a human depends on its strength which is represented by a number from 1 to 20. Similarly, the amount of damage that a human can cause a monster depends on his or her strength, which is represented by a number in the range 1 – 10. Each human also has a name.

A typical level for a game would look like the following:

0 1 2 3 4 5 6 7 8 9
0 . . . . . . . . . . . .
1 . . . . . # . . . . .
2 . . . O . . . . . . .
3 . @ . O . . . * . .
4 . . . O . . . . . . .
5 . . O O . . . . . .
6 . . . . . . . @ . . .
7 # . . . . . . . . . . .
8 . . . # . . . . . . . .
9 . . . . . . . . . . . .

This shows 1 treasure chest, 2 humans, 3 monsters and 5 obstacles on this level. Note that a full stop is used to denote an unoccupied square in the level.

Write an object oriented program which will enable a level designer to add and delete entities to and from a given level. Initially, the level will have no entities in it. Your program should be controlled using a simple console based menu similar to the one shown below:

Menu:
1 Add a human
2 Add a monster
3 Add an obstacle
4 Add a treasure chest
5 Delete an entity
6 Display level
7 Display the properties of an entity
8 Print menu
9 Exit

When adding an entity to the level, the user should be prompted for the X and Y coordinates of the square on which to place the entity. Note that an entity can only be placed on an empty square. Note also that the user should be prompted for the relevant details to be entered depending on which entity is being added (e.g. to add a human, the user should be prompted to enter the human’s name, their health as an intiger in the range 0 to100 and their strength as an integer in the range of 1 to 10).

When deleting an entity, it is only necessary to identify the square the entity occupies using its X and Y coordinate.

The 'Display level' option should display the current contents of the room to the console output using a format similar to that shown above.

When item 7 on the menu is selected, the user should be prompted for the X and Y coordinates of the square on which the entity in question is located. Your program should then display all the details of the entity on that square. For example, if square (6,7) was selected from the display shown at the top of this page, then the output might be as follows:

Entity properties:
Type: human
Name: Harold
Health: 47
Strength 5

If there is no entity on that square then your program should output a suitable message

************************************************** ****************************
anyone can help me please?

Thanx in advance

Charlie
Oct 24 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
Design a little class hierarchy that models living and dead things; humans, monsters,
treasures and obstacles. All of them are 'Things' which makes up a nice interface.

Your rooms are simply two dimensional arrays of Things. You end up with something
like this:

Expand|Select|Wrap|Line Numbers
  1. public interface Thing {
  2.    public int getStrength();
  3.    public void setStrength(int strengh);
  4.    public int getHealth();
  5.    public void setHealth(int health);
  6.    public String getName();
  7. }
  8. public abstract class AbstractThing implements Thing {
  9.    private String name;
  10.    public AbstractThing(String name) { this.name= name; }
  11.    public String getName() { return name; }
  12. }
  13. public abstract class DeadThing extends AbstractThing {
  14.    public DeadThing(String name) { super(name); }
  15.    public int getStrenght() { return 0; }
  16.    public int getHealth() { return 0; }
  17.  
  18.    // dead things don't have a health nor a strength
  19.    public void setStrength(int strength) { }
  20.    public void setHealth(int health) { }
  21. }
  22. public class Obstacle extends DeadThing {
  23.    public Obstacle() { super("obstacle"); }
  24. }
  25. // etc. etc.
  26.  
I'm sure you can handle it from here.

kind regards,

Jos
Oct 24 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.