Lab Week 7 - Implementing Move in The Mazegame
Lab Week 7 - Implementing Move in The Mazegame
import java.util.ArrayList;
public ParsedInput() {
setArguments(new ArrayList());
setCommand("");
}
import java.util.ArrayList;
import java.util.Arrays;
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.
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;
import java.util.Scanner;
import mazegame.boundary.IMazeClient;
import java.io.IOException;
import java.util.ArrayList;
import mazegame.SimpleConsoleClient;
import mazegame.boundary.IMazeClient;
import mazegame.boundary.IMazeData;
import mazegame.entity.Player;
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.
“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”
package mazegame.entity;
public Player() {
}
package mazegame.entity;
import java.util.HashMap;
public Location () { }
Run the game and type “move west” to verify the result. At this point we have implemented player movement but it is very
crude. You might like to think about how this might be improved from a usability perspective with some play testing (for
example creating a ToString method in Location including both the name of the Location and the Description and then
printing that to the user might be better than just the Location description alone). But this is left as a further optional
exercise for you.