diff --git a/data/rooms.dat b/data/rooms.dat index 0fe5972..fd87a32 100644 --- a/data/rooms.dat +++ b/data/rooms.dat @@ -1,18 +1,22 @@ Room Name: Circle Room -Room Description: You are in the circular room. The windows to the west are bolted shut and curtains cover them. To the east, a hallway. +Room Description: You are in the circular room. The windows to the west are bolted shut and curtains cover them. To the east, a hallway. Exit Rooms: E-Apple Hallway +Items: Candlestick, Candlestick Room Name: Apple Hallway -Room Description: You are in an empty hallway. Many closed doors surround you. To the west is the round room and north is the stairs. You cannot see what they lead to. +Room Description: You are in an empty hallway. Many closed doors surround you. To the west is the round room and north is the stairs. You cannot see what they lead to. Exit Rooms: Circle Room, N-Porcupine Stairs +Items: Room Name: Porcupine Stairs -Room Description: You are in the Porcupine Stairs. Below you is the second floor and above you is the third floor. +Room Description: You are in the Porcupine Stairs. The stone, cold walls are trapped in darkness. Below you is the second floor and above you is the third floor. Exit Rooms: up-third floor, down-second floor +Items: -Room Name: Hallway 3 -Room Description: You are in an empty hallway. Many closed doors surround you. To the west is the round room and north is the stairs. You cannot see what they lead to. +Room Name: Bulb Room +Room Description: You are in the Bulb Room. There is a ragged red rug with a noticeable lump in the middle. One window facing north is bolted shut. Exit Rooms: W-Round Room (third floor), N-Stairs to second floor +Items: Room Name: Hallway 3 Room Description: You are in an empty hallway. Many closed doors surround you. To the west is the round room and north is the stairs. You cannot see what they lead to. diff --git a/src/com/bayviewglen/zork/Command.java b/src/com/bayviewglen/zork/Command.java index 700468a..1e9766e 100644 --- a/src/com/bayviewglen/zork/Command.java +++ b/src/com/bayviewglen/zork/Command.java @@ -1,87 +1,100 @@ -package com.bayviewglen.zork; - -import java.util.ArrayList; - -/** - * Class Command - Part of the "Zork" game. - * - * author: Michael Kolling version: 1.0 date: July 1999 - * - * This class holds information about a command that was issued by the user. A - * command currently consists of two strings: a command word and a second word - * (for example, if the command was "take map", then the two strings obviously - * are "take" and "map"). - * - * The way this is used is: Commands are already checked for being valid command - * words. If the user entered an invalid command (a word that is not known) then - * the command word is . - * - * If the command had only one word, then the second word is . - * - * The second word is not checked at the moment. It can be anything. If this - * game is extended to deal with items, then the second part of the command - * should probably be changed to be an item rather than a String. - */ -class Command { - private String commandWord; - private String direction; - private ArrayList otherWords; - private String item; - - /** - * 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, ArrayList otherWords, String direction, String item) { - commandWord = firstWord; - this.otherWords = otherWords; - this.direction = direction; - this.item = item; - } - - /** - * Return the command word (the first word) of this command. If the command - * was not understood, the result is null. - */ - public String getCommandWord() { - return commandWord; - } - - /** - * Return the second word of this command. Returns null if there was no - * second word. - */ - public ArrayList getOtherWords() { - return otherWords; - } - /* - public String getSecondWord() { - return otherWords.get(0); - } - */ - - /** - * Return true if this command was not understood. - */ - public boolean isUnknown() { - return (commandWord == null); - } - - /** - * Return true if the command has a second word. - */ - public boolean hasSecondWord() { - return otherWords.size() > 0; - } - - public boolean hasItem(){ - return item.equals(""); - } - public boolean hasDirection() { - return CommandWords.isDirection(commandWord); - } - public String getDirection() { - return direction; - } -} +package com.bayviewglen.zork; + +import java.util.ArrayList; + +/** + * Class Command - Part of the "Zork" game. + * + * author: Michael Kolling version: 1.0 date: July 1999 + * + * This class holds information about a command that was issued by the user. A + * command currently consists of two strings: a command word and a second word + * (for example, if the command was "take map", then the two strings obviously + * are "take" and "map"). + * + * The way this is used is: Commands are already checked for being valid command + * words. If the user entered an invalid command (a word that is not known) then + * the command word is . + * + * If the command had only one word, then the second word is . + * + * The second word is not checked at the moment. It can be anything. If this + * game is extended to deal with items, then the second part of the command + * should probably be changed to be an item rather than a String. + * test + */ +class Command { + private String commandWord; + private String direction; + private ArrayList otherWords; + private String item; + + /** + * 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, ArrayList otherWords, String direction, String item) { + commandWord = firstWord; + this.otherWords = otherWords; + this.direction = direction; + if(direction.equals("e")) + this.direction = "east"; + if(direction.equals("w")) + this.direction = "west"; + if(direction.equals("s")) + this.direction = "south"; + if(direction.equals("n")) + this.direction = "north"; + if(direction.equals("u")) + this.direction = "up"; + if(direction.equals("d")) + this.direction = "down"; + this.item = item; + } + + /** + * Return the command word (the first word) of this command. If the command + * was not understood, the result is null. + */ + public String getCommandWord() { + return commandWord; + } + + /** + * Return the second word of this command. Returns null if there was no + * second word. + */ + public ArrayList getOtherWords() { + return otherWords; + } + /* + public String getSecondWord() { + return otherWords.get(0); + } + */ + + /** + * Return true if this command was not understood. + */ + public boolean isUnknown() { + return (commandWord == null); + } + + /** + * Return true if the command has a second word. + */ + public boolean hasSecondWord() { + return otherWords.size() > 0; + } + + public boolean hasItem(){ + return item.equals(""); + } + public boolean hasDirection() { + return CommandWords.isDirection(direction); + } + public String getDirection() { + return direction; + } +} diff --git a/src/com/bayviewglen/zork/CommandWords.java b/src/com/bayviewglen/zork/CommandWords.java index dc7c225..70c57e0 100644 --- a/src/com/bayviewglen/zork/CommandWords.java +++ b/src/com/bayviewglen/zork/CommandWords.java @@ -1,74 +1,78 @@ -package com.bayviewglen.zork; - -import java.io.File; -import java.util.HashMap; -import java.util.Scanner; - -/* - * Author: Michael Kolling. - * Version: 1.0 - * Date: July 1999 - * - * This class holds an enumeration of all command words known to the game. - * It is used to recognise commands as they are typed in. - * - * This class is part of the "Zork" game. - */ -class CommandWords { - // a constant array that holds all valid command words - private static HashMap m_words = new HashMap();; - /** - * Constructor - initialise the command words. - */ - public CommandWords() { - // 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; - } - } - // Check if given string is direction - public static boolean isDirection(String aString) { - return m_words.get(aString).equals("direction"); - } - - public static boolean isItem(String aString){ - try { - return m_words.get(aString).equals("item"); - } catch(Exception e) { - return false; - } - } - - /* - * 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(); - } +package com.bayviewglen.zork; + +import java.io.File; +import java.util.HashMap; +import java.util.Scanner; + +/* + * Author: Michael Kolling. + * Version: 1.0 + * Date: July 1999 + * + * This class holds an enumeration of all command words known to the game. + * It is used to recognise commands as they are typed in. + * + * This class is part of the "Zork" game. + */ +class CommandWords { + // a constant array that holds all valid command words + private static HashMap m_words = new HashMap();; + /** + * Constructor - initialise the command words. + */ + public CommandWords() { + // 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; + } + } + // Check if given string is direction + public static boolean isDirection(String aString) { + try { + return m_words.get(aString).equals("direction"); + }catch(Exception e) { + return false; + } + } + + public static boolean isItem(String aString){ + try { + return m_words.get(aString).equals("item"); + } catch(Exception e) { + return false; + } + } + + /* + * 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(); + } } \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Entities/Player.java b/src/com/bayviewglen/zork/Entities/Player.java index 297296f..94007f2 100644 --- a/src/com/bayviewglen/zork/Entities/Player.java +++ b/src/com/bayviewglen/zork/Entities/Player.java @@ -1,29 +1,29 @@ -package com.bayviewglen.zork.Entities; - -import com.bayviewglen.zork.Items.Item; -import java.util.ArrayList; - -public class Player extends Entity{ - private ArrayList inventory = new ArrayList(); - private final int INVENTORY_CAPACITY = 120; - private int currentInventoryWeight; - - public Player() { - super(); - } - - private boolean addToInventory(Item item){ - if(currentInventoryWeight + item.getWeight() < INVENTORY_CAPACITY){ - inventory.add(item); - System.out.println(item.getName() + " add"); - return true; - } - return false; - } - - private void removeFromInventory(Item item){ - inventory.remove(item); - - } - -} +package com.bayviewglen.zork.Entities; + +import com.bayviewglen.zork.Items.Item; +import java.util.ArrayList; + +public class Player extends Entity{ + private ArrayList inventory = new ArrayList(); + private final int INVENTORY_CAPACITY = 120; + private int currentInventoryWeight; + + public Player() { + super(); + } + + public boolean addToInventory(Item item){ + if(currentInventoryWeight + item.getWeight() < INVENTORY_CAPACITY){ + inventory.add(item); + System.out.println(item.getName() + " add"); + return true; + } + return false; + } + + public void removeFromInventory(Item item){ + inventory.remove(item); + + } + +} diff --git a/src/com/bayviewglen/zork/Game.java b/src/com/bayviewglen/zork/Game.java index b3bce28..62f872a 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -1,207 +1,227 @@ -package com.bayviewglen.zork; - -import java.io.File; -import java.io.FileNotFoundException; -import java.util.HashMap; -import java.util.Scanner; - -import com.bayviewglen.zork.Items.*; - -/** - * Class Game - the main class of the "Zork" game. - * - * Author: Michael Kolling Version: 1.1 Date: March 2000 - * - * This class is the main class of the "Zork" application. Zork is a very - * simple, text based adventure game. Users can walk around some scenery. That's - * all. It should really be extended to make it more interesting! - * - * To play this game, create an instance of this class and call the "play" - * routine. - * - * This main class creates and initialises all the others: it creates all rooms, - * creates the parser and starts the game. It also evaluates the commands that - * the parser returns. - */ -class Game { - private Parser parser; - private Room currentRoom; - // This is a MASTER object that contains all of the rooms and is easily - // accessible. - // The key will be the name of the room -> no spaces (Use all caps and - // underscore -> Great Room would have a key of GREAT_ROOM - // In a hashmap keys are case sensitive. - // masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the - // Great Room (assuming you have one). - private HashMap masterRoomMap; - private HashMap itemsInRooms = new HashMap(); - - private void initRooms(String fileName) throws Exception { - itemsInRooms.put(new Candlestick(), "Candlestick"); - masterRoomMap = new HashMap(); - Scanner roomScanner; - try { - HashMap> exits = new HashMap>(); - roomScanner = new Scanner(new File(fileName)); - while (roomScanner.hasNext()) { - Room room = new Room(); - // Read the Name - String roomName = roomScanner.nextLine(); - room.setRoomName(roomName.split(":")[1].trim()); - // Read the Description - String roomDescription = roomScanner.nextLine(); - room.setDescription(roomDescription.split(":")[1].replaceAll("
", "\n").trim()); - // Read the Exits - String roomExits = roomScanner.nextLine(); - // An array of strings in the format E-RoomName - String[] rooms = roomExits.split(":")[1].split(","); - HashMap temp = new HashMap(); - for (String s : rooms) { - temp.put(s.split("-")[0].trim(), s.split("-")[1]); - } - - exits.put(roomName.substring(10).trim().toUpperCase().replaceAll(" ", "_"), temp); - - // This puts the room we created (Without the exits in the - // masterMap) - masterRoomMap.put(roomName.toUpperCase().substring(10).trim().replaceAll(" ", "_"), room); - - // Now we better set the exits. - } - - for (String key : masterRoomMap.keySet()) { - Room roomTemp = masterRoomMap.get(key); - HashMap tempExits = exits.get(key); - for (String s : tempExits.keySet()) { - // s = direction - // value is the room. - - String roomName2 = tempExits.get(s.trim()); - Room exitRoom = masterRoomMap.get(roomName2.toUpperCase().replaceAll(" ", "_")); - roomTemp.setExit(s.trim().charAt(0), exitRoom); - - } - - } - - roomScanner.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - - /** - * Create the game and initialise its internal map. - */ - public Game() { - try { - initRooms("data/Rooms.dat"); - currentRoom = masterRoomMap.get("ROOM_1"); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - parser = new Parser(); - } - - /** - * Main play routine. Loops until end of play. - */ - public void play() { - printWelcome(); - // Enter the main command loop. Here we repeatedly read commands and - // execute them until the game is over. - - boolean finished = false; - while (!finished) { - Command command = parser.getCommand(); - finished = processCommand(command); - } - System.out.println("Thank you for playing. Good bye."); - } - - /** - * Print out the opening message for the player. - */ - private void printWelcome() { - System.out.println(); - System.out.println("Welcome to Zork!"); - System.out.println("Zork is a new, incredibly boring adventure game, for now..."); - System.out.println("Type 'help' if you need help."); - System.out.println(); - System.out.println(currentRoom.longDescription()); - } - - /** - * Given a command, process (that is: execute) the command. If this command - * 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 "go": case "n": case "s": case "e": case "w": case "north": case "south": case "west": case "east": case "up": case "down": - goRoom(command); - break; - 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; - case "look": - for (Item i : itemsInRooms.keySet()) { - System.out.print(i.getName() + " "); - } - System.out.println(); - break; - default: - return false; - } - return false; - } - - // implementations of user commands: - /** - * Print out some help information. Here we print some stupid, cryptic - * message and a list of the command words. - */ - private void printHelp() { - System.out.println("You are lost. You are alone. You wander"); - System.out.println("around at Monash Uni, Peninsula Campus."); - System.out.println(); - System.out.println("Your command words are:"); - parser.showCommands(); - } - - /** - * Try to go to one direction. If there is an exit, enter the new room, - * otherwise print an error message. - */ - private void goRoom(Command command) { - 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.getDirection(); - // Try to leave current room. - Room nextRoom = currentRoom.nextRoom(direction); - if (nextRoom == null) - System.out.println("There is no door!"); - else { - currentRoom = nextRoom; - System.out.println(currentRoom.longDescription()); - } - } - +package com.bayviewglen.zork; + +import java.io.File; +import java.io.FileNotFoundException; +import java.lang.reflect.Constructor; +import java.util.HashMap; +import java.util.Scanner; + +import com.bayviewglen.zork.Items.*; + +/** + * Class Game - the main class of the "Zork" game. + * + * Author: Michael Kolling Version: 1.1 Date: March 2000 + * + * This class is the main class of the "Zork" application. Zork is a very + * simple, text based adventure game. Users can walk around some scenery. That's + * all. It should really be extended to make it more interesting! + * + * To play this game, create an instance of this class and call the "play" + * routine. + * + * This main class creates and initialises all the others: it creates all rooms, + * creates the parser and starts the game. It also evaluates the commands that + * the parser returns. + */ +class Game { + private Parser parser; + private Room currentRoom; + // This is a MASTER object that contains all of the rooms and is easily + // accessible. + // The key will be the name of the room -> no spaces (Use all caps and + // underscore -> Great Room would have a key of GREAT_ROOM + // In a hashmap keys are case sensitive. + // masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the + // Great Room (assuming you have one). + private HashMap masterRoomMap; + //private HashMap itemsInRooms = new HashMap(); + + private void initRooms(String fileName) throws Exception { + //itemsInRooms.put(new Candlestick(), "Candlestick"); + masterRoomMap = new HashMap(); + Scanner roomScanner; + try { + HashMap> exits = new HashMap>(); + roomScanner = new Scanner(new File(fileName)); + while (roomScanner.hasNext()) { + Room room = new Room(); + // Read the Name + String roomName = roomScanner.nextLine(); + room.setRoomName(roomName.split(":")[1].trim()); + // Read the Description + String roomDescription = roomScanner.nextLine(); + room.setDescription(roomDescription.split(":")[1].replaceAll("
", "\n").trim()); + // Read the Items + String items = roomScanner.nextLine(); + try { + String[] itemArr = items.split(":")[1].split(","); + for(String s : itemArr) { + Class clazz = Class.forName("com.bayviewglen.zork.Items." + s.trim()); + Constructor ctor = clazz.getConstructor(); + Item object = (Item) ctor.newInstance(); + room.addItem(object); + } + }catch(Exception e) { + } + + // Read the Exits + String roomExits = roomScanner.nextLine(); + // An array of strings in the format E-RoomName + String[] rooms = roomExits.split(":")[1].split(","); + HashMap temp = new HashMap(); + for (String s : rooms) { + temp.put(s.split("-")[0].trim(), s.split("-")[1]); + } + + exits.put(roomName.substring(10).trim().toUpperCase().replaceAll(" ", "_"), temp); + + // This puts the room we created (Without the exits in the + // masterMap) + masterRoomMap.put(roomName.toUpperCase().substring(10).trim().replaceAll(" ", "_"), room); + + // Now we better set the exits. + } + + for (String key : masterRoomMap.keySet()) { + Room roomTemp = masterRoomMap.get(key); + HashMap tempExits = exits.get(key); + for (String s : tempExits.keySet()) { + // s = direction + // value is the room. + + String roomName2 = tempExits.get(s.trim()); + Room exitRoom = masterRoomMap.get(roomName2.toUpperCase().replaceAll(" ", "_")); + roomTemp.setExit(s.trim().charAt(0), exitRoom); + + } + + } + + roomScanner.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + /** + * Create the game and initialise its internal map. + */ + public Game() { + try { + initRooms("data/Rooms.dat"); + currentRoom = masterRoomMap.get("ROOM_1"); + } catch (Exception e) { + e.printStackTrace(); + } + parser = new Parser(); + } + + /** + * Main play routine. Loops until end of play. + */ + public void play() { + printWelcome(); + // Enter the main command loop. Here we repeatedly read commands and + // execute them until the game is over. + + boolean finished = false; + while (!finished) { + Command command = parser.getCommand(); + finished = processCommand(command); + } + System.out.println("Thank you for playing. Good bye."); + } + + /** + * Print out the opening message for the player. + */ + private void printWelcome() { + System.out.println(); + System.out.println("Welcome to Zork!"); + System.out.println("Zork is a new, incredibly boring adventure game, for now..."); + System.out.println("Type 'help' if you need help."); + System.out.println(); + System.out.println(currentRoom.longDescription()); + } + + /** + * Given a command, process (that is: execute) the command. If this command + * 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 "go": case "n": case "s": case "e": case "w": case "north": case "south": case "west": case "east": case "up": case "down": + goRoom(command); + break; + 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; + case "look": + System.out.print("Items: "); + for(Item i : currentRoom.getItems()) { + System.out.print(i.getName() + " "); + } + System.out.println(); + /* + for (Item i : itemsInRooms.keySet()) { + System.out.print(i.getName() + " "); + } + System.out.println(); + */ + break; + default: + return false; + } + return false; + } + + // implementations of user commands: + /** + * Print out some help information. Here we print some stupid, cryptic + * message and a list of the command words. + */ + private void printHelp() { + System.out.println("You are lost. You are alone. You wander"); + System.out.println("around at Monash Uni, Peninsula Campus."); + System.out.println(); + System.out.println("Your command words are:"); + parser.showCommands(); + } + + /** + * Try to go to one direction. If there is an exit, enter the new room, + * otherwise print an error message. + */ + private void goRoom(Command command) { + 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.getDirection(); + // Try to leave current room. + Room nextRoom = currentRoom.nextRoom(direction); + if (nextRoom == null) + System.out.println("There is no door!"); + else { + currentRoom = nextRoom; + System.out.println(currentRoom.longDescription()); + } + } + } \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Parser.java b/src/com/bayviewglen/zork/Parser.java index 6d9c733..662d4f5 100644 --- a/src/com/bayviewglen/zork/Parser.java +++ b/src/com/bayviewglen/zork/Parser.java @@ -1,78 +1,78 @@ - -package com.bayviewglen.zork; - -/* - * Author: Michael Kolling - * Version: 1.0 - * Date: July 1999 - * - * This class is part of Zork. Zork is a simple, text based adventure game. - * - * This parser reads user input and tries to interpret it as a "Zork" - * command. Every time it is called it reads a line from the terminal and - * tries to interpret the line as a two word command. It returns the command - * as an object of class Command. - * - * The parser has a set of known command words. It checks user input against - * the known commands, and if the input is not one of the known commands, it - * returns a command object that is marked as an unknown command. - */ -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 - - public Parser() { - commands = new CommandWords(); - } - - public Command getCommand() { - String inputLine = ""; // will hold the full input line - String verb = ""; - String direction = ""; - String item = ""; - //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(); - } catch (java.io.IOException exc) { - 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 words = new ArrayList(); + ArrayList otherWords = new ArrayList(); + System.out.print("> "); // print prompt + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + try { + inputLine = reader.readLine(); + } catch (java.io.IOException exc) { + System.out.println("There was an error during reading: " + exc.getMessage()); + } + StringTokenizer tokenizer = new StringTokenizer(inputLine.toLowerCase()); + while(tokenizer.hasMoreTokens()) { + words.add(tokenizer.nextToken()); + } + for(int i=0; i exits; // stores exits of this room. - - /** - * Create a room described "description". Initially, it has no exits. - * "description" is something like "a kitchen" or "an open court yard". - */ - public Room(String description) { - this.description = description; - exits = new HashMap(); - } - - public Room() { - // default constructor. - roomName = "DEFAULT ROOM"; - description = "DEFAULT DESCRIPTION"; - exits = new HashMap(); - } - - public void setExit(char direction, Room r) throws Exception { - String dir = ""; - switch (direction) { - case 'E': - dir = "east"; - break; - case 'W': - dir = "west"; - break; - case 'S': - dir = "south"; - break; - case 'N': - dir = "north"; - break; - case 'U': - dir = "up"; - break; - case 'D': - dir = "down"; - break; - default: - throw new Exception("Invalid Direction"); - - } - - exits.put(dir, r); - } - - /** - * Define the exits of this room. Every direction either leads to another - * room or is null (no exit there). - */ - public void setExits(Room north, Room east, Room south, Room west, Room up, Room down) { - if (north != null) - exits.put("north", north); - if (east != null) - exits.put("east", east); - if (south != null) - exits.put("south", south); - if (west != null) - exits.put("west", west); - if (up != null) - exits.put("up", up); - if (up != null) - exits.put("down", down); - - } - - /** - * Return the description of the room (the one that was defined in the - * constructor). - */ - public String shortDescription() { - return "Room: " + roomName + "\n\n" + description; - } - - /** - * Return a long description of this room, on the form: You are in the - * kitchen. Exits: north west - */ - public String longDescription() { - - return "Room: " + roomName + "\n\n" + description + "\n" + exitString(); - } - - /** - * Return a string describing the room's exits, for example "Exits: north - * west ". - */ - private String exitString() { - String returnString = "Exits:"; - Set keys = exits.keySet(); - for (Iterator iter = keys.iterator(); iter.hasNext();) - returnString += " " + iter.next(); - return returnString; - } - - /** - * Return the room that is reached if we go from this room in direction - * "direction". If there is no room in that direction, return null. - */ - public Room nextRoom(String direction) { - return (Room) exits.get(direction); - } - - public String getRoomName() { - return roomName; - } - - public void setRoomName(String roomName) { - this.roomName = roomName; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } -} + +package com.bayviewglen.zork; + +/* + * Class Room - a room in an adventure game. + * + * Author: Michael Kolling + * Version: 1.1 + * Date: August 2000 + * + * This class is part of Zork. Zork is a simple, text based adventure game. + * + * "Room" represents one location in the scenery of the game. It is + * connected to at most four other rooms via exits. The exits are labelled + * north, east, south, west. For each direction, the room stores a reference + * to the neighbouring room, or null if there is no exit in that direction. + */ +import java.util.Set; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import com.bayviewglen.zork.Items.*; + +class Room { + private String roomName; + private String description; + private HashMap exits; // stores exits of this room. + private ArrayList items; + + /** + * Create a room described "description". Initially, it has no exits. + * "description" is something like "a kitchen" or "an open court yard". + */ + public Room(String description) { + this.description = description; + exits = new HashMap(); + items = new ArrayList(); + } + + public Room() { + // default constructor. + roomName = "DEFAULT ROOM"; + description = "DEFAULT DESCRIPTION"; + exits = new HashMap(); + items = new ArrayList(); + } + + public void setExit(char direction, Room r) throws Exception { + String dir = ""; + switch (direction) { + case 'E': + dir = "east"; + break; + case 'W': + dir = "west"; + break; + case 'S': + dir = "south"; + break; + case 'N': + dir = "north"; + break; + case 'U': + dir = "up"; + break; + case 'D': + dir = "down"; + break; + default: + throw new Exception("Invalid Direction"); + + } + + exits.put(dir, r); + } + + /* + * Add passed in item to item array list + */ + public void addItem(Item item) { + items.add(item); + } + + /* + * Remove first instance of item passed in + * Return item if removed, otherwise return null + */ + public Item removeItem(Item item) { + for(int i=0; i= items.size()) + return null; + Item temp = items.get(i); + items.remove(i); + return temp; + } + + public ArrayList getItems() { + return items; + } + + public Item getItem(int i) { + return items.get(i); + } + + /** + * Define the exits of this room. Every direction either leads to another + * room or is null (no exit there). + */ + public void setExits(Room north, Room east, Room south, Room west, Room up, Room down) { + if (north != null) + exits.put("north", north); + if (east != null) + exits.put("east", east); + if (south != null) + exits.put("south", south); + if (west != null) + exits.put("west", west); + if (up != null) + exits.put("up", up); + if (up != null) + exits.put("down", down); + + } + + /** + * Return the description of the room (the one that was defined in the + * constructor). + */ + public String shortDescription() { + return "Room: " + roomName + "\n\n" + description; + } + + /** + * Return a long description of this room, on the form: You are in the + * kitchen. Exits: north west + */ + public String longDescription() { + + return "Room: " + roomName + "\n\n" + description + "\n" + exitString(); + } + + /** + * Return a string describing the room's exits, for example "Exits: north + * west ". + */ + private String exitString() { + String returnString = "Exits:"; + Set keys = exits.keySet(); + for (Iterator iter = keys.iterator(); iter.hasNext();) + returnString += " " + iter.next(); + return returnString; + } + + /** + * Return the room that is reached if we go from this room in direction + * "direction". If there is no room in that direction, return null. + */ + public Room nextRoom(String direction) { + return (Room) exits.get(direction); + } + + public String getRoomName() { + return roomName; + } + + public void setRoomName(String roomName) { + this.roomName = roomName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +}