Reputation:
My assignment is to create a text-based game where you create three classes. Rooms, Items and Persons. Where Persons can move between Rooms with items in them.
I don't fully understand how I'm supposed to create a connection between the Rooms.
Here's my code so far, I have coded the classes for persons and items aswell but I decided to leave it out as it is not relevant.
public class Game {
public static void main(String[] args) {
//I don't like this part of my code.
Room roomA = new Room();
Room roomB = new Room();
Room roomC = new Room();
Room roomD = new Room();
roomA = new Room(null,roomD,roomB,null);
roomB = new Room(null,null,roomC,roomA);
roomC = new Room(null,null,null,roomB);
roomD = new Room(roomA,null,null,null);
roomA = new Room(null,roomD,roomB,null);
roomB = new Room(null,null,roomC,roomA);
roomC = new Room(null,null,null,roomB);
}
}
class Room {
Room[] exit = new Room[4];
public Room() {
}
public Room(Room north, Room south, Room east, Room west) {
exit[0] = north;
exit[1] = south;
exit[2] = east;
exit[3] = west;
}
}
This seems very complicated for what it's supposed to do. Is there any way to do this differently?
Upvotes: 0
Views: 252
Reputation: 31699
One thing that I think could be a problem is that you are creating 11 rooms. I'm not sure if this is what you want, but if you want to create only 4, this code will have problems. For example:
roomB = new Room(null,null,roomC,roomA);
...
roomA = new Room(null,roomD,roomB,null);
roomB = new Room(null,null,roomC,roomA);
roomB
will be set to a new object. When roomA
is created, it will be set up so that its "east" exit is to the roomB
you created. But later, when you say roomB = new Room(...)
again, you create yet another new object whose "west" exit is roomA
. Note that now you have a "room B" object whose west door leads to "room A", but that same "room A" has an east door that leads to a different "room B" object. Basically you have two (or more) different objects occupying the same space on your grid. I suspect this isn't what you want.
When you're creating new objects that point to each other, you can't do it with "new" alone, I think. Better would be to add a method to Room
like
setExit(Direction direction, Room destination)
where Direction
is an enum
type with like public enum Direction { NORTH, SOUTH, EAST, WEST, }
. Then remove the north
, south
, east
, west
parameters from the constructor. The code to set the rooms up would look like
roomA = new Room();
...
roomD = new Room();
roomA.setExit(Direction.SOUTH, roomD);
roomA.setExit(Direction.EAST, roomB);
roomB.setExit(Direction.EAST, roomC);
roomB.setExit(Direction.WEST, roomA);
... and so on
Now you have only four objects that have exits that may refer to each other, which I think is what you want.
Upvotes: 0
Reputation: 347264
Don't do...
Room roomA = new Room();
Room roomB = new Room();
Room roomC = new Room();
Room roomD = new Room();
roomA = new Room(null,roomD,roomB,null);
//...
You're re-assigning the rooms, which will break their references and make it impossible to navigate.
If you want to make easier to use, you might consider using method chaining...
roomA.southExit(roomD).eastExit(roomB);
which might look something like...
class Room {
Room[] exit = new Room[4];
public Room() {
}
public Room southExit(Room room) {
exit[1] = room;
return this;
}
public Room eastExit(Room room) {
exit[2] = room;
return this;
}
//...
}
Another approach might be to create Map
class, which manages the navigation. The idea might be to create a 2D matrix of rooms, which the Map
can look up a given room and determine which directions a player might be able to move in. A null
cell would mean it's blocked, a non null
cell would be a room the player can move into.
Conceptually, you would be able to draw a gird on a piece of paper and map out the rooms on it, which would provide you with a "visiual" representation of the matrix. You would then need to translate this into code, probably using a 2D array of rooms, but I would wrap it within the Map
class to make it easier to query and manage, but that is me...
I guess it comes down to how much complexity you want...
Upvotes: 2