Lab week 7 – Implementing Move in the MazeGame Explore the codebase 1. Download the “Lab 7” code (this code was discussed in the lecture and has implemented the startup use case). 2. Unzip the code...

1 answer below »
gg


Lab week 7 – Implementing Move in the MazeGame Explore the codebase 1. Download the “Lab 7” code (this code was discussed in the lecture and has implemented the startup use case). 2. Unzip the code and open up the solution in Eclipse by performing the following steps. a. Open Eclipse b. Open the file menu and click import . . . c. From the dialog box i. Open the “general” tab ii. Click “Existing Projects into Workspace” iii. Click next d. Click “browse” and navigate that holds the lab 7 code i. The folder ABOVE the bin and src folders ii. Make sure the check box is clicked in the “projects” text box iii. Click “finish” e. The project should now appear in the “projects explorer” pane on the left of Eclipse 3. Spend some time exploring the code, change the startup location in the HardCodedData class and run the project to test the results. 4. Create an Enterprise Architect project called Lab 7 and reverse engineer the code to create class diagrams. Creating a command parser Before we can get started on implementing commands and adding further functionality to the Maze Game we need a way of breaking up the user input. Commands have a format of command i.e.: move west So what we need to do is break up our user input from a single string into a number of words, and work out which are commands and which are arguments. We can assume that the first word encountered in user input is the command, and what follows are the argument(s). We could even make our command parser a little more user friendly by dropping off commonly used words that are neither commands nor useful arguments. ie: if the user typed “go to the north” we could drop to and the. Let’s start by creating a class to represent our input after it has been parsed into command + arguments. Create a new class in the Control package called ParsedInput and enter the following: package mazegame.control; import java.util.ArrayList; public class ParsedInput { private String command; private ArrayList arguments; public ParsedInput() { setArguments(new ArrayList()); setCommand(""); } public ParsedInput(String command, ArrayList arguments){ this.setCommand(command); this.setArguments(arguments); } public String getCommand() { return command; } public void setCommand(String command) { this.command = command; } public ArrayList getArguments() { return arguments; } public void setArguments(ArrayList arguments) { this.arguments = arguments; } } Now create a new class called Parser in the same package with the following code: package mazegame.control; import java.util.ArrayList; import java.util.Arrays; public class Parser { private ArrayList dropWords; private ArrayListvalidCommands; public Parser(ArrayList validCommands){ dropWords = new ArrayList(Arrays.asList("an","and","the","this", "to")); this.validCommands = validCommands; } public ParsedInput parse(String rawInput) { ParsedInput parsedInput = new ParsedInput(); String lowercaseInput = rawInput.toLowerCase(); ArrayList stringTokens = new ArrayList(Arrays.asList(lowercaseInput.split(" "))); for (String token : stringTokens) { if (validCommands.contains(token)) { parsedInput.setCommand(token); } else if (!dropWords.contains(token)) parsedInput.getArguments().add(token); } return parsedInput; } } So we now have a class which can parse our user input, provided it is given a list of commands when it gets constructed. Incorporating Command handling in the DungeonMaster class Now that we have a parser we need to put it to work. But before we do that we need to make a small change to our user interface to accommodate retrieving a command from the player. Our IMazeClient interface currently has the following two methods declared: package mazegame.boundary; public interface IMazeClient { public String getReply (String question); public void playerMessage (String message); } We could probably use GetReply for the purpose of retrieving commands from a player, but the method is really designed to ask players a question. So let’s introduce a new method specifically to capture player commands. package mazegame.boundary; public interface IMazeClient { public String getReply (String question); public void playerMessage (String message); public String getCommand(); } We can now adjust our SimpleConsoleClient class accordingly: package mazegame; import java.util.Scanner; import mazegame.boundary.IMazeClient; public class SimpleConsoleClient implements IMazeClient { public String getReply (String question) { System.out.println("\n" + question + " "); Scanner in = new Scanner (System.in); return in.nextLine(); } public void playerMessage (String message) { System.out.print(message); } public String getCommand() { System.out.print ("\n\n>>>\t"); return new Scanner(System.in).nextLine(); } } Now that our client is setup to retrieve commands from the user we can modify DungeonMaster as follows: package mazegame.control; import java.io.IOException; import java.util.ArrayList; import mazegame.SimpleConsoleClient; import mazegame.boundary.IMazeClient; import mazegame.boundary.IMazeData; import mazegame.entity.Player; public class DungeonMaster { private IMazeClient gameClient; private IMazeData gameData; private Player thePlayer; private boolean continueGame; private ArrayList commands; private Parser theParser; public DungeonMaster(IMazeData gameData, IMazeClient gameClient) { this.gameData = gameData; this.gameClient = gameClient; this.continueGame = true; commands = new ArrayList(); commands.add("quit"); commands.add("move"); theParser = new Parser (commands); } public void printWelcome() { gameClient.playerMessage(gameData.getWelcomeMessage()); } public void setupPlayer() { String playerName = gameClient.getReply("What name do you choose to be known by?"); thePlayer = new Player(playerName); gameClient.playerMessage("Welcome " + playerName + "\n\n"); gameClient.playerMessage("You find yourself looking at "); gameClient.playerMessage(gameData.getStartingLocation().getDescription()); // gameClient.getReply(">"); } public void runGame() { printWelcome(); setupPlayer(); while (continueGame) { continueGame = processPlayerTurn(); } } public boolean processPlayerTurn() { ParsedInput userInput = theParser.parse(gameClient.getCommand()); if (commands.contains(userInput.getCommand())) { if (userInput.getCommand().equals("quit")) return false; if (userInput.getCommand().equals("move")) { gameClient.playerMessage("You entered the move command"); return true; } } gameClient.playerMessage("We don't recognise that command - try again!"); return true; } } Build your code and run it. You should be able to test it out as follows: So our game can now interpret commands, but other than quit, it doesn’t really do anything. Furthermore, our DungeonMaster class is starting to get very messy and may require refactoring. This will be dealt with next week when we discuss the Command design pattern. Implementing the Move Player command Our RAD describes a Move Party User Story: · “Move Party - players specify the direction in which they wish their party to move. If an available exit exists in the direction specified, the system updates the player's party location and returns a description of the new location” At this stage we have decided to leave parties out so we rewrite the user story as: · “Move Player - players specify the direction in which they wish to move. If an available exit exists in the direction specified, the system updates the player's location and returns a description of the new location” Let’s implement this user story by first changing the Player class as follows: package mazegame.entity; public class Player extends Character { private Location currentLocation; public Player() { } public Player(String name) { super (name); } public Location getCurrentLocation() { return currentLocation; } public void setCurrentLocation(Location currentLocation) { this.currentLocation = currentLocation; } } Now we amend the SetupPlayer method in DungeonMaster as follows: public void setupPlayer() { String playerName = gameClient.getReply("What name do you choose to be known by?"); thePlayer = new Player(playerName); thePlayer.setCurrentLocation(gameData.getStartingLocation()); gameClient.playerMessage("Welcome " + playerName + "\n\n"); gameClient.playerMessage("You find yourself looking at "); gameClient.playerMessage(gameData.getStartingLocation().getDescription()); // gameClient.getReply(">"); } Next we need to change the Location class so we can retrieve an Exit. package mazegame.entity; import java.util.HashMap; public class Location { private HashMap exits; private String description; private String label; public Location () { } public Location (String description, String label) { this.setDescription(description); this.setLabel(label); exits = new HashMap(); } public boolean addExit (String exitLabel, Exit theExit) { if (exits.containsKey(exitLabel)) return false; exits.put(exitLabel, theExit); return true; } public Exit getExit(String exitLabel)
Answered Same DayOct 20, 2021ITECH7201Federation University Australia

Answer To: Lab week 7 – Implementing Move in the MazeGame Explore the codebase 1. Download the “Lab 7” code...

Valupadasu answered on Oct 24 2021
134 Votes
LAB7/.classpath

    
    
    
LAB7/.idea/.gitignore
# Default ignored files
/shelf/
/workspace.xml
LAB7/.idea/aws.xml















LAB7/.idea/misc.xml




LAB7/.idea/modules.xml






LAB7/.idea/workspace.xml














































































1568819643966


1568819643966






































LAB7/.project

     Lab7-1-StartupSequence-StartupCodeLab7
    
    
    
    
        
             org.eclipse.jdt.core.javabuilder
            
            
        
    
    
         org.eclipse.jdt.core.javanature
    
LAB7/bin/mazegame/boundary/IMazeClient.class
package mazegame.boundary;
public abstract interface IMazeClient {
public abstract String getReply(String);
public abstract void playerMessage(String);
public abstract String getCommand();
}
LAB7/bin/mazegame/boundary/IMazeData.class
package mazegame.boundary;
public abstract interface IMazeData {
public abstract mazegame.entity.Location getStartingLocation();
public abstract String getWelcomeMessage();
}

LAB7/bin/mazegame/control/CommandHandler.class
package mazegame.control;
public synchronized class CommandHandler {
private commandState.CommandState availableCommands;
public void CommandHandler();
public CommandResponse processTurn(String, mazegame.entity.Player);
private ParsedInput parse(String);
}
LAB7/bin/mazegame/control/CommandResponse.class
package mazegame.control;
public synchronized class CommandResponse {
private Boolean finishedGame;
private String message;
public void CommandResponse(String);
public void CommandResponse(String, Boolean);
public void setFinishedGame(Boolean);
public boolean isFinishedGame();
public String getMessage();
public void setMessage(String);
}
LAB7/bin/mazegame/control/commands/AttackCommand.class
package mazegame.control.commands;
public synchronized class AttackCommand implements Command {
public void AttackCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/BuyCommand.class
package mazegame.control.commands;
public synchronized class BuyCommand implements Command {
public void BuyCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/Command.class
package mazegame.control.commands;
public abstract interface Command {
public abstract mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/DropCommand.class
package mazegame.control.commands;
public synchronized class DropCommand implements Command {
public void DropCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/EquipCommand.class
package mazegame.control.commands;
public synchronized class EquipCommand implements Command {
public void EquipCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/GetCommand.class
package mazegame.control.commands;
public synchronized class GetCommand implements Command {
public void GetCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/InvCommand.class
package mazegame.control.commands;
public synchronized class InvCommand implements Command {
public void InvCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/LookCommand.class
package mazegame.control.commands;
public synchronized class LookCommand implements Command {
private mazegame.control.CommandResponse response;
public void LookCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/MoveCommand.class
package mazegame.control.commands;
public synchronized class MoveCommand implements Command {
public void MoveCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/QuitCommand.class
package mazegame.control.commands;
public synchronized class QuitCommand implements Command {
public void QuitCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/SellCommand.class
package mazegame.control.commands;
public synchronized class SellCommand implements Command {
public void SellCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/UnequipCommand.class
package mazegame.control.commands;
public synchronized class UnequipCommand implements Command {
public void UnequipCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commands/UnlockCommand.class
package mazegame.control.commands;
public synchronized class UnlockCommand implements Command {
public void UnlockCommand();
public mazegame.control.CommandResponse execute(mazegame.control.ParsedInput, mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commandState/AttackState.class
package mazegame.control.commandState;
public synchronized class AttackState extends CommerceState {
public void AttackState();
public CommandState update(mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commandState/CommandState.class
package mazegame.control.commandState;
public abstract synchronized class CommandState {
private java.util.HashMap availableCommands;
public abstract CommandState update(mazegame.entity.Player);
public void CommandState();
public java.util.HashMap getAvailableCommands();
public void setAvailableCommands(java.util.HashMap);
public mazegame.control.commands.Command getCommand(String);
public java.util.ArrayList getLabels();
}
LAB7/bin/mazegame/control/commandState/CommerceState.class
package mazegame.control.commandState;
public synchronized class CommerceState extends CommandState {
public void CommerceState();
public CommandState update(mazegame.entity.Player);
}
LAB7/bin/mazegame/control/commandState/MovementState.class
package mazegame.control.commandState;
public synchronized class MovementState extends CommandState {
public void MovementState();
public CommandState update(mazegame.entity.Player);
}
LAB7/bin/mazegame/control/DungeonMaster.class
package mazegame.control;
public synchronized class DungeonMaster {
private mazegame.boundary.IMazeClient gameClient;
private mazegame.boundary.IMazeData gameData;
private mazegame.entity.Player thePlayer;
private CommandHandler playerTurnHandler;
public void DungeonMaster(mazegame.boundary.IMazeData, mazegame.boundary.IMazeClient);
public void printWelcome();
public void setupPlayer();
public void runGame();
public boolean handlePlayerTurn();
}
LAB7/bin/mazegame/control/MazeGame.class
package mazegame.control;
public synchronized class MazeGame {
public void MazeGame();
public static void main(String[]);
}
LAB7/bin/mazegame/control/ParsedInput.class
package mazegame.control;
public synchronized class ParsedInput {
private String command;
private java.util.ArrayList arguments;
public void ParsedInput();
public void ParsedInput(String, java.util.ArrayList);
public String getCommand();
public void setCommand(String);
public java.util.ArrayList getArguments();
public void setArguments(java.util.ArrayList);
}
LAB7/bin/mazegame/control/Parser.class
package mazegame.control;
public synchronized class Parser {
private java.util.ArrayList dropWords;
private java.util.ArrayList validCommands;
public void Parser(java.util.ArrayList);
public ParsedInput parse(String);
}
LAB7/bin/mazegame/entity/Armor.class
package mazegame.entity;
public synchronized class Armor extends Item {
private int bonus;
public void Armor(String, int, double, String);
public int getBonus();
public void setBonus(int);
}
LAB7/bin/mazegame/entity/Blacksmith.class
package mazegame.entity;
public synchronized class Blacksmith extends Location {
public void Blacksmith();
public void Blacksmith(String, String);
}
LAB7/bin/mazegame/entity/Character.class
package mazegame.entity;
public synchronized class Character {
private int agility;
private int lifePoints;
private String name;
private int strength;
public Dice m_Dice;
public Weapon m_Weapon;
public void Character();
public void Character(String);
public int getAgility();
public void setAgility(int);
public int getLifePoints();
public void setLifePoints(int);
public String getName();
private void setName(String);
public int getStrength();
public void setStrength(int);
}
LAB7/bin/mazegame/entity/Collectible.class
package mazegame.entity;
public synchronized class Collectible extends Item {
private int restoreLifepoint;
public void Collectible(String, int);
public int getRestoreLifepoint();
}
LAB7/bin/mazegame/entity/Dice.class
package mazegame.entity;
public synchronized class Dice {
private int faces;
private static java.util.Random generator;
public void Dice(int);
public int roll();
static void ();
}
LAB7/bin/mazegame/entity/Exit.class
package mazegame.entity;
public synchronized class Exit {
private String description;
private Location destination;
private boolean locked;
public void Exit(String, Location);
public void Exit(String, Location, boolean);
public boolean isLocked();
public void setLocked(boolean);
public Location getDestination();
public void setDestination(Location);
public String getDescription();
public void setDescription(String);
}
LAB7/bin/mazegame/entity/Inventory.class
package mazegame.entity;
public synchronized class Inventory {
private Money gold;
private java.util.HashMap itemList;
public void Inventory();
public void addMoney(double);
public boolean removeMoney(int);
public Money getGold();
public boolean addItem(Item);
public Item removeItem(String);
public Item getItem(String);
public String printItemList();
public String toString();
public java.util.HashMap getItemList();
public Item findItem(String);
public boolean containsItem(String);
}
LAB7/bin/mazegame/entity/Item.class
package mazegame.entity;
public abstract synchronized class Item {
private String label;
private int price;
private double weight;
private String description;
public void Item(String);
public void Item(String, int, double, String);
public String getLabel();
public int getPrice();
public double getWeight();
public String getDescription();
public String toString();
}
LAB7/bin/mazegame/entity/Location.class
package mazegame.entity;
public synchronized class Location {
private java.util.HashMap exits;
private String description;
private String label;
private java.util.HashMap nonPlayerCharacters;
private java.util.HashMap weapons;
private java.util.HashMap inventory;
public void Location();
public void Location(String, String);
public boolean addNonPlayerCharacters(String, NonPlayerCharacter);
public boolean addWeapons(String, Item);
public boolean addExit(String, Exit);
public boolean addItems(String, Item);
public Exit getExit(String);
public NonPlayerCharacter getNonPlayerCharacters(String);
public Item getItem(String);
public void removeItem(String);
public String getDescription();
public void setDescription(String);
public String getLabel();
public void setLabel(String);
public String availableExits();
public String availableCharacters();
public java.util.HashMap getNonPlayerCharacters();
public String availableItems();
public boolean containsExit(String);
public boolean containsCharacter(String);
public boolean containsWeapon(String);
public String toString();
}
LAB7/bin/mazegame/entity/Money.class
package mazegame.entity;
public synchronized class Money {
private double total;
public void Money();
public void Money(double);
public void Add(double);
public boolean Subtract(double);
public double getWeight();
public String toString();
public double getTotal();
}
LAB7/bin/mazegame/entity/NonPlayerCharacter.class
package mazegame.entity;
public synchronized class NonPlayerCharacter extends Character {
private Boolean hostile;
private String conversation;
private Inventory inventory;
private java.util.ArrayList equipped;
public void NonPlayerCharacter();
public void NonPlayerCharacter(String, int, int, int, Boolean);
public String getName();
public boolean getHostile();
public void setHostile(boolean);
public int getStrength();
public int getAgility();
public int getLifePoints();
public String toString();
public String getConversation();
public void setConversation(String);
public void equipWeapon(Weapon);
}
LAB7/bin/mazegame/entity/Player.class
package mazegame.entity;
public synchronized class Player extends Character {
private Location currentLocation;
private Location lastLocation;
private static Player instance;
private int weightLimit;
private Inventory playerInventory;
private mazegame.utility.WeightLimit weightModifier;
private java.util.ArrayList equipped;
public Location getLastLocation();
public void setLastLocation(Location);
private void Player();
private void Player(String, int, int, int);
public static Player getInstance(String, int, int, int);
public int getWeightLimit();
public void setWeightLimit(int);
public Location getCurrentLocation();
public void setCurrentLocation(Location);
public Inventory getPlayerInventory();
public int getStrength();
public void setStrength(int);
public int getLifePoints();
public int getAgility();
public String equipWeapon(Weapon);
public java.util.ArrayList getEquipped();
public void setEquipped(java.util.ArrayList);
static void ();
}
LAB7/bin/mazegame/entity/Weapon.class
package mazegame.entity;
public synchronized class Weapon extends Item {
public Dice m_Dice;
public void Weapon(String, int, double, String);
public String getLabel();
public String getDescription();
public double getWeight();
}
LAB7/bin/mazegame/HardCodedData.class
package mazegame;
public synchronized class HardCodedData implements boundary.IMazeData {
private entity.Location startUp;
private utility.WeightLimit weightLimit;
private utility.WeaponTable weapons;
public void HardCodedData();
public entity.Location getStartingLocation();
public String getWelcomeMessage();
private void createLocations();
private void populateStrengthTable();
private void populateAgilityTable();
private void populateWeightLimitTable();
public void populateWeaponsTable();
}
LAB7/bin/mazegame/SimpleConsoleClient.class
package mazegame;
public synchronized class SimpleConsoleClient implements boundary.IMazeClient {
public void SimpleConsoleClient();
public String getReply(String);
public void playerMessage(String);
public String getCommand();
}
LAB7/bin/mazegame/utility/AgilityTable.class
package mazegame.utility;
public synchronized class AgilityTable {
private static AgilityTable instance;
private java.util.HashMap lookup;
private void AgilityTable(java.util.HashMap);
public static AgilityTable getInstance();
public void setModifier(int, int);
public int getModifier(int);
}
LAB7/bin/mazegame/utility/DiceRoller.class
package mazegame.utility;
public synchronized class DiceRoller {
private static DiceRoller instance;
private mazegame.entity.Dice d6;
private void DiceRoller();
public static DiceRoller getInstance();
public int generateAbilityScore();
static void ();
}
LAB7/bin/mazegame/utility/NonPlayerCharacterCollection.class
package mazegame.utility;
public synchronized class NonPlayerCharacterCollection extends java.util.HashMap {
public void NonPlayerCharacterCollection();
public String toString();
}
LAB7/bin/mazegame/utility/StrengthTable.class
package mazegame.utility;
public synchronized class StrengthTable {
private static StrengthTable instance;
private java.util.HashMap lookup;
private void StrengthTable(java.util.HashMap);
public static StrengthTable getInstance();
public void setModifier(int, int);
public int getModifier(int);
}
LAB7/bin/mazegame/utility/WeaponTable.class
package mazegame.utility;
public synchronized class WeaponTable {
private static WeaponTable instance;
private java.util.HashMap lookup;
private void WeaponTable(java.util.HashMap);
public static WeaponTable getInstance();
public void setWeapon(String, mazegame.entity.Item);
public Object getWeapon(String);
}
LAB7/bin/mazegame/utility/WeightLimit.class
package mazegame.utility;
public synchronized class WeightLimit {
private static WeightLimit instance;
private java.util.HashMap lookup;
private void WeightLimit(java.util.HashMap);
public static WeightLimit getInstance();
public void setModifier(int, int);
public int getModifier(int);
}
LAB7/Lab7.iml









...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here