/*Drew Schuster dtschust ECE462 */ import java.io.File; import javax.xml.parsers.*; import org.w3c.dom.*; import java.util.*; /* Generic object, everything inherits from this*/ class ZorkObject { public String status; public ArrayList trigger = new ArrayList(); public ZorkObject() { } } /* Generic condition*/ abstract class ZorkCondition { String object; public abstract boolean evaluate(Zork zork); } /* Status conditions*/ class ZorkConditionStatus extends ZorkCondition { String status; public boolean evaluate(Zork zork) { ZorkObject tested = zork.Objects.get(object); if (tested != null && tested.status.equals(status)) return true; else return false; } } /* Has conditions*/ class ZorkConditionHas extends ZorkCondition { String has; String owner; 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")) { if (zork.Inventory.get(object) != null && has.equals("yes") || zork.Inventory.get(object) == null && has.equals("no")) { return true; } else { return false; } } else { /* is it a room?*/ ZorkRoom roomObject = zork.Rooms.get(owner); if ( roomObject != null ) { if ((roomObject).item.get(object) != null && has.equals("yes") || (roomObject).item.get(object) == null && has.equals("no")) { return true; } else { return false; } } /* is it a container?*/ else { ZorkContainer containerObject = zork.Containers.get(owner); if (containerObject != null ) { if ((containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no")) { return true; } else { return false; } } } } return false; } } /* Special Command condition*/ class ZorkCommand extends ZorkCondition { String command; public boolean evaluate(Zork zork) { if (command.equals(zork.userInput)) return true; else return false; } } /*Trigger*/ class ZorkTrigger { public ArrayList conditions = new ArrayList(); String type="single"; /*By default, single*/ boolean hasCommand = false; public ArrayList print = new ArrayList(); public ArrayList action = new ArrayList(); public boolean evaluate(Zork zork) { for(int i=0;i border = new HashMap(); public HashMap container = new HashMap(); public HashMap item = new HashMap(); public HashMap creature = new HashMap(); public ZorkRoom() { } } /* Item*/ class ZorkItem extends ZorkObject { public String name; public String description; public String writing; public ArrayList turnOnPrint = new ArrayList(); public ArrayList turnOnAction = new ArrayList(); public ZorkItem() { } } /* Container*/ class ZorkContainer extends ZorkObject { public String name; public HashMap item = new HashMap(); public String description; public ArrayList accept = new ArrayList(); boolean isOpen; public ZorkContainer() { } } /* Creature*/ class ZorkCreature extends ZorkObject { public String name; public String description; public HashMap vulnerability = new HashMap(); public ArrayList conditions = new ArrayList(); public ArrayList print = new ArrayList(); public ArrayList action = new ArrayList(); public ZorkCreature() { } /* Evaluate the success of an attack*/ public boolean attack(Zork zork,String weapon) { if (vulnerability.get(weapon) == null) { return false; } for(int i=0;i 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 HashMap ObjectLookup = new HashMap(); public String currentRoom; public File file; public Zork(String filename) { int i,j,k,l,x,y,z; file = new File(filename); if (!file.canRead()) { System.out.println("Error opening file. Exiting..."); return; } try { /* Open the xml file*/ DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(file); Element rootElement = doc.getDocumentElement(); /* Every single first generation child is a room, container, creature, or item. So load them in*/ NodeList nodes = rootElement.getChildNodes(); for (k=0;k 0) { tempRoom.type = getString((Element)type.item(0)); } else { tempRoom.type = "regular"; } NodeList status = element.getElementsByTagName("status"); if (status.getLength() > 0) { tempRoom.status = getString((Element)type.item(0)); } else { tempRoom.status = ""; } NodeList description = element.getElementsByTagName("description"); tempRoom.description = getString((Element)description.item(0)); NodeList items = element.getElementsByTagName("item"); for (j=0;j 0) { ZorkConditionHas tempConditionHas = new ZorkConditionHas(); tempConditionHas.has = getString((Element)has.item(0)); tempConditionHas.object = getString((Element)object.item(0)); NodeList owner = condition.getElementsByTagName("owner"); tempConditionHas.owner = getString((Element)owner.item(0)); tempTrigger.conditions.add(tempConditionHas); } else { ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(); tempConditionStatus.object = getString((Element)object.item(0)); NodeList sstatus = condition.getElementsByTagName("status"); tempConditionStatus.status = getString((Element)sstatus.item(0)); tempTrigger.conditions.add(tempConditionStatus); } } NodeList ttype = element.getElementsByTagName("type"); if (ttype.getLength() > 0) { tempTrigger.type = getString((Element)ttype.item(0)); } else { tempTrigger.type = "single"; } NodeList prints = trigger.getElementsByTagName("print"); for (l=0;l 0) tempItem.name = getString((Element)name.item(0)); NodeList status = element.getElementsByTagName("status"); if (status.getLength() > 0) { tempItem.status = getString((Element)status.item(0)); } else { tempItem.status = ""; } NodeList description = element.getElementsByTagName("description"); if (description.getLength()>0) tempItem.description = getString((Element)description.item(0)); NodeList writing = element.getElementsByTagName("writing"); if (writing.getLength()>0) tempItem.writing = getString((Element)writing.item(0)); NodeList turnon = element.getElementsByTagName("turnon"); if (turnon.getLength()>0) { NodeList prints = element.getElementsByTagName("print"); for(j=0;j 0) { ZorkConditionHas tempConditionHas = new ZorkConditionHas(); tempConditionHas.has = getString((Element)has.item(0)); tempConditionHas.object = getString((Element)object.item(0)); NodeList owner = condition.getElementsByTagName("owner"); tempConditionHas.owner = getString((Element)owner.item(0)); tempTrigger.conditions.add(tempConditionHas); } else { ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(); tempConditionStatus.object = getString((Element)object.item(0)); NodeList sstatus = condition.getElementsByTagName("status"); tempConditionStatus.status = getString((Element)sstatus.item(0)); tempTrigger.conditions.add(tempConditionStatus); } } NodeList ttype = element.getElementsByTagName("type"); if (ttype.getLength() > 0) { tempTrigger.type = getString((Element)ttype.item(0)); } else { tempTrigger.type = "single"; } NodeList prints = trigger.getElementsByTagName("print"); for (l=0;l0) tempCont.name = getString((Element)name.item(0)); NodeList status = element.getElementsByTagName("status"); if (status.getLength()>0) tempCont.status = getString((Element)status.item(0)); /*Initially assume a closed container*/ tempCont.isOpen = false; NodeList description = element.getElementsByTagName("description"); if (description.getLength()>0) tempCont.description = getString((Element)description.item(0)); NodeList accepts = element.getElementsByTagName("accept"); for(j=0;j 0) { ZorkConditionHas tempConditionHas = new ZorkConditionHas(); tempConditionHas.has = getString((Element)has.item(0)); tempConditionHas.object = getString((Element)object.item(0)); NodeList owner = condition.getElementsByTagName("owner"); tempConditionHas.owner = getString((Element)owner.item(0)); tempTrigger.conditions.add(tempConditionHas); } else { ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(); tempConditionStatus.object = getString((Element)object.item(0)); NodeList sstatus = condition.getElementsByTagName("status"); tempConditionStatus.status = getString((Element)sstatus.item(0)); tempTrigger.conditions.add(tempConditionStatus); } } NodeList ttype = element.getElementsByTagName("type"); if (ttype.getLength() > 0) { tempTrigger.type = getString((Element)ttype.item(0)); } else { tempTrigger.type = "single"; } NodeList prints = trigger.getElementsByTagName("print"); for (l=0;l0) tempCreature.name = getString((Element)name.item(0)); NodeList status = element.getElementsByTagName("status"); if (status.getLength()>0) tempCreature.status = getString((Element)status.item(0)); NodeList description = element.getElementsByTagName("description"); if (description.getLength()>0) tempCreature.description = getString((Element)description.item(0)); NodeList vulns = element.getElementsByTagName("vulnerability"); for(j=0;j 0) { ZorkConditionHas tempConditionHas = new ZorkConditionHas(); tempConditionHas.has = getString((Element)has.item(0)); tempConditionHas.object = getString((Element)object.item(0)); NodeList owner = condition.getElementsByTagName("owner"); tempConditionHas.owner = getString((Element)owner.item(0)); tempCreature.conditions.add(tempConditionHas); } else { ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(); tempConditionStatus.object = getString((Element)object.item(0)); NodeList sstatus = condition.getElementsByTagName("status"); tempConditionStatus.status = getString((Element)sstatus.item(0)); tempCreature.conditions.add(tempConditionStatus); } } NodeList prints = attack.getElementsByTagName("print"); for (l=0;l 0) { ZorkConditionHas tempConditionHas = new ZorkConditionHas(); tempConditionHas.has = getString((Element)has.item(0)); tempConditionHas.object = getString((Element)object.item(0)); NodeList owner = condition.getElementsByTagName("owner"); tempConditionHas.owner = getString((Element)owner.item(0)); tempTrigger.conditions.add(tempConditionHas); } else { ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus(); tempConditionStatus.object = getString((Element)object.item(0)); NodeList sstatus = condition.getElementsByTagName("status"); tempConditionStatus.status = getString((Element)sstatus.item(0)); tempTrigger.conditions.add(tempConditionStatus); } } NodeList ttype = element.getElementsByTagName("type"); if (ttype.getLength() > 0) { tempTrigger.type = getString((Element)ttype.item(0)); } else { tempTrigger.type = "single"; } NodeList prints = trigger.getElementsByTagName("print"); for (l=0;l element that is not one of the "Special Commands"*/ public void action(String input) { int i,j,k,l,x,y,z; String tempString; ZorkContainer tempContainer; /* Movement */ if (input.equals("n") || input.equals("s") || input.equals("e") || input.equals("w")) { move(input); } /* Inventory */ else if (input.equals("i")) { inventory(); } /* Take */ else if (input.startsWith("take") && input.split(" ").length>=1) { tempString = input.split(" ")[1]; if ((Rooms.get(currentRoom)).item.get(tempString) != null) { Inventory.put(tempString,tempString); ZorkRoom tempRoom = (Rooms.get(currentRoom)); tempRoom.item.remove(tempString); 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 : Rooms.get(currentRoom).container.keySet()) { tempContainer = Containers.get(key); if (tempContainer != null && tempContainer.isOpen && tempContainer.item.get(tempString) != null) { Inventory.put(tempString,tempString); tempContainer.item.remove(tempString); 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)*/ else if (input.equals("open exit")) { if (Rooms.get(currentRoom).type.equals("exit")) { System.out.println("Game Over"); System.exit(0); } else { System.out.println("Error"); } } /* Open a container */ else if (input.startsWith("open") && input.split(" ").length>=1) { tempString = input.split(" ")[1]; String found = Rooms.get(currentRoom).container.get(tempString); if (found != null) { tempContainer = Containers.get(tempString); tempContainer.isOpen = true; containerInventory(tempContainer.item,tempString); } else { System.out.println("Error"); } } /* Read an object */ else if (input.startsWith("read") && input.split(" ").length>=1) { tempString = input.split(" ")[1]; ZorkItem tempItem; if (Inventory.get(tempString) != null) { tempItem = 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*/ else if (input.startsWith("drop") && input.split(" ").length>=1) { tempString = input.split(" ")[1]; if (Inventory.get(tempString) != null) { ZorkRoom tempRoom = Rooms.get(currentRoom); tempRoom.item.put(tempString,tempString); Rooms.put(tempRoom.name,tempRoom); Inventory.remove(tempString); System.out.println(tempString+" dropped."); } else { System.out.println("Error"); } } /* Put an item somewhere */ else if (input.startsWith("put") && input.split(" ").length>=4) { tempString = input.split(" ")[1]; String destination = input.split(" ")[3]; if (Rooms.get(currentRoom).container.get(destination) != null && Containers.get(destination).isOpen && Inventory.get(tempString) != null) { tempContainer = Containers.get(destination); tempContainer.item.put(tempString,tempString); Inventory.remove(tempString); System.out.println("Item "+tempString+" added to "+destination+"."); } else { System.out.println("Error"); } } /* Turn on an item*/ else if (input.startsWith("turn on") && input.split(" ").length>=3) { tempString = input.split(" ")[2]; ZorkItem tempItem; if (Inventory.get(tempString) != null) { tempItem = Items.get(tempString); System.out.println("You activate the "+tempString+"."); if (tempItem != null) { for (y=0;y=4) { tempString = input.split(" ")[1]; ZorkCreature tempCreature; String weapon = input.split(" ")[3]; if (Rooms.get(currentRoom).creature.get(tempString) != null) { tempCreature = Creatures.get(tempString); if (tempCreature != null && Inventory.get(weapon)!= null) { if (tempCreature.attack(this,weapon)) { System.out.println("You assault the "+tempString+" with the "+weapon+"."); for (y=0;y 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 */ public void inventory() { String output = "Inventory: "; if (Inventory.isEmpty()) { System.out.println("Inventory: empty"); } else { for (String key : Inventory.keySet()) { output += key+", "; } output = output.substring(0,output.length()-2); System.out.println(output); } } /* I love how basic java main functions are sometimes.*/ public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java Zork [filename]"); return; } new Zork(args[0]); } }