This commit is contained in:
2025-02-11 13:17:22 +01:00
parent 6d4abadb09
commit c5f0773686

View File

@@ -5,27 +5,27 @@ package de.szut.zuul;
* "World of Zuul" is a very simple, text based adventure game. Users * "World of Zuul" is a very simple, text based adventure game. Users
* can walk around some scenery. That's all. It should really be extended * can walk around some scenery. That's all. It should really be extended
* to make it more interesting! * to make it more interesting!
* *
* To play this game, create an instance of this class and call the "play" * To play this game, create an instance of this class and call the "play"
* method. * method.
* *
* This main class creates and initialises all the others: it creates all * This main class creates and initialises all the others: it creates all
* rooms, creates the parser and starts the game. It also evaluates and * rooms, creates the parser and starts the game. It also evaluates and
* executes the commands that the parser returns. * executes the commands that the parser returns.
* *
* @author Michael Kölling and David J. Barnes * @author Michael Kölling and David J. Barnes
* @version 2016.02.29 * @version 2016.02.29
*/ */
public class Game public class Game
{ {
private Parser parser; private Parser parser;
private Room currentRoom; private Room currentRoom;
/** /**
* Create the game and initialise its internal map. * Create the game and initialise its internal map.
*/ */
public Game() public Game()
{ {
createRooms(); createRooms();
parser = new Parser(); parser = new Parser();
@@ -37,7 +37,7 @@ public class Game
private void createRooms() private void createRooms()
{ {
Room marketsquare, templePyramid, tavern, sacrificialSite, hut, jungle, secretPassage, cave, beach; Room marketsquare, templePyramid, tavern, sacrificialSite, hut, jungle, secretPassage, cave, beach;
// create the rooms // create the rooms
marketsquare = new Room("on the market square"); marketsquare = new Room("on the market square");
templePyramid = new Room("in a temple pyramid"); templePyramid = new Room("in a temple pyramid");
@@ -66,13 +66,13 @@ public class Game
/** /**
* Main play routine. Loops until end of play. * Main play routine. Loops until end of play.
*/ */
public void play() public void play()
{ {
printWelcome(); printWelcome();
// Enter the main command loop. Here we repeatedly read commands and // Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over. // execute them until the game is over.
boolean finished = false; boolean finished = false;
while (! finished) { while (! finished) {
Command command = parser.getCommand(); Command command = parser.getCommand();
@@ -91,21 +91,7 @@ public class Game
System.out.println("World of Zuul is a new, incredibly boring adventure game."); System.out.println("World of Zuul is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help."); System.out.println("Type 'help' if you need help.");
System.out.println(); System.out.println();
System.out.println("You are " + currentRoom.getDescription()); printRoomInformation(currentRoom);
System.out.print("Exits: ");
if(currentRoom.northExit != null) {
System.out.print("north ");
}
if(currentRoom.eastExit != null) {
System.out.print("east ");
}
if(currentRoom.southExit != null) {
System.out.print("south ");
}
if(currentRoom.westExit != null) {
System.out.print("west ");
}
System.out.println();
} }
/** /**
@@ -113,7 +99,7 @@ public class Game
* @param command The command to be processed. * @param command The command to be processed.
* @return true If the command ends the game, false otherwise. * @return true If the command ends the game, false otherwise.
*/ */
private boolean processCommand(Command command) private boolean processCommand(Command command)
{ {
boolean wantToQuit = false; boolean wantToQuit = false;
@@ -140,10 +126,10 @@ public class Game
/** /**
* Print out some help information. * Print out some help information.
* Here we print some stupid, cryptic message and a list of the * Here we print some stupid, cryptic message and a list of the
* command words. * command words.
*/ */
private void printHelp() private void printHelp()
{ {
System.out.println("You are lost. You are alone. You wander"); System.out.println("You are lost. You are alone. You wander");
System.out.println("through the jungle. At once there is a glade. On it there a buildings..."); System.out.println("through the jungle. At once there is a glade. On it there a buildings...");
@@ -152,11 +138,11 @@ public class Game
System.out.println(" go quit help"); System.out.println(" go quit help");
} }
/** /**
* Try to go in one direction. If there is an exit, enter * Try to go in one direction. If there is an exit, enter
* the new room, otherwise print an error message. * the new room, otherwise print an error message.
*/ */
private void goRoom(Command command) private void goRoom(Command command)
{ {
if(!command.hasSecondWord()) { if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go... // if there is no second word, we don't know where to go...
@@ -186,30 +172,16 @@ public class Game
} }
else { else {
currentRoom = nextRoom; currentRoom = nextRoom;
System.out.println("You are " + currentRoom.getDescription()); printRoomInformation(currentRoom);
System.out.print("Exits: ");
if(currentRoom.northExit != null) {
System.out.print("north ");
}
if(currentRoom.eastExit != null) {
System.out.print("east ");
}
if(currentRoom.southExit != null) {
System.out.print("south ");
}
if(currentRoom.westExit != null) {
System.out.print("west ");
}
System.out.println();
} }
} }
/** /**
* "Quit" was entered. Check the rest of the command to see * "Quit" was entered. Check the rest of the command to see
* whether we really quit the game. * whether we really quit the game.
* @return true, if this command quits the game, false otherwise. * @return true, if this command quits the game, false otherwise.
*/ */
private boolean quit(Command command) private boolean quit(Command command)
{ {
if(command.hasSecondWord()) { if(command.hasSecondWord()) {
System.out.println("Quit what?"); System.out.println("Quit what?");
@@ -219,4 +191,22 @@ public class Game
return true; // signal that we want to quit return true; // signal that we want to quit
} }
} }
private void printRoomInformation(Room currentRoom)
{
System.out.println("You are " + currentRoom.getDescription());
System.out.print("Exits: ");
if(currentRoom.northExit != null) {
System.out.print("north ");
}
if(currentRoom.eastExit != null) {
System.out.print("east ");
}
if(currentRoom.southExit != null) {
System.out.print("south ");
}
if(currentRoom.westExit != null) {
System.out.print("west ");
}
System.out.println();
}
} }