changed types of some collections

This commit is contained in:
RaffaeleMorganti 2022-11-19 08:48:21 +01:00
parent 2e12ab7f1f
commit 4a7eca5302
10 changed files with 130 additions and 112 deletions

View File

@ -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<String, String> 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<String, String> 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;

View File

@ -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;
}
}

View File

@ -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<String, String> item = new HashMap<>();
public Set<String> item = new HashSet<String>();
public String description;
public ArrayList<String> accept = new ArrayList<>();
public boolean isOpen;

View File

@ -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<String, String> vulnerability = new HashMap<>();
public Set<String> vulnerability = new HashSet<>();
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
public ArrayList<String> print = new ArrayList<>();
public ArrayList<String> 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) {

View File

@ -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<String> turnOnPrint = new ArrayList<>();

View File

@ -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<ZorkTrigger> trigger = new ArrayList<>();
public ZorkObject() {

View File

@ -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<String, String> border = new HashMap<>();
public HashMap<String, String> container = new HashMap<>();
public HashMap<String, String> item = new HashMap<>();
public HashMap<String, String> creature = new HashMap<>();
public Set<String> container = new HashSet<>();
public Set<String> item = new HashSet<>();
public Set<String> creature = new HashSet<>();
public ZorkRoom() {
}

View File

@ -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<String, ZorkRoom> Rooms = new HashMap<>();
public HashMap<String, ZorkItem> Items = new HashMap<>();
public HashMap<String, ZorkContainer> Containers = new HashMap<>();
public HashMap<String, ZorkObject> Objects = new HashMap<>();
public HashMap<String, ZorkCreature> Creatures = new HashMap<>();
public HashMap<String, String> Inventory = new HashMap<>();
public ZorkMap<ZorkRoom> Rooms = new ZorkMap<>();
public ZorkMap<ZorkItem> Items = new ZorkMap<>();
public ZorkMap<ZorkContainer> Containers = new ZorkMap<>();
public ZorkMap<ZorkObject> Objects = new ZorkMap<>();
public ZorkMap<ZorkCreature> Creatures = new ZorkMap<>();
public Set<String> Inventory = new HashSet<>();
public HashMap<String, String> ObjectLookup = new HashMap<>();
}

View File

@ -0,0 +1,13 @@
package com.github.dtschust.zork.parser;
import com.github.dtschust.zork.ZorkObject;
import java.util.HashMap;
public class ZorkMap<T extends ZorkObject> extends HashMap<String, T>{
public T put(T object){
return put(object.name, object);
}
}

View File

@ -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");
}