Fixed parser, added items starting in rooms, moved item room storage
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
Room Name: Room 1
|
||||
Room Description: You are in a stone-walled room, surrounded by
|
||||
Exit Rooms: E-Room 2
|
||||
Room Name: Room 2
|
||||
Room Description: This is Room 2
|
||||
Exit Rooms: W-Room 1, U-Room 3
|
||||
Room Name: Room 3
|
||||
Room Description: You are upstairs. You are likely to be eaten by a giant worm!
|
||||
Exit Rooms: D-Room 2
|
||||
Room Name: Room 1
|
||||
Room Description: You are in a stone-walled room, surrounded by
|
||||
Items:Candlestick,Candlestick
|
||||
Exit Rooms: E-Room 2
|
||||
Room Name: Room 2
|
||||
Room Description: This is Room 2
|
||||
Items:
|
||||
Exit Rooms: W-Room 1, U-Room 3
|
||||
Room Name: Room 3
|
||||
Room Description: You are upstairs. You are likely to be eaten by a giant worm!
|
||||
Items:
|
||||
Exit Rooms: D-Room 2
|
||||
|
||||
@@ -1,87 +1,99 @@
|
||||
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 <null>.
|
||||
*
|
||||
* If the command had only one word, then the second word is <null>.
|
||||
*
|
||||
* 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<String> 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<String> 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<String> 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 <null>.
|
||||
*
|
||||
* If the command had only one word, then the second word is <null>.
|
||||
*
|
||||
* 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<String> 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<String> 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<String> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String, String> m_words = new HashMap<String, String>();;
|
||||
/**
|
||||
* 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<String, String> m_words = new HashMap<String, String>();;
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
@@ -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<Item> inventory = new ArrayList<Item>();
|
||||
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<Item> inventory = new ArrayList<Item>();
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String, Room> masterRoomMap;
|
||||
private HashMap<Item, String> itemsInRooms = new HashMap<Item, String>();
|
||||
|
||||
private void initRooms(String fileName) throws Exception {
|
||||
itemsInRooms.put(new Candlestick(), "Candlestick");
|
||||
masterRoomMap = new HashMap<String, Room>();
|
||||
Scanner roomScanner;
|
||||
try {
|
||||
HashMap<String, HashMap<String, String>> exits = new HashMap<String, HashMap<String, String>>();
|
||||
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("<br>", "\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<String, String> temp = new HashMap<String, String>();
|
||||
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<String, String> 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<String, Room> masterRoomMap;
|
||||
//private HashMap<Item, String> itemsInRooms = new HashMap<Item, String>();
|
||||
|
||||
private void initRooms(String fileName) throws Exception {
|
||||
//itemsInRooms.put(new Candlestick(), "Candlestick");
|
||||
masterRoomMap = new HashMap<String, Room>();
|
||||
Scanner roomScanner;
|
||||
try {
|
||||
HashMap<String, HashMap<String, String>> exits = new HashMap<String, HashMap<String, String>>();
|
||||
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("<br>", "\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<String, String> temp = new HashMap<String, String>();
|
||||
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<String, String> 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> words = new ArrayList<String>();
|
||||
ArrayList<String> otherWords = new ArrayList<String>();
|
||||
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.size(); i++) {
|
||||
if(CommandWords.isCommand(words.get(i))) {
|
||||
verb = words.get(i);
|
||||
if(CommandWords.isDirection(words.get(i))) {
|
||||
direction = words.get(i);
|
||||
}
|
||||
}else if(CommandWords.isItem(words.get(i))){
|
||||
item = words.get(i);
|
||||
}
|
||||
else {
|
||||
otherWords.add(words.get(i));
|
||||
}
|
||||
}
|
||||
//System.out.println(verb);
|
||||
if (CommandWords.isCommand(verb))
|
||||
return new Command(verb, otherWords, direction, item);
|
||||
else
|
||||
return new Command(null, otherWords, direction, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print out a list of valid command words.
|
||||
*/
|
||||
public void showCommands() {
|
||||
commands.showAll();
|
||||
}
|
||||
}
|
||||
|
||||
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<String> words = new ArrayList<String>();
|
||||
ArrayList<String> otherWords = new ArrayList<String>();
|
||||
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<words.size(); i++) {
|
||||
if(CommandWords.isCommand(words.get(i))) {
|
||||
verb = words.get(i);
|
||||
if(CommandWords.isDirection(words.get(i))) {
|
||||
direction = words.get(i);
|
||||
}
|
||||
}else if(CommandWords.isItem(words.get(i))){
|
||||
item = words.get(i);
|
||||
}
|
||||
else {
|
||||
otherWords.add(words.get(i));
|
||||
}
|
||||
}
|
||||
//System.out.println(verb);
|
||||
if (CommandWords.isCommand(verb))
|
||||
return new Command(verb, otherWords, direction, item);
|
||||
else
|
||||
return new Command(null, otherWords, direction, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print out a list of valid command words.
|
||||
*/
|
||||
public void showCommands() {
|
||||
commands.showAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,144 +1,190 @@
|
||||
|
||||
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.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
class Room {
|
||||
private String roomName;
|
||||
private String description;
|
||||
private HashMap<String, Room> 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<String, Room>();
|
||||
}
|
||||
|
||||
public Room() {
|
||||
// default constructor.
|
||||
roomName = "DEFAULT ROOM";
|
||||
description = "DEFAULT DESCRIPTION";
|
||||
exits = new HashMap<String, Room>();
|
||||
}
|
||||
|
||||
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<String, Room> exits; // stores exits of this room.
|
||||
private ArrayList<Item> 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<String, Room>();
|
||||
items = new ArrayList<Item>();
|
||||
}
|
||||
|
||||
public Room() {
|
||||
// default constructor.
|
||||
roomName = "DEFAULT ROOM";
|
||||
description = "DEFAULT DESCRIPTION";
|
||||
exits = new HashMap<String, Room>();
|
||||
items = new ArrayList<Item>();
|
||||
}
|
||||
|
||||
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(); i++) {
|
||||
if(item.equals(items.get(i))) {
|
||||
items.remove(i);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove item at index
|
||||
* Return item if removed, otherwise return null
|
||||
*/
|
||||
public Item removeItem(int i) {
|
||||
if(i >= items.size())
|
||||
return null;
|
||||
Item temp = items.get(i);
|
||||
items.remove(i);
|
||||
return temp;
|
||||
}
|
||||
|
||||
public ArrayList<Item> 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user