/* Alan Renaud */
/* Assignment 3 Sample Solution */
/* Using Arrays */
/* Oct. 17, 1999 */
/* Cave.java */
/* Plays the Demon game using a Room.java definition that uses arrays */
public class Cave {
public static void
main(String args[]) {
TokenReader in = new TokenReader(System.in);
System.out.print("How many rooms?
");
System.out.flush();
int numRooms = in.readInt();
for
(int i=1; i <=
numRooms; i++)
new Room();
System.out.println("Type in the room-room pairs, terminated by 0
0");
int first = in.readInt();
int second = in.readInt();
while ((first <=
numRooms)&&(first>0)&&(second <= numRooms)&&(second > 0)) {
Room.connect(Room.findRoom(first),Room.findRoom(second));
first = in.readInt();
second = in.readInt();
}
Room.showRooms();
Room player = Room.findRoom((int)(Math.random()*numRooms) +
1);
Room demon = Room.findRoom((int)(Math.random()*numRooms) +
1);
int door;
while (player.ID() !=
demon.ID()) {
System.out.println("You are in " + player);
System.out.println("Demon is in " + demon);
System.out.print("Which door?
");
System.out.flush();
door = in.readInt();
if (player.farRoom(door)!=null)
player=
player.farRoom(door);
door = (int)(1+
Math.random()*(Room.MAXDOORS));
if
(demon.farRoom(door)!=null)
demon =
demon.farRoom(door);
}
System.out.println("The demon captures you! Game
over!");
}
}
/* SAMPLE OUTPUT:
How many rooms? 8
Type in the room-room pairs, terminated by 0 0
1 3 1 4 1 5 2 4 2 5 2 8 3 6 3 6 4 6 8 8 0 0
You are in Room 6. <1> --> 3 <2> --> 3 <3> --> 4
Demon is in Room 4. <1> --> 1 <2> --> 2 <3> --> 6
Which door? 3
You are in Room 4. <1> --> 1 <2> --> 2 <3> --> 6
Demon is in Room 6. <1> --> 3 <2> --> 3 <3> --> 4
Which door? 3
You are in Room 6. <1> --> 3 <2> --> 3 <3> --> 4
Demon is in Room 3. <1> --> 1 <2> --> 6 <3> --> 6
Which door? 2
The demon captures you! Game over!
*/