Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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 »
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 Extinguishe
4. Torch
5. Wate
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 cu
ent room is not null.
· Add an if statement to the printWelcome method to print “down”, if the downExit of the cu
ent 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 cu
ent room is not null.
· Add an if statement to the printWelcome method to print down if the downExit of the cu
ent 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 cu
ent 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 e
ors. 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 cu
ent 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 cu
ent room is not null.
· Add an if statement to the printWelcome method to add down to exit String if the downExit of the cu
ent 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 Akba
Student id XXXXXXXXXX
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\ cu
ently\ 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 XXXXXXXXXXr\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\ \ \ \ \ \ \ \ XXXXXXXXXXwas\ 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 XXXXXXXXXXr\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 a
ay 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 cu
entRoom;
java.util.A
ayList 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 XXXXXXXXXXr\n
comment1.params=
comment1.target=Game()
comment1.text=\r\n\ Create\ the\ game\ and\ initialise\ its\ internal\ map.\r\n
comment10.params=command
comment10
Answered Same Day Feb 17, 2021

Solution

Arun Shankar answered on Feb 17 2021
160 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\ play.\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\ e
or\ 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.A
ayList;
public class Room {
    private String description;
    private HashMap exits; 
 stores exits of this room.
    private A
ayList 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 A
ayList();
    }
    
    
 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 cu
entRoom;
private int top;
int score;
private Room[] roomStack;
java.util.A
ayList 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