Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I added transporterRoom which is supposed to bring the user to a random room, I am having trouble with that Sample Run public class Game

I added transporterRoom which is supposed to bring the user to a random room, I am having trouble with thatimage text in transcribed

Sample Run

public class Game { private final Parser parser; private Room currentRoom; private Scenario scenario; public Game() { scenario = new Scenario(); currentRoom = scenario.getStartRoom(); parser = new Parser(); }

/** * Main play routine. Loops until end of play. */ public void play() { printWelcome(); boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); } System.out.println("Thank you for playing. Good bye."); }

private void printWelcome() { System.out.println(); System.out.println("Welcome to the World of Zuul!"); 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(); 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); } // else command not recognised. 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 in 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()); } }

/** * "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 } } }

public class Room { private String description; private HashMap exits; // stores exits of this room.

/** * Create a room described "description". Initially, it has * no exits. "description" is something like "a kitchen" or * "an open court yard". * @param description The room's description. */ public Room(String description) { this.description = description; exits = new HashMap(); }

/** * Define an exit from this room. * @param direction The direction of the exit. * @param neighbor The room to which the exit leads. */ public void setExit(String direction, Room neighbor) { exits.put(direction, neighbor); }

/** * @return The short description of the room * (the one that was defined in the constructor). */ public String getShortDescription() { return description; } public String getExitLocationsString(){ String str = "Exits: "; for(String exitDir : exits.keySet()){ str += exitDir.toString()+" "; } return str; } /** * Return a description of the room in the form: * You are in the kitchen. * Exits: north west * @return A long description of this room */ public String getLongDescription() { return "You are " + description + ". " + getExitString(); }

/** * Return a string describing the room's exits, for example * "Exits: north west". * @return Details of the room's exits. */ private String getExitString() { String returnString = "Exits:"; Set keys = exits.keySet(); for(String exit : keys) { returnString += " " + exit; } 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. * @param direction The exit's direction. * @return The room in the given direction. */ public Room getExit(String direction) { return exits.get(direction); } }

public class Scenario { private List rooms; private Room startRoom; private Random random;

/** * Create a scenario */ public Scenario() { random = new Random(); // Set up your rooms, exits, and items // Move code from Game.createRooms here

// Set the start room

// Create the rooms ArrayList and add your rooms to it Room outside, living , restroom, bedroom, guest , kitchen, exercise; // create the rooms outside = new Room("Outside the main entrance of House"); living = new Room("in a Living room"); restroom = new Room("in a Restroom"); bedroom = new Room("in a Bedroom"); guest = new Room("in a Guestroom"); kitchen = new Room("in a Kitchen"); exercise = new Room("in a Exerciseroom"); // initialise room exits outside.setExit("north", living); living.setExit("north", kitchen); living.setExit("east", restroom); living.setExit("south", outside); living.setExit("west", bedroom); bedroom.setExit("east", living); restroom.setExit("west", living); kitchen.setExit("east", exercise); kitchen.setExit("south", living); kitchen.setExit("west", guest); guest.setExit("east", kitchen); exercise.setExit("west", kitchen); rooms = Arrays.asList(new Room[]{outside, living, restroom, bedroom, guest , kitchen, exercise}); startRoom = outside; // start game outside

}

/** * @return the start room for this scenario */ public Room getStartRoom() { // complete this method return this.startRoom; } /** * @return a random room from this scenario */ public Room getRandomRoom() { // complete this method return rooms.get(random.nextInt(rooms.size())); } }

public class TransporterRoom extends Room{ private Scenario scenario; /** * @param description String description of this room. * @param scenario The Scenario that's used in the Game class. */ public TransporterRoom(String description, Scenario scenario){ super(description); this.scenario = scenario; } /** * @return a random room using the findRandomRoom() method. */ @Override public Room getExit(String dir){ return findRandomRoom(); } /** * @return a random room using the Scenario given in this class's constructor */ public Room findRandomRoom(){ return scenario.getRandomRoom(); } /** * @return similar string as Room class, but with an extra message */ @Override public String getExitLocationsString() { return super.getExitLocationsString() + " Also, Extra message"; } }

Options Welcome to the World of Zuul! World of Zuul is a new, incredibly boring adventure game Type 'help' if you need help You are outside the main entrance of the university Exits: south north east west >go north You are in a transporter room You do not see any exits... try going somewhere > go jfkds You are in a lecture theater. Exits: west >go west You are outside the main entrance of the university Exits: south north east west >go north You are in a transporter room You do not see any exits... try going somewhere > go fjlkd You are in a computing lab This room contains an old Apple II computer Exits: east north

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions