Question
Consider adding a back command to your Zuul game, as described in Exercise 6e: 8.23 in the book (or for more of a challenge, Exercise
- Consider adding a "back" command to your Zuul game, as described in Exercise 6e: 8.23 in the book (or for more of a challenge, Exercise 6e: 8.26 ). Describe how this new command would work from the player's perspective, including how the program should behave in a variety of cases. (See Exercise 6e: 8.24 .) Then, describe a design for implementing this feature, including which classes would need to be modified, and description of any new classes.
- Consider adding a time limit to your Zuul game, as described in Exercise 6e: 8.41 /in the book. Describe how this would work from the player's perspective, including how time will be counted, what task the player needs to complete in time, and how the game will behave if the player does / does not complete the task in time. Then, describe a design for implementing this feature, including which classes would need to be modified, and description of any new classes.
- Read section 6e: 8.13 in the book, and review the textbook projects zuul-with-enums-v1 and zuul-with-enums-v2. Describe the advantages of using enums. Assume you have implemented enums-v2 in your Game. Describe the code changes that would be required to offer your game in Spanish (all text the player sees would be in Spanish), including which classes would need to be modified, and description of any new classes.
Task this on book
Exercise 8.23 Implement a back command. This command does not have a second word. Entering the back command takes the player into the previous room he/she was in.
Exercise 8.24 Test your new command. Does it work as expected? Also, test cases where the command is used incorrectly. For example, what does your program do if a player types a second word after the back command? Does it behave sensibly?
Exercise 8.41 Add some form of time limit to your game. If a certain task is not completed in a specified time, the player loses. A time limit can easily be implemented by counting the number of moves or the number of entered commands. You do not need to use real-time.
the code is
Command.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 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. * * If the command had only one word, then the second word is . * * @author Michael Klling and David J. Barnes * @version 2016.02.29 */ 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. */ public String getCommandWord() { return commandWord; } /** * @return The second word of this command. Returns null if there was no * second word. */ public String getSecondWord() { return secondWord; } /** * @return true if this command was not understood. */ public boolean isUnknown() { return (commandWord == null); } /** * @return true if the command has a second word. */ public boolean hasSecondWord() { return (secondWord != null); } command words.java
public class CommandWords { // a constant array that holds all valid command words private static final String[] validCommands = { "go", "quit", "help" , "look" , "cook" }; /** * Constructor - initialise the command words. */ public CommandWords() { // nothing to do at the moment... } /** * Check whether a given String is a valid command word. * @return true if it is, false if it isn't. */ 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 String getCommandList() { String commandList =""; for(String command: validCommands) { commandList += command +" "; } return commandList; } }
Game.java
public class Game { private Parser parser; private Room currentRoom; private Item item; /** * 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, livingroom, ritanroom, ramiroom, bathroom ,bedroom; Item picture; // create the rooms outside = new Room("outside the house"); bedroom = new Room("in main bedroom"); rroom = new Room("in the R's room"); riroom = new Room("in the Ri's room"); livingroom = new Room("in a family room"); bathroom = new Room("in the bathroom"); picture = new Item("family picture",2); livingroom.additem(picture); // initialise room exits outside.setExit("north",livingroom ); bedroom.setExit("east", livingroom); bathroom.setExit("west", livingroom); bathroom.setExit("north", riroom); bathroom.setExit("south", rroom); riroom.setExit("south", bathroom); rroom.setExit("north", bathroom); livingroom.setExit("south",outside); livingroom.setExit("west",bedroom); livingroom.setExit("east",bathroom); 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 having fun together. Good bye."); } /** * Print out the opening message for the player. */ private void printWelcome() { System.out.println(); System.out.println("Welcome to the my house!"); System.out.println("feel free to walk around."); System.out.println("ask '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("look")) { look(); } else if (commandWord.equals("cook")) { cook(); } 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("we are having fun gathering , making food , playing game "); System.out.println("at the house."); System.out.println(); System.out.println("Your command words are:"); System.out.println(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()); } } /** * this method print getlong description */ private void look () { System.out.println(currentRoom.getLongDescription()); } /** * this method print lets make your favorite desh */ private void cook () { System.out.println ("lets make your favorite desh"); } /** * "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 } } }
}
Room .JAva
public class Room { private String description; private HashMapexits; // stores exits of this room. private Item 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<>(); this.item = item; } /** * 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; } /** * 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() { if (item != null){ return "You are " + description + ". " + getExitString()+ item.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); } /** * create a variety of items */ public void additem(Item item){ // this.item = item; } }
Parser
ublic 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); } /** * @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() { String commandList =""; commandList =commands.getCommandList(); return commandList; } }
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