This may be a duplicate question as I don\'t know to phrase the search query. I\'m creating a Zork-like text based game in Java where the character moves to different rooms whic
The first thing to decide is what constitutes a valid direction: is it from a fixed list or can it be freeform text? The simplest solution is to have the four cardinal directions. Some have suggested doing this as an int array. That might be a valid solution in C/C++/C# (enums in all of them are just int constants) but there is no reason to do it in Java.
In Java you can use a (typesafe) enum—which incidentally can have state and behaviour—and use an EnumMap, which is highly efficient. Internally it's just an array indexed by enum ordinal value. You might argue what's the difference between that and an int array? The answer is that the int array inside EnumMap
is an internal implementation detail for a typesafe random access collection.
If you allow freeform text for the exit direction, your structure will look something like this:
Map exits;
I don't recommend this however. I recommend enumerating possible directions:
public enum Direction {
NORTH("north", "n"),
NORTHWEST("northwest", "nw"),
...
IN("in"),
OUT("out");
private final static Map INSTANCES;
static {
Map map = new HashMap();
for (Direction direction : values()) {
for (String exit : direction.exits) {
if (map.containsKey(exit)) {
throw new IllegalStateException("Exit '" + exit + "' duplicated");
}
map.put(exit, direction);
}
}
INSTANCES = Collections.unmodifiableMap(map);
}
private final List exits;
Direction(String... exits) {
this.exits = Collections.unmodifiableList(Arrays.asList(exits));
}
public List getExits() { return exits; }
public String getName() { return exits.get(0); }
public static Map getInstances() { return INSTANCES; }
public static Direction getDirection(String exit) { return INSTANCES.get(exit); }
}
which you then store with:
private final Map exits =
new EnumMap(Direction.class);
That gives you type-safety, performance and extensibility.
The first way to think about this is as a Map:
Map exits;
where the key is a freeform direction (north, east, south, etc).
Next question: what is an Exit? In the simplest case, an Exit is simply what Room you end up in but then you start asking all sorts of questions like:
It is necessary to consider the interface for a text adventure game. A player types in commands in the following form:
Verb [[preposition1] object1 [[preposition2] object2]]
That's one possibility at least. Examples would include:
So the above covers a fairly comprehensive set of behaviour. The point of all this is:
so:
public enum Command { LOOK, HIT, WAVE, OPEN, CLOSE, ... };
(and there will no doubt be behaviour associated with those instances) and:
public class GameObject {
boolean isSupported(Command command);
boolean trigger(Command command);
}
public class Exit extends GameObject {
...
}
GameObjects may also have other state such as whether they can be seen or not. Interestingly, the Direction enum instances are also arguably Commands, which again changes the abstraction.
So hopefully that should help point you in the right direction. There is no "right" answer for the abstraction because it all depends on what you need to model and support. This should hopefully give you a starting point however.