Question
public class Game { private Parser parser; private Room currentRoom; /** * Create the game and initialise its internal map. */ public Game() { createRooms();
public 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 foyer,battlezone,art_gallery,mummy_tomb,bone_house,passage, chamber,crypt,vault; // create the rooms foyer = new Room("in the Museum Foyer"); battlezone = new Room("in the Battlezone"); art_gallery = new Room("in the Art Gallery"); mummy_tomb = new Room("in a ancient Egyptian Mummy Tomb"); bone_house = new Room("in a Bone House"); passage = new Room("in a dark passage"); chamber = new Room("in a chamber"); crypt = new Room("in a crypt"); vault = new Room("in the vault"); // initialise room exits foyer.setExit("north", battlezone); foyer.setExit("east", art_gallery); foyer.setExit("west", mummy_tomb); battlezone.setExit("south", foyer); battlezone.setExit("east", passage); mummy_tomb.setExit("east", foyer); mummy_tomb.setExit("west", crypt); mummy_tomb.setExit("down", vault); art_gallery.setExit("west", foyer); art_gallery.setExit("east", bone_house); bone_house.setExit("west", art_gallery); bone_house.setExit("south", chamber); bone_house.setExit("up", passage); passage.setExit("down", bone_house); passage.setExit("west", battlezone); chamber.setExit("north", bone_house); crypt.setExit("east", mummy_tomb); vault.setExit("up", mummy_tomb); currentRoom = foyer; // game starts from foyer } /** * 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("You gave in so easily :-( Good bye!"); }
/** * Print out the opening message for the player. */ private void printWelcome() { System.out.println(); System.out.println("Welcome!! to the game 'Lost in the Cursed Museum'"); System.out.println("Lost in the Cursed Museum is a new, flimsy lame 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. * @param command The command to be processed. * @return true If the command ends the game, false otherwise. */ 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("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("in the Museum :-?."); System.out.println(); System.out.println("Your command words are:"); System.out.println(" go quit help"); }
/** * Try to go in 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()); } }
/** * "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 } } }
My question and another part of the code is in the earlier posted question please refer to that as I am unable to post my whole code again.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started