13. november 2002 - 13:16
#6
Her er den klasse jeg kalder game:
class Game
{
private Parser parser;
private Room currentRoom;
/**
* Create the game and initialise its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room plaza, pub, kirke, motel, huset, hall, spisestue, bibliotek, trapperum, indgang, rotte, fange, nøgle, indgang1, trappetiltårn, jagtrum, balsal, balkon, tower;
// UDENFOR
plaza = new Room("Du står nu ude på pladsen");
pub = new Room("Velkommen til Vodka De Kovaltsenko");
kirke = new Room("Du er nu i kirke");
motel = new Room("Velkommen til Mies Sleepover");
huset = new Room("Du står nu udenfor huset");
// HAUNTED HOUSE
//STUE
hall = new Room("Du er nu i Hall'en");
spisestue = new Room("Du er nu i spisestuen");
bibliotek = new Room("Velkommen til det støvede bibliotek");
trapperum = new Room("Du står nu ved to trapper - en op og en ned");
// KÆLDER
indgang = new Room("Du er nu kommet ned i den mørke kælder");
rotte = new Room("Du er nu blevet spist af en mutantrotte");
fange = new Room("Du er nu gået ind i fangekælderen");
nøgle = new Room("Du er nu kommet ind i rummet med nøglen");
// 1. SAL
indgang1 = new Room("Du er nu på 1. sal");
trappetiltårn = new Room("Du er nu i et rum hvor der går en trappe op til tårnet");
jagtrum = new Room("Du er nu i husets jagtrum");
balsal = new Room("Du er nu i balsalen");
balkon = new Room("Du står på balkonnen og kigger på den flotte have");
// TÅRN
tower = new Room("Du er nu i tårnet.... udsigten er bare for fed");
// PLAZA
plaza.setExit("east", pub);
plaza.setLook("east", "Der er noget");
plaza.setExit("south", motel);
plaza.setExit("west", kirke);
plaza.setExit("north", huset);
// UDENFOR HUSET
huset.setExit("north", hall);
huset.setExit("south", plaza);
// HUSET STUEN
hall.setExit("east", bibliotek);
hall.setExit("south", huset);
hall.setExit("west", spisestue);
hall.setExit("north", trapperum);
trapperum.setExit("up", indgang1);
trapperum.setExit("down", indgang);
trapperum.setExit("south", hall);
bibliotek.setExit("west", hall);
spisestue.setExit("east", hall);
// HUSET KÆLDEREN
indgang.setExit("up", trapperum);
indgang.setExit("east", nøgle);
indgang.setExit("north", rotte);
indgang.setExit("west", fange);
nøgle.setExit("west", indgang);
fange.setExit("east", indgang);
// HUSET 1.SAL
indgang1.setExit("down", trapperum);
indgang1.setExit("east", jagtrum);
indgang1.setExit("north", balkon);
indgang1.setExit("west", balsal);
balkon.setExit("south", indgang1);
jagtrum.setExit("west", indgang1);
jagtrum.setExit("south", trappetiltårn);
trappetiltårn.setExit("north", jagtrum);
trappetiltårn.setExit("up", tower);
tower.setExit("down", trappetiltårn);
balsal.setExit("east", indgang1);
currentRoom = plaza;
}
/**
* Main play routine. Loops until end of play.
*/
public void play()
{
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over.
boolean finished = false;
while (! finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing. Good bye.");
}
/**
* Print out the opening message for the player.
*/
private void printWelcome()
{
System.out.println();
System.out.println("Welcome to Adventure!");
System.out.println("Adventure is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println();
System.out.println(currentRoom.getLongDescription());
}
/**
* Given a command, process (that is: execute) the command.
* If this command ends the game, true is returned, otherwise false is
* returned.
*/
private boolean processCommand(Command command)
{
boolean wantToQuit = false;
if(command.isUnknown()) {
System.out.println("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
if (commandWord.equals("help"))
printHelp();
else if (commandWord.equals("go"))
goRoom(command);
else if (commandWord.equals("look"))
lookAround(command);
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
return wantToQuit;
}
// implementations of user commands:
/**
* Print out some help information.
* Here we print some stupid, cryptic message and a list of the
* command words.
*/
private void printHelp()
{
System.out.println("You are lost. You are alone. You wander");
System.out.println("around at the university.");
System.out.println();
System.out.println("Your command words are:");
parser.showCommands();
}
/**
* Try to go to one direction. If there is an exit, enter the new
* room, otherwise print an error message.
*/
private void goRoom(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null)
System.out.println("There is no door!");
else {
currentRoom = nextRoom;
System.out.println(currentRoom.getLongDescription());
}
}
private void lookAround(Command command)
{
if(!command.hasSecondWord()) {
System.out.println("Hvor skal jeg kigge hen?");
return;
}
String kigRetning = command.getSecondWord();
String beskrivelse = (String)look.get(kigRetning);
System.out.println(beskrivelse);
}
/**
* "Quit" was entered. Check the rest of the command to see
* whether we really quit the game. Return true, if this command
* quits the game, false otherwise.
*/
private boolean quit(Command command)
{
if(command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
}
else
return true; // signal that we want to quit
}
}
13. november 2002 - 13:17
#7
og her er klassen Room:
class Room
{
private String description;
private HashMap look;
private HashMap exits; // stores exits of this room.
/**
* Create a room described "description". Initially, it has no exits.
* "description" is something like "in a kitchen" or "in an open court
* yard".
*/
public Room(String description)
{
this.description = description;
look = new HashMap();
exits = new HashMap();
}
/**
* Define an exit from this room.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
public void setLook(String direction, String rummetsUdseende)
{
look.put(direction, rummetsUdseende);
}
/**
* Return the description of the room (the one that was defined in the
* constructor).
*/
public String getShortDescription()
{
return description;
}
/**
* Return a long description of this room, in the form:
* You are in the kitchen.
* Exits: north west
*/
public String getLongDescription()
{
return "You are " + description + ".\n" + getExitString();
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
*/
private String getExitString()
{
String returnString = "Exits:";
Set keys = exits.keySet();
for(Iterator iter = keys.iterator(); iter.hasNext(); )
returnString += " " + iter.next();
return returnString;
}
/**
* Return the room that is reached if we go from this room in direction
* "direction". If there is no room in that direction, return null.
*/
public Room getExit(String direction)
{
return (Room)exits.get(direction);
}
public Room getLook(String direction)
{
return (Room)look.get(direction);
}
}