Fixed parser, added items starting in rooms, moved item room storage

This commit is contained in:
jslightham
2019-05-08 11:43:21 -04:00
parent 0c44589f03
commit 7fa30879c0
7 changed files with 711 additions and 626 deletions

View File

@@ -1,9 +1,12 @@
Room Name: Room 1 Room Name: Room 1
Room Description: You are in a stone-walled room, surrounded by Room Description: You are in a stone-walled room, surrounded by
Items:Candlestick,Candlestick
Exit Rooms: E-Room 2 Exit Rooms: E-Room 2
Room Name: Room 2 Room Name: Room 2
Room Description: This is Room 2 Room Description: This is Room 2
Items:
Exit Rooms: W-Room 1, U-Room 3 Exit Rooms: W-Room 1, U-Room 3
Room Name: Room 3 Room Name: Room 3
Room Description: You are upstairs. You are likely to be eaten by a giant worm! Room Description: You are upstairs. You are likely to be eaten by a giant worm!
Items:
Exit Rooms: D-Room 2 Exit Rooms: D-Room 2

View File

@@ -37,6 +37,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 +91,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;

View File

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

View File

@@ -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);
} }

View File

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

View File

@@ -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());
} }

View File

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