From 4a7eca5302feb792be3490d8275351b47db9ec7d Mon Sep 17 00:00:00 2001 From: RaffaeleMorganti Date: Sat, 19 Nov 2022 08:48:21 +0100 Subject: [PATCH] changed types of some collections --- .../java/com/github/dtschust/zork/Zork.java | 147 +++++++++--------- .../dtschust/zork/ZorkConditionHas.java | 15 +- .../github/dtschust/zork/ZorkContainer.java | 5 +- .../github/dtschust/zork/ZorkCreature.java | 7 +- .../com/github/dtschust/zork/ZorkItem.java | 1 - .../com/github/dtschust/zork/ZorkObject.java | 2 + .../com/github/dtschust/zork/ZorkRoom.java | 9 +- .../github/dtschust/zork/parser/ZorkGame.java | 16 +- .../github/dtschust/zork/parser/ZorkMap.java | 13 ++ .../dtschust/zork/parser/ZorkReader.java | 27 ++-- 10 files changed, 130 insertions(+), 112 deletions(-) create mode 100644 src/main/java/com/github/dtschust/zork/parser/ZorkMap.java diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index e6fb451..44c1b26 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -8,9 +8,11 @@ package com.github.dtschust.zork; import com.github.dtschust.zork.parser.ZorkGame; import com.github.dtschust.zork.parser.ZorkReader; -import java.util.HashMap; +import java.util.Map; import java.util.Scanner; +import static java.util.Map.entry; + /* And away we go*/ public class Zork { @@ -140,21 +142,21 @@ public class Zork { if (destinationType.equals("room")) { ZorkRoom tempRoom = game.Rooms.get(destination); if (objectType.equals("item")) - tempRoom.item.put(object, object); + tempRoom.item.add(object); else if (objectType.equals("creature")) - tempRoom.creature.put(object, object); + tempRoom.creature.add(object); else if (objectType.equals("container")) - tempRoom.container.put(object, object); + tempRoom.container.add(object); else System.out.println("Error"); - game.Rooms.put(tempRoom.name, tempRoom); + game.Rooms.put(tempRoom); } else if (destinationType.equals("container")) { ZorkContainer tempContainer = game.Containers.get(destination); if (objectType.equals("item")) - tempContainer.item.put(object, object); + tempContainer.item.add(object); else System.out.println("Error"); - game.Containers.put(tempContainer.name, tempContainer); + game.Containers.put(tempContainer); } else { System.out.println("Error"); } @@ -172,37 +174,37 @@ public class Zork { tempRoom.border.remove(key2); } } - game.Rooms.put(tempRoom.name, tempRoom); + game.Rooms.put(tempRoom); } } else if (objectType.equals("item")) { for (String key : game.Rooms.keySet()) { ZorkRoom tempRoom = game.Rooms.get(key); - if (tempRoom.item.get(object) != null) { + if (tempRoom.item.contains(object)) { tempRoom.item.remove(object); - game.Rooms.put(tempRoom.name, tempRoom); + game.Rooms.put(tempRoom); } } for (String key : game.Containers.keySet()) { ZorkContainer tempContainer = game.Containers.get(key); - if (tempContainer.item.get(object) != null) { + if (tempContainer.item.contains(object)) { tempContainer.item.remove(object); - game.Containers.put(tempContainer.name, tempContainer); + game.Containers.put(tempContainer); } } } else if (objectType.equals("container")) { for (String key : game.Rooms.keySet()) { ZorkRoom tempRoom = game.Rooms.get(key); - if (tempRoom.container.get(object) != null) { + if (tempRoom.container.contains(object)) { tempRoom.container.remove(object); - game.Rooms.put(tempRoom.name, tempRoom); + game.Rooms.put(tempRoom); } } } else if (objectType.equals("creature")) { for (String key : game.Rooms.keySet()) { ZorkRoom tempRoom = game.Rooms.get(key); - if (tempRoom.creature.get(object) != null) { + if (tempRoom.creature.contains(object)) { tempRoom.creature.remove(object); - game.Rooms.put(tempRoom.name, tempRoom); + game.Rooms.put(tempRoom); } } } @@ -214,30 +216,30 @@ public class Zork { if (objectType.equals("room")) { ZorkRoom tempRoom = game.Rooms.get(object); tempRoom.status = newStatus; - game.Rooms.put(tempRoom.name, tempRoom); + game.Rooms.put(tempRoom); } else if (objectType.equals("container")) { ZorkContainer tempContainer = game.Containers.get(object); tempContainer.status = newStatus; - game.Containers.put(tempContainer.name, tempContainer); + game.Containers.put(tempContainer); } else if (objectType.equals("creature")) { ZorkCreature tempCreature = game.Creatures.get(object); tempCreature.status = newStatus; - game.Creatures.put(tempCreature.name, tempCreature); + game.Creatures.put(tempCreature); } else if (objectType.equals("item")) { ZorkItem tempItem = game.Items.get(object); tempItem.status = newStatus; - game.Items.put(tempItem.name, tempItem); + game.Items.put(tempItem); } } private void doActionAttack(String tempString, String weapon) { ZorkCreature tempCreature; - if (game.Rooms.get(currentRoom).creature.get(tempString) != null) { + if (game.Rooms.get(currentRoom).creature.contains(tempString)) { tempCreature = game.Creatures.get(tempString); - if (tempCreature != null && game.Inventory.get(weapon) != null) { + if (tempCreature != null && game.Inventory.contains(weapon)) { if (tempCreature.attack(this, weapon)) { System.out.println("You assault the " + tempString + " with the " + weapon + "."); for (String print: tempCreature.print) { @@ -254,7 +256,7 @@ public class Zork { } private void doActionTurnOn(String tempString) { - if (game.Inventory.get(tempString) != null) { + if (game.Inventory.contains(tempString)) { ZorkItem tempItem = game.Items.get(tempString); System.out.println("You activate the " + tempString + "."); if (tempItem != null) { @@ -271,21 +273,23 @@ public class Zork { } private void doActionPutItem(String tempString, String destination) { - if (game.Rooms.get(currentRoom).container.get(destination) != null && game.Containers.get(destination).isOpen && game.Inventory.get(tempString) != null) { - ZorkContainer tempContainer = game.Containers.get(destination); - tempContainer.item.put(tempString, tempString); - game.Inventory.remove(tempString); - System.out.println("Item " + tempString + " added to " + destination + "."); - } else { - System.out.println("Error"); + if (game.Rooms.get(currentRoom).container.contains(destination)){ + if(game.Containers.get(destination).isOpen && game.Inventory.contains(tempString)) { + ZorkContainer tempContainer = game.Containers.get(destination); + tempContainer.item.add(tempString); + game.Inventory.remove(tempString); + System.out.println("Item " + tempString + " added to " + destination + "."); + return; + } } + System.out.println("Error"); } private void doActionDropItem(String tempString) { - if (game.Inventory.get(tempString) != null) { + if (game.Inventory.contains(tempString)) { ZorkRoom tempRoom = game.Rooms.get(currentRoom); - tempRoom.item.put(tempString, tempString); - game.Rooms.put(tempRoom.name, tempRoom); + tempRoom.item.add(tempString); + game.Rooms.put(tempRoom); game.Inventory.remove(tempString); System.out.println(tempString + " dropped."); } else { @@ -294,7 +298,7 @@ public class Zork { } private void doActionReadObject(String tempString) { - if (game.Inventory.get(tempString) != null) { + if (game.Inventory.contains(tempString)) { ZorkItem tempItem = game.Items.get(tempString); if (tempItem.writing != null && tempItem.writing != "") { System.out.println(tempItem.writing); @@ -308,11 +312,20 @@ public class Zork { private void doActionOpenContainer(String tempString) { ZorkContainer tempContainer; - String found = game.Rooms.get(currentRoom).container.get(tempString); - if (found != null) { + if (game.Rooms.get(currentRoom).container.contains(tempString)) { tempContainer = game.Containers.get(tempString); tempContainer.isOpen = true; - doActionContainerInventory(tempContainer.item, tempString); + String output = ""; + if (tempContainer.item.isEmpty()) { + System.out.println(tempString + " is empty"); + } else { + System.out.print(tempString + " contains "); + for (String key : tempContainer.item) { + output += key + ", "; + } + output = output.substring(0, output.length() - 2); + System.out.println(output + "."); + } } else { System.out.println("Error"); } @@ -329,18 +342,14 @@ public class Zork { /*Basic movement function */ private void doActionMove(String direction) { - String fullDirection = ""; - if (direction.equals("n")) { - fullDirection = "north"; - } else if (direction.equals("s")) { - fullDirection = "south"; - } else if (direction.equals("e")) { - fullDirection = "east"; - } else if (direction.equals("w")) { - fullDirection = "west"; - } + final Map fullDirections = Map.ofEntries( + entry("n", "north"), + entry("s", "south"), + entry("e", "east"), + entry("w", "west") + ); - String destination = (game.Rooms.get(currentRoom)).border.get(fullDirection); + String destination = (game.Rooms.get(currentRoom)).border.get(fullDirections.get(direction)); if (destination != null) { currentRoom = destination; System.out.println(game.Rooms.get(currentRoom).description); @@ -349,20 +358,6 @@ public class Zork { } } - /* Print out the what's in a container when it's been opened*/ - public void doActionContainerInventory(HashMap Container, String Name) { - String output = ""; - if (Container.isEmpty()) { - System.out.println(Name + " is empty"); - } else { - System.out.print(Name + " contains "); - for (String key : Container.keySet()) { - output += key + ", "; - } - output = output.substring(0, output.length() - 2); - System.out.println(output + "."); - } - } /* Print out the inventory when user types i */ private void doActionInventory() { @@ -370,7 +365,7 @@ public class Zork { if (game.Inventory.isEmpty()) { System.out.println("Inventory: empty"); } else { - for (String key : game.Inventory.keySet()) { + for (String key : game.Inventory) { output += key + ", "; } output = output.substring(0, output.length() - 2); @@ -379,21 +374,21 @@ public class Zork { } private void doActionTake(String tempString) { - if ((game.Rooms.get(currentRoom)).item.get(tempString) != null) { - game.Inventory.put(tempString, tempString); + if ((game.Rooms.get(currentRoom)).item.contains(tempString)) { + game.Inventory.add(tempString); ZorkRoom tempRoom = (game.Rooms.get(currentRoom)); tempRoom.item.remove(tempString); - game.Rooms.put(tempRoom.name, tempRoom); + game.Rooms.put(tempRoom); System.out.println("Item " + tempString + " added to inventory."); } else { /*Search all containers in the current room for the item!*/ boolean found = false; - for (String key : game.Rooms.get(currentRoom).container.keySet()) { + for (String key : game.Rooms.get(currentRoom).container) { ZorkContainer tempContainer = game.Containers.get(key); - if (tempContainer != null && tempContainer.isOpen && tempContainer.item.get(tempString) != null) { - game.Inventory.put(tempString, tempString); + if (tempContainer != null && tempContainer.isOpen && tempContainer.item.contains(tempString)) { + game.Inventory.add(tempString); tempContainer.item.remove(tempString); - game.Containers.put(tempContainer.name, tempContainer); + game.Containers.put(tempContainer); System.out.println("Item " + tempString + " added to inventory."); found = true; break; @@ -429,7 +424,7 @@ public class Zork { private boolean doTriggersContainersInRoom() { boolean skip = false; - for (String key : game.Rooms.get(currentRoom).container.keySet()) { + for (String key : game.Rooms.get(currentRoom).container) { skip = skip || doZorkTriggers(game.Containers.get(key)); } return skip; @@ -437,9 +432,9 @@ public class Zork { private boolean doTriggersItemsInContainersInRoom() { boolean skip = false; - for (String key : game.Rooms.get(currentRoom).container.keySet()) { + for (String key : game.Rooms.get(currentRoom).container) { ZorkContainer tempContainer = game.Containers.get(key); - for (String key2 : tempContainer.item.keySet()) { + for (String key2 : tempContainer.item) { skip = skip || doZorkTriggers(game.Items.get(key2)); } } @@ -448,7 +443,7 @@ public class Zork { private boolean doTriggersItemsInRoom() { boolean skip = false; - for (String key : game.Rooms.get(currentRoom).item.keySet()) { + for (String key : game.Rooms.get(currentRoom).item) { skip = skip || doZorkTriggers(game.Items.get(key)); } return skip; @@ -456,7 +451,7 @@ public class Zork { private boolean doTriggersItemsInInventory() { boolean skip = false; - for (String key : game.Inventory.keySet()) { + for (String key : game.Inventory) { skip = skip || doZorkTriggers(game.Items.get(key)); } return skip; @@ -464,7 +459,7 @@ public class Zork { private boolean doTriggersCreaturesInRoom() { boolean skip = false; - for (String key : game.Rooms.get(currentRoom).creature.keySet()) { + for (String key : game.Rooms.get(currentRoom).creature) { skip = skip || doZorkTriggers(game.Creatures.get(key)); } return skip; diff --git a/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java b/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java index 161aa11..a069858 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java +++ b/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java @@ -1,5 +1,7 @@ package com.github.dtschust.zork; +import java.util.Set; + /* Has conditions*/ public class ZorkConditionHas extends ZorkCondition { private final String has; @@ -15,23 +17,28 @@ public class ZorkConditionHas extends ZorkCondition { public boolean evaluate(Zork zork) { /*Inventory is a special case as it isn't the name of any object in the game, check for it specifically*/ if (owner.equals("inventory")) { - return zork.game.Inventory.get(object) != null && has.equals("yes") || zork.game.Inventory.get(object) == null && has.equals("no"); + return evaluateCondition(zork.game.Inventory, object); } else { /* is it a room?*/ ZorkRoom roomObject = zork.game.Rooms.get(owner); if (roomObject != null) { - return (roomObject).item.get(object) != null && has.equals("yes") || (roomObject).item.get(object) == null && has.equals("no"); + return evaluateCondition(roomObject.item, object); } /* is it a container?*/ else { ZorkContainer containerObject = zork.game.Containers.get(owner); if (containerObject != null) { - return (containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no"); - + return evaluateCondition(containerObject.item, object); } } } return false; } + + private boolean evaluateCondition(Set items, String object){ + if(has.equals("yes")) return items.contains(object); + else if(has.equals("no")) return !items.contains(object); + return false; + } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkContainer.java b/src/main/java/com/github/dtschust/zork/ZorkContainer.java index 1900a53..b0dec51 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkContainer.java +++ b/src/main/java/com/github/dtschust/zork/ZorkContainer.java @@ -2,11 +2,12 @@ package com.github.dtschust.zork; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; /* Container*/ public class ZorkContainer extends ZorkObject { - public String name; - public HashMap item = new HashMap<>(); + public Set item = new HashSet(); public String description; public ArrayList accept = new ArrayList<>(); public boolean isOpen; diff --git a/src/main/java/com/github/dtschust/zork/ZorkCreature.java b/src/main/java/com/github/dtschust/zork/ZorkCreature.java index 32cdef4..3198a41 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkCreature.java +++ b/src/main/java/com/github/dtschust/zork/ZorkCreature.java @@ -2,12 +2,13 @@ package com.github.dtschust.zork; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; /* Creature*/ public class ZorkCreature extends ZorkObject { - public String name; public String description; - public HashMap vulnerability = new HashMap<>(); + public Set vulnerability = new HashSet<>(); public ArrayList conditions = new ArrayList<>(); public ArrayList print = new ArrayList<>(); public ArrayList action = new ArrayList<>(); @@ -17,7 +18,7 @@ public class ZorkCreature extends ZorkObject { /* Evaluate the success of an attack*/ public boolean attack(Zork zork, String weapon) { - if (vulnerability.get(weapon) == null) { + if (!vulnerability.contains(weapon)) { return false; } for (ZorkCondition condition : conditions) { diff --git a/src/main/java/com/github/dtschust/zork/ZorkItem.java b/src/main/java/com/github/dtschust/zork/ZorkItem.java index 6af7143..20d21f5 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkItem.java +++ b/src/main/java/com/github/dtschust/zork/ZorkItem.java @@ -4,7 +4,6 @@ import java.util.ArrayList; /* Item*/ public class ZorkItem extends ZorkObject { - public String name; public String description; public String writing; public ArrayList turnOnPrint = new ArrayList<>(); diff --git a/src/main/java/com/github/dtschust/zork/ZorkObject.java b/src/main/java/com/github/dtschust/zork/ZorkObject.java index 079a6cc..016175f 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkObject.java +++ b/src/main/java/com/github/dtschust/zork/ZorkObject.java @@ -5,6 +5,8 @@ import java.util.ArrayList; /* Generic object, everything inherits from this*/ public class ZorkObject { public String status; + + public String name; public ArrayList trigger = new ArrayList<>(); public ZorkObject() { diff --git a/src/main/java/com/github/dtschust/zork/ZorkRoom.java b/src/main/java/com/github/dtschust/zork/ZorkRoom.java index debe9f3..15634a8 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkRoom.java +++ b/src/main/java/com/github/dtschust/zork/ZorkRoom.java @@ -1,16 +1,17 @@ package com.github.dtschust.zork; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; /* Room*/ public class ZorkRoom extends ZorkObject { - public String name; public String type = "regular"; public String description; public HashMap border = new HashMap<>(); - public HashMap container = new HashMap<>(); - public HashMap item = new HashMap<>(); - public HashMap creature = new HashMap<>(); + public Set container = new HashSet<>(); + public Set item = new HashSet<>(); + public Set creature = new HashSet<>(); public ZorkRoom() { } diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkGame.java b/src/main/java/com/github/dtschust/zork/parser/ZorkGame.java index 0d93076..5bfe596 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ZorkGame.java +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkGame.java @@ -2,17 +2,17 @@ package com.github.dtschust.zork.parser; import com.github.dtschust.zork.*; -import java.io.File; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; public class ZorkGame { - public HashMap Rooms = new HashMap<>(); - public HashMap Items = new HashMap<>(); - public HashMap Containers = new HashMap<>(); - public HashMap Objects = new HashMap<>(); - public HashMap Creatures = new HashMap<>(); - public HashMap Inventory = new HashMap<>(); + public ZorkMap Rooms = new ZorkMap<>(); + public ZorkMap Items = new ZorkMap<>(); + public ZorkMap Containers = new ZorkMap<>(); + public ZorkMap Objects = new ZorkMap<>(); + public ZorkMap Creatures = new ZorkMap<>(); + public Set Inventory = new HashSet<>(); public HashMap ObjectLookup = new HashMap<>(); - } diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkMap.java b/src/main/java/com/github/dtschust/zork/parser/ZorkMap.java new file mode 100644 index 0000000..31f73d7 --- /dev/null +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkMap.java @@ -0,0 +1,13 @@ +package com.github.dtschust.zork.parser; + +import com.github.dtschust.zork.ZorkObject; + +import java.util.HashMap; + +public class ZorkMap extends HashMap{ + + public T put(T object){ + return put(object.name, object); + } +} + diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkReader.java b/src/main/java/com/github/dtschust/zork/parser/ZorkReader.java index f5e49d3..47d9314 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ZorkReader.java +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkReader.java @@ -81,14 +81,14 @@ public class ZorkReader { for (j = 0; j < items.getLength(); j++) { Element item = (Element) items.item(j); String itemName = getString(item); - tempRoom.item.put(itemName, itemName); + tempRoom.item.add(itemName); } NodeList creatures = element.getElementsByTagName("creature"); for (j = 0; j < creatures.getLength(); j++) { Element creature = (Element) creatures.item(j); String creatureName = getString(creature); - tempRoom.creature.put(creatureName, creatureName); + tempRoom.creature.add(creatureName); } readTriggersInObject(element, tempRoom); @@ -97,7 +97,7 @@ public class ZorkReader { for (j = 0; j < containers.getLength(); j++) { Element container = (Element) containers.item(j); String containerName = getString(container); - tempRoom.container.put(containerName, containerName); + tempRoom.container.add(containerName); } NodeList borders = element.getElementsByTagName("border"); @@ -107,10 +107,9 @@ public class ZorkReader { String borderName = getString((Element) border.getElementsByTagName("name").item(0)); tempRoom.border.put(borderDirection, borderName); } - /*Add this room to the rooms hashmap, put it in the generic objects hashmap, and store it's type in the objectlookup hashmap*/ - data.Rooms.put(tempRoom.name, tempRoom); - data.Objects.put(tempRoom.name, tempRoom); + data.Rooms.put(tempRoom); + data.Objects.put(tempRoom); data.ObjectLookup.put(tempRoom.name, "room"); } @@ -154,8 +153,8 @@ public class ZorkReader { readTriggersInObject(element, tempItem); /* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/ - data.Items.put(tempItem.name, tempItem); - data.Objects.put(tempItem.name, tempItem); + data.Items.put(tempItem); + data.Objects.put(tempItem); data.ObjectLookup.put(tempItem.name, "item"); } @@ -191,14 +190,14 @@ public class ZorkReader { for (j = 0; j < citems.getLength(); j++) { Element item = (Element) citems.item(j); String itemName = getString(item); - tempCont.item.put(itemName, itemName); + tempCont.item.add(itemName); } readTriggersInObject(element, tempCont); /* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/ - data.Containers.put(tempCont.name, tempCont); - data.Objects.put(tempCont.name, tempCont); + data.Containers.put(tempCont); + data.Objects.put(tempCont); data.ObjectLookup.put(tempCont.name, "container"); } @@ -223,7 +222,7 @@ public class ZorkReader { NodeList vulns = element.getElementsByTagName("vulnerability"); for (j = 0; j < vulns.getLength(); j++) { String vulnString = getString((Element) vulns.item(j)); - tempCreature.vulnerability.put(vulnString, vulnString); + tempCreature.vulnerability.add(vulnString); } NodeList attacks = element.getElementsByTagName("attack"); @@ -247,8 +246,8 @@ public class ZorkReader { readTriggersInObject(element, tempCreature); /* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/ - data.Creatures.put(tempCreature.name, tempCreature); - data.Objects.put(tempCreature.name, tempCreature); + data.Creatures.put(tempCreature); + data.Objects.put(tempCreature); data.ObjectLookup.put(tempCreature.name, "creature"); }