MuhammadAkbar-A2/MuhammadAkbar-A2-Documentation.docx Written Exercise 1 Describe your game including the back story and the setting. My game is based in a school. There are some rooms in the school...

1 answer below »
This assignment is following up of assignment 2 which I made previously and in this part It needed to modify the methods and the commands which I made in the last Assignment it has two files to be done one is word document which explains all the methods and written exercises and the programming file needs all the coding which needs to be done on Blue J


MuhammadAkbar-A2/MuhammadAkbar-A2-Documentation.docx Written Exercise 1 Describe your game including the back story and the setting. My game is based in a school. There are some rooms in the school and there are some exits at every room. In my game there is a fire blew up in the school, in order to win the game player must get out of the school in certain number of moves. Each room is locked by default and have one item in each room, the player must find safety items and add them into his inventory and then go to the any exit in given moves, if the player is unable to do that then he will lose the game. The player can have any number of items in his inventory. List the items in the game 1. Fire Blanket 2. Mask 3. Fire Extinguisher 4. Torch 5. Water 6. Sand Explain how the player wins The player must collect all the items in the game, add them into his inventory and then reach to any exit in moves before the fire goes out of control to win or else, he will lose. Written Exercise 2 Written Exercise 3 Class Room Instance variables · Add an instance variable of type Room for the up exit · Add an instance variable of type Room for the down exit setExits method · Add a parameter of type Room to the setExits method for the up exit · Add a parameter of type Room to the setExits method for the down exit · Add an if statement to the setExits method to assign the up parameter to the up instance variable · Add an if statement to the setExits method to assign the down parameter to the down instance variable getDescription method · no changes needed Class Game Instance variables · no changes needed createRooms method · Add an upExit argument of type Room to all the rooms calling setExits. · Add an downExit argument of type Room to all the rooms calling setExits method. play method · no changes needed printWelcome method · Add an if statement to the printWelcome method to print “up”, if the upExit of the current room is not null. · Add an if statement to the printWelcome method to print “down”, if the downExit of the current room is not null. processCommand method · no changes needed printHelp method · no changes needed goRoom method · Add an if statement to set the nextRoom to up room if the direction is "up" · Add an if statement to set the nextRoom to up room if the direction is "down" · Add an if statement to the printWelcome method to print up if the upExit of the current room is not null. · Add an if statement to the printWelcome method to print down if the downExit of the current room is not null. quit method · no changes needed Written Exercise 4 The printWelcome method and the goRoom methods contain code duplication and both print a description of the current room and a list of the exits. This is not a good design. Explain why. Ans. We should avoid duplication and write the common code in a separate method. Because if we keep duplicating and realised later that we need to make a small change then we will have to go in all the places where we duplicate the code and do it one by one and there will higher chance of errors. That’s the reason its always a good idea to have a separate method. Written Exercise 5 In the previous exercise, you created the getRoomExitsAndDescription method that prints a description of the current room and a list of the exits. This code is contained in the Game class. This is not a good design. Explain why. Ans. getRoomExitsAndDescription is an attributes of game class. As all the attributes and methods of an object must be binded together in a single entity, so getRoomExitsAndDescription should be a part of room method. Written Exercise 6 Class Room Instance variables · Add an instance variable of type Room for the up exit · Add an instance variable of type Room for the down exit setExits method · Add a parameter of type Room to the setExits method for the up exit · Add a parameter of type Room to the setExits method for the down exit · Add an if statement to the setExits method to assign the up parameter to the up instance variable · Add an if statement to the setExits method to assign the down parameter to the down instance variable getDescription method · no changes needed getExitString method · Add an if statement to the printWelcome method to add up to exit String if the upExit of the current room is not null. · Add an if statement to the printWelcome method to add down to exit String if the downExit of the current room is not null. Class Game Instance variables · no changes needed createRooms method · Add an argument of type Room to all the rooms calling setExits method for the up exit. · Add an argument of type Room to all the rooms calling setExits method for the down exit. play method · no changes needed printWelcome method · no changes needed processCommand method · no changes needed printHelp method · no changes needed goRoom method · Add an if statement to set the nextRoom to up room if the direction is "up" · Add an if statement to set the nextRoom to up room if the direction is "down" quit method · no changes needed Written Exercise 7 Answer the following question. Has the design improved? You must explain why or why not. Ans. Yes, because We took of the code duplications and now if we want to change anything, we will only have to do it once in our method. Design is really improved now. Its more readable. Name – Muhammad Gulraiz Akbar Student id - 23316337 MuhammadAkbar-A2/MuhammadAkbar-Final/Command.class public synchronized class Command { private String commandWord; private String secondWord; public void Command(String, String); public String getCommandWord(); public String getSecondWord(); public boolean isUnknown(); public boolean hasSecondWord(); } MuhammadAkbar-A2/MuhammadAkbar-Final/Command.ctxt #BlueJ class context comment0.target=Command comment0.text=\r\n\ This\ class\ is\ part\ of\ the\ "World\ of\ Zuul"\ application.\ \r\n\ "World\ of\ Zuul"\ is\ a\ very\ simple,\ text\ based\ adventure\ game.\ \ \r\n\r\n\ This\ class\ holds\ information\ about\ a\ command\ that\ was\ issued\ by\ the\ user.\r\n\ A\ command\ currently\ consists\ of\ two\ strings\:\ a\ command\ word\ and\ a\ second\r\n\ word\ (for\ example,\ if\ the\ command\ was\ "take\ map",\ then\ the\ two\ strings\r\n\ obviously\ are\ "take"\ and\ "map").\r\n\ \r\n\ The\ way\ this\ is\ used\ is\:\ Commands\ are\ already\ checked\ for\ being\ valid\r\n\ command\ words.\ If\ the\ user\ entered\ an\ invalid\ command\ (a\ word\ that\ is\ not\r\n\ known)\ then\ the\ command\ word\ is\ .\r\n\r\n\ If\ the\ command\ had\ only\ one\ word,\ then\ the\ second\ word\ is\ .\r\n\ \r\n\ @author\ \ Michael\ K\u00F6lling\ and\ David\ J.\ Barnes\r\n\ @version\ 2016.02.29\r\n comment1.params=firstWord\ secondWord comment1.target=Command(java.lang.String,\ java.lang.String) comment1.text=\r\n\ Create\ a\ command\ object.\ First\ and\ second\ word\ must\ be\ supplied,\ but\r\n\ either\ one\ (or\ both)\ can\ be\ null.\r\n\ @param\ firstWord\ The\ first\ word\ of\ the\ command.\ Null\ if\ the\ command\r\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ was\ not\ recognised.\r\n\ @param\ secondWord\ The\ second\ word\ of\ the\ command.\r\n comment2.params= comment2.target=java.lang.String\ getCommandWord() comment2.text=\r\n\ Return\ the\ command\ word\ (the\ first\ word)\ of\ this\ command.\ If\ the\r\n\ command\ was\ not\ understood,\ the\ result\ is\ null.\r\n\ @return\ The\ command\ word.\r\n comment3.params= comment3.target=java.lang.String\ getSecondWord() comment3.text=\r\n\ @return\ The\ second\ word\ of\ this\ command.\ Returns\ null\ if\ there\ was\ no\r\n\ second\ word.\r\n comment4.params= comment4.target=boolean\ isUnknown() comment4.text=\r\n\ @return\ true\ if\ this\ command\ was\ not\ understood.\r\n comment5.params= comment5.target=boolean\ hasSecondWord() comment5.text=\r\n\ @return\ true\ if\ the\ command\ has\ a\ second\ word.\r\n numComments=6 MuhammadAkbar-A2/MuhammadAkbar-Final/Command.java MuhammadAkbar-A2/MuhammadAkbar-Final/Command.java 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);     } } MuhammadAkbar-A2/MuhammadAkbar-Final/CommandWords.class public synchronized class CommandWords { private static final String[] validCommands; public void CommandWords(); public boolean isCommand(String); public void showAll(); static void (); } MuhammadAkbar-A2/MuhammadAkbar-Final/CommandWords.ctxt #BlueJ class context comment0.target=CommandWords comment0.text=\r\n\ This\ class\ is\ part\ of\ the\ "World\ of\ Zuul"\ application.\ "World\ of\ Zuul"\ is\ a\r\n\ very\ simple,\ text\ based\ adventure\ game.\r\n\ \r\n\ This\ class\ holds\ an\ enumeration\ of\ all\ command\ words\ known\ to\ the\ game.\ It\ is\r\n\ used\ to\ recognise\ commands\ as\ they\ are\ typed\ in.\r\n\r\n\ @author\ Michael\ K\u00F6lling\ and\ David\ J.\ Barnes\r\n\ @version\ 2016.02.29\r\n comment1.params= comment1.target=CommandWords() comment1.text=\r\n\ Constructor\ -\ initialise\ the\ command\ words.\r\n comment2.params=aString comment2.target=boolean\ isCommand(java.lang.String) comment2.text=\r\n\ Check\ whether\ a\ given\ String\ is\ a\ valid\ command\ word.\r\n\ \r\n\ @return\ true\ if\ it\ is,\ false\ if\ it\ isn't.\r\n comment3.params= comment3.target=void\ showAll() comment3.text=\r\n\ Print\ all\ valid\ commands\ to\ System.out.\r\n numComments=4 MuhammadAkbar-A2/MuhammadAkbar-Final/CommandWords.java MuhammadAkbar-A2/MuhammadAkbar-Final/CommandWords.java public class CommandWords {     // a constant array that holds all valid command words     private static final String[] validCommands = { "go", "quit", "help", "take", "inventory", };     /**      * 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 void showall() {         for (string command : validcommands) {             system.out.print(command + "  ");         }         system.out.println();     } } muhammadakbar-a2/muhammadakbar-final/game.class public synchronized class game { private parser parser; private room currentroom; java.util.arraylist useritem; public void game(); private void createrooms(); public void play(); private void printwelcome(); private boolean processcommand(command); private void printhelp(); private void goroom(command); private void takeitem(command); void displayinventory(); private boolean quit(command); } muhammadakbar-a2/muhammadakbar-final/game.ctxt #bluej class context comment0.target=game comment0.text=\r\n\ class\ room\ -\ a\ room\ in\ an\ adventure\ survival\ game.\r\n\r\n\ this\ class\ is\ the\ main\ class\ of\ the\ "anna's\ survival"\ application.\ "anna's\r\n\ survial"\ is\ a\ very\ simple,\ text\ based\ adventure\ survival\ game.\ users\ can\ walk\r\n\ anna\ around\ the\ school\ premises\ and\ lead\ her\ out\ of\ the\ shcool\ safely\ without\r\n\ exposing\ her\ to\ the\ deadly\ radiation\ due\ to\ chernobyl\ disaster.\ that's\ all.\r\n\ it\ should\ really\ be\ extended\ to\ make\ it\ more\ interesting\!\r\n\r\n\ a\ "room"\ represents\ one\ room\ inside\ the\ school\ premise\ of\ the\ game.\ it\ is\r\n\ connected\ to\ other\ rooms\ via\ exits.\ for\ each\ existing\ exit,\ the\ room\ stores\ a\r\n\ reference\ to\ the\ neighboring\ room.\r\n\ \r\n\ @author\ john\ dow\r\n\ @version\ 2016.02.29\r\n comment1.params= comment1.target=game() comment1.text=\r\n\ create\ the\ game\ and\ initialise\ its\ internal\ map.\r\n comment10.params=command comment10             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();=""     }="" }="" muhammadakbar-a2/muhammadakbar-final/game.class="" public="" synchronized="" class="" game="" {="" private="" parser="" parser;="" private="" room="" currentroom;="" java.util.arraylist="" useritem;="" public="" void="" game();="" private="" void="" createrooms();="" public="" void="" play();="" private="" void="" printwelcome();="" private="" boolean="" processcommand(command);="" private="" void="" printhelp();="" private="" void="" goroom(command);="" private="" void="" takeitem(command);="" void="" displayinventory();="" private="" boolean="" quit(command);="" }="" muhammadakbar-a2/muhammadakbar-final/game.ctxt="" #bluej="" class="" context="" comment0.target="Game" comment0.text="\r\n\" class\="" room\="" -\="" a\="" room\="" in\="" an\="" adventure\="" survival\="" game.\r\n\r\n\="" this\="" class\="" is\="" the\="" main\="" class\="" of\="" the\="" "anna's\="" survival"\="" application.\="" "anna's\r\n\="" survial"\="" is\="" a\="" very\="" simple,\="" text\="" based\="" adventure\="" survival\="" game.\="" users\="" can\="" walk\r\n\="" anna\="" around\="" the\="" school\="" premises\="" and\="" lead\="" her\="" out\="" of\="" the\="" shcool\="" safely\="" without\r\n\="" exposing\="" her\="" to\="" the\="" deadly\="" radiation\="" due\="" to\="" chernobyl\="" disaster.\="" that's\="" all.\r\n\="" it\="" should\="" really\="" be\="" extended\="" to\="" make\="" it\="" more\="" interesting\!\r\n\r\n\="" a\="" "room"\="" represents\="" one\="" room\="" inside\="" the\="" school\="" premise\="" of\="" the\="" game.\="" it\="" is\r\n\="" connected\="" to\="" other\="" rooms\="" via\="" exits.\="" for\="" each\="" existing\="" exit,\="" the\="" room\="" stores\="" a\r\n\="" reference\="" to\="" the\="" neighboring\="" room.\r\n\="" \r\n\="" @author\="" john\="" dow\r\n\="" @version\="" 2016.02.29\r\n="" comment1.params="comment1.target=Game()" comment1.text="\r\n\" create\="" the\="" game\="" and\="" initialise\="" its\="" internal\="" map.\r\n="" comment10.params="command">
Answered Same DayFeb 17, 2021

Answer To: MuhammadAkbar-A2/MuhammadAkbar-A2-Documentation.docx Written Exercise 1 Describe your game including...

Arun Shankar answered on Feb 17 2021
147 Votes
__MACOSX/._MuhammadAkbar-Final
MuhammadAkbar-Final/Game.ctxt
#BlueJ class context
comment0.target=Game
comment1.params=
comment1.target=Game()
comment1.text=\n\ Create\ the\ game\ and\ initialise\ its\ internal\ map.\n
comment10.params=command
comment10.target=void\ dropItem(Command)
comment11.params=command
comment11.target=boolean\ takeItem(Command)
comment12.params=
comment12.target=void\ displayInventory()
comment13.params=command
comment13.target=boolean\ quit(Command)
comment13.text=\n\ "Quit"\ was\ entered.\ Check\ the\ rest\ of\ the\ command\ to\ see\ whether\ we\ really\n\ quit\ the\ game.\n\ \n\ @return\ true,\ if\ this\ command\ quits\ the\ game,\ false\ otherwise.\n
comment2.params=
comment2.target=void\ createRooms()
comment2.text=\n\ Create\ all\ the\ rooms\ and\ link\ their\ exits\ together.\n
comment3.params=
comment3.target=void\ play()
comment3.text=\n\ Main\ play\ routine.\ Loops\ until\ end\ of\ p
lay.\n
comment4.params=
comment4.target=void\ printWelcome()
comment4.text=\n\ Print\ out\ the\ opening\ message\ for\ the\ player.\n
comment5.params=command
comment5.target=boolean\ processCommand(Command)
comment5.text=\n\ Given\ a\ command,\ process\ (that\ is\:\ execute)\ the\ command.\n\ \n\ @param\ command\ The\ command\ to\ be\ processed.\n\ @return\ true\ If\ the\ command\ ends\ the\ game,\ false\ otherwise.\n
comment6.params=
comment6.target=void\ printHelp()
comment6.text=\n\ Print\ out\ some\ help\ information.\ Here\ we\ print\ some\ stupid,\ cryptic\ message\n\ and\ a\ list\ of\ the\ command\ words.\n
comment7.params=room
comment7.target=void\ push(Room)
comment8.params=command
comment8.target=void\ goRoom(Command)
comment8.text=\n\ Try\ to\ in\ to\ one\ direction.\ If\ there\ is\ an\ exit,\ enter\ the\ new\ room,\n\ otherwise\ print\ an\ error\ message.\n
comment9.params=command
comment9.target=void\ goBack(Command)
numComments=14
__MACOSX/MuhammadAkbar-Final/._Game.ctxt
MuhammadAkbar-Final/Room.java
MuhammadAkbar-Final/Room.java
import java.util.Set;
import java.util.HashMap;
import java.util.ArrayList;
public class Room {
    private String description;
    private HashMap exits; // stores exits of this room.
    private ArrayList items;
    private boolean isExitRoom;
    /**
     * 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.
     * @paraam isExit true iff the room is an exit room
     */
    public Room(String description, boolean isExit)
    {
        this.description = description;
        exits = new HashMap<>();
        isExitRoom = isExit;
        items = new ArrayList();
    }

    // function to check if the room is an exit room
    public boolean isAnExitRoom(){
        return isExitRoom;
    }
    // ! function to add a item to the room
    public void addItem(Item item)
    {
        items.add(item);
    }
    // !function to remove item from the room
    public boolean removeItem(Item item)
    {
        if(!items.contains(item))
            return false;
        items.remove(item);
        return true;
    }
    // !let the user take the item from the room
    public Item getItem(String itemName)
    {
        for(int i = 0; i < items.size(); i++)
        {
            if(items.get(i).getName().equalsIgnoreCase(itemName))
                return items.get(i);
        }
        return null;
    }
    /**
     * 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 getItemString()
    {
        String result = "";
        for(int i = 0; i < items.size(); i++)
            result += items.get(i).getName() + ": " + items.get(i).getDescription() + "\n";
        return result;
    }

    public Item getItemFromName(String name)
    {
        for(int i = 0; i < items.size(); i++)
        {
            if(items.get(i).getName().equalsIgnoreCase(name))
                return items.get(i);
        }
        return null;
    }
    /**
     * Return a description of the room in the form: You are in the kitchen. Exits:
     * north west also if there is a item in the room it displays the name and the
     * description
     * 
     * @return A long description of this room
     */
    public String getLongDescription()
    {
        return description + "\n" + getExitString() + "\nItems:\n" + getItemString();
    }
    /**
     * 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);
    }
}
__MACOSX/MuhammadAkbar-Final/._Room.java
MuhammadAkbar-Final/CommandWords.ctxt
#BlueJ class context
comment0.target=CommandWords
comment1.params=
comment1.target=CommandWords()
comment1.text=\n\ Constructor\ -\ initialise\ the\ command\ words.\n
comment2.params=aString
comment2.target=boolean\ isCommand(java.lang.String)
comment2.text=\n\ Check\ whether\ a\ given\ String\ is\ a\ valid\ command\ word.\n\ \n\ @return\ true\ if\ it\ is,\ false\ if\ it\ isn't.\n
comment3.params=
comment3.target=void\ showAll()
comment3.text=\n\ Print\ all\ valid\ commands\ to\ System.out.\n
numComments=4
__MACOSX/MuhammadAkbar-Final/._CommandWords.ctxt
MuhammadAkbar-Final/Parser.java
MuhammadAkbar-Final/Parser.java
import java.util.Scanner;
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);
    }
    /**
     * @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();
    }
}
__MACOSX/MuhammadAkbar-Final/._Parser.java
MuhammadAkbar-Final/Game.class
public synchronized class Game {
private Parser parser;
private Room currentRoom;
private int top;
int score;
private Room[] roomStack;
java.util.ArrayList userItem;
public void Game();
private void createRooms();
public void play();
private void printWelcome();
private boolean processCommand(Command);
private void printHelp();
private void push(Room);
private void goRoom(Command);
private void goBack(Command);
public void dropItem(Command);
private boolean takeItem(Command);
void displayInventory();
private boolean quit(Command);
}
__MACOSX/MuhammadAkbar-Final/._Game.class
MuhammadAkbar-Final/CommandWords.class
public synchronized class CommandWords {
private static final String[] validCommands;
public void CommandWords();
public boolean isCommand(String);
public void showAll();
static void ();
}
__MACOSX/MuhammadAkbar-Final/._CommandWords.class
MuhammadAkbar-Final/Item.class
synchronized class Item {
String name;
String description;
void Item(String, String);
public String getName();
public String getDescription();
}
__MACOSX/MuhammadAkbar-Final/._Item.class
MuhammadAkbar-Final/Item.ctxt
#BlueJ class context
comment0.target=Item
comment1.params=name\ description
comment1.target=Item(java.lang.String,\...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here