FileDocCategorySizeDatePackage
MetroLib.javaAPI DocExample1968Sun Mar 16 15:12:14 GMT 2003metrolib

MetroLib

public class MetroLib extends Object

Fields Summary
private Room
room
Constructors Summary
MetroLib()

		room = Rooms.getFirstRoom();
	
Methods Summary
public static voidmain(java.lang.String[] args)
Main program, get the game going and run it.

		println("This is a trivial game demonstrating OO structures.");
		BufferedReader is = new BufferedReader(
			new InputStreamReader(System.in));
		MetroLib game = new MetroLib();
		println("You can in general move north south east or west");
		println("");
		// Manually print out first room's message.
		println(game.room.entryMessage);

		// Main loop: run the game.
		int cmd;
		while ((cmd = game.parseCmd(is)) != Cmd.QUIT) {
			game.play(cmd);
		}
	
intparseCmd(java.io.BufferedReader is)
Read a line and delegate it to the parser

		String line = is.readLine();
		return Cmd.parseCmd(line);
	
voidplay(int cmd)

		Room newRoom = null;
		boolean noWay = false;
		switch(cmd) {
		case Cmd.UNKNOWN:
			println("Unknown command!");
			break;
		case Cmd.NORTH:
			if (room.north != null) {
				newRoom = room.north;
			} else
				noWay = true;
			break;
		case Cmd.EAST:
			if (room.east != null) {
				newRoom = room.east;
			} else
				noWay = true;
			break;
		case Cmd.SOUTH:
			if (room.south != null) {
				newRoom = room.south;
			} else
				noWay = true;
			break;
		case Cmd.WEST:
			if (room.west != null) {
				newRoom = room.west;
			} else
				noWay = true;
			break;
		case Cmd.HELP:
			println("Sorry, help not written yet. I cannot help it");
			break;
		default:
			println("LOGIC ERROR: Unhandled case " + (char)cmd);
			break;
		}
		if (noWay) {
			println("I see no way to go in that direction");
			return;
		}
		if (newRoom != null) {
			if (room.exitMessage != null) {
				println(room.exitMessage);
			}
			// The magic happens: we change rooms.
			room = newRoom;
			println(room.entryMessage);
		}
	
static voidprintln(java.lang.String s)

		System.out.println(s);