i use bluejay as my java editor The assignment isnt long even though it looks lo
ID: 3595612 • Letter: I
Question
i use bluejay as my java editor
The assignment isnt long even though it looks long but it is mostly just fill in the gap in the classes which can be found below after the questions
Part 1: Two new commands: "look" and "eat".
Add the "look" command to the Zuul game. This involves three steps:
Add the "look" command to the list of valid commands in the "CommandWords" class.
Add a "look" method to the "Game" class. This is a void method with one parameter, the command. All it does is output the detailed description of the current room, see "Room" method "getLongDescription".
Update "Game" method "processCommand" to deal with the "look" command. The "look" command should just invoke the "look" method that you added in the previous step.
Now add an "eat" command to the Zuul game. The "eat" command should output a message to say that you have eaten and are no longer hungry. The steps to add the "eat" command are similar to those for the "look" command (above).
Part 2: Commands output by "Game" for design flexibility (e.g. adding a GUI in a future release).
Change method "showAll" in class "CommandWords" to "getCommandList" and have it return a String instead of printing the commands.
This also requires a change in class "Parser". Method "showCommands" should become "getCommands", and again return a String.
Finally, this requires a change in class "Game" in method "printHelp". (This method will now print the information.)
Part 3: Rooms contain items.
Each Item is made up of a description (String) and weight (double).
Create an Item class. All you need is a constructor and a method that returns a string representation of the item (containing its description and weight).
Each room may contain any number of items, so these will be stored in an ArrayList.
You will need a method to add an item to a room.
The information about each item in the room is part of the room's (long) description.
Update the "createRooms" method in "Game" to create some items and add them to some rooms so that you can test your code.
Part 4: Another new command: "back"
The "back" command can have only one word. If there is a second word in the command, just print "Back what?".
The "back" command takes you to the previous room that you visited. (The one you were in immediately before this one.) Print a message to say that you have gone back.
If you are at the beginning of the game, you cannot go back, so just print a message explaining this.
You will need to make various changes to implement this command correctly.
Hint: If you invoke the "back" command twice in a row, where should you be?
Part 5: "look" and "eat" are one word commands.
Check that commands "look" and "eat" output "Look what?" and "Eat what?" if there is a second word in the command.
Part 6: Another new command "stackBack"
"stackBack" is a one word command.
"stackBack" allows you to go back one room at a time as far as you like (until you reach the beginning of the game).
If you are at the beginning of the game, you cannot go "stackBack", so just print a message explaining this.
You will need to use a Stack (see the Java documentation for information on this class).
Again, you will need to make various changes to implement this command correctly.
Hint: If you invoke the "stackBack" command twice in a row, where should you be? (The answer will normally be different from that of part 4.)
Hint: If you invoke "stackBack" enough times, you will eventually return to the beginning of the game.
Part 7: Tidying up.Double check all the files that you added or edited (i.e. "CommandWords.java", "Game.java", "Item.java", "Parser.java", and "Room.java") for good programming style and comments.
You should not have made any changes to "Command.java", so leave it as is.
Double check your code for proper formatting and indentation.
Make sure that you have sufficient documentation, both javadoc and comments that will help us understand your code.
Generate your javadoc documentation to check that it looks ok.
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* This class is the main class of the "World of Zuul" application.
* "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
* to make it more interesting!
*
* To play this game, create an instance of this class and call the "play"
* method.
*
* This main class creates and initialises all the others: it creates all
* rooms, creates the parser and starts the game. It also evaluates and
* executes the commands that the parser returns.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*/
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, theatre, pub, lab, office;
// 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");
// initialise room exits
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
theatre.setExit("west", outside);
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
office.setExit("west", 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(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 a 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.
*
* @param command The command to be processed
*/
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.
*
* @param command The command to be processed
* @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
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* This class holds information about a command that was issued by the user.
* A command currently consists of two strings: a command word and a second
* word (for example, if the command was "take map", then the two strings
* obviously are "take" and "map").
*
* The way this is used is: Commands are already checked for being valid
* command words. If the user entered an invalid command (a word that is not
* known) then the command word is <null>.
*
* If the command had only one word, then the second word is< null>.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*/
public class Command
{
private String commandWord;
private String secondWord;
/**
* Create a command object. First and second word must be supplied, but
* either one (or both) can be null.
*
* @param firstWord The first word of the command. Null if the command
* was not recognised
* @param secondWord The second word of the command
*/
public Command(String firstWord, String secondWord)
{
commandWord = firstWord;
this.secondWord = secondWord;
}
/**
* Return the command word (the first word) of this command. If the
* command was not understood, the result is null.
*
* @return The command word, or null if not understood
*/
public String getCommandWord()
{
return commandWord;
}
/**
* Returns the second word of the command. Returns null if there was no
* second word.
*
* @return The second word of this command, or null if only one word
*/
public String getSecondWord()
{
return secondWord;
}
/**
* Returns true if the command is unknown.
*
* @return true if this command was not understood, false otherwise
*/
public boolean isUnknown()
{
return (commandWord == null);
}
/**
* Returns true if the command has a second word.
*
* @return true if the command has a second word, false otherwise
*/
public boolean hasSecondWord()
{
return (secondWord != null);
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* This class holds an enumeration of all command words known to the game.
* It is used to recognise commands as they are typed in.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*/
public class CommandWords
{
// a constant array that holds all valid command words
private static final String[] validCommands = {
"go", "quit", "help"
};
/**
* Constructor - initialise the command words.
*/
public CommandWords()
{
// nothing to do at the moment...
}
/**
* Check whether a given String is a valid command word.
*
* @param aString The String to check
* @return true if it is valid, false otherwise
*/
public boolean isCommand(String aString)
{
for(int i = 0; i < validCommands.length; i++) {
if(validCommands[i].equals(aString))
return true;
}
// if we get here, the string was not found in the commands
return false;
}
/**
* Print all valid commands to System.out.
*/
public void showAll()
{
for(String command: validCommands) {
System.out.print(command + " ");
}
System.out.println();
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
/**
* Class Room - a room in an adventure game.
*
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* A "Room" represents one location in the scenery of the game. It is
* connected to other rooms via exits. For each existing exit, the room
* stores a reference to the neighboring room.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*/
public class Room
{
private String description;
private HashMap<String, Room> 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<String, Room>();
}
/**
* Define an exit from this room.
*
* @param direction The direction of the exit
* @param neighbour The room to which the exit leads
*/
public void setExit(String direction, Room neighbour)
{
exits.put(direction, neighbour);
}
/**
* Returns a short description of the room, i.e. the one that
* was defined in the constructor
*
* @return The short description of the room
*/
public String getShortDescription()
{
return description;
}
/**
* Return a long 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<String> 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);
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* This parser reads user input and tries to interpret it as an "Adventure"
* command. Every time it is called it reads a line from the terminal and
* tries to interpret the line as a two word command. It returns the command
* as an object of class Command.
*
* The parser has a set of known command words. It checks user input against
* the known commands, and if the input is not one of the known commands, it
* returns a command object that is marked as an unknown command.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*/
public class Parser
{
private CommandWords commands; // holds all valid command words
private Scanner reader; // source of command input
/**
* Create a parser to read from the terminal window.
*/
public Parser()
{
commands = new CommandWords();
reader = new Scanner(System.in);
}
/**
* Get the command from the user.
*
* @return The next command from the user
*/
public Command getCommand()
{
String inputLine; // will hold the full input line
String word1 = null;
String word2 = null;
System.out.print("> "); // print prompt
inputLine = reader.nextLine();
// Find up to two words on the line.
Scanner tokenizer = new Scanner(inputLine);
if(tokenizer.hasNext()) {
word1 = tokenizer.next(); // get first word
if(tokenizer.hasNext()) {
word2 = tokenizer.next(); // get second word
// note: we just ignore the rest of the input line.
}
}
// Now check whether this word is known. If so, create a command
// with it. If not, create a "null" command (for unknown command).
if(commands.isCommand(word1)) {
return new Command(word1, word2);
}
else {
return new Command(null, word2);
}
}
/**
* Print out a list of valid command words.
*/
public void showCommands()
{
commands.showAll();
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
Points Answered: 1,2,3,5,7
Since as per the current implementation rooms travelled are not saved. Functionality of Back and stackback (Pt.4/6) cannot be implemented
PARSER.java
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* This parser reads user input and tries to interpret it as an "Adventure"
* command. Every time it is called it reads a line from the terminal and
* tries to interpret the line as a two word command. It returns the command
* as an object of class Command.
*
* The parser has a set of known command words. It checks user input against
* the known commands, and if the input is not one of the known commands, it
* returns a command object that is marked as an unknown command.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*/
public class Parser
{
private CommandWords commands; // holds all valid command words
private Scanner reader; // source of command input
/**
* Create a parser to read from the terminal window.
*/
public Parser()
{
commands = new CommandWords();
reader = new Scanner(System.in);
}
/**
* Get the command from the user.
*
* @return The next command from the user
*/
public Command getCommand()
{
String inputLine; // will hold the full input line
String word1 = null;
String word2 = null;
System.out.print("> "); // print prompt
inputLine = reader.nextLine();
// Find up to two words on the line.
Scanner tokenizer = new Scanner(inputLine);
if(tokenizer.hasNext()) {
word1 = tokenizer.next(); // get first word
if(tokenizer.hasNext()) {
word2 = tokenizer.next(); // get second word
// note: we just ignore the rest of the input line.
}
}
// Now check whether this word is known. If so, create a command
// with it. If not, create a "null" command (for unknown command).
if(commands.isCommand(word1)) {
return new Command(word1, word2);
}
else {
return new Command(null, word2);
}
}
/**
* Print out a list of valid command words.
*/
public String showCommands()
{
return commands.getCommandList();
}
}
Game.java
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* This class is the main class of the "World of Zuul" application.
* "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
* to make it more interesting!
*
* To play this game, create an instance of this class and call the "play"
* method.
*
* This main class creates and initialises all the others: it creates all
* rooms, creates the parser and starts the game. It also evaluates and
* executes the commands that the parser returns.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*/
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, theatre, pub, lab, office;
// 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");
//Add Items to rooms
office.addItem(new Item("Chair", 5.0));
lab.addItem(new Item("Appratus", 3.0));
lab.addItem(new Item("Funnel", 0.5));
// initialise room exits
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
theatre.setExit("west", outside);
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
office.setExit("west", 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 detailed description of the current room
*/
private void look(Command command)
{
if(command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Look what?");
}
else{
System.out.println(currentRoom.getLongDescription());
}
}
/**
* Print out eating status
*/
private void eat(Command command)
{
if(command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Look what?");
}
else{
System.out.println("You have eaten and are no longer hungry!!");
}
}
/**
* 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(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 if (commandWord.equals("look")) {
look(command);
}
else if (commandWord.equals("eat")) {
eat(command);
}
// else command not recognised.
return wantToQuit;
}
// implementations of user commands:
/**
* Print out some help information.
* Here we print a 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.
*
* @param command The command to be processed
*/
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.
*
* @param command The command to be processed
* @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
}
}
}
CommandWords.java
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* This class holds an enumeration of all command words known to the game.
* It is used to recognise commands as they are typed in.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*/
public class CommandWords
{
// a constant array that holds all valid command words
private static final String[] validCommands = {
"go", "quit", "help","look","eat"
};
/**
* Constructor - initialise the command words.
*/
public CommandWords()
{
// nothing to do at the moment...
}
/**
* Check whether a given String is a valid command word.
*
* @param aString The String to check
* @return true if it is valid, false otherwise
*/
public boolean isCommand(String aString)
{
for(int i = 0; i < validCommands.length; i++) {
if(validCommands[i].equals(aString))
return true;
}
// if we get here, the string was not found in the commands
return false;
}
/**
* Return list of all valid commands
*/
public String getCommandList()
{
String commandList="";
for( String command: validCommands) {
commandList=command + " ";
}
return commandList;
}
}
Item.java
public class Item {
private String description;
private double weight;
public Item(String description, double weight) {
super();
this.description = description;
this.weight = weight;
}
@Override
public String toString() {
return "Item [description=" + description + ", weight=" + weight + "]";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.