From 9805c8ea8452551f0cd232ff8192285961f111c6 Mon Sep 17 00:00:00 2001 From: jslightham <31053827+jslightham@users.noreply.github.com> Date: Wed, 24 Apr 2019 21:25:48 -0400 Subject: [PATCH] worked on parser --- src/com/bayviewglen/zork/Command.java | 28 +++++++++--- src/com/bayviewglen/zork/CommandWords.java | 53 ++++++++++++++++++++-- src/com/bayviewglen/zork/Game.java | 11 ++++- src/com/bayviewglen/zork/Parser.java | 33 +++++++++++--- 4 files changed, 107 insertions(+), 18 deletions(-) diff --git a/src/com/bayviewglen/zork/Command.java b/src/com/bayviewglen/zork/Command.java index 1e80b73..461262a 100644 --- a/src/com/bayviewglen/zork/Command.java +++ b/src/com/bayviewglen/zork/Command.java @@ -1,5 +1,7 @@ package com.bayviewglen.zork; +import java.util.ArrayList; + /** * Class Command - Part of the "Zork" game. * @@ -22,16 +24,18 @@ package com.bayviewglen.zork; */ class Command { private String commandWord; - private String secondWord; + private String direction; + private ArrayList otherWords; /** * Create a command object. First and second word must be supplied, but * either one (or both) can be null. The command word should be null to * indicate that this was a command that is not recognised by this game. */ - public Command(String firstWord, String secondWord) { + public Command(String firstWord, ArrayList otherWords, String direction) { commandWord = firstWord; - this.secondWord = secondWord; + this.otherWords = otherWords; + this.direction = direction; } /** @@ -46,9 +50,14 @@ class Command { * Return the second word of this command. Returns null if there was no * second word. */ - public String getSecondWord() { - return secondWord; + public ArrayList getOtherWords() { + return otherWords; } + /* + public String getSecondWord() { + return otherWords.get(0); + } + */ /** * Return true if this command was not understood. @@ -61,6 +70,13 @@ class Command { * Return true if the command has a second word. */ public boolean hasSecondWord() { - return (secondWord != null); + return otherWords.size() > 0; + } + + public boolean hasDirection() { + return CommandWords.isDirection(commandWord); + } + public String getDirection() { + return direction; } } diff --git a/src/com/bayviewglen/zork/CommandWords.java b/src/com/bayviewglen/zork/CommandWords.java index c0d6f29..56bc1d0 100644 --- a/src/com/bayviewglen/zork/CommandWords.java +++ b/src/com/bayviewglen/zork/CommandWords.java @@ -1,5 +1,7 @@ package com.bayviewglen.zork; +import java.util.HashMap; + /* * Author: Michael Kolling. * Version: 1.0 @@ -12,35 +14,78 @@ package com.bayviewglen.zork; */ class CommandWords { // a constant array that holds all valid command words - private static final String validCommands[] = { "go", "quit", "help", "eat", "jump" }; - + private static HashMap m_words = new HashMap();; /** * Constructor - initialise the command words. */ public CommandWords() { - // nothing to do at the moment... + m_words.put("go", "verb"); + m_words.put("quit", "verb"); + m_words.put("help", "verb"); + m_words.put("jump", "verb"); + m_words.put("north", "direction"); + m_words.put("south", "direction"); + m_words.put("west", "direction"); + m_words.put("east", "direction"); + m_words.put("n", "direction"); + m_words.put("w", "direction"); + m_words.put("s", "direction"); + m_words.put("e", "direction"); + m_words.put("up", "direction"); + m_words.put("down", "direction"); } /** * Check whether a given String is a valid command word. Return true if it * is, false if it isn't. **/ - public boolean isCommand(String aString) { + public static boolean isCommand(String aString) { + try { + return (m_words.get(aString).equals("verb") || m_words.get(aString).equals("direction")); + }catch(Exception e) { + return false; + } + /* for (int i = 0; i < validCommands.length; i++) { if (validCommands[i].equals(aString)) return true; } // if we get here, the string was not found in the commands return false; + */ + } + public static boolean isDirection(String aString) { + return m_words.get(aString).equals("direction"); } /* * Print all valid commands to System.out. */ public void showAll() { + for (String i : m_words.keySet()) { + if(m_words.get(i).equals("verb")){ + System.out.print(i + " "); + } + } + System.out.println(); + + /* for (int i = 0; i < validCommands.length; i++) { System.out.print(validCommands[i] + " "); } System.out.println(); + */ } + + /* + public boolean isVerb(String string) { + try { + if(m_words.get(string).equals("verb")) + return true; + } catch(Exception e) { + return false; + } + return false; + } + */ } diff --git a/src/com/bayviewglen/zork/Game.java b/src/com/bayviewglen/zork/Game.java index 245dfa2..02c5e50 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -133,11 +133,18 @@ class Game { * ends the game, true is returned, otherwise false is returned. */ private boolean processCommand(Command command) { + if (command.isUnknown()) { System.out.println("I don't know what you mean..."); return false; } String commandWord = command.getCommandWord(); + switch(commandWord) { + case "n": case "s": case "e": case "w": case "north": case "south": case "west": case "east": case "up": case "down": + goRoom(command); + break; + + } if (commandWord.equals("help")) printHelp(); else if (commandWord.equals("go")) @@ -173,12 +180,12 @@ class Game { * otherwise print an error message. */ private void goRoom(Command command) { - if (!command.hasSecondWord()) { + if (!command.hasDirection()) { // if there is no second word, we don't know where to go... System.out.println("Go where?"); return; } - String direction = command.getSecondWord(); + String direction = command.getDirection(); // Try to leave current room. Room nextRoom = currentRoom.nextRoom(direction); if (nextRoom == null) diff --git a/src/com/bayviewglen/zork/Parser.java b/src/com/bayviewglen/zork/Parser.java index c33f2e2..db35972 100644 --- a/src/com/bayviewglen/zork/Parser.java +++ b/src/com/bayviewglen/zork/Parser.java @@ -19,7 +19,9 @@ package com.bayviewglen.zork; */ import java.io.BufferedReader; import java.io.InputStreamReader; +import java.util.ArrayList; import java.util.StringTokenizer; +import com.bayviewglen.zork.CommandWords; class Parser { private CommandWords commands; // holds all valid command words @@ -30,9 +32,12 @@ class Parser { public Command getCommand() { String inputLine = ""; // will hold the full input line - String word1; - String word2; - System.out.print("> "); // print prompt + String verb = ""; + String direction = ""; + //String word2; + ArrayList words = new ArrayList(); + ArrayList otherWords = new ArrayList(); + System.out.print("> "); // print prompt BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { inputLine = reader.readLine(); @@ -40,6 +45,20 @@ class Parser { System.out.println("There was an error during reading: " + exc.getMessage()); } StringTokenizer tokenizer = new StringTokenizer(inputLine); + while(tokenizer.hasMoreTokens()) { + words.add(tokenizer.nextToken()); + } + for(int i=0; i