Hey, new to java, but trying to build a text-based adventure game similar in gameplay style to the original Zork series.
http://pot.home.xs4all.nl/infocom/zork1.html
This is not homework. It started as a way to practice what i've learned, but now I am determined to make this work with the simplest code possible.
Here's my question. I have been leaning heavily on switching for the navigation system, and am looking for a simpler solution.
Here's what my navigation test looks like so far
public static void main(String[] args)
{
int roomNumber = 0;
boolean exit = false;
String command;
Scanner input = new Scanner(System.in);
Navigation nav = new Navigation();
printTitle();
do{
room = nav.setRoom(roomNumber);
while(roomNumber == 0 && exit == false)
{
room = nav.setRoom(roomNumber);
nav.enterRoom(roomNumber);
command = getInput(input);
switch(command)
{
case "go north":
case "go n":
case "n":
roomNumber = 1;
break;
case "exit":
case "x":
exit = true;
break;
default:
nav.commandHandler(command, roomNumber, exit);
break;
}
}
the commandHandler method referred to is here:
public void commandHandler(String command, int currentRoom, boolean exit)
{
Location room = setRoom(currentRoom);
switch(command)
{
case "look":
room.printFullDesc();
break;
case "look up":
case "look u":
case "l up":
case "l u":
room.printUpDescription();
break;
case "look down":
case "look d":
case "l down":
case "l d":
room.printDownDescription();
break;
case "look north":
case "look n":
case "l north":
case "l n":
room.printNorthDescription();
break;
case "look east":
case "look e":
case "l east":
case "l e":
room.printEastDescription();
break;
case "look south":
case "look s":
case "l south":
case "l s":
room.printSouthDescription();
break;
case "look west":
case "look w":
case "l west":
case "l w":
room.printWestDescription();
break;
}
Is there better way to do this than trying to anticipate every possible input by the user and adding it to the switch?