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
|
||||
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
|
||||
|
||||
@@ -37,6 +37,18 @@ class Command {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -79,7 +91,7 @@ class Command {
|
||||
return item.equals("");
|
||||
}
|
||||
public boolean hasDirection() {
|
||||
return CommandWords.isDirection(commandWord);
|
||||
return CommandWords.isDirection(direction);
|
||||
}
|
||||
public String getDirection() {
|
||||
return direction;
|
||||
|
||||
@@ -49,7 +49,11 @@ class CommandWords {
|
||||
}
|
||||
// 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){
|
||||
|
||||
@@ -12,7 +12,7 @@ public class Player extends Entity{
|
||||
super();
|
||||
}
|
||||
|
||||
private boolean addToInventory(Item item){
|
||||
public boolean addToInventory(Item item){
|
||||
if(currentInventoryWeight + item.getWeight() < INVENTORY_CAPACITY){
|
||||
inventory.add(item);
|
||||
System.out.println(item.getName() + " add");
|
||||
@@ -21,7 +21,7 @@ public class Player extends Entity{
|
||||
return false;
|
||||
}
|
||||
|
||||
private void removeFromInventory(Item item){
|
||||
public void removeFromInventory(Item item){
|
||||
inventory.remove(item);
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ 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;
|
||||
|
||||
@@ -34,10 +35,10 @@ class Game {
|
||||
// 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 HashMap<Item, String> itemsInRooms = new HashMap<Item, String>();
|
||||
|
||||
private void initRooms(String fileName) throws Exception {
|
||||
itemsInRooms.put(new Candlestick(), "Candlestick");
|
||||
//itemsInRooms.put(new Candlestick(), "Candlestick");
|
||||
masterRoomMap = new HashMap<String, Room>();
|
||||
Scanner roomScanner;
|
||||
try {
|
||||
@@ -51,6 +52,19 @@ class Game {
|
||||
// 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
|
||||
@@ -98,7 +112,6 @@ class Game {
|
||||
initRooms("data/Rooms.dat");
|
||||
currentRoom = masterRoomMap.get("ROOM_1");
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
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?");
|
||||
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;
|
||||
|
||||
@@ -45,7 +45,7 @@ class Parser {
|
||||
} catch (java.io.IOException exc) {
|
||||
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()) {
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
@@ -31,6 +34,7 @@ class Room {
|
||||
public Room(String description) {
|
||||
this.description = description;
|
||||
exits = new HashMap<String, Room>();
|
||||
items = new ArrayList<Item>();
|
||||
}
|
||||
|
||||
public Room() {
|
||||
@@ -38,6 +42,7 @@ class Room {
|
||||
roomName = "DEFAULT ROOM";
|
||||
description = "DEFAULT DESCRIPTION";
|
||||
exits = new HashMap<String, Room>();
|
||||
items = new ArrayList<Item>();
|
||||
}
|
||||
|
||||
public void setExit(char direction, Room r) throws Exception {
|
||||
@@ -69,6 +74,47 @@ class Room {
|
||||
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).
|
||||
|
||||
Reference in New Issue
Block a user