From 389a7e3c05c5b19913aded19037e6084118aebce Mon Sep 17 00:00:00 2001 From: Luca Carnegie <41924665+lcarnegie@users.noreply.github.com> Date: Fri, 31 May 2019 20:10:16 -0400 Subject: [PATCH] fixing item display --- data/rooms.dat | 6 +- data/synonyms.dat | 1 + src/com/bayviewglen/zork/Game.java | 624 +++++++++++++++-------------- src/com/bayviewglen/zork/Room.java | 16 +- 4 files changed, 339 insertions(+), 308 deletions(-) diff --git a/data/rooms.dat b/data/rooms.dat index ee27cde..ddd92e6 100644 --- a/data/rooms.dat +++ b/data/rooms.dat @@ -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
let in just enough light to see. You spot a man in a tailored suit moving some crates around. Locked: 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
Sir Pellatt has wrongfully imprisoned you. If you answer my riddle I can give you something to
help you with your escape - nothing comes for free you know!", "What goes moo?", "Cows", Lockpick Exit Rooms: W-Apple Hallway @@ -116,10 +116,10 @@ Locked: false Boarded: false Items:Shaving Cream,Toothbrush,Toothpaste Riddler: -Exit Rooms: N-Sir Henry Mill Pellatt's Bathroom +Exit Rooms: N-Sir Henry Mill Pellatt's Bedroom Room name: Linen Closet -Room Description: A whiff of lavender-scented laundry detergent enters your nostrils as you step into
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
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
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
Lady Pellatt don't even know this room exists. Locked: false Boarded: false Items:Clothes diff --git a/data/synonyms.dat b/data/synonyms.dat index c6e80bd..a9b1aa8 100644 --- a/data/synonyms.dat +++ b/data/synonyms.dat @@ -11,6 +11,7 @@ aid, help benefit, help man, riddler butler, riddler +kevin, riddler helper, riddler leave, quit move, go diff --git a/src/com/bayviewglen/zork/Game.java b/src/com/bayviewglen/zork/Game.java index 98b3334..2e79bcc 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -42,10 +42,10 @@ class Game { private HashMap masterRoomMap; private HashMap masterEnemyMap; private Combat currentCombat = null; - //private HashMap itemsInRooms = new HashMap(); + // private HashMap itemsInRooms = new HashMap(); private void initRooms(String fileName) throws Exception { - //itemsInRooms.put(new Candlestick(), "Candlestick"); + // itemsInRooms.put(new Candlestick(), "Candlestick"); masterRoomMap = new HashMap(); Scanner roomScanner; try { @@ -55,49 +55,48 @@ class Game { Room room = new Room(); // Read the Name String roomName = roomScanner.nextLine(); - room.setRoomName(roomName.split(":")[1].trim()); + room.setRoomName(roomName.split(": ")[1].trim()); // Read the Description String roomDescription = roomScanner.nextLine(); - room.setDescription(roomDescription.split(":")[1].replaceAll("
", "\n").trim()); + room.setDescription(roomDescription.split(": ")[1].replaceAll("
", "\n").trim()); // Read the locked state - boolean locked = Boolean.parseBoolean(roomScanner.nextLine().split(":")[1].replaceAll("
", "\n").trim()); + boolean locked = Boolean.parseBoolean(roomScanner.nextLine().split(": ")[1].replaceAll("
", "\n").trim()); room.setLocked(locked); // Read the boarded state - boolean boarded = Boolean.parseBoolean(roomScanner.nextLine().split(":")[1].replaceAll("
", "\n").trim()); + boolean boarded = Boolean.parseBoolean(roomScanner.nextLine().split(": ")[1].replaceAll("
", "\n").trim()); room.setBoarded(boarded); // 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); + 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) { } - }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(); try { - int comma1 = riddlerInfo.indexOf(","); - int comma2 = riddlerInfo.indexOf(",", riddlerInfo.indexOf(",") + 1); - int comma3 = riddlerInfo.indexOf(",", comma2 +1); - String message = riddlerInfo.substring(1, comma1 - 1).replaceAll("", ",").replaceAll("
", "\n"); + int comma1 = riddlerInfo.indexOf(","); + int comma2 = riddlerInfo.indexOf(",", riddlerInfo.indexOf(",") + 1); + int comma3 = riddlerInfo.indexOf(",", comma2 + 1); + String message = riddlerInfo.substring(1, comma1 - 1).replaceAll("", ",").replaceAll("
","\n"); String question = riddlerInfo.substring(comma1 + 3, comma2 - 1).replaceAll("", ",").replaceAll("
", "\n"); String answer = riddlerInfo.substring(comma2 + 3, comma3 - 1).replaceAll("", ",").replaceAll("
", "\n"); 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 Class clazz = Class.forName("com.bayviewglen.zork.Items." + item.trim()); Constructor ctor = clazz.getConstructor(); 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); - }catch(Exception e) { + } catch (Exception e) { } - - + // Read the Exits String roomExits = roomScanner.nextLine(); // 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) masterRoomMap.put(roomName.toUpperCase().substring(10).trim().replaceAll(" ", "_"), room); - - if(roomScanner.hasNextLine()) { - roomScanner.nextLine(); - } + + if (roomScanner.hasNextLine()) + roomScanner.nextLine(); + } for (String key : masterRoomMap.keySet()) { @@ -137,16 +136,16 @@ class Game { e.printStackTrace(); } } - + private void initEnemies(String fileName) throws Exception { masterEnemyMap = new HashMap(); Scanner enemyScanner = null; Enemy e = null; try { enemyScanner = new Scanner(new File(fileName)); - + while (enemyScanner.hasNext()) { - e = new Enemy(); + e = new Enemy(); // Read the Name String enemyName = enemyScanner.nextLine(); e.setName(enemyName.split(":")[1].trim()); @@ -159,12 +158,12 @@ class Game { // Read the Damage Given int damageGiven = Integer.parseInt(enemyScanner.nextLine().split(":")[1].trim()); e.setDamageGiven(damageGiven); - } - }catch(Exception ex) { } - masterEnemyMap.put(e, e.getRoom()); - enemyScanner.close(); - } + } catch (Exception ex) { + } + masterEnemyMap.put(e, e.getRoom()); + enemyScanner.close(); + } /** * Create the game and initialise its internal map. @@ -180,30 +179,30 @@ class Game { parser = new Parser(); player = new Player(); } - + /** * Print out the opening message for the player. */ private boolean printWelcome() { - Scanner in = new Scanner(System.in); - boolean isNotValid = true; + Scanner in = new Scanner(System.in); + boolean isNotValid = true; 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("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("> "); String i = in.nextLine(); - if(i.toLowerCase().equals("play") || i.toLowerCase().equals("p")) { + if (i.toLowerCase().equals("play") || i.toLowerCase().equals("p")) { return true; - }else if(i.toLowerCase().equals("quit") || i.toLowerCase().equals("q")) { - in.close(); - return false; + } else if (i.toLowerCase().equals("quit") || i.toLowerCase().equals("q")) { + in.close(); + 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(); - return false; - + in.close(); + return false; } @@ -211,40 +210,41 @@ class Game { * Main play routine. Loops until end of play. */ public void play() { - if(printWelcome()) { - // Enter the main command loop. Here we repeatedly read commands and - // 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"); + if (printWelcome()) { + // Enter the main command loop. Here we repeatedly read commands and + // 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("\n\nEscape Casa Loma"); System.out.println("---------------------\n"); - System.out.print(currentRoom.longDescription()); - System.out.println(currentRoom.exitString()); + System.out.print(currentRoom.longDescription()); System.out.println(currentRoom.itemString()); + System.out.println(currentRoom.exitString()); boolean finished = false; while (!finished) { - if(currentCombat != null) { - if(currentCombat.getEnemy().getHealth() <= 0.0) { + if (currentCombat != null) { + if (currentCombat.getEnemy().getHealth() <= 0.0) { System.out.println("You destroyed " + currentCombat.getEnemy().getName()); masterEnemyMap.values().remove(currentRoom.getRoomName()); currentCombat = null; - } - else if(currentCombat.getTurn() == 1) { + } else if (currentCombat.getTurn() == 1) { currentCombat.enemyAttack(); - if(currentCombat.getPlayer().getHealth() <=0.0) { + if (currentCombat.getPlayer().getHealth() <= 0.0) { System.out.println("You were destroyed by " + currentCombat.getEnemy().getName()); - for(int i =0; i 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 hasItem = false; - for(int i=0; i 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 hasItem = false; + 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..."); - player.eat(); - System.out.println("Your health is now at " + player.getHealth() + "%"); - player.removeFromInventory(object); - if(currentCombat != null) - currentCombat.setEnemyTurn(); - }else if(object.isConsumable()) { - System.out.println("You don't have a " + command.getItem()); - }else { - System.out.println("Sorry, you can't eat a " + command.getItem()); - } - } catch(Exception e) { + } + if (object.isConsumable() && hasItem) { + System.out.println("Nom Nom Nom..."); + player.eat(); + System.out.println("Your health is now at " + player.getHealth() + "%"); + player.removeFromInventory(object); + if (currentCombat != null) + currentCombat.setEnemyTurn(); + } else if (object.isConsumable()) { + System.out.println("You don't have a " + command.getItem()); + } else { System.out.println("Sorry, you can't eat a " + command.getItem()); } - }else { - System.out.println("Eat what?"); + } catch (Exception e) { + System.out.println("Sorry, you can't eat a " + command.getItem()); } - break; - case "talk": - 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(); + } else { + System.out.println("Eat what?"); + } + break; + case "talk": + if (currentCombat == null) { + 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.print("Enter your guess here: "); - String guess = rScanner.nextLine(); - if(guess.toLowerCase().equals(answer.toLowerCase())) { - Item prize = currentRoom.getRiddler().getPrize(); - String prizeName = prize.getName(); + String guess = rScanner.nextLine(); + if (guess.toLowerCase().equals(answer.toLowerCase())) { + Item prize = currentRoom.getRiddler().getPrize(); + String prizeName = prize.getName(); 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("I've got to go find Mr. Pellatt now. Good luck with your escape!"); - currentRoom.removeRiddler(); - }else { + currentRoom.removeRiddler(); + } else { System.out.println("Sorry, you can't carry any more, but a " + prize + " has been added to your room."); currentRoom.addItem(prize); } - }else { + } else { System.out.println("Sorry, that isn't the answer. Think about it, then try again."); } - }else { + } else { System.out.println("Talk to who?"); } - break; - case "scream": - System.out.println("Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhh!"); - break; - case "take": - boolean hasAll = false; - for(String a : command.getOtherWords()) { - if(a.equals("all")) - hasAll = true; + }else { + System.out.println("You can't talk to someone while in battle!"); + } + break; + case "scream": + System.out.println("Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhh!"); + break; + case "take": + 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){ - for(int i =0; i 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(); + 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"); + } else { + System.out.println("You can't carry any more stuff!"); } + } catch (Exception e) { + } - else 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(); - if(!currentRoom.hasItem(object)) { - System.out.println("This room has no " + command.getItem() + "!"); + } else { + System.out.println("Take what?"); + } + + break; + + case "look": + System.out.print(currentRoom.longDescription()); + 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 { - System.out.println("Take what?"); - } - - break; - - 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