From 14bd1aa83f3d5ec648fa5d55fbf3c00e7083adb2 Mon Sep 17 00:00:00 2001 From: jslightham <31053827+jslightham@users.noreply.github.com> Date: Wed, 24 Apr 2019 22:52:15 -0400 Subject: [PATCH] import words through file --- data/words.dat | 14 ++++++ src/com/bayviewglen/zork/CommandWords.java | 57 ++++++---------------- src/com/bayviewglen/zork/Game.java | 30 ++++++------ 3 files changed, 44 insertions(+), 57 deletions(-) create mode 100644 data/words.dat diff --git a/data/words.dat b/data/words.dat new file mode 100644 index 0000000..153524f --- /dev/null +++ b/data/words.dat @@ -0,0 +1,14 @@ +go, verb +quit, verb +help, verb +jump, verb +north, direction +south, direction +west, direction +east, direction +n, direction +w, direction +s, direction +e, direction +up, direction +down, direction \ No newline at end of file diff --git a/src/com/bayviewglen/zork/CommandWords.java b/src/com/bayviewglen/zork/CommandWords.java index 56bc1d0..b219abb 100644 --- a/src/com/bayviewglen/zork/CommandWords.java +++ b/src/com/bayviewglen/zork/CommandWords.java @@ -1,6 +1,8 @@ package com.bayviewglen.zork; +import java.io.File; import java.util.HashMap; +import java.util.Scanner; /* * Author: Michael Kolling. @@ -19,41 +21,33 @@ class CommandWords { * Constructor - initialise the command words. */ public CommandWords() { - 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"); + // Import words from words.dat into HashMap + try { + Scanner in = new Scanner(new File("data/words.dat")); + while(in.hasNext()){ + String text = in.nextLine(); + String[] textarr = text.split(","); + m_words.put(textarr[0], textarr[1].substring(1)); + } + in.close(); + }catch (Exception e) { + e.printStackTrace(); + } } /** * Check whether a given String is a valid command word. Return true if it * is, false if it isn't. **/ + // Check if given string is verb or direction 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; - */ } + // Check if given string is direction public static boolean isDirection(String aString) { return m_words.get(aString).equals("direction"); } @@ -68,24 +62,5 @@ class CommandWords { } } 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 02c5e50..02d222b 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -140,24 +140,22 @@ class Game { } 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": + case "go": 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")) - goRoom(command); - else if (commandWord.equals("jump")) - System.out.println("Good Job!"); - else if (commandWord.equals("quit")) { - if (command.hasSecondWord()) - System.out.println("Quit what?"); - else - return true; // signal that we want to quit - } else if (commandWord.equals("eat")) { - System.out.println("Do you really think you should be eating at a time like this?"); + case "help": + printHelp(); + break; + case "jump": + System.out.println("Good Job!"); + break; + case "quit": + return true; + case "eat": + System.out.println("Do you really think you should be eating at a time like this?"); + break; + default: + return false; } return false; }