Zork class extract methods on actions and triggers

This commit is contained in:
RaffaeleMorganti 2022-11-18 16:56:32 +01:00
parent 0a5c67ea69
commit 5d098bf32d

View file

@ -14,7 +14,7 @@ import java.util.Scanner;
/* And away we go*/ /* And away we go*/
public class Zork { public class Zork {
public String userInput = ""; public String userInput;
public String currentRoom; public String currentRoom;
public ZorkGame game; public ZorkGame game;
Scanner source = new Scanner(System.in); Scanner source = new Scanner(System.in);
@ -28,35 +28,29 @@ public class Zork {
/*Phew, we're finally done with that ... let's get playing!!*/ /*Phew, we're finally done with that ... let's get playing!!*/
/* Some temporary initialization variables*/ /* Some temporary initialization variables*/
String tempString;
ZorkContainer tempContainer;
ZorkTrigger tempTrigger;
currentRoom = "Entrance"; currentRoom = "Entrance";
boolean skip = false; boolean ended = false;
/* Print out the first entrance description, starting the game!*/ /* Print out the first entrance description, starting the game!*/
System.out.println(game.Rooms.get(currentRoom).description); System.out.println(game.Rooms.get(currentRoom).description);
/* There is no stopping in Zork, until we're done!!*/ /* There is no stopping in Zork, until we're done!!*/
while (true) { do {
userInput = source.nextLine(); userInput = source.nextLine();
/*Now that we have the user command, check the input*/ /*Now that we have the user command, check the input*/
skip = checkAllTriggers(); if (!executeTriggers()) {
/* If we haven't skipped, perform the user action*/
ended = executeAction(userInput);
/*We only skip if a matched trigger overwrites the execution of a command*/ /* Clear the user input, and check the triggers again (various states have changed, gnomes need to be found!*/
if (skip) { userInput = "";
continue; executeTriggers();
} }
} while (!ended);
/* If we haven't skipped, perform the user action*/ // single point of termination
action(userInput); System.exit(0);
/* Clear the user input, and check the triggers again (various states have changed, gnomes need to be found!*/
userInput = "";
checkAllTriggers();
}
} }
@ -69,306 +63,273 @@ public class Zork {
new Zork(args[0]); new Zork(args[0]);
} }
/* Execute a user action or an action command from some <action> element that is not one of the "Special Commands"*/ public boolean executeAction(String input) {
public void action(String input) { String[] action = input.split(" ");
int y; /* Update: figure out what type of item it is, and then change it's status*/
String tempString; if (action[0].equals("Update")) {
ZorkContainer tempContainer; doActionUpdate(action[1], action[3]);
/* Movement */
if (input.equals("n") || input.equals("s") || input.equals("e") || input.equals("w")) {
move(input);
} }
/* Inventory */ /*Game Over: pretty straight forward*/
else if (input.equals("i")) { else if (input.equals("Game Over")) {
inventory(); return doActionGameWon();
} }
/* Take */ /* Add: figure out what type the destination is, then what type the object is. Then add object to destination if it makes sense */
else if (input.startsWith("take") && input.split(" ").length >= 1) { else if (action[0].equals("Add")) {
tempString = input.split(" ")[1]; doActionAdd(action[1], action[3]);
if ((game.Rooms.get(currentRoom)).item.get(tempString) != null) {
game.Inventory.put(tempString, tempString);
ZorkRoom tempRoom = (game.Rooms.get(currentRoom));
tempRoom.item.remove(tempString);
game.Rooms.put(tempRoom.name, 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()) {
tempContainer = game.Containers.get(key);
if (tempContainer != null && tempContainer.isOpen && tempContainer.item.get(tempString) != null) {
game.Inventory.put(tempString, tempString);
tempContainer.item.remove(tempString);
game.Containers.put(tempContainer.name, tempContainer);
System.out.println("Item " + tempString + " added to inventory.");
found = true;
break;
}
}
if (!found)
System.out.println("Error");
}
} }
/* Open Exit (you should be so lucky)*/ /* Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky */
else if (action[0].equals("Delete")) {
doActionDelete(action[1]);
}
/*If it's not a "Special Action", just treat it normally */
/* Execute a user action or an action command from some <action> element that is not one of the "Special Commands"*/
/* Movement */
else if (action[0].length() == 1 && "nswe".contains(action[0])) {
doActionMove(action[0]);
}
/* Inventory */
else if (action[0].equals("i")) {
doActionInventory();
}
/* Take */
else if (action[0].equals("take") && action.length > 1) {
doActionTake(action[1]);
}
/* Open Exit (you should be so lucky)*/
else if (input.equals("open exit")) { else if (input.equals("open exit")) {
if (game.Rooms.get(currentRoom).type.equals("exit")) { return doActionOpenExit();
System.out.println("Game Over");
System.exit(0);
} else {
System.out.println("Error");
}
} }
/* Open a container */ /* Open a container */
else if (input.startsWith("open") && input.split(" ").length >= 1) { else if (action[0].equals("open") && action.length > 1) {
tempString = input.split(" ")[1]; doActionOpenContainer(action[1]);
String found = game.Rooms.get(currentRoom).container.get(tempString);
if (found != null) {
tempContainer = game.Containers.get(tempString);
tempContainer.isOpen = true;
containerInventory(tempContainer.item, tempString);
} else {
System.out.println("Error");
}
} }
/* Read an object */ /* Read an object */
else if (input.startsWith("read") && input.split(" ").length >= 1) { else if (action[0].equals("read") && action.length > 1) {
tempString = input.split(" ")[1]; doActionReadObject(action[1]);
ZorkItem tempItem;
if (game.Inventory.get(tempString) != null) {
tempItem = game.Items.get(tempString);
if (tempItem.writing != null && tempItem.writing != "") {
System.out.println(tempItem.writing);
} else {
System.out.println("Nothing written.");
}
} else {
System.out.println("Error");
}
} }
/* Drop an item*/ /* Drop an item*/
else if (input.startsWith("drop") && input.split(" ").length >= 1) { else if (action[0].equals("drop") && action.length > 1) {
tempString = input.split(" ")[1]; doActionDropItem(action[1]);
if (game.Inventory.get(tempString) != null) {
ZorkRoom tempRoom = game.Rooms.get(currentRoom);
tempRoom.item.put(tempString, tempString);
game.Rooms.put(tempRoom.name, tempRoom);
game.Inventory.remove(tempString);
System.out.println(tempString + " dropped.");
} else {
System.out.println("Error");
}
} }
/* Put an item somewhere */ /* Put an item somewhere */
else if (input.startsWith("put") && input.split(" ").length >= 4) { else if (action[0].equals("put") && action.length >= 4) {
tempString = input.split(" ")[1]; doActionPutItem(action[1], action[3]);
String destination = input.split(" ")[3];
if (game.Rooms.get(currentRoom).container.get(destination) != null && game.Containers.get(destination).isOpen && game.Inventory.get(tempString) != null) {
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");
}
} }
/* Turn on an item*/ /* Turn on an item*/
else if (input.startsWith("turn on") && input.split(" ").length >= 3) { else if (input.startsWith("turn on") && action.length >= 3) {
tempString = input.split(" ")[2]; doActionTurnOn(action[2]);
ZorkItem tempItem; }
if (game.Inventory.get(tempString) != null) { /* Attempt an attack, you feeling lucky?*/
tempItem = game.Items.get(tempString); else if (action[0].equals("attack") && action.length >= 4) {
System.out.println("You activate the " + tempString + "."); doActionAttack(action[1], action[3]);
if (tempItem != null) { }
for (y = 0; y < tempItem.turnOnPrint.size(); y++) { /* Invalid command*/
System.out.println(tempItem.turnOnPrint.get(y)); else System.out.println("Error");
} return false;
for (y = 0; y < tempItem.turnOnAction.size(); y++) { }
performAction(tempItem.turnOnAction.get(y));
}
} else {
System.out.println("Error");
}
} else {
System.out.println("Error");
}
} private boolean doActionGameWon() {
/* Attempt an attack, you feeling lucky?*/ System.out.println("Victory!");
else if (input.startsWith("attack") && input.split(" ").length >= 4) { return true;
tempString = input.split(" ")[1]; }
ZorkCreature tempCreature;
String weapon = input.split(" ")[3]; private void doActionAdd(String object, String destination) {
if (game.Rooms.get(currentRoom).creature.get(tempString) != null) { String objectType;
tempCreature = game.Creatures.get(tempString); objectType = game.ObjectLookup.get(object);
if (tempCreature != null && game.Inventory.get(weapon) != null) { String destinationType = game.ObjectLookup.get(destination);
if (tempCreature.attack(this, weapon)) { if (destinationType.equals("room")) {
System.out.println("You assault the " + tempString + " with the " + weapon + "."); ZorkRoom tempRoom = game.Rooms.get(destination);
for (y = 0; y < tempCreature.print.size(); y++) { if (objectType.equals("item"))
System.out.println(tempCreature.print.get(y)); tempRoom.item.put(object, object);
} else if (objectType.equals("creature"))
for (y = 0; y < tempCreature.action.size(); y++) { tempRoom.creature.put(object, object);
performAction(tempCreature.action.get(y)); else if (objectType.equals("container"))
} tempRoom.container.put(object, object);
} else { else
System.out.println("Error");
}
} else {
System.out.println("Error");
}
} else
System.out.println("Error"); System.out.println("Error");
} game.Rooms.put(tempRoom.name, tempRoom);
/* Invalid command*/ } else if (destinationType.equals("container")) {
else { ZorkContainer tempContainer = game.Containers.get(destination);
if (objectType.equals("item"))
tempContainer.item.put(object, object);
else
System.out.println("Error");
game.Containers.put(tempContainer.name, tempContainer);
} else {
System.out.println("Error"); System.out.println("Error");
} }
} }
/* Check triggers */ private void doActionDelete(String object) {
public boolean checkAllTriggers() { String objectType;
objectType = game.ObjectLookup.get(object);
game.Objects.remove(object);
if (objectType.equals("room")) {
for (String key : game.Rooms.keySet()) {
ZorkRoom tempRoom = game.Rooms.get(key);
for (String key2 : tempRoom.border.keySet()) {
if (tempRoom.border.get(key2).equals(object)) {
tempRoom.border.remove(key2);
}
}
game.Rooms.put(tempRoom.name, tempRoom);
}
} else if (objectType.equals("item")) {
for (String key : game.Rooms.keySet()) {
ZorkRoom tempRoom = game.Rooms.get(key);
if (tempRoom.item.get(object) != null) {
tempRoom.item.remove(object);
game.Rooms.put(tempRoom.name, tempRoom);
}
}
for (String key : game.Containers.keySet()) {
ZorkContainer tempContainer = game.Containers.get(key);
if (tempContainer.item.get(object) != null) {
tempContainer.item.remove(object);
game.Containers.put(tempContainer.name, tempContainer);
}
}
} else if (objectType.equals("container")) {
for (String key : game.Rooms.keySet()) {
ZorkRoom tempRoom = game.Rooms.get(key);
if (tempRoom.container.get(object) != null) {
tempRoom.container.remove(object);
game.Rooms.put(tempRoom.name, tempRoom);
}
}
} else if (objectType.equals("creature")) {
for (String key : game.Rooms.keySet()) {
ZorkRoom tempRoom = game.Rooms.get(key);
if (tempRoom.creature.get(object) != null) {
tempRoom.creature.remove(object);
game.Rooms.put(tempRoom.name, tempRoom);
}
}
}
}
/*Variable initialization*/ private void doActionUpdate(String object, String newStatus) {
boolean skip = false; String objectType;
int x, y; objectType = game.ObjectLookup.get(object);
ZorkTrigger tempTrigger; if (objectType.equals("room")) {
ZorkRoom tempRoom = game.Rooms.get(object);
tempRoom.status = newStatus;
game.Rooms.put(tempRoom.name, tempRoom);
} else if (objectType.equals("container")) {
ZorkContainer tempContainer = game.Containers.get(object);
tempContainer.status = newStatus;
game.Containers.put(tempContainer.name, tempContainer);
} else if (objectType.equals("creature")) {
ZorkCreature tempCreature = game.Creatures.get(object);
tempCreature.status = newStatus;
game.Creatures.put(tempCreature.name, tempCreature);
} else if (objectType.equals("item")) {
ZorkItem tempItem = game.Items.get(object);
tempItem.status = newStatus;
game.Items.put(tempItem.name, tempItem);
}
}
private void doActionAttack(String tempString, String weapon) {
ZorkCreature tempCreature;
if (game.Rooms.get(currentRoom).creature.get(tempString) != null) {
tempCreature = game.Creatures.get(tempString);
if (tempCreature != null && game.Inventory.get(weapon) != null) {
if (tempCreature.attack(this, weapon)) {
System.out.println("You assault the " + tempString + " with the " + weapon + ".");
for (String print: tempCreature.print) {
System.out.println(print);
}
for (String action: tempCreature.action) {
executeAction(action);
}
return;
}
}
}
System.out.println("Error");
}
private void doActionTurnOn(String tempString) {
if (game.Inventory.get(tempString) != null) {
ZorkItem tempItem = game.Items.get(tempString);
System.out.println("You activate the " + tempString + ".");
if (tempItem != null) {
for (String print: tempItem.turnOnPrint) {
System.out.println(print);
}
for (String action: tempItem.turnOnAction) {
executeAction(action);
}
return;
}
}
System.out.println("Error");
}
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");
}
}
private void doActionDropItem(String tempString) {
if (game.Inventory.get(tempString) != null) {
ZorkRoom tempRoom = game.Rooms.get(currentRoom);
tempRoom.item.put(tempString, tempString);
game.Rooms.put(tempRoom.name, tempRoom);
game.Inventory.remove(tempString);
System.out.println(tempString + " dropped.");
} else {
System.out.println("Error");
}
}
private void doActionReadObject(String tempString) {
if (game.Inventory.get(tempString) != null) {
ZorkItem tempItem = game.Items.get(tempString);
if (tempItem.writing != null && tempItem.writing != "") {
System.out.println(tempItem.writing);
} else {
System.out.println("Nothing written.");
}
} else {
System.out.println("Error");
}
}
private void doActionOpenContainer(String tempString) {
ZorkContainer tempContainer; ZorkContainer tempContainer;
String found = game.Rooms.get(currentRoom).container.get(tempString);
/*Check Room triggers*/ if (found != null) {
for (x = 0; x < game.Rooms.get(currentRoom).trigger.size(); x++) { tempContainer = game.Containers.get(tempString);
tempTrigger = game.Rooms.get(currentRoom).trigger.get(x); tempContainer.isOpen = true;
if (tempTrigger.evaluate(this)) { doActionContainerInventory(tempContainer.item, tempString);
for (y = 0; y < tempTrigger.print.size(); y++) { } else {
System.out.println(tempTrigger.print.get(y)); System.out.println("Error");
}
for (y = 0; y < tempTrigger.action.size(); y++) {
performAction(tempTrigger.action.get(y));
}
if (tempTrigger.hasCommand) {
skip = true;
}
if (tempTrigger.type.equals("single")) {
game.Rooms.get(currentRoom).trigger.remove(x);
}
}
} }
}
/* Check items in the containers in the room */ private boolean doActionOpenExit() {
for (String key : game.Rooms.get(currentRoom).container.keySet()) { if (game.Rooms.get(currentRoom).type.equals("exit")) {
tempContainer = game.Containers.get(key); System.out.println("Game Over");
for (String key2 : tempContainer.item.keySet()) { return true;
ZorkItem tempItem = game.Items.get(key2);
for (x = 0; x < tempItem.trigger.size(); x++) {
tempTrigger = tempItem.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (y = 0; y < tempTrigger.print.size(); y++) {
System.out.println(tempTrigger.print.get(y));
}
for (y = 0; y < tempTrigger.action.size(); y++) {
performAction(tempTrigger.action.get(y));
}
if (tempTrigger.hasCommand) {
skip = true;
}
if (tempTrigger.type.equals("single")) {
tempItem.trigger.remove(x);
}
}
}
}
/* Check all containers in the room*/
for (x = 0; x < tempContainer.trigger.size(); x++) {
tempTrigger = tempContainer.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (y = 0; y < tempTrigger.print.size(); y++) {
System.out.println(tempTrigger.print.get(y));
}
for (y = 0; y < tempTrigger.action.size(); y++) {
performAction(tempTrigger.action.get(y));
}
if (tempTrigger.hasCommand) {
skip = true;
}
if (tempTrigger.type.equals("single")) {
tempContainer.trigger.remove(x);
}
}
}
} }
System.out.println("Error");
/* Check all creatures in the room */ return false;
for (String key : game.Rooms.get(currentRoom).creature.keySet()) {
ZorkCreature tempCreature = game.Creatures.get(key);
for (x = 0; x < tempCreature.trigger.size(); x++) {
tempTrigger = tempCreature.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (y = 0; y < tempTrigger.print.size(); y++) {
System.out.println(tempTrigger.print.get(y));
}
for (y = 0; y < tempTrigger.action.size(); y++) {
performAction(tempTrigger.action.get(y));
}
if (tempTrigger.hasCommand) {
skip = true;
}
if (tempTrigger.type.equals("single")) {
tempCreature.trigger.remove(x);
}
}
}
}
/* Check items in inventory */
for (String key : game.Inventory.keySet()) {
ZorkItem tempItem = game.Items.get(key);
for (x = 0; x < tempItem.trigger.size(); x++) {
tempTrigger = tempItem.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (y = 0; y < tempTrigger.print.size(); y++) {
System.out.println(tempTrigger.print.get(y));
}
for (y = 0; y < tempTrigger.action.size(); y++) {
performAction(tempTrigger.action.get(y));
}
if (tempTrigger.hasCommand) {
skip = true;
}
if (tempTrigger.type.equals("single")) {
tempItem.trigger.remove(x);
}
}
}
}
/* Check items in room */
for (String key : game.Rooms.get(currentRoom).item.keySet()) {
ZorkItem tempItem = game.Items.get(key);
for (x = 0; x < tempItem.trigger.size(); x++) {
tempTrigger = tempItem.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (y = 0; y < tempTrigger.print.size(); y++) {
System.out.println(tempTrigger.print.get(y));
}
for (y = 0; y < tempTrigger.action.size(); y++) {
performAction(tempTrigger.action.get(y));
}
if (tempTrigger.hasCommand) {
skip = true;
}
if (tempTrigger.type.equals("single")) {
tempItem.trigger.remove(x);
}
}
}
}
return skip;
} }
/*Basic movement function */ /*Basic movement function */
public void move(String direction) { private void doActionMove(String direction) {
String fullDirection = ""; String fullDirection = "";
String destination = "";
if (direction.equals("n")) { if (direction.equals("n")) {
fullDirection = "north"; fullDirection = "north";
} else if (direction.equals("s")) { } else if (direction.equals("s")) {
@ -379,7 +340,7 @@ public class Zork {
fullDirection = "west"; fullDirection = "west";
} }
destination = (game.Rooms.get(currentRoom)).border.get(fullDirection); String destination = (game.Rooms.get(currentRoom)).border.get(fullDirection);
if (destination != null) { if (destination != null) {
currentRoom = destination; currentRoom = destination;
System.out.println(game.Rooms.get(currentRoom).description); System.out.println(game.Rooms.get(currentRoom).description);
@ -388,134 +349,8 @@ public class Zork {
} }
} }
/* This is used to perform the "Special Actions"*/
public void performAction(String action) {
String object;
String objectType;
/* Update: figure out what type of item it is, and then change it's status*/
if (action.startsWith("Update")) {
object = action.split(" ")[1];
String newStatus = action.split(" ")[3];
objectType = game.ObjectLookup.get(object);
if (objectType.equals("room")) {
ZorkRoom tempRoom = game.Rooms.get(object);
tempRoom.status = newStatus;
game.Rooms.put(tempRoom.name, tempRoom);
} else if (objectType.equals("container")) {
ZorkContainer tempContainer = game.Containers.get(object);
tempContainer.status = newStatus;
game.Containers.put(tempContainer.name, tempContainer);
} else if (objectType.equals("creature")) {
ZorkCreature tempCreature = game.Creatures.get(object);
tempCreature.status = newStatus;
game.Creatures.put(tempCreature.name, tempCreature);
} else if (objectType.equals("item")) {
ZorkItem tempItem = game.Items.get(object);
tempItem.status = newStatus;
game.Items.put(tempItem.name, tempItem);
}
}
/*Game Over: pretty straight forward*/
else if (action.equals("Game Over")) {
System.out.println("Victory!");
System.exit(0);
}
/* Add: figure out what type the destination is, then what type the object is. Then add object to destination if it makes sense */
else if (action.startsWith("Add")) {
String destination = action.split(" ")[3];
object = action.split(" ")[1];
objectType = game.ObjectLookup.get(object);
String destinationType = game.ObjectLookup.get(destination);
if (destinationType.equals("room")) {
ZorkRoom tempRoom = game.Rooms.get(destination);
if (objectType.equals("item"))
tempRoom.item.put(object, object);
else if (objectType.equals("creature"))
tempRoom.creature.put(object, object);
else if (objectType.equals("container"))
tempRoom.container.put(object, object);
else
System.out.println("Error");
game.Rooms.put(tempRoom.name, tempRoom);
} else if (destinationType.equals("container")) {
ZorkContainer tempContainer = game.Containers.get(destination);
if (objectType.equals("item"))
tempContainer.item.put(object, object);
else
System.out.println("Error");
game.Containers.put(tempContainer.name, tempContainer);
} else {
System.out.println("Error");
}
}
/* Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky */
else if (action.startsWith("Delete")) {
object = action.split(" ")[1];
objectType = game.ObjectLookup.get(object);
game.Objects.remove(object);
if (objectType.equals("room")) {
ZorkRoom tempRoom;
for (String key : game.Rooms.keySet()) {
tempRoom = game.Rooms.get(key);
for (String key2 : tempRoom.border.keySet()) {
if (tempRoom.border.get(key2).equals(object)) {
tempRoom.border.remove(key2);
}
}
game.Rooms.put(tempRoom.name, tempRoom);
}
} else if (objectType.equals("item")) {
ZorkRoom tempRoom;
for (String key : game.Rooms.keySet()) {
tempRoom = game.Rooms.get(key);
if (tempRoom.item.get(object) != null) {
tempRoom.item.remove(object);
game.Rooms.put(tempRoom.name, tempRoom);
}
}
ZorkContainer tempContainer;
for (String key : game.Containers.keySet()) {
tempContainer = game.Containers.get(key);
if (tempContainer.item.get(object) != null) {
tempContainer.item.remove(object);
game.Containers.put(tempContainer.name, tempContainer);
}
}
} else if (objectType.equals("container")) {
ZorkRoom tempRoom;
for (String key : game.Rooms.keySet()) {
tempRoom = game.Rooms.get(key);
if (tempRoom.container.get(object) != null) {
tempRoom.container.remove(object);
game.Rooms.put(tempRoom.name, tempRoom);
}
}
} else if (objectType.equals("creature")) {
ZorkRoom tempRoom;
for (String key : game.Rooms.keySet()) {
tempRoom = game.Rooms.get(key);
if (tempRoom.creature.get(object) != null) {
tempRoom.creature.remove(object);
game.Rooms.put(tempRoom.name, tempRoom);
}
}
}
} else {
/*If it's not a "Special Action", just treat it normally */
userInput = action;
action(action);
}
}
/* Print out the what's in a container when it's been opened*/ /* Print out the what's in a container when it's been opened*/
public void containerInventory(HashMap<String, String> Container, String Name) { public void doActionContainerInventory(HashMap<String, String> Container, String Name) {
String output = ""; String output = "";
if (Container.isEmpty()) { if (Container.isEmpty()) {
System.out.println(Name + " is empty"); System.out.println(Name + " is empty");
@ -530,7 +365,7 @@ public class Zork {
} }
/* Print out the inventory when user types i */ /* Print out the inventory when user types i */
public void inventory() { private void doActionInventory() {
String output = "Inventory: "; String output = "Inventory: ";
if (game.Inventory.isEmpty()) { if (game.Inventory.isEmpty()) {
System.out.println("Inventory: empty"); System.out.println("Inventory: empty");
@ -542,4 +377,192 @@ public class Zork {
System.out.println(output); System.out.println(output);
} }
} }
private void doActionTake(String tempString) {
if ((game.Rooms.get(currentRoom)).item.get(tempString) != null) {
game.Inventory.put(tempString, tempString);
ZorkRoom tempRoom = (game.Rooms.get(currentRoom));
tempRoom.item.remove(tempString);
game.Rooms.put(tempRoom.name, 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()) {
ZorkContainer tempContainer = game.Containers.get(key);
if (tempContainer != null && tempContainer.isOpen && tempContainer.item.get(tempString) != null) {
game.Inventory.put(tempString, tempString);
tempContainer.item.remove(tempString);
game.Containers.put(tempContainer.name, tempContainer);
System.out.println("Item " + tempString + " added to inventory.");
found = true;
break;
}
}
if (!found)
System.out.println("Error");
}
}
/* Check triggers */
public boolean executeTriggers() {
/*Variable initialization*/
boolean skip = false;
/*Check Room triggers*/
skip = skip || doTriggersRoom();
/* Check items in the containers in the room */
skip = skip || doTriggersItemsInContainersInRoom();
/* Check all containers in the room*/
skip = skip || doTriggersContainersInRoom();
/* Check all creatures in the room */
skip = skip || doTriggersCreaturesInRoom();
/* Check items in inventory */
skip = skip || doTriggersItemsInInventory();
/* Check items in room */
skip = skip || doTriggersItemsInRoom();
return skip;
}
private boolean doTriggersContainersInRoom() {
boolean skip = false;
for (String key : game.Rooms.get(currentRoom).container.keySet()) {
ZorkContainer tempContainer = game.Containers.get(key);
for (int x = 0; x < tempContainer.trigger.size(); x++) {
ZorkTrigger tempTrigger = tempContainer.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (String print: tempTrigger.print) {
System.out.println(print);
}
for (String action: tempTrigger.action) {
executeAction(action);
}
skip = skip || tempTrigger.hasCommand;
if (tempTrigger.type.equals("single")) {
tempContainer.trigger.remove(x);
}
}
}
}
return skip;
}
private boolean doTriggersItemsInContainersInRoom() {
boolean skip = false;
for (String key : game.Rooms.get(currentRoom).container.keySet()) {
ZorkContainer tempContainer = game.Containers.get(key);
for (String key2 : tempContainer.item.keySet()) {
ZorkItem tempItem = game.Items.get(key2);
for (int x = 0; x < tempItem.trigger.size(); x++) {
ZorkTrigger tempTrigger = tempItem.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (String print : tempTrigger.print) {
System.out.println(print);
}
for (String action : tempTrigger.action) {
executeAction(action);
}
skip = skip || tempTrigger.hasCommand;
if (tempTrigger.type.equals("single")) {
tempItem.trigger.remove(x);
}
}
}
}
}
return skip;
}
private boolean doTriggersItemsInRoom() {
boolean skip = false;
for (String key : game.Rooms.get(currentRoom).item.keySet()) {
ZorkItem tempItem = game.Items.get(key);
for (int x = 0; x < tempItem.trigger.size(); x++) {
ZorkTrigger tempTrigger = tempItem.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (String print: tempTrigger.print) {
System.out.println(print);
}
for (String action: tempTrigger.action) {
executeAction(action);
}
skip = skip || tempTrigger.hasCommand;
if (tempTrigger.type.equals("single")) {
tempItem.trigger.remove(x);
}
}
}
}
return skip;
}
private boolean doTriggersItemsInInventory() {
boolean skip = false;
for (String key : game.Inventory.keySet()) {
ZorkItem tempItem = game.Items.get(key);
for (int x = 0; x < tempItem.trigger.size(); x++) {
ZorkTrigger tempTrigger = tempItem.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (String print: tempTrigger.print) {
System.out.println(print);
}
for (String action: tempTrigger.action) {
executeAction(action);
}
skip = skip || tempTrigger.hasCommand;
if (tempTrigger.type.equals("single")) {
tempItem.trigger.remove(x);
}
}
}
}
return skip;
}
private boolean doTriggersCreaturesInRoom() {
boolean skip = false;
for (String key : game.Rooms.get(currentRoom).creature.keySet()) {
ZorkCreature tempCreature = game.Creatures.get(key);
for (int x = 0; x < tempCreature.trigger.size(); x++) {
ZorkTrigger tempTrigger = tempCreature.trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (String print: tempTrigger.print) {
System.out.println(print);
}
for (String action: tempTrigger.action) {
executeAction(action);
}
skip = skip || tempTrigger.hasCommand;
if (tempTrigger.type.equals("single")) {
tempCreature.trigger.remove(x);
}
}
}
}
return skip;
}
private boolean doTriggersRoom() {
boolean skip = false;
for (int x = 0; x < game.Rooms.get(currentRoom).trigger.size(); x++) {
ZorkTrigger tempTrigger = game.Rooms.get(currentRoom).trigger.get(x);
if (tempTrigger.evaluate(this)) {
for (String print: tempTrigger.print) {
System.out.println(print);
}
for (String action: tempTrigger.action) {
executeAction(action);
}
skip = skip || tempTrigger.hasCommand;
if (tempTrigger.type.equals("single")) {
game.Rooms.get(currentRoom).trigger.remove(x);
}
}
}
return skip;
}
} }