This commit is contained in:
jslightham
2019-04-26 15:35:03 -04:00
parent 14bd1aa83f
commit 3358439e02
10 changed files with 140 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
Room Name: Room 1 Room Name: Room 1
Room Description: This is room<br>1 Room Description: You are in a stone-walled room, surrounded by
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

View File

@@ -11,4 +11,7 @@ w, direction
s, direction s, direction
e, direction e, direction
up, direction up, direction
down, direction down, direction
eat, verb
candlestick, item
look, verb

View File

@@ -26,16 +26,18 @@ class Command {
private String commandWord; private String commandWord;
private String direction; private String direction;
private ArrayList<String> otherWords; private ArrayList<String> otherWords;
private String item;
/** /**
* Create a command object. First and second word must be supplied, but * 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 * 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. * indicate that this was a command that is not recognised by this game.
*/ */
public Command(String firstWord, ArrayList<String> otherWords, String direction) { public Command(String firstWord, ArrayList<String> otherWords, String direction, String item) {
commandWord = firstWord; commandWord = firstWord;
this.otherWords = otherWords; this.otherWords = otherWords;
this.direction = direction; this.direction = direction;
this.item = item;
} }
/** /**
@@ -73,6 +75,9 @@ class Command {
return otherWords.size() > 0; return otherWords.size() > 0;
} }
public boolean hasItem(){
return item.equals("");
}
public boolean hasDirection() { public boolean hasDirection() {
return CommandWords.isDirection(commandWord); return CommandWords.isDirection(commandWord);
} }

View File

@@ -52,6 +52,14 @@ class CommandWords {
return m_words.get(aString).equals("direction"); 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. * Print all valid commands to System.out.
*/ */
@@ -63,4 +71,4 @@ class CommandWords {
} }
System.out.println(); System.out.println();
} }
} }

View File

@@ -0,0 +1,6 @@
package com.bayviewglen.zork.Entities;
public class Entity {
private int hunger;
private int health;
}

View File

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

View File

@@ -5,6 +5,8 @@ import java.io.FileNotFoundException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Scanner; import java.util.Scanner;
import com.bayviewglen.zork.Items.*;
/** /**
* Class Game - the main class of the "Zork" game. * Class Game - the main class of the "Zork" game.
* *
@@ -32,8 +34,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 void initRooms(String fileName) throws Exception { private void initRooms(String fileName) throws Exception {
itemsInRooms.put(new Candlestick(), "Candlestick");
masterRoomMap = new HashMap<String, Room>(); masterRoomMap = new HashMap<String, Room>();
Scanner roomScanner; Scanner roomScanner;
try { try {
@@ -154,6 +158,12 @@ class Game {
case "eat": case "eat":
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":
for (Item i : itemsInRooms.keySet()) {
System.out.print(i.getName() + " ");
}
System.out.println();
break;
default: default:
return false; return false;
} }
@@ -194,4 +204,4 @@ class Game {
} }
} }
} }

View File

@@ -0,0 +1,9 @@
package com.bayviewglen.zork.Items;
public class Candlestick extends Item{
public Candlestick(){
super(1, "Candlestick", "A slender stick of wax with a small wick of cotton sticking out of the top", true, 100, 1);
}
}

View File

@@ -0,0 +1,58 @@
package com.bayviewglen.zork.Items;
public class Item {
private int id;
private String name;
private String description;
private boolean isConsumable;
private int health;
private int weight;
public Item(int id, String name, String description, boolean isConsumable, int health, int weight) {
this.id = id;
this.name = name;
this.description = description;
this.isConsumable = isConsumable;
this.health = health;
this.weight = weight;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public boolean isConsumable() {
return isConsumable;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
public boolean equals(Item item){
return this.id == item.id && this.name.equals(item.name) && this.description.equals(item.description) && this.isConsumable == item.isConsumable && this.health == item.health && this.weight == item.weight;
}
}

View File

@@ -34,6 +34,7 @@ class Parser {
String inputLine = ""; // will hold the full input line String inputLine = ""; // will hold the full input line
String verb = ""; String verb = "";
String direction = ""; String direction = "";
String item = "";
//String word2; //String word2;
ArrayList<String> words = new ArrayList<String>(); ArrayList<String> words = new ArrayList<String>();
ArrayList<String> otherWords = new ArrayList<String>(); ArrayList<String> otherWords = new ArrayList<String>();
@@ -54,28 +55,18 @@ class Parser {
if(CommandWords.isDirection(words.get(i))) { if(CommandWords.isDirection(words.get(i))) {
direction = words.get(i); direction = words.get(i);
} }
}else { }else if(CommandWords.isItem(words.get(i))){
item = words.get(i);
}
else {
otherWords.add(words.get(i)); otherWords.add(words.get(i));
} }
} }
/*
if (tokenizer.hasMoreTokens())
word1 = tokenizer.nextToken(); // get first word
else
word1 = null;
if (tokenizer.hasMoreTokens())
word2 = tokenizer.nextToken(); // get second word
else
word2 = null;
// note: we just ignore the rest of the input line.
// Now check whether this word is known. If so, create a command
// with it. If not, create a "nil" command (for unknown command).
*/
//System.out.println(verb); //System.out.println(verb);
if (CommandWords.isCommand(verb)) if (CommandWords.isCommand(verb))
return new Command(verb, otherWords, direction); return new Command(verb, otherWords, direction, item);
else else
return new Command(null, otherWords, direction); return new Command(null, otherWords, direction, item);
} }
/** /**