import java.util.*; public class AdventureDemo { public static void main(String[] args) { Adventure game = new Adventure(); game.play(); } } class Adventure { // instance variables (aka "fields") private Room currentRoom; private Scanner scanner = new Scanner(System.in); private boolean gameOver; private ArrayList inventory; public Adventure() { Room rm5419a = new Room("5419a lab"); Room rm5419b = new Room("5419b lab"); Room hall5400 = new Room("5400 hallway"); rm5419b.setExit(Dir.SOUTH, hall5400); rm5419b.setExit(Dir.WEST, rm5419a); hall5400.setExit(Dir.NORTH, rm5419b); rm5419a.setExit(Dir.EAST, rm5419b); Thing johnTran = new Thing("JohnTran"); rm5419b.addThing(johnTran); Thing projector = new Thing("Projector"); rm5419b.addThing(projector); currentRoom = rm5419b; inventory = new ArrayList(); gameOver = false; } private void printInventory() { System.out.print("You are carrying: "); if (this.inventory.size() == 0) System.out.println("Nothing!"); else System.out.println(this.inventory); } private void printObjectsInRoom() { System.out.println("You can see: " + currentRoom.getThings()); } private void printLegalExits() { System.out.print("You can go: "); boolean hasExits = false; for (int dir=0; dir "); String cmd = scanner.nextLine().toLowerCase(); String[] cmdWords = cmd.split(" "); String verb = cmdWords[0]; String noun = (cmdWords.length < 2 ? "" : cmdWords[1]); if (verb.equals("go")) doGo(noun); else if (verb.equals("get")) doGet(noun); else if (verb.equals("n")) doGo("north"); else if (verb.equals("quit")) doQuit(); else if (verb.equals("q")) doQuit(); else System.out.println("I don't know how to " + verb); } public void play() { while (gameOver == false) { System.out.println("\nYou are in " + currentRoom); printObjectsInRoom(); printInventory(); printLegalExits(); doCommand(); } System.out.println("You win!"); } } class Dir { public static final int NORTH = 0; public static final int SOUTH = 1; public static final int EAST = 2; public static final int WEST = 3; public static final int UP = 4; public static final int DOWN = 5; public static final int DIRS = 6; private static final String[] dirNames = { "North", "South", "East", "West", "Up", "Down" }; public static String getDirName(int dir) { return dirNames[dir]; } public static int getDir(String dirName) { for (int i=0; i things; // like Thing[] // constructor public Room(String name) { this.name = name; this.exits = new Room[Dir.DIRS]; this.things = new ArrayList(); } // accessor for name public String getName() { return this.name; } // accessor and mutator for this.things public ArrayList getThings() { return this.things; } public Thing findThing(String thingName) { for (int i=0; i