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

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 »

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
. Open the file menu and click import . . .
c. From the dialog box
i. Open the “general” ta
ii. Click “Existing Projects into Workspace”
iii. Click next
d. Click “
owse” 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 parse
Before we can get started on implementing commands and adding further functionality to the Maze Game we need a way of
eaking up the user input. Commands have a format of command i.e.: move west
So what we need to do is
eak 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.A
ayList;
public class ParsedInput {
    
    private String command;
    private A
ayList arguments;
    public ParsedInput() {
    setArguments(new A
ayList());
    setCommand("");
    }
    public ParsedInput(String command, A
ayList arguments){
    this.setCommand(command);
    this.setArguments(arguments);
    }
    public String getCommand() {
         return command;
    }
    public void setCommand(String command) {
        this.command = command;
    }
    public A
ayList getArguments() {
        return arguments;
    }
    public void setArguments(A
ayList 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.A
ayList;
import java.util.A
ays;
public class Parser {
    private A
ayList dropWords;
private A
ayListvalidCommands;
public Parser(A
ayList validCommands){     
XXXXXXXXXXdropWords = new A
ayList(A
ays.asList("an","and","the","this", "to"));
XXXXXXXXXXthis.validCommands = validCommands;
}
public ParsedInput parse(String rawInput)
{
XXXXXXXXXXParsedInput parsedInput = new ParsedInput();
XXXXXXXXXXString lowercaseInput = rawInput.toLowerCase();
XXXXXXXXXXA
ayList stringTokens = new A
ayList(A
ays.asList(lowercaseInput.split(" ")));

XXXXXXXXXXfor (String token : stringTokens)
{
XXXXXXXXXXif (validCommands.contains(token))
{
XXXXXXXXXXparsedInput.setCommand(token);
}
XXXXXXXXXXelse if (!dropWords.contains(token))
XXXXXXXXXXparsedInput.getArguments().add(token);
}
XXXXXXXXXXreturn 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 cu
ently 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.A
ayList;
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 A
ayList commands;
    private Parser theParser;
    
     public DungeonMaster(IMazeData gameData, IMazeClient gameClient) {
XXXXXXXXXXthis.gameData = gameData;
XXXXXXXXXXthis.gameClient = gameClient;
XXXXXXXXXXthis.continueGame = true;
XXXXXXXXXXcommands = new A
ayList();
XXXXXXXXXXcommands.add("quit");
XXXXXXXXXXcommands.add("move");
XXXXXXXXXXtheParser = new Parser (commands);
}
public void printWelcome() {
XXXXXXXXXXgameClient.playerMessage(gameData.getWelcomeMessage());
}
public void setupPlayer() {
XXXXXXXXXXString playerName = gameClient.getReply("What name do you choose to be known by?");
XXXXXXXXXXthePlayer = new Player(playerName);
XXXXXXXXXXgameClient.playerMessage("Welcome " + playerName + "\n\n");
XXXXXXXXXXgameClient.playerMessage("You find yourself looking at ");
XXXXXXXXXXgameClient.playerMessage(gameData.getStartingLocation().getDescription());

gameClient.getReply("
Hit Enter to exit
");
}
public void runGame() {
XXXXXXXXXXprintWelcome();
XXXXXXXXXXsetupPlayer();
XXXXXXXXXXwhile (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 cu
entLocation;
    public Player() {
    }
    public Player(String name) {
     super (name);
    }
    public Location getCu
entLocation() {
        return cu
entLocation;
    }
    public void setCu
entLocation(Location cu
entLocation) {
        this.cu
entLocation = cu
entLocation;
    }
}
Now we amend the SetupPlayer method in DungeonMaster as follows:
     public void setupPlayer() {
XXXXXXXXXXString playerName = gameClient.getReply("What name do you choose to be known by?");
XXXXXXXXXXthePlayer = new Player(playerName);
XXXXXXXXXXthePlayer.setCu
entLocation(gameData.getStartingLocation());
XXXXXXXXXXgameClient.playerMessage("Welcome " + playerName + "\n\n");
XXXXXXXXXXgameClient.playerMessage("You find yourself looking at ");
XXXXXXXXXXgameClient.playerMessage(gameData.getStartingLocation().getDescription());

gameClient.getReply("
Hit Enter to exit
");
}
    
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 Day May 20, 2021

Solution

Pritam answered on May 31 2021
138 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 Location 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 te
or", "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 te
ifying 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 te
ifying 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