-2

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

}

senox13
  • 315
  • 2
  • 10
  • 3
    And the problem is...? – SevenBits May 15 '15 at 21:41
  • 1
    Java style: Variable names and method names start with a lowercase letter, otherwise they look like a class name. "CheckFirst(String Word)" should be "checkFirst(String word)", "String ToTake" should be "String toTake". Of course, this won't fix the problem you're having, it's just style that happens to be common to Java code. So, what's your problem? – David Koelle May 15 '15 at 21:49
  • You really need to indicate *what* isn't working. If you specify the actual behaviour you obseve when running the code, then we can make a pretty good guess about what's wrong based on the posted code and the expected behaviour. – Stephen Byrne May 16 '15 at 00:18

1 Answers1

0

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();
        }
    }
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111