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 »
How much is that ??can you check it?


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
Answered Same DayMay 20, 2021

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

Pritam answered on May 31 2021
151 Votes
HardCodedData.java
HardCodedData.java
package mazegame;
import mazegame.boundary.IMazeData;
import mazegame.entity.Exit;
import mazegame.entity.Location;
public class HardCodedData implements IMazeData {
    private Locat
ion startUp;

    public HardCodedData()
    {
        createLocations();
    }

    public Location getStartingLocation()
    {
        return startUp;
    }

    public String getWelcomeMessage()
    {
        return "Welcome to the Mount Helanous";
    }

    private void createLocations () 
    {
        startUp = new Location ("an office with paper strewn everywhere, how anyone can work here effectively is a mystery", "Julies Office");
        Location lounge = new Location ("an open space containing comfortable looking couches and artwork", "Airport Lounge");
        Location t127 = new Location ("a lecture theatre", "T127");
        Location gregsOffice = new Location ("a spinning vortex of terror", "Greg's Office");

        // Connect Locations
        startUp.addExit("south",  new Exit ("you see an open space to the south", lounge));
        lounge.addExit("north", new Exit("you see a mound of paper to the north", startUp));
        startUp.addExit("west", new Exit("you see a terrifying office to the west", gregsOffice));
        gregsOffice.addExit("east", new Exit("you see a mound of paper to the east", startUp));
        t127.addExit("south", new Exit("you see a mound of paper to the south", startUp));
        startUp.addExit("north", new Exit("you see a bleak place to the north", t127));
        lounge.addExit("northwest", new Exit("you see a terrifying office to the northwest", gregsOffice));
        gregsOffice.addExit("southeast", new Exit("you see an open space to the southeast", lounge));
 
    }
}
SimpleConsoleClient.java
SimpleConsoleClient.java
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 (Strin...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here