fixing item display

This commit is contained in:
Luca Carnegie
2019-05-31 20:10:16 -04:00
parent ad08e5482a
commit 389a7e3c05
4 changed files with 339 additions and 308 deletions

View File

@@ -2,7 +2,7 @@ Room name: Circle Room
Room Description: You are in a circular room. The windows to the east are covered with boards that<br>let in just enough light to see. You spot a man in a tailored suit moving some crates around. Room Description: You are in a circular room. The windows to the east are covered with boards that<br>let in just enough light to see. You spot a man in a tailored suit moving some crates around.
Locked: false Locked: false
Boarded: false Boarded: false
Items: Lightbulb,Candlestick Items: Shavingcream
Riddler: "Hello there. My name is Kevin and I am Sir Pellatt's butler. I understand that my master<comma><br>Sir Pellatt has wrongfully imprisoned you. If you answer my riddle<comma> I can give you something to<br>help you with your escape - nothing comes for free you know!", "What goes moo?", "Cows", Lockpick Riddler: "Hello there. My name is Kevin and I am Sir Pellatt's butler. I understand that my master<comma><br>Sir Pellatt has wrongfully imprisoned you. If you answer my riddle<comma> I can give you something to<br>help you with your escape - nothing comes for free you know!", "What goes moo?", "Cows", Lockpick
Exit Rooms: W-Apple Hallway Exit Rooms: W-Apple Hallway
@@ -116,10 +116,10 @@ Locked: false
Boarded: false Boarded: false
Items:Shaving Cream,Toothbrush,Toothpaste Items:Shaving Cream,Toothbrush,Toothpaste
Riddler: Riddler:
Exit Rooms: N-Sir Henry Mill Pellatt's Bathroom Exit Rooms: N-Sir Henry Mill Pellatt's Bedroom
Room name: Linen Closet Room name: Linen Closet
Room Description: A whiff of lavender-scented laundry detergent enters your nostrils as you step into<br>a dark room. Ah, you must be in the Linen Closet. No one really comes in here other than the servants. I'm pretty sure Sir Pellatt and<br>Lady Pellat don't even know this room exists. Room Description: A whiff of lavender-scented laundry detergent enters your nostrils as you step into<br>a dark room. Ah, you must be in the Linen Closet. No one really comes in here other than the servants. I'm pretty sure Sir Pellatt and<br>Lady Pellatt don't even know this room exists.
Locked: false Locked: false
Boarded: false Boarded: false
Items:Clothes Items:Clothes

View File

@@ -11,6 +11,7 @@ aid, help
benefit, help benefit, help
man, riddler man, riddler
butler, riddler butler, riddler
kevin, riddler
helper, riddler helper, riddler
leave, quit leave, quit
move, go move, go

View File

@@ -42,10 +42,10 @@ class Game {
private HashMap<String, Room> masterRoomMap; private HashMap<String, Room> masterRoomMap;
private HashMap<Enemy, String> masterEnemyMap; private HashMap<Enemy, String> masterEnemyMap;
private Combat currentCombat = null; private Combat currentCombat = null;
//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 {
@@ -55,49 +55,48 @@ class Game {
Room room = new Room(); Room room = new Room();
// Read the Name // Read the Name
String roomName = roomScanner.nextLine(); String roomName = roomScanner.nextLine();
room.setRoomName(roomName.split(":")[1].trim()); room.setRoomName(roomName.split(": ")[1].trim());
// 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 locked state // Read the locked state
boolean locked = Boolean.parseBoolean(roomScanner.nextLine().split(":")[1].replaceAll("<br>", "\n").trim()); boolean locked = Boolean.parseBoolean(roomScanner.nextLine().split(": ")[1].replaceAll("<br>", "\n").trim());
room.setLocked(locked); room.setLocked(locked);
// Read the boarded state // Read the boarded state
boolean boarded = Boolean.parseBoolean(roomScanner.nextLine().split(":")[1].replaceAll("<br>", "\n").trim()); boolean boarded = Boolean.parseBoolean(roomScanner.nextLine().split(": ")[1].replaceAll("<br>", "\n").trim());
room.setBoarded(boarded); room.setBoarded(boarded);
// Read the Items // Read the Items
String items = roomScanner.nextLine(); String items = roomScanner.nextLine();
try { try {
String[] itemArr = items.split(":")[1].split(","); String[] itemArr = items.split(": ")[1].split(",");
for(String s : itemArr) { for (String s : itemArr) {
Class<?> clazz = Class.forName("com.bayviewglen.zork.Items." + s.trim()); Class<?> clazz = Class.forName("com.bayviewglen.zork.Items." + s.trim());
Constructor<?> ctor = clazz.getConstructor(); Constructor<?> ctor = clazz.getConstructor();
Item object = (Item) ctor.newInstance(); Item object = (Item) ctor.newInstance();
room.addItem(object); room.addItem(object);
}
} catch (Exception e) {
} }
}catch(Exception e) { // Initialize the riddle in the room, if it exists
}
//Initialize the riddle in the room, if it exists
String riddlerInfo = roomScanner.nextLine().split(":", 2)[1].trim(); String riddlerInfo = roomScanner.nextLine().split(":", 2)[1].trim();
try { try {
int comma1 = riddlerInfo.indexOf(","); int comma1 = riddlerInfo.indexOf(",");
int comma2 = riddlerInfo.indexOf(",", riddlerInfo.indexOf(",") + 1); int comma2 = riddlerInfo.indexOf(",", riddlerInfo.indexOf(",") + 1);
int comma3 = riddlerInfo.indexOf(",", comma2 +1); int comma3 = riddlerInfo.indexOf(",", comma2 + 1);
String message = riddlerInfo.substring(1, comma1 - 1).replaceAll("<comma>", ",").replaceAll("<br>", "\n"); String message = riddlerInfo.substring(1, comma1 - 1).replaceAll("<comma>", ",").replaceAll("<br>","\n");
String question = riddlerInfo.substring(comma1 + 3, comma2 - 1).replaceAll("<comma>", ",").replaceAll("<br>", "\n"); String question = riddlerInfo.substring(comma1 + 3, comma2 - 1).replaceAll("<comma>", ",").replaceAll("<br>", "\n");
String answer = riddlerInfo.substring(comma2 + 3, comma3 - 1).replaceAll("<comma>", ",").replaceAll("<br>", "\n"); String answer = riddlerInfo.substring(comma2 + 3, comma3 - 1).replaceAll("<comma>", ",").replaceAll("<br>", "\n");
Riddle riddleObj = new Riddle(question, answer); Riddle riddleObj = new Riddle(question, answer);
String item = riddlerInfo.substring(comma3 + 2, riddlerInfo.length()); String item = riddlerInfo.substring(comma3 + 2, riddlerInfo.length());
// Initializes prize object // Initializes prize object
Class<?> clazz = Class.forName("com.bayviewglen.zork.Items." + item.trim()); Class<?> clazz = Class.forName("com.bayviewglen.zork.Items." + item.trim());
Constructor<?> ctor = clazz.getConstructor(); Constructor<?> ctor = clazz.getConstructor();
Item prize = (Item) ctor.newInstance(); Item prize = (Item) ctor.newInstance();
Riddler butler = new Riddler(100, 100, riddleObj, message, prize); Riddler butler = new Riddler(100, 100, riddleObj, message, prize);
room.addRiddler(butler); room.addRiddler(butler);
}catch(Exception e) { } 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
@@ -111,10 +110,10 @@ class Game {
// This puts the room we created (Without the exits in the masterMap) // This puts the room we created (Without the exits in the masterMap)
masterRoomMap.put(roomName.toUpperCase().substring(10).trim().replaceAll(" ", "_"), room); masterRoomMap.put(roomName.toUpperCase().substring(10).trim().replaceAll(" ", "_"), room);
if(roomScanner.hasNextLine()) { if (roomScanner.hasNextLine())
roomScanner.nextLine(); roomScanner.nextLine();
}
} }
for (String key : masterRoomMap.keySet()) { for (String key : masterRoomMap.keySet()) {
@@ -137,16 +136,16 @@ class Game {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void initEnemies(String fileName) throws Exception { private void initEnemies(String fileName) throws Exception {
masterEnemyMap = new HashMap<Enemy, String>(); masterEnemyMap = new HashMap<Enemy, String>();
Scanner enemyScanner = null; Scanner enemyScanner = null;
Enemy e = null; Enemy e = null;
try { try {
enemyScanner = new Scanner(new File(fileName)); enemyScanner = new Scanner(new File(fileName));
while (enemyScanner.hasNext()) { while (enemyScanner.hasNext()) {
e = new Enemy(); e = new Enemy();
// Read the Name // Read the Name
String enemyName = enemyScanner.nextLine(); String enemyName = enemyScanner.nextLine();
e.setName(enemyName.split(":")[1].trim()); e.setName(enemyName.split(":")[1].trim());
@@ -159,12 +158,12 @@ class Game {
// Read the Damage Given // Read the Damage Given
int damageGiven = Integer.parseInt(enemyScanner.nextLine().split(":")[1].trim()); int damageGiven = Integer.parseInt(enemyScanner.nextLine().split(":")[1].trim());
e.setDamageGiven(damageGiven); e.setDamageGiven(damageGiven);
}
}catch(Exception ex) {
} }
masterEnemyMap.put(e, e.getRoom()); } catch (Exception ex) {
enemyScanner.close(); }
} masterEnemyMap.put(e, e.getRoom());
enemyScanner.close();
}
/** /**
* Create the game and initialise its internal map. * Create the game and initialise its internal map.
@@ -180,30 +179,30 @@ class Game {
parser = new Parser(); parser = new Parser();
player = new Player(); player = new Player();
} }
/** /**
* Print out the opening message for the player. * Print out the opening message for the player.
*/ */
private boolean printWelcome() { private boolean printWelcome() {
Scanner in = new Scanner(System.in); Scanner in = new Scanner(System.in);
boolean isNotValid = true; boolean isNotValid = true;
System.out.println("Welcome to ESCAPE CASA LOMA!\n-----"); System.out.println("Welcome to ESCAPE CASA LOMA!\n-----");
System.out.println("A new, fresh take on the escape-room,\nby Johnathon, Luca, Victoria and Evan "); System.out.println("A new, fresh take on the escape-room,\nby Johnathon, Luca, Victoria and Evan ");
System.out.println("Type \"play\" to play the game. If you wish to close the game at any time, type \"quit\"."); System.out.println("Type \"play\" to play the game. If you wish to close the game at any time, type \"quit\".");
while(isNotValid) { while (isNotValid) {
System.out.print("> "); System.out.print("> ");
String i = in.nextLine(); String i = in.nextLine();
if(i.toLowerCase().equals("play") || i.toLowerCase().equals("p")) { if (i.toLowerCase().equals("play") || i.toLowerCase().equals("p")) {
return true; return true;
}else if(i.toLowerCase().equals("quit") || i.toLowerCase().equals("q")) { } else if (i.toLowerCase().equals("quit") || i.toLowerCase().equals("q")) {
in.close(); in.close();
return false; return false;
} }
System.out.println("That is not a valid response. Type \"play\" to play the game. If you wish to close the game, type \"quit\"."); System.out.println(
"That is not a valid response. Type \"play\" to play the game. If you wish to close the game, type \"quit\".");
} }
in.close(); in.close();
return false; return false;
} }
@@ -211,40 +210,41 @@ class Game {
* Main play routine. Loops until end of play. * Main play routine. Loops until end of play.
*/ */
public void play() { public void play() {
if(printWelcome()) { if (printWelcome()) {
// Enter the main command loop. Here we repeatedly read commands and // Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over. // execute them until the game is over.
System.out.println("\nType 'help' if you need help, consult the wiki \non GitHub if you are confused and enjoy the game!\n"); System.out.println(
"\nType 'help' if you need help, consult the wiki \non GitHub if you are confused and enjoy the game!\n");
System.out.println("\n\nEscape Casa Loma"); System.out.println("\n\nEscape Casa Loma");
System.out.println("---------------------\n"); System.out.println("---------------------\n");
System.out.print(currentRoom.longDescription()); System.out.print(currentRoom.longDescription());
System.out.println(currentRoom.exitString());
System.out.println(currentRoom.itemString()); System.out.println(currentRoom.itemString());
System.out.println(currentRoom.exitString());
boolean finished = false; boolean finished = false;
while (!finished) { while (!finished) {
if(currentCombat != null) { if (currentCombat != null) {
if(currentCombat.getEnemy().getHealth() <= 0.0) { if (currentCombat.getEnemy().getHealth() <= 0.0) {
System.out.println("You destroyed " + currentCombat.getEnemy().getName()); System.out.println("You destroyed " + currentCombat.getEnemy().getName());
masterEnemyMap.values().remove(currentRoom.getRoomName()); masterEnemyMap.values().remove(currentRoom.getRoomName());
currentCombat = null; currentCombat = null;
} } else if (currentCombat.getTurn() == 1) {
else if(currentCombat.getTurn() == 1) {
currentCombat.enemyAttack(); currentCombat.enemyAttack();
if(currentCombat.getPlayer().getHealth() <=0.0) { if (currentCombat.getPlayer().getHealth() <= 0.0) {
System.out.println("You were destroyed by " + currentCombat.getEnemy().getName()); System.out.println("You were destroyed by " + currentCombat.getEnemy().getName());
for(int i =0; i<player.getInventory().size(); i++) { for (int i = 0; i < player.getInventory().size(); i++) {
currentRoom.addItem(player.getInventory().get(i)); currentRoom.addItem(player.getInventory().get(i));
player.removeFromInventory(player.getInventory().get(i)); player.removeFromInventory(player.getInventory().get(i));
} }
currentRoom = masterRoomMap.get("CIRCLE_ROOM"); currentRoom = masterRoomMap.get("CIRCLE_ROOM");
System.out.println("Poof! You looked pretty banged up there, so I brought you back to the circle room. Your items are where you died."); System.out.println(
"Poof! You looked pretty banged up there, so I brought you back to the circle room. Your items are where you died.");
player.setHealth(100.0); player.setHealth(100.0);
currentCombat.getEnemy().setHealth(100.0); currentCombat.getEnemy().setHealth(100.0);
currentCombat = null; currentCombat = null;
} }
} }
} }
Command command = parser.getCommand(); Command command = parser.getCommand();
finished = processCommand(command); finished = processCommand(command);
} }
@@ -252,305 +252,327 @@ class Game {
System.out.println("Thank you for playing. Goodbye!"); System.out.println("Thank you for playing. Goodbye!");
} }
/** /**
* Given a command, process (that is: execute) the command. If this command * Given a command, process (that is: execute) the command. If this command ends
* ends the game, true is returned, otherwise false is returned. * the game, true is returned, otherwise false is returned.
*/ */
private boolean processCommand(Command command) { private boolean processCommand(Command command) {
if (command.isUnknown()) { if (command.isUnknown()) {
System.out.println("I don't know what you mean..."); System.out.println("I don't know what you mean...");
return false; return false;
} }
String commandWord = command.getCommandWord(); String commandWord = command.getCommandWord();
switch(commandWord) { switch (commandWord) {
case "open": case "unlock": case "open":
case "unlock":
boolean hasLockPick = false; boolean hasLockPick = false;
for(int i =0; i<player.getInventory().size(); i++) { for (int i = 0; i < player.getInventory().size(); i++) {
if(player.getInventory().get(i).equals(new Lockpick())) { if (player.getInventory().get(i).equals(new Lockpick())) {
hasLockPick = true; hasLockPick = true;
break; break;
} }
} }
if(command.hasDirection() && hasLockPick) { if (command.hasDirection() && hasLockPick) {
Room nextRoom = currentRoom.nextRoom(command.getDirection()); Room nextRoom = currentRoom.nextRoom(command.getDirection());
try { try {
if(nextRoom.getLocked()) { if (nextRoom.getLocked()) {
nextRoom.setLocked(false); nextRoom.setLocked(false);
player.removeFromInventory(new Lockpick()); player.removeFromInventory(new Lockpick());
System.out.println("After a little bit of picking, a click is heard and the door opens slightly!"); System.out.println(
}else{ "After a little bit of picking, a click is heard and the door opens slightly!");
System.out.println("That door is already unlocked!"); } else {
} System.out.println("That door is already unlocked!");
}catch(Exception e) { }
} catch (Exception e) {
System.out.println("There is no door there!"); System.out.println("There is no door there!");
} }
}else if(!command.hasDirection()){ } else if (!command.hasDirection()) {
System.out.println("In what direction do you want to go in?"); System.out.println("In what direction do you want to go in?");
}else { } else {
System.out.println("What do you want to open the door with?"); System.out.println("What do you want to open the door with?");
} }
boolean hasCrowbar = false; boolean hasCrowbar = false;
for(int i =0; i<player.getInventory().size(); i++) { for (int i = 0; i < player.getInventory().size(); i++) {
if(player.getInventory().get(i).equals(new Crowbar())) { if (player.getInventory().get(i).equals(new Crowbar())) {
hasCrowbar = true; hasCrowbar = true;
break; break;
} }
} }
if(command.hasDirection() && hasCrowbar) { if (command.hasDirection() && hasCrowbar) {
Room nextRoom = currentRoom.nextRoom(command.getDirection()); Room nextRoom = currentRoom.nextRoom(command.getDirection());
try { try {
if(nextRoom.getBoarded()) { if (nextRoom.getBoarded()) {
nextRoom.setBoarded(false); nextRoom.setBoarded(false);
player.removeFromInventory(new Crowbar()); player.removeFromInventory(new Crowbar());
System.out.println("With great effort, you pry the boards off the door with the crowbar! However, it breaks and is no longer useable."); System.out.println(
}else{ "With great effort, you pry the boards off the door with the crowbar! However, it breaks and is no longer useable.");
System.out.println("That door is already unlocked!"); } else {
} System.out.println("That door is already unlocked!");
}catch(Exception e) { }
} catch (Exception e) {
System.out.println("There is no door there!"); System.out.println("There is no door there!");
} }
}else if(!command.hasDirection()){ } else if (!command.hasDirection()) {
}else { } else {
} }
break; break;
case "go": case "n": case "s": case "e": case "w": case "north": case "south": case "west": case "east": case "up": case "down": case "d": case "u": case "go":
if(currentCombat == null) case "n":
case "s":
case "e":
case "w":
case "north":
case "south":
case "west":
case "east":
case "up":
case "down":
case "d":
case "u":
if (currentCombat == null)
goRoom(command); goRoom(command);
else else
System.out.println("You can't leave the room during combat!"); System.out.println("You can't leave the room during combat!");
break; break;
case "help": case "help":
printHelp(); printHelp();
break; break;
case "jump": case "jump":
System.out.println("Good Job!"); System.out.println("Good Job!");
break; break;
case "quit": case "quit":
return true; return true;
case "die": case "die":
System.out.println("If you insist... \nPoof! You're gone. You're out of the castle now, but now a new, grand new adventure begins..."); System.out.println("If you insist... \nPoof! You're gone. You're out of the castle now, but now a new, grand new adventure begins...");
return true; return true;
case "eat": case "eat":
if(command.hasItem()) { if (command.hasItem()) {
Class<?> clazz; Class<?> clazz;
Item object; Item object;
try { try {
clazz = Class.forName("com.bayviewglen.zork.Items." + command.getItem().substring(0, 1).toUpperCase().trim() + command.getItem().substring(1).trim()); clazz = Class.forName(
Constructor<?> ctor = clazz.getConstructor(); "com.bayviewglen.zork.Items." + command.getItem().substring(0, 1).toUpperCase().trim()
object = (Item) ctor.newInstance(); + command.getItem().substring(1).trim());
boolean hasItem = false; Constructor<?> ctor = clazz.getConstructor();
for(int i=0; i<player.getInventory().size(); i++) { object = (Item) ctor.newInstance();
if(object.equals(player.getInventory().get(i))) { boolean hasItem = false;
hasItem = true; for (int i = 0; i < player.getInventory().size(); i++) {
} if (object.equals(player.getInventory().get(i))) {
hasItem = true;
} }
if(object.isConsumable() && hasItem) { }
System.out.println("Nom Nom Nom..."); if (object.isConsumable() && hasItem) {
player.eat(); System.out.println("Nom Nom Nom...");
System.out.println("Your health is now at " + player.getHealth() + "%"); player.eat();
player.removeFromInventory(object); System.out.println("Your health is now at " + player.getHealth() + "%");
if(currentCombat != null) player.removeFromInventory(object);
currentCombat.setEnemyTurn(); if (currentCombat != null)
}else if(object.isConsumable()) { currentCombat.setEnemyTurn();
System.out.println("You don't have a " + command.getItem()); } else if (object.isConsumable()) {
}else { System.out.println("You don't have a " + command.getItem());
System.out.println("Sorry, you can't eat a " + command.getItem()); } else {
}
} catch(Exception e) {
System.out.println("Sorry, you can't eat a " + command.getItem()); System.out.println("Sorry, you can't eat a " + command.getItem());
} }
}else { } catch (Exception e) {
System.out.println("Eat what?"); System.out.println("Sorry, you can't eat a " + command.getItem());
} }
break; } else {
case "talk": System.out.println("Eat what?");
if(currentRoom.hasRiddler()) { }
Scanner rScanner = new Scanner(System.in); break;
String message = currentRoom.getRiddler().getMessage(); case "talk":
String riddle = currentRoom.getRiddler().getRiddle().getQuestion(); if (currentCombat == null) {
String answer = currentRoom.getRiddler().getRiddle().getAnswer(); if (currentRoom.hasRiddler()) {
Scanner rScanner = new Scanner(System.in);
String message = currentRoom.getRiddler().getMessage();
String riddle = currentRoom.getRiddler().getRiddle().getQuestion();
String answer = currentRoom.getRiddler().getRiddle().getAnswer();
System.out.println(message + "\n\nHere's my riddle: " + riddle); System.out.println(message + "\n\nHere's my riddle: " + riddle);
System.out.print("Enter your guess here: "); System.out.print("Enter your guess here: ");
String guess = rScanner.nextLine(); String guess = rScanner.nextLine();
if(guess.toLowerCase().equals(answer.toLowerCase())) { if (guess.toLowerCase().equals(answer.toLowerCase())) {
Item prize = currentRoom.getRiddler().getPrize(); Item prize = currentRoom.getRiddler().getPrize();
String prizeName = prize.getName(); String prizeName = prize.getName();
System.out.println("Congratulations! You solved my riddle! As your reward, you get a " + prizeName + "!"); System.out.println("Congratulations! You solved my riddle! As your reward, you get a " + prizeName + "!");
if(player.addToInventory(prize)) { if (player.addToInventory(prize)) {
System.out.println("A " + prizeName + " has been added to your inventory."); System.out.println("A " + prizeName + " has been added to your inventory.");
System.out.println("I've got to go find Mr. Pellatt now. Good luck with your escape!"); System.out.println("I've got to go find Mr. Pellatt now. Good luck with your escape!");
currentRoom.removeRiddler(); currentRoom.removeRiddler();
}else { } else {
System.out.println("Sorry, you can't carry any more, but a " + prize + " has been added to your room."); System.out.println("Sorry, you can't carry any more, but a " + prize + " has been added to your room.");
currentRoom.addItem(prize); currentRoom.addItem(prize);
} }
}else { } else {
System.out.println("Sorry, that isn't the answer. Think about it, then try again."); System.out.println("Sorry, that isn't the answer. Think about it, then try again.");
} }
}else { } else {
System.out.println("Talk to who?"); System.out.println("Talk to who?");
} }
break; }else {
case "scream": System.out.println("You can't talk to someone while in battle!");
System.out.println("Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhh!"); }
break; break;
case "take": case "scream":
boolean hasAll = false; System.out.println("Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhh!");
for(String a : command.getOtherWords()) { break;
if(a.equals("all")) case "take":
hasAll = true; boolean hasAll = false;
for (String a : command.getOtherWords()) {
if (a.equals("all"))
hasAll = true;
}
if (hasAll) {
for (int i = 0; i < currentRoom.getItems().size(); i++) {
if (player.addToInventory(currentRoom.getItem(i))) {
currentRoom.removeItem(i);
i--;
} else {
System.out.println("You can't carry any more stuff!");
break;
}
System.out.println("Taken");
} }
if(hasAll){ } else if (command.hasItem()) {
for(int i =0; i<currentRoom.getItems().size(); i++) { Class<?> clazz;
if(player.addToInventory(currentRoom.getItem(i))) { Item object;
currentRoom.removeItem(i); try {
i--; clazz = Class.forName(
}else { "com.bayviewglen.zork.Items." + command.getItem().substring(0, 1).toUpperCase().trim()
System.out.println("You can't carry any more stuff!"); + command.getItem().substring(1).trim());
break; Constructor<?> ctor = clazz.getConstructor();
} object = (Item) ctor.newInstance();
if (!currentRoom.hasItem(object)) {
System.out.println("This room has no " + command.getItem() + "!");
} else if (player.addToInventory(object)) {
currentRoom.removeItem(object);
System.out.println("Taken"); System.out.println("Taken");
} else {
System.out.println("You can't carry any more stuff!");
} }
} catch (Exception e) {
} }
else if(command.hasItem()) { } else {
Class<?> clazz; System.out.println("Take what?");
Item object; }
try {
clazz = Class.forName("com.bayviewglen.zork.Items." + command.getItem().substring(0, 1).toUpperCase().trim() + command.getItem().substring(1).trim()); break;
Constructor<?> ctor = clazz.getConstructor();
object = (Item) ctor.newInstance(); case "look":
if(!currentRoom.hasItem(object)) { System.out.print(currentRoom.longDescription());
System.out.println("This room has no " + command.getItem() + "!"); System.out.println(currentRoom.itemString());
System.out.println(currentRoom.exitString());
break;
case "inventory":
case "i":
boolean hasPlayerItems = false;
String itemsP = "";
for (Item i : player.getInventory()) {
hasPlayerItems = true;
itemsP += i.getName() + " ";
}
if (hasPlayerItems) {
System.out.print("Inventory: ");
System.out.print(itemsP);
System.out.println();
} else {
System.out.println("You have nothing on you. Try and find some items.");
}
break;
case "drop":
if (command.hasItem()) {
Class<?> clazz;
Item object;
try {
clazz = Class.forName(
"com.bayviewglen.zork.Items." + command.getItem().substring(0, 1).toUpperCase().trim()
+ command.getItem().substring(1).trim());
Constructor<?> ctor = clazz.getConstructor();
object = (Item) ctor.newInstance();
boolean has = false;
for (int i = 0; i < player.getInventory().size(); i++) {
if (player.getInventory().get(i).equals(object)) {
has = true;
} }
else if(player.addToInventory(object)) {
currentRoom.removeItem(object);
System.out.println("Taken");
}else {
System.out.println("You can't carry any more stuff!");
}
} catch(Exception e) {
} }
}else { if (has) {
System.out.println("Take what?"); player.removeFromInventory(object);
} currentRoom.addItem(object);
System.out.println("You dropped your " + object.getName());
break; } else {
System.out.println("You do not have a " + object.getName());
case "look":
System.out.print(currentRoom.longDescription());
System.out.println(currentRoom.exitString());
System.out.println(currentRoom.itemString());
break;
case "inventory": case "i":
boolean hasPlayerItems = false;
String itemsP = "";
for(Item i : player.getInventory()) {
hasPlayerItems = true;
itemsP += i.getName() + " ";
}
if(hasPlayerItems) {
System.out.print("Inventory: ");
System.out.print(itemsP);
System.out.println();
}else {
System.out.println("You have nothing on you. Try and find some items.");
}
break;
case "drop":
if(command.hasItem()) {
Class<?> clazz;
Item object;
try {
clazz = Class.forName("com.bayviewglen.zork.Items." + command.getItem().substring(0, 1).toUpperCase().trim() + command.getItem().substring(1).trim());
Constructor<?> ctor = clazz.getConstructor();
object = (Item) ctor.newInstance();
boolean has = false;
for(int i =0; i<player.getInventory().size(); i++) {
if(player.getInventory().get(i).equals(object)) {
has = true;
}
}
if(has) {
player.removeFromInventory(object);
currentRoom.addItem(object);
System.out.println("You dropped your " + object.getName());
}else {
System.out.println("You do not have a " + object.getName());
}
} catch(Exception e) {
} }
}else { } catch (Exception e) {
System.out.println("Drop what?");
} }
break; } else {
case "attack": System.out.println("Drop what?");
if(currentCombat == null) { }
if(command.hasEnemy()) { break;
Enemy enemy = null; case "attack":
for (Enemy i : masterEnemyMap.keySet()) { if (currentCombat == null) {
if(masterEnemyMap.get(i).equals(currentRoom.getRoomName())) { if (command.hasEnemy()) {
enemy = i; Enemy enemy = null;
} for (Enemy i : masterEnemyMap.keySet()) {
if (masterEnemyMap.get(i).equals(currentRoom.getRoomName())) {
enemy = i;
} }
if(enemy != null) { }
if(command.hasItem()) { if (enemy != null) {
boolean has = false; if (command.hasItem()) {
for(Item i : player.getInventory()) { boolean has = false;
if(i.getName().toLowerCase().replaceAll("\\s+","").equals(command.getItem())) { for (Item i : player.getInventory()) {
has = true; if (i.getName().toLowerCase().replaceAll("\\s+", "").equals(command.getItem())) {
} has = true;
} }
if(has) {
currentCombat = new Combat(player, enemy);
currentCombat.playerAttack(command.getItem());
}else {
System.out.println("You do not have that weapon!");
}
}else {
System.out.println("Attack with what?");
} }
}else { if (has) {
System.out.println("That enemy is not in this room!"); currentCombat = new Combat(player, enemy);
currentCombat.playerAttack(command.getItem());
} else {
System.out.println("You do not have that weapon!");
} }
}else { } else {
System.out.println("Attack with what?");
}
} else {
System.out.println("That enemy is not in this room!");
}
} else {
System.out.println("Attack what?"); System.out.println("Attack what?");
} }
} else { } else {
if(command.hasItem()) { if (command.hasItem()) {
boolean has = false; boolean has = false;
for(Item i : player.getInventory()) { for (Item i : player.getInventory()) {
if(i.getName().toLowerCase().replaceAll("\\s+","").equals(command.getItem())) { if (i.getName().toLowerCase().replaceAll("\\s+", "").equals(command.getItem())) {
has = true; has = true;
}
}
if(has) {
currentCombat.playerAttack(command.getItem());
}else {
System.out.println("You do not have that weapon!");
} }
} }
if (has) {
currentCombat.playerAttack(command.getItem());
} else {
System.out.println("You do not have that weapon!");
}
} }
break; }
default: break;
return false; default:
return false;
} }
return false; return false;
} }
// implementations of user commands: // implementations of user commands:
/** /**
* Print out some help information. Here we print some stupid, cryptic * Print out some help information. Here we print some stupid, cryptic message
* message and a list of the command words. * and a list of the command words.
*/ */
private void printHelp() { private void printHelp() {
System.out.println("Here's what you can do:"); System.out.println("Here's what you can do:");
@@ -572,38 +594,38 @@ class Game {
Room nextRoom = currentRoom.nextRoom(direction); Room nextRoom = currentRoom.nextRoom(direction);
if (nextRoom == null) if (nextRoom == null)
System.out.println("There is no door!"); System.out.println("There is no door!");
else if(nextRoom.getLocked() && nextRoom.getBoarded()) { else if (nextRoom.getLocked() && nextRoom.getBoarded()) {
System.out.println("The door is locked and boarded shut. You need to find a key and crowbar to open it."); System.out.println("The door is locked and boarded shut. You need to find a key and crowbar to open it.");
} } else if (nextRoom.getLocked()) {
else if(nextRoom.getLocked()) {
System.out.println("The door is locked. You need a key to open it."); System.out.println("The door is locked. You need a key to open it.");
} } else {
else {
currentRoom = nextRoom; currentRoom = nextRoom;
System.out.print(currentRoom.longDescription()); System.out.print(currentRoom.longDescription());
boolean hasEnemy = false; boolean hasEnemy = false;
Enemy enemy = null; Enemy enemy = null;
String room = ""; String room = "";
for (String i : masterEnemyMap.values()) { for (String i : masterEnemyMap.values()) {
if(currentRoom.getRoomName().equals(i)) { if (currentRoom.getRoomName().equals(i)) {
hasEnemy = true; hasEnemy = true;
room = i; room = i;
} }
} }
for (Enemy i : masterEnemyMap.keySet()) { for (Enemy i : masterEnemyMap.keySet()) {
if(masterEnemyMap.get(i).equals(room)) { if (masterEnemyMap.get(i).equals(room)) {
enemy = i; enemy = i;
} }
} }
if(hasEnemy) { if (hasEnemy) {
System.out.println(enemy.getName() + ", " + enemy.getDescription() + " has appeared!"); System.out.println(enemy.getName() + ", " + enemy.getDescription() + " has appeared!");
System.out.println(currentRoom.itemString());
System.out.println(currentRoom.exitString()); System.out.println(currentRoom.exitString());
}else { } else {
System.out.println(currentRoom.itemString());
System.out.println(currentRoom.exitString()); System.out.println(currentRoom.exitString());
} }
} }
} }
public void removeEnemy(String r) { public void removeEnemy(String r) {
masterEnemyMap.values().remove(r); masterEnemyMap.values().remove(r);
} }

View File

@@ -194,11 +194,19 @@ class Room {
} }
public String itemString(){ public String itemString(){
String items = "Items in Room: " + this.items.get(0).getName(); boolean hasItems = false;
for(int i = 1; i < this.items.size(); i++) { String items = "";
items += ", " + this.items.get(i).getName(); String itemString = "Items in room: ";
for(int i = 0; i < this.getItems().size() - 1; i++) {
hasItems = true;
items += this.getItems().get(i).getName() + ", ";
} }
return items; if(hasItems) {
items += this.getItems().get(this.getItems().size() - 1).getName();
return itemString + items;
}else {
return itemString + "none";
}
} }
/** /**