Merge branch 'master' of https://github.com/jslightham/Zork/
Conflicts: data/rooms.dat
This commit is contained in:
@@ -1,18 +1,22 @@
|
|||||||
Room Name: Circle Room
|
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
|
Exit Rooms: E-Apple Hallway
|
||||||
|
Items: Candlestick, Candlestick
|
||||||
|
|
||||||
Room Name: Apple Hallway
|
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
|
Exit Rooms: Circle Room, N-Porcupine Stairs
|
||||||
|
Items:
|
||||||
|
|
||||||
Room Name: Porcupine Stairs
|
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
|
Exit Rooms: up-third floor, down-second floor
|
||||||
|
Items:
|
||||||
|
|
||||||
Room Name: Hallway 3
|
Room Name: Bulb Room
|
||||||
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 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
|
Exit Rooms: W-Round Room (third floor), N-Stairs to second floor
|
||||||
|
Items:
|
||||||
|
|
||||||
Room Name: Hallway 3
|
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 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.
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import java.util.ArrayList;
|
|||||||
* The second word is not checked at the moment. It can be anything. If this
|
* 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
|
* 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.
|
* should probably be changed to be an item rather than a String.
|
||||||
|
* test
|
||||||
*/
|
*/
|
||||||
class Command {
|
class Command {
|
||||||
private String commandWord;
|
private String commandWord;
|
||||||
@@ -37,6 +38,18 @@ class Command {
|
|||||||
commandWord = firstWord;
|
commandWord = firstWord;
|
||||||
this.otherWords = otherWords;
|
this.otherWords = otherWords;
|
||||||
this.direction = direction;
|
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;
|
this.item = item;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +92,7 @@ class Command {
|
|||||||
return item.equals("");
|
return item.equals("");
|
||||||
}
|
}
|
||||||
public boolean hasDirection() {
|
public boolean hasDirection() {
|
||||||
return CommandWords.isDirection(commandWord);
|
return CommandWords.isDirection(direction);
|
||||||
}
|
}
|
||||||
public String getDirection() {
|
public String getDirection() {
|
||||||
return direction;
|
return direction;
|
||||||
|
|||||||
@@ -49,7 +49,11 @@ class CommandWords {
|
|||||||
}
|
}
|
||||||
// Check if given string is direction
|
// Check if given string is direction
|
||||||
public static boolean isDirection(String aString) {
|
public static boolean isDirection(String aString) {
|
||||||
|
try {
|
||||||
return m_words.get(aString).equals("direction");
|
return m_words.get(aString).equals("direction");
|
||||||
|
}catch(Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isItem(String aString){
|
public static boolean isItem(String aString){
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class Player extends Entity{
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean addToInventory(Item item){
|
public boolean addToInventory(Item item){
|
||||||
if(currentInventoryWeight + item.getWeight() < INVENTORY_CAPACITY){
|
if(currentInventoryWeight + item.getWeight() < INVENTORY_CAPACITY){
|
||||||
inventory.add(item);
|
inventory.add(item);
|
||||||
System.out.println(item.getName() + " add");
|
System.out.println(item.getName() + " add");
|
||||||
@@ -21,7 +21,7 @@ public class Player extends Entity{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeFromInventory(Item item){
|
public void removeFromInventory(Item item){
|
||||||
inventory.remove(item);
|
inventory.remove(item);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.bayviewglen.zork;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
@@ -34,10 +35,10 @@ class Game {
|
|||||||
// masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the
|
// masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the
|
||||||
// Great Room (assuming you have one).
|
// Great Room (assuming you have one).
|
||||||
private HashMap<String, Room> masterRoomMap;
|
private HashMap<String, Room> masterRoomMap;
|
||||||
private HashMap<Item, String> itemsInRooms = new HashMap<Item, String>();
|
//private HashMap<Item, String> itemsInRooms = new HashMap<Item, String>();
|
||||||
|
|
||||||
private void initRooms(String fileName) throws Exception {
|
private void initRooms(String fileName) throws Exception {
|
||||||
itemsInRooms.put(new Candlestick(), "Candlestick");
|
//itemsInRooms.put(new Candlestick(), "Candlestick");
|
||||||
masterRoomMap = new HashMap<String, Room>();
|
masterRoomMap = new HashMap<String, Room>();
|
||||||
Scanner roomScanner;
|
Scanner roomScanner;
|
||||||
try {
|
try {
|
||||||
@@ -51,6 +52,19 @@ class Game {
|
|||||||
// Read the Description
|
// Read the Description
|
||||||
String roomDescription = roomScanner.nextLine();
|
String roomDescription = roomScanner.nextLine();
|
||||||
room.setDescription(roomDescription.split(":")[1].replaceAll("<br>", "\n").trim());
|
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
|
// Read the Exits
|
||||||
String roomExits = roomScanner.nextLine();
|
String roomExits = roomScanner.nextLine();
|
||||||
// An array of strings in the format E-RoomName
|
// An array of strings in the format E-RoomName
|
||||||
@@ -98,7 +112,6 @@ class Game {
|
|||||||
initRooms("data/Rooms.dat");
|
initRooms("data/Rooms.dat");
|
||||||
currentRoom = masterRoomMap.get("ROOM_1");
|
currentRoom = masterRoomMap.get("ROOM_1");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
parser = new Parser();
|
parser = new Parser();
|
||||||
@@ -159,10 +172,17 @@ class Game {
|
|||||||
System.out.println("Do you really think you should be eating at a time like this?");
|
System.out.println("Do you really think you should be eating at a time like this?");
|
||||||
break;
|
break;
|
||||||
case "look":
|
case "look":
|
||||||
|
System.out.print("Items: ");
|
||||||
|
for(Item i : currentRoom.getItems()) {
|
||||||
|
System.out.print(i.getName() + " ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
/*
|
||||||
for (Item i : itemsInRooms.keySet()) {
|
for (Item i : itemsInRooms.keySet()) {
|
||||||
System.out.print(i.getName() + " ");
|
System.out.print(i.getName() + " ");
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
*/
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class Parser {
|
|||||||
} catch (java.io.IOException exc) {
|
} catch (java.io.IOException exc) {
|
||||||
System.out.println("There was an error during reading: " + exc.getMessage());
|
System.out.println("There was an error during reading: " + exc.getMessage());
|
||||||
}
|
}
|
||||||
StringTokenizer tokenizer = new StringTokenizer(inputLine);
|
StringTokenizer tokenizer = new StringTokenizer(inputLine.toLowerCase());
|
||||||
while(tokenizer.hasMoreTokens()) {
|
while(tokenizer.hasMoreTokens()) {
|
||||||
words.add(tokenizer.nextToken());
|
words.add(tokenizer.nextToken());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,16 @@ package com.bayviewglen.zork;
|
|||||||
* to the neighbouring room, or null if there is no exit in that direction.
|
* to the neighbouring room, or null if there is no exit in that direction.
|
||||||
*/
|
*/
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import com.bayviewglen.zork.Items.*;
|
||||||
|
|
||||||
class Room {
|
class Room {
|
||||||
private String roomName;
|
private String roomName;
|
||||||
private String description;
|
private String description;
|
||||||
private HashMap<String, Room> exits; // stores exits of this room.
|
private HashMap<String, Room> exits; // stores exits of this room.
|
||||||
|
private ArrayList<Item> items;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a room described "description". Initially, it has no exits.
|
* Create a room described "description". Initially, it has no exits.
|
||||||
@@ -31,6 +34,7 @@ class Room {
|
|||||||
public Room(String description) {
|
public Room(String description) {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
exits = new HashMap<String, Room>();
|
exits = new HashMap<String, Room>();
|
||||||
|
items = new ArrayList<Item>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Room() {
|
public Room() {
|
||||||
@@ -38,6 +42,7 @@ class Room {
|
|||||||
roomName = "DEFAULT ROOM";
|
roomName = "DEFAULT ROOM";
|
||||||
description = "DEFAULT DESCRIPTION";
|
description = "DEFAULT DESCRIPTION";
|
||||||
exits = new HashMap<String, Room>();
|
exits = new HashMap<String, Room>();
|
||||||
|
items = new ArrayList<Item>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setExit(char direction, Room r) throws Exception {
|
public void setExit(char direction, Room r) throws Exception {
|
||||||
@@ -69,6 +74,47 @@ class Room {
|
|||||||
exits.put(dir, r);
|
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
|
* Define the exits of this room. Every direction either leads to another
|
||||||
* room or is null (no exit there).
|
* room or is null (no exit there).
|
||||||
|
|||||||
Reference in New Issue
Block a user