Add \"up\" and \"down\" directions to the Zuul-Bad project by making the necessa
ID: 3669366 • Letter: A
Question
Add "up" and "down" directions to the Zuul-Bad project by making the necessary changes in Room and Game class
public class Room
{
public String description;
public Room northExit;
public Room southExit;
public Room eastExit;
public Room westExit;
/**
* 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;
}
/**
* Define the exits of this room. Every direction either leads
* to another room or is null (no exit there).
* @param north The north exit.
* @param east The east east.
* @param south The south exit.
* @param west The west exit.
*/
public void setExits(Room north, Room east, Room south, Room west)
{
if(north != null)
northExit = north;
if(east != null)
eastExit = east;
if(south != null)
southExit = south;
if(west != null)
westExit = west;
}
/**
* @return The description of the room.
*/
public String getDescription()
{
return description;
}
}
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 outside, theater, pub, lab, office;
// create the rooms
outside = new Room("outside the main entrance of the university");
theater = new Room("in a lecture theater");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
// initialise room exits
outside.setExits(null, theater, lab, pub);
theater.setExits(null, null, null, outside);
pub.setExits(null, outside, null, null);
lab.setExits(outside, office, null, null);
office.setExits(null, null, null, lab);
currentRoom = outside; // start game outside
}
/**
* 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 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("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();
}
/**
* 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("around at the university.");
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 = null;
if(direction.equals("north")) {
nextRoom = currentRoom.northExit;
}
if(direction.equals("east")) {
nextRoom = currentRoom.eastExit;
}
if(direction.equals("south")) {
nextRoom = currentRoom.southExit;
}
if(direction.equals("west")) {
nextRoom = currentRoom.westExit;
}
if (nextRoom == null) {
System.out.println("There is no door!");
}
else {
currentRoom = nextRoom;
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();
}
}
/**
* "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
Explanation / Answer
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
* Item class creates an item to go into a room
*
* @author (Carolyn Leithner)
* @version (2/10/2011)
*/
// Exercise 7.20
public class Item
{
// instance variables - replace the example below with your own
private String description;
private int weight;
//private HashMap items;
// a constant array that holds all items
// exercise 7.20
//private ArrayList<Item> items;
private ArrayList<Item> list;
/**
* Constructor for objects of class Item
* Create an item described "description", and Weight
*/
public Item(String description, int weight)
{
// initialise instance variables
this.description = description;
weight = weight;
this.list = new ArrayList<Item>();
//Item item = new Item();
addItem();
}
/**
* Return the description of the item
*/
public String getShortDescription()
{
return description;
}
/**
* Return the weight of the item
*/
public int weight()
{
return weight;
}
/**
* Return a long description of the item that includes the description
* and weight.
*/
public String toString()
{
return "Item " + description + " Weight " + weight;
// return "Item: " + list;
}
// exercise 7.20
/**
* add an item to the room when it is created
*/
public void addItem(String description,int weight)
{
list.add(new Item (description, weight));
}
/**
* create a variety of items
*/
public void add(Item aItem)
{
list.add(aItem);
}
/**
* get the item
*/
public ArrayList<Item> getItems()
{
return list;
}
/**
*
*/
public String getItemString()
{
String returnString = "Item:";
{
returnString += " " + list;
}
return returnString;
}
/**
*
*/
public void addItem()
{
Item item1 = new Item("rock ", 3);
Item item2 = new Item("book ", 4);
Item item3 = new Item("pen ", 1);
Item item4 = new Item("stick ", 2);
Item item5 = new Item("beaker ", 1);
Item item6 = new Item("phone " , 2);
// this.list.add(new Item ("rock", 3));
// this.list.add(new Item ("phone", 2));
// this.list.add(new Item ("pen", 1));
// this.list.add(new Item ("beaker", 1));
// this.list.add(new Item ("stick", 2));
// this.list.add(new Item ("book", 1));
}
}
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
* This is the Zuul-Starter file used for the Homework. Please add the
* appropriate class comments below
*
* @author: Carolyn Leithner
* @version: Feb 10,2011
*/
public class Room
{
public String description;
private HashMap<String, Room> exits;
/*
private Room northExit;
private Room southExit;
private Room eastExit;
private Room westExit;
*/
// exercise 7.20
//private ArrayList<Item> items;
//private HashMap<String, Item> items;
private String item;
/**
* 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<String, Room>();
//addItem();
//items = new ArrayList<String, Room>();
}
// starter file
/**
* 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 description of the room.
*/
public String getDescription()
{
return description;
}
public Room getExit(String direction) {
return exits.get(direction);
/*
if(direction.equals("north")) {
return northExit;
}
if(direction.equals("east")) {
return eastExit;
}
if(direction.equals("south")) {
return southExit;
}
if(direction.equals("west")) {
return westExit;
}
return null;
*/
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
* @return Details of the room's exits.
*/
public String getExitString()
{
/*
String exitString = "Exits: ";
if(northExit != null)
exitString += "north ";
if(eastExit != null)
exitString += "east ";
if(southExit != null)
exitString += "south ";
if(westExit != null)
exitString += "west ";
return exitString;
*/
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys) {
returnString += " " + exit;
}
return returnString;
}
//exercise 7.11
/**
* Return a long description of this room, of the form:
* You are in the kitchen
* Exits: north west
* @return A description of the room, including exits.
*/
public String getLongDescription()
{
return "You are " + description +". " + getExitString() + ". ";
// + "Items in Room" + getItemString();
}
// // exercise 7.20
// /**
// * add an item to the room when it is created
// */
// public void addItem(Item item)
// {
// addItem (item);
// }
}
/**
* This is the Zuul-Starter file used for the Homework. Please add the
* appropriate class comments below
*
* @author:
* @version:
*/
public class Game
{
private Parser parser;
private Room currentRoom;
private Item item1;
/**
* 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 outside, theatre, pub, lab, office, cellar;
// create the rooms
outside = new Room("outside the main entrance of the university");
theatre = new Room("in a lecture theatre");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
cellar = new Room("in the cellar");
// initialise room exits
//outside.setExits(null, theatre, lab, pub);
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
//theatre.setExits(null, null, null, outside);
theatre.setExit("west", outside);
//pub.setExits(null, outside, null, null);
pub.setExit("east", outside);
//lab.setExits(outside, office, null, null);
lab.setExit("north", outside);
lab.setExit("east", office);
//office.setExits(null, null, null, lab);
office.setExit("west", lab);
office.setExit("down", cellar);
cellar.setExit("up", office);
Item item1 = new Item("rock ", 3);
Item item2 = new Item("book ", 4);
Item item3 = new Item("pen ", 1);
Item item4 = new Item("stick ", 2);
Item item5 = new Item("beaker ", 1);
Item item6 = new Item("phone " , 2);
currentRoom = outside; // start game outside
}
/**
* 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 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();
printLocationInfo();
/*
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();
*/
}
/**
* 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);
// exercise 7.14
else if (commandWord.equals("look"))
look();
return wantToQuit;
}
// starter file
/**
* 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:");
// exercise 7.16
parser.showCommands();
System.out.println(" go quit help");
}
/**
* 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(direction.equals("north")) {
nextRoom = currentRoom.northExit;
}
if(direction.equals("east")) {
nextRoom = currentRoom.eastExit;
}
if(direction.equals("south")) {
nextRoom = currentRoom.southExit;
}
if(direction.equals("west")) {
nextRoom = currentRoom.westExit;
}
*/
if (nextRoom == null) {
System.out.println("There is no door!");
}
else {
currentRoom = nextRoom;
printLocationInfo();
/*
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();
*/
}
}
/**
* "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
}
}
// exercise 7.14
private void look()
{
System.out.println(currentRoom.getLongDescription());
}
public void printLocationInfo() {
//System.out.println("You are " + currentRoom.getDescription());
//System.out.println(currentRoom.getExitString());
//exercise 7.11
System.out.println(currentRoom.getLongDescription());
/*
System.out.print("Exits: ");
if(currentRoom.getExit("north") != null) {
System.out.print("north ");
}
if(currentRoom.getExit("east") != null) {
System.out.print("east ");
}
if(currentRoom.getExit("south") != null) {
System.out.print("south ");
}
if(currentRoom.getExit("west") != null) {
System.out.print("west ");
}
*/
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.