import words through file

This commit is contained in:
jslightham
2019-04-24 22:52:15 -04:00
parent 9805c8ea84
commit 14bd1aa83f
3 changed files with 44 additions and 57 deletions

14
data/words.dat Normal file
View File

@@ -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

View File

@@ -1,6 +1,8 @@
package com.bayviewglen.zork; package com.bayviewglen.zork;
import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Scanner;
/* /*
* Author: Michael Kolling. * Author: Michael Kolling.
@@ -19,41 +21,33 @@ class CommandWords {
* Constructor - initialise the command words. * Constructor - initialise the command words.
*/ */
public CommandWords() { public CommandWords() {
m_words.put("go", "verb"); // Import words from words.dat into HashMap
m_words.put("quit", "verb"); try {
m_words.put("help", "verb"); Scanner in = new Scanner(new File("data/words.dat"));
m_words.put("jump", "verb"); while(in.hasNext()){
m_words.put("north", "direction"); String text = in.nextLine();
m_words.put("south", "direction"); String[] textarr = text.split(",");
m_words.put("west", "direction"); m_words.put(textarr[0], textarr[1].substring(1));
m_words.put("east", "direction"); }
m_words.put("n", "direction"); in.close();
m_words.put("w", "direction"); }catch (Exception e) {
m_words.put("s", "direction"); e.printStackTrace();
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 * Check whether a given String is a valid command word. Return true if it
* is, false if it isn't. * is, false if it isn't.
**/ **/
// Check if given string is verb or direction
public static boolean isCommand(String aString) { public static boolean isCommand(String aString) {
try { try {
return (m_words.get(aString).equals("verb") || m_words.get(aString).equals("direction")); return (m_words.get(aString).equals("verb") || m_words.get(aString).equals("direction"));
}catch(Exception e) { }catch(Exception e) {
return false; 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) { public static boolean isDirection(String aString) {
return m_words.get(aString).equals("direction"); return m_words.get(aString).equals("direction");
} }
@@ -68,24 +62,5 @@ class CommandWords {
} }
} }
System.out.println(); 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;
}
*/
} }

View File

@@ -140,24 +140,22 @@ class Game {
} }
String commandWord = command.getCommandWord(); String commandWord = command.getCommandWord();
switch(commandWord) { 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); goRoom(command);
break; break;
case "help":
} printHelp();
if (commandWord.equals("help")) break;
printHelp(); case "jump":
else if (commandWord.equals("go")) System.out.println("Good Job!");
goRoom(command); break;
else if (commandWord.equals("jump")) case "quit":
System.out.println("Good Job!"); return true;
else if (commandWord.equals("quit")) { case "eat":
if (command.hasSecondWord()) System.out.println("Do you really think you should be eating at a time like this?");
System.out.println("Quit what?"); break;
else default:
return true; // signal that we want to quit return false;
} else if (commandWord.equals("eat")) {
System.out.println("Do you really think you should be eating at a time like this?");
} }
return false; return false;
} }