I’m working on a text-based adventure game and I’m trying to externalise as much of the setup as possible so that I can work with a friend who wants to write the storyline. I have three classes so far. A room class with a title, description, and an array of Exits. Also an Exit class with the following constructor.
public Exit(int direction, Room connection);
Exits also have public int variables referring to the different directions:
public static final int NORTH = 0;
This is all so I can set up an exit on a room by saying:
Room r = new Room("Title","Description");
Room r2 = new Room("Title", "Description");
r.addExit(new Exit(Exit.NORTH, r2);
This would make an exit on the room r that is on the north side and leads to the room r2. Now for the externalisation I’m trying to make a .txt file where I can simply put the current room number, the exit direction (string), and the room number it leads to.
I can do this just fine as far as reading the file goes but where I’m struggling is when I’m setting up the direction, I can’t say
Exit. /*String read from file*/
So how can I access those public integers from the Exit class using a string from the txt file?
The quick and dirty fix is to implement a method to do string comparison and return the appropriate constant. But you should also consider using an
enumas suggested by BRPocock, it’s a much cleaner way to do it.