diff --git a/data/rooms.dat b/data/rooms.dat index 9a7797c..8b4358e 100644 --- a/data/rooms.dat +++ b/data/rooms.dat @@ -16,7 +16,7 @@ Exit Rooms: down-Second floor, s-Apple Hallway Room Name: Porcupine Stairs (2nd floor) Room Description: You are now on the second floor. The hallway is completely deserted. A few doors are left slightly ajar. Items: -Exit Rooms: +Exit Rooms: s-Sir Henry Mill Pellat's Bedroom, w-Bulb Room, Room Name: Bulb Room 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. diff --git a/data/words.dat b/data/words.dat index 7aa696f..a65d149 100644 --- a/data/words.dat +++ b/data/words.dat @@ -12,6 +12,9 @@ s, direction e, direction up, direction down, direction +d, direction +u, direction eat, verb candlestick, item -look, verb \ No newline at end of file +look, verb +take, verb \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Command.java b/src/com/bayviewglen/zork/Command.java index 1e9766e..ead2e74 100644 --- a/src/com/bayviewglen/zork/Command.java +++ b/src/com/bayviewglen/zork/Command.java @@ -89,7 +89,7 @@ class Command { } public boolean hasItem(){ - return item.equals(""); + return !item.equals(""); } public boolean hasDirection() { return CommandWords.isDirection(direction); @@ -97,4 +97,7 @@ class Command { public String getDirection() { return direction; } + public String getItem() { + return item; + } } diff --git a/src/com/bayviewglen/zork/Entities/Player.java b/src/com/bayviewglen/zork/Entities/Player.java index 94007f2..45d42b4 100644 --- a/src/com/bayviewglen/zork/Entities/Player.java +++ b/src/com/bayviewglen/zork/Entities/Player.java @@ -1,6 +1,8 @@ package com.bayviewglen.zork.Entities; -import com.bayviewglen.zork.Items.Item; +import com.bayviewglen.zork.Items.*; + +import java.lang.reflect.Constructor; import java.util.ArrayList; public class Player extends Entity{ @@ -12,11 +14,20 @@ public class Player extends Entity{ super(); } - public boolean addToInventory(Item item){ - if(currentInventoryWeight + item.getWeight() < INVENTORY_CAPACITY){ - inventory.add(item); - System.out.println(item.getName() + " add"); - return true; + public boolean addToInventory(String item){ + Class clazz; + Item object; + try { + clazz = Class.forName("com.bayviewglen.zork.Items." + item.trim()); + Constructor ctor = clazz.getConstructor(); + object = (Item) ctor.newInstance(); + if(currentInventoryWeight + object.getWeight() < INVENTORY_CAPACITY){ + inventory.add(object); + + return true; + } + } catch (Exception e) { + return false; } return false; } diff --git a/src/com/bayviewglen/zork/Game.java b/src/com/bayviewglen/zork/Game.java index 62f872a..e52d85e 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -6,6 +6,7 @@ import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Scanner; +import com.bayviewglen.zork.Entities.Player; import com.bayviewglen.zork.Items.*; /** @@ -26,6 +27,7 @@ import com.bayviewglen.zork.Items.*; */ class Game { private Parser parser; + private Player player; private Room currentRoom; // This is a MASTER object that contains all of the rooms and is easily // accessible. @@ -110,11 +112,12 @@ class Game { public Game() { try { initRooms("data/Rooms.dat"); - currentRoom = masterRoomMap.get("ROOM_1"); + currentRoom = masterRoomMap.get("CIRCLE_ROOM"); } catch (Exception e) { e.printStackTrace(); } parser = new Parser(); + player = new Player(); } /** @@ -171,6 +174,20 @@ class Game { case "eat": System.out.println("Do you really think you should be eating at a time like this?"); break; + case "take": + if(command.hasItem()) { + if(player.addToInventory(command.getItem())) { + System.out.println("Taken"); + }else { + System.out.println("You cannot carry any more!"); + } + + }else { + System.out.println("Take what?"); + } + + break; + case "look": System.out.print("Items: "); for(Item i : currentRoom.getItems()) {