Assignment 3 solutions

one key thing to note is that many of you wrote code that was unnecessarily complicated. please try to understand why our code suffices.
 

/* Christine Chung */
/* Assignment 3 Sample Solution */
/* Sept.
23, 1999 */
/* Room.java */
/* Rooms connected by up to 3 doors. */

public class Room {

    // Each room r has a unique r.id > 0;
    // nextId is the ID# of the next room to be created
    private static int nextId = 1
    private int id; 

    // For each room r, r.previous is the room created immediately before
    // r was created, or null if r was the first room created. The most recent
    // room to have been created is "last"
    private static Room last;
    private Room previous;

    // the three rooms the room is connected to
    private Room door1, door2, door3;

    // Create a new room with a unique ID# and a reference to the previous room
    public Room() {
        id = nextId; nextId = nextId + 1;
        previous = last; last = this;
    }

    // Return the room with ID# targetId, or null if there is no such room
    public static Room findRoom(int targetId) {
        Room now = last;
        while (now != null && now.id != targetId)
            now = now.previous;
        return now;
    }

    // return a description: room ID, and which rooms are behind which doors
    public String toString() { 
        /* Two approaches: 
            (1) concatenate the output string as you go along, or
            (2) declare and set string variables and concatenate them at the end.
        Two options:
        (a) only print doors that go somewhere
        (b) also print doors that go nowhere

        Approach (1) and option (a):
        String s = "Room " + id;
        if (door1 != null) s = s + " <1> --> " + door1.id;
        if (door2 != null) s = s + " <2> --> " + door2.id;
        if (door3 != null) s = s + " <3> --> " + door3.id;
        return s;

        Approach (2) and option (b):
        */
       
String door1ID = "nowhere", door2ID = "nowhere", door3ID = "nowhere";

        if (door1 != null) door1ID = "" + door1.id;
        if (door2 != null) door2ID = "" + door2.id;
        if (door3 != null) door3ID = "" + door3.id;

        return "Room " + id + " <1> --> " + door1ID + " <2> --> " + door2ID 
            + " <3> --> " + door3ID;
    }

    // accessor for id field
    public int ID() {
        return id;
    }

    // print all rooms and their properties
    public static void showRooms() {
        Room now = last;
        if (now == null)
            System.out.println("There are no rooms.");
        else 
            while (now != null) {
                System.out.println(now);
                now = now.previous;
            }
    }

    // connect two rooms, room1 and room2, together via a bi-directional tunnel
    public static void connect(Room room1, Room room2) {
        // check to see if have enough free doors --
        // a self-loop tunnel requires TWO free doors!
       
if (!room1.doorFree() || !room2.doorFree())
            System.out.println(room1.id + " and " + room2.id 
                + " could not be connected... not enough doors!");
        else if (room1 == room2 && room1.door2 != null)
            System.out.println (room1.id 
                + " could not be connected with itself... not enough doors!");
        else
            // both rooms have free doors, so create a tunnel between them
           
room1.uniConnect(room2);
            room2.uniConnect(room1);
        }
    }

    // return whether "there is a free door"
    private boolean doorFree() {
        return door1 == null || door2 == null || door3 == null;
    } 

    // assume have a free door: connect room r to next free door
    private void uniConnect(Room r) {
        if (door1 == null) door1 = r;
        else if (door2 == null) door2 = r;
        else door3 = r;
    }

    // returns room on the other side of "door"
    public Room farRoom(int door) {
        if (door == 1) return door1;
        else if (door == 2) return door2;
        else if (door == 3) return door3;
        else return null;
    }

    // Test harness.
    public static void main(String[] args) {
        // test constructor
       
Room r1 = new Room(), r2 = new Room(), r3 = new Room(); 
        // test toString
       
System.out.println(r1);
        System.out.println(r2);
        System.out.println(r3);
        // test findRoom 3 ways: print, reference, ID
       
System.out.println(findRoom(3));
        if (findRoom(2) != r2)
            System.out.println("findRoom error!");
        if (findRoom(1).ID() != 1)
            System.out.println("findRoom.ID error!");
        // test showRooms
        showRooms();
        // test connect and farRoom()
       
connect(r1, r2);
        if (r1.farRoom(1)!= r2) 
            System.out.println("Connect failure 1"); 
        if (r2.farRoom(1)!= r1) 
            System.out.println("Connect failure 2"); 
        if (r1.farRoom(2) != null) 
            System.out.println("Connect failure 3"); 
        if (r2.farRoom(2) != null) 
            System.out.println("Connect failure 4");
        connect(r1, r3); 
        if (r1.farRoom(1) != r2)
            System.out.println("Connect failure 5"); 
        if (r1.farRoom(2) != r3) 
            System.out.println("Connect failure 6"); 
        if (r3.farRoom(2) != r1) 
            System.out.println("Connect failure 7"); 
            showRooms();
        }

} // end Class Room


/* Christine Chung */
/* Assignment
3 Sample Solution */
/* Sept.
23, 1999 */
/* Application.java */

/* Simple adventure game in a cave with a player and demon running around */

public class Application {
    public static void main(String[] args) {
        int numRooms; // number of rooms user wishes to create
        int first, second; // holders for room ID numbers being read in
        int door; // door that user  goes through
        int demonDoor; // door that demon  goes through
        Room player, demon; //rooms that the player and demon are in

        // initialize Text object in to read from standard input.
        TokenReader in = new TokenReader(System.in);

        //prompt user (not required)
        System.out.print("How many rooms? ");
        System.out.flush();
        numRooms = in.readInt();

         // create numRooms number of rooms
        int counter = 0; // number of rooms already created
        while (counter != numRooms) {
            counter++;
            new Room();
        }

        System.out.println("Type in the room-room pairs, terminated by 0 0");
        first = in.readInt();
        second = in.readInt();
          // create cave with appropriate tunnels as specified by input
       
while (first != 0) {
            // connect first and second with a 2-way tunnel
           
Room.connect(Room.findRoom(first), Room.findRoom(second));
            // read next door
          
 first = in.readInt();
           second = in.readInt();
        }

        // display rooms and their properties
        Room.showRooms();
        // start player and demon in random rooms
       
player = Room.findRoom(1+(int)(Math.random()*numRooms));
        demon = Room.findRoom(1+(int)(Math.random()*numRooms));
        
        // play the game with player until demon finds player

        while (player.ID() != demon.ID()) { // or "player != demon"
            System.out.println("You are in " + player);
            System.out.println("Demon is in " + demon);

             // player makes a move
            System.out.print("Which door? ");
            System.out.flush();
            door = in.readInt();
            if (player.farRoom(door) != null)
                player = player.farRoom(door);
            // demon makes a random move
           
demonDoor = (1+(int)(Math.random()*4)); 
            if (demon.farRoom(door) != null) 
                demon = demon.farRoom(demonDoor);
        }
        System.out.println("The demon captures you! Game Over!");
    }
} // end class Application