From 3358439e0261825b4cb30934251b43cc7c740027 Mon Sep 17 00:00:00 2001 From: jslightham <31053827+jslightham@users.noreply.github.com> Date: Fri, 26 Apr 2019 15:35:03 -0400 Subject: [PATCH] items --- data/rooms.dat | 2 +- data/words.dat | 5 +- src/com/bayviewglen/zork/Command.java | 7 ++- src/com/bayviewglen/zork/CommandWords.java | 10 +++- src/com/bayviewglen/zork/Entities/Entity.java | 6 ++ src/com/bayviewglen/zork/Entities/Player.java | 29 ++++++++++ src/com/bayviewglen/zork/Game.java | 12 +++- .../bayviewglen/zork/Items/Candlestick.java | 9 +++ src/com/bayviewglen/zork/Items/Item.java | 58 +++++++++++++++++++ src/com/bayviewglen/zork/Parser.java | 23 +++----- 10 files changed, 140 insertions(+), 21 deletions(-) create mode 100644 src/com/bayviewglen/zork/Entities/Entity.java create mode 100644 src/com/bayviewglen/zork/Entities/Player.java create mode 100644 src/com/bayviewglen/zork/Items/Candlestick.java create mode 100644 src/com/bayviewglen/zork/Items/Item.java diff --git a/data/rooms.dat b/data/rooms.dat index 5bf1be5..75186b2 100644 --- a/data/rooms.dat +++ b/data/rooms.dat @@ -1,5 +1,5 @@ Room Name: Room 1 -Room Description: This is 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 diff --git a/data/words.dat b/data/words.dat index 153524f..7aa696f 100644 --- a/data/words.dat +++ b/data/words.dat @@ -11,4 +11,7 @@ w, direction s, direction e, direction up, direction -down, direction \ No newline at end of file +down, direction +eat, verb +candlestick, item +look, verb \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Command.java b/src/com/bayviewglen/zork/Command.java index 461262a..700468a 100644 --- a/src/com/bayviewglen/zork/Command.java +++ b/src/com/bayviewglen/zork/Command.java @@ -26,16 +26,18 @@ class Command { private String commandWord; private String direction; private ArrayList 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 otherWords, String direction) { + public Command(String firstWord, ArrayList otherWords, String direction, String item) { commandWord = firstWord; this.otherWords = otherWords; this.direction = direction; + this.item = item; } /** @@ -73,6 +75,9 @@ class Command { return otherWords.size() > 0; } + public boolean hasItem(){ + return item.equals(""); + } public boolean hasDirection() { return CommandWords.isDirection(commandWord); } diff --git a/src/com/bayviewglen/zork/CommandWords.java b/src/com/bayviewglen/zork/CommandWords.java index b219abb..dc7c225 100644 --- a/src/com/bayviewglen/zork/CommandWords.java +++ b/src/com/bayviewglen/zork/CommandWords.java @@ -52,6 +52,14 @@ class CommandWords { 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. */ @@ -63,4 +71,4 @@ class CommandWords { } System.out.println(); } -} +} \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Entities/Entity.java b/src/com/bayviewglen/zork/Entities/Entity.java new file mode 100644 index 0000000..bb2b79a --- /dev/null +++ b/src/com/bayviewglen/zork/Entities/Entity.java @@ -0,0 +1,6 @@ +package com.bayviewglen.zork.Entities; + +public class Entity { + private int hunger; + private int health; +} diff --git a/src/com/bayviewglen/zork/Entities/Player.java b/src/com/bayviewglen/zork/Entities/Player.java new file mode 100644 index 0000000..297296f --- /dev/null +++ b/src/com/bayviewglen/zork/Entities/Player.java @@ -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 inventory = new ArrayList(); + 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); + + } + +} diff --git a/src/com/bayviewglen/zork/Game.java b/src/com/bayviewglen/zork/Game.java index 02d222b..b3bce28 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -5,6 +5,8 @@ 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. * @@ -32,8 +34,10 @@ class Game { // masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the // Great Room (assuming you have one). private HashMap masterRoomMap; + private HashMap itemsInRooms = new HashMap(); private void initRooms(String fileName) throws Exception { + itemsInRooms.put(new Candlestick(), "Candlestick"); masterRoomMap = new HashMap(); Scanner roomScanner; try { @@ -154,6 +158,12 @@ class Game { 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; } @@ -194,4 +204,4 @@ class Game { } } -} +} \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Items/Candlestick.java b/src/com/bayviewglen/zork/Items/Candlestick.java new file mode 100644 index 0000000..44fd709 --- /dev/null +++ b/src/com/bayviewglen/zork/Items/Candlestick.java @@ -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); + } + + +} \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Items/Item.java b/src/com/bayviewglen/zork/Items/Item.java new file mode 100644 index 0000000..7e4abcc --- /dev/null +++ b/src/com/bayviewglen/zork/Items/Item.java @@ -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; + } + +} diff --git a/src/com/bayviewglen/zork/Parser.java b/src/com/bayviewglen/zork/Parser.java index db35972..6d9c733 100644 --- a/src/com/bayviewglen/zork/Parser.java +++ b/src/com/bayviewglen/zork/Parser.java @@ -34,6 +34,7 @@ class Parser { String inputLine = ""; // will hold the full input line String verb = ""; String direction = ""; + String item = ""; //String word2; ArrayList words = new ArrayList(); ArrayList otherWords = new ArrayList(); @@ -54,28 +55,18 @@ class Parser { if(CommandWords.isDirection(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)); } } - /* - 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); if (CommandWords.isCommand(verb)) - return new Command(verb, otherWords, direction); + return new Command(verb, otherWords, direction, item); else - return new Command(null, otherWords, direction); + return new Command(null, otherWords, direction, item); } /**