Search code examples
javaparsingtext-based

Java: Selecting object in text based game


I've been working on a text based game for class and have been able to get most of it working with a little trial and error. But I've run into a roadblock with the user input parsing, which I've already had to simplify more than I like. The idea is to have a method that takes a string as the input, then calls a method in an object that was instantiated in another class. Here is the code, and yes, I know it's a convoluted mess.

It seems that I phrased this question a bit badly. I haven't actually encountered any problem beyond simply having no idea how to do what I want to. The approach suggested by Gilbert La Blanc was very good though, and has solved my problem. Maybe I actually will pass this final! Thank you all for your help.

import java.util.Scanner;

public class Parser{
static String[] Prepositions = {"on", "in", "under"};
static String Word;

public static void Parse(String Raw){
    Raw.toLowerCase(); //Make input lower case

    Scanner Splitter = new Scanner(Raw).useDelimiter(" "); //Split into seperate words
    Word = Splitter.next(); //Get first word
    Splitter.close();
    CheckFirst(Word);
}

public static void CheckFirst(String Word){
    switch(Word){
        case "north":
        case "n":
        case "south":
        case "s":
        case "east":
        case "e":
        case "west":
        case "w":
            Player.Location.LeaveRoom(Word);
            break;
        case "take": TakeWhat(); break;
        default: System.out.print("That was absolute gibberish!"); break;
    }
}

public static void TakeWhat(){
    System.out.println("Take what?");
    System.out.print("> ");
    String ToTake = Game.Input.next();
    ToTake.toLowerCase();

    switch(ToTake){
    case "key": break;
    default: TakeWhat();
    }
}

}


Solution

  • You already have two sets of words (prepositions, word) and are adding a third set (object). You can see how the if / switch statements will get unweildly.

    One way to manage this is to create a List of words and actions.

    You would start with an interface.

    package com.ggl.text.game;
    
    public interface WordAction {
    
        public boolean isWord(String word);
    
        public void execute();
    
    }
    

    Next, you create classes to perform the actions corresponding to the words. Here's one example class.

    package com.ggl.text.game;
    
    public class NorthAction implements WordAction {
    
        private static final String WORD = "north";
    
        @Override
        public boolean isWord(String word) {
            word = word.toLowerCase();
            if (WORD.equals(word) || (WORD.startsWith(word))) {
                return true;
            } else {
                return false;
            }
        }
    
        @Override
        public void execute() {
            // TODO Move player North
        }
    
    }
    

    Finally, you create a List of WordAction classes. You add all of the action classes you've created to the List.

        List<WordAction> actions = new ArrayList<WordAction>();
        actions.add(new NorthAction());
    

    Later, when you've parsed the input string, you iterate through the List and execute the execute method like this.

        for (WordAction wordAction : actions) {
            if (wordAction.isWord("north")) {
                wordAction.execute();
            }
        }