Hey, new to java, but trying to build a text-based adventure game similar in gameplay style to the original Zork series.

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();


            room = nav.setRoom(roomNumber);

            while(roomNumber == 0 && exit == false)
                room = nav.setRoom(roomNumber);
                command = getInput(input);

                case "go north":
                case "go n":
                case "n":
                    roomNumber = 1;
                case "exit":
                case "x":
                    exit = true;
                    nav.commandHandler(command, roomNumber, exit);


the commandHandler method referred to is here:

public void commandHandler(String command, int currentRoom, boolean exit)
        Location room = setRoom(currentRoom);

        case "look":
        case "look up":
        case "look u":
        case "l up":
        case "l u":
        case "look down":
        case "look d":
        case "l down":
        case "l d":
        case "look north":
        case "look n":
        case "l north":
        case "l n":
        case "look east":
        case "look e":
        case "l east":
        case "l e":
        case "look south":
        case "look s":
        case "l south":
        case "l s":
        case "look west":
        case "look w":
        case "l west":
        case "l w":

Is there better way to do this than trying to anticipate every possible input by the user and adding it to the switch?

One possible solutions could be
1. create a command interpreter. In this case all the commands like "n" or "north" would give back an int (eg.

private final statice COMMAND_WAS_GO_NORTH = 1;
         private final static COMMAND_WAS_GO_EAST = 2;
         private int interpretCommand(String command) {
                switch command:
                    case "north": return(COMMAND_WAS_GO_NORTH);

You can also create a dictionary and compare subsrtings of the command to it;
eg. you create objects with a String for the commandword
and the commandID for the command
eg, commandWord = "north" and commandID=COMMAND_WAS_GO_NORTH
So you can have multiple words with the same commandID in a database (file)
In the command interpreter you check all of the words to get the commandID and later
you do whatever you want to do based on the commandID (and you dont use the real word)
You can create a multi-input if you don't want to create substrings from the command line:
eg. First input line is: Do what? answer could be: north, south, take, fight
Second input line is: specify it? lamp, orc
in the end you will get a commandID for action, and a specificID, so you can check
whether the command is valid in this case or not

  1. create an array of valid movements for every room object

    then check it
    targetRoom = Room[currentRoom].validMovement(interpretCommand(usersCommand));
    if (targetRoom != null) currentRoom = targetRoom;

thanks for the help! I will try that and report back when i have it working!

Long time ago, when we were taugh basic OOP we could choose to re-create old, not oop programs. One of my classmates exam was to write a basic text-based rpg. He used an old book: "Frank Dacosta: Writing BASIC Adventure Programs for the TRS-80" (published in 1982). Although it was a veeery old book (TRS-80 computers had only 48K(!) memory) and the example program in the book was written in Basic, but if you take the basic ideas you can convert them into some kind OOP - although I don't know how hard or how useful would be nowadays to use these basic ideas.

Unfortunately I don't know any new book about this topic (maybe somebody can if ask for it). Maybe the book isn't wort buying, but because it is quite old, maybe you can find a copy in a library or in some countries maybe you can download legally a free copy. (I don't know the copyright laws in your country, so check it...)

I have found a way to make the navigation system work, along with compressing it.
I taught myself the String commands i needed, and went from there.
I will post a link to a gitHub repository with my code tomorrow... upwards of 500 lines of code in use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.