// AdventureDemo.java // This code was written in class on Thu 26-Mar-2009. // As we wrote almost 200 lines in just over an hour, // it does not have perfect style (eg, it is uncommented!). // But it does establish a basic framework for writing // a reasonably playable text adventure, and as such will // serve as the starting point for hw9. Enjoy! import java.util.*; class AdventureDemo { public static void main(String[] args) { Adventure game = new Adventure(); game.play(); } } class Adventure { private Room currentRoom; private ArrayList inventory; public Adventure() { inventory = new ArrayList(); Room weh5419 = new Room("5419 Wean Hall"); weh5419.addThing(new Thing("Computer")); Room wehLobby = new Room("Wean Hall 5th Floor Lobby"); wehLobby.addThing(new Thing("Coffee Cart", "Cart")); weh5419.setExit(Direction.WEST, wehLobby); wehLobby.setExit(Direction.EAST, weh5419); currentRoom = weh5419; } public void play() { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("\n-------------------------------"); System.out.println("I am in " + currentRoom.getDescription()); System.out.println("I can see:"); int thingCount = currentRoom.getThingCount(); if (thingCount == 0) System.out.println(" nothing!"); else { for (int i=0; i "); String[] cmds = scanner.nextLine().toLowerCase().split(" "); String verb = cmds[0]; String noun = (cmds.length > 1) ? cmds[1] : "none"; if (verb.equals("quit")) { System.out.println("Bye!"); break; } else if (verb.equals("go")) doGo(noun); else if (verb.equals("get")) doGet(noun); else if (verb.equals("put")) doPut(noun); else if (verb.equals("take") && noun.equals("inventory")) doTakeInventory(); else if (verb.equals("i")) doTakeInventory(); else System.out.println("I don't know how to " + verb); } } private void doTakeInventory() { System.out.println("I am carrying the following:"); int thingCount = inventory.size(); if (thingCount == 0) System.out.println(" nothing!"); else { for (int i=0; i things; public Room(String description) { this.description = description; exits = new Room[4]; things = new ArrayList(); } public void setExit(int direction, Room room) { exits[direction] = room; } public Room getExit(int direction) { if ((direction < 0) || (direction > 3)) return null; return exits[direction]; } public String getDescription() { return this.description; } public void addThing(Thing thing) { things.add(thing); } public void removeThing(Thing thing) { things.remove(thing); } public int getThingCount() { return things.size(); } public Thing getThing(int i) { return things.get(i); } public Thing getThing(String thingName) { for (int i=0; i