Вы находитесь на странице: 1из 29

Maze Game

ITECH7201 Software Engineering: Analysis and Design Assignment 2

Prepared by
Yang Li (30319191)

Date: 3/06/2017
ITECH7201 Software Engineering: Analysis and Design Assignment 2

Table of Contents
1 Introduction 3
2. Statement of contribution 3
3. Group part – Maze Game map 4
4. Group part – Environment introduce 4
5. Group part – Move/Look command 4
5.1 Move command ............................................................................................................................... 4

5.2 Look command ................................................................................................................................ 5

6. Group part – List/Get/Drop item command 6


6.1 List command .................................................................................................................................. 6

6.2 Get command ................................................................................................................................... 6

6.3 Drop command ................................................................................................................................ 7

7. Group part – Buy/Sell item command 8


7.1 Buy command .................................................................................................................................. 8

7.2 Sell command .................................................................................................................................. 9

8. Group part – Attack/Potion command 10


8.1 Attack command ............................................................................................................................ 10

8.2 Potion command ........................................................................................................................... 11

9. Group part – Unit tests 12


10. Individual part – User stories 17
11. Individual part – Class diagrams for Lab 7 & 8 18
11.1 Lab 7 Class Diagram ................................................................................................................... 18

11.2 Lab 8 Class Diagram ................................................................................................................... 22

12. Individual part – Sequence diagrams 26


12.1 Sequence Diagram for “?” command ....................................................................................... 26

12.2 Sequence Diagram for “?” command ....................................................................................... 27

13. Individual part – Punchtime report 28


References 29

2
ITECH7201 Software Engineering: Analysis and Design Assignment 2

1 Introduction

This is the report for assignment 2 of ITECH 7201 Software Engineering:


Analysis and Design. The task of the assignment is studying and modifying given
java code of Maze Game, so that to develop more functions.
There are two parts in this report: group part and individual part.
In group part, the customized map will be shown, game environment and
commands will also be introduced.
In individual part, each member will give its own version of:
 User stories of deliverables
 Class diagrams for Lab 7 and Lab 8
 Sequence diagrams for two of the item management commands from
different groups
 Punchtime report

2. Statement of contribution

Lab 7, 8 & 10 by: Haiyang Ma


Game map design & draw by: Haiyang Ma
Move & Look commands by: Yang Li
List/Get/Drop item commands by: Haiyang Ma
Buy/Sell items commands by: Yang Li
Attack & Potion commands by: Haiyang Ma
Unit tests by: Yang Li
Presentation file by: Yang Li

3
ITECH7201 Software Engineering: Analysis and Design Assignment 2

3. Group part – Maze Game map


The following picture is the maze game map. The original picture can be found
from the attached file package.

Maze Game Map

4. Group part – Environment introduce


The game is running in a Java console. User needs to input words as commands
to execute functions. User can “move” to different locations, “look” items and NPC
located at current location, “get item” from or “drop item” to locations, “list” items
on hand, “buy item” from or “sell item” to a shop, “attack NPC” if there is a hostile
NPC stays at the location, use “potion” to restore life points.
There are 7 locations in this game (as shown above), 5 normal locations, including
1 startup location, 2 special locations which are shops. In the normal locations,
there are gold pieces; in the shop locations, nothing is placed.
When the game starts, the player is standing at the startup location. Initial status
of the play is: strength (10), agility (10), life point (30), gold (20), 3 potions.

5. Group part – Move/Look command

5.1 Move command


The format of move command is: move/go [direction]
4
ITECH7201 Software Engineering: Analysis and Design Assignment 2

When the command is entered, the play moves from current location to another
one, and everything at the new location will be displayed. If there is no exit in the
direction which entered by the user, an error message will be displayed and the
player still stay at current location.
Source code:
package mazegame.control;

import mazegame.entity.Exit;
import mazegame.entity.Player;

public class MoveCommand implements Command {

public CommandResponse execute (ParsedInput userInput, Player thePlayer) {


if (userInput.getArguments().size() == 0) {
return new CommandResponse ("If you want to move you need to tell me where.");
}
String exitLabel = (String) userInput.getArguments().get(0);
Exit desiredExit =
thePlayer.getCurrentLocation().getExitCollection().getExit(exitLabel);
if (desiredExit == null) {
return new CommandResponse("There is no exit there . . . try moving somewhere
else!");
}
thePlayer.setCurrentLocation(desiredExit.getDestination());
return new CommandResponse("You successfully move " + exitLabel + " and find
yourself looking at " + thePlayer.getCurrentLocation().getDescription() + "\n\n" +
thePlayer.getCurrentLocation().toString());
}
}

5.2 Look command


The format of look command is: look or look [direction]
“look” means look current location, “look [direction]” means look specific location
that was entered.
When the look command is entered, everything at the location will be displayed,
but the play will stay at the same place.
Source code:
package mazegame.control;

import mazegame.entity.Exit;
import mazegame.entity.Player;

public class LookCommand implements Command {

private CommandResponse response;

public CommandResponse execute(ParsedInput userInput, Player thePlayer) {


response = new CommandResponse("Can't find that to look at here!");
if(userInput.getArguments().size() == 0) {
response.setMessage(thePlayer.getCurrentLocation().toString());
return response;
}
for (Object argument: userInput.getArguments()) {
if
(thePlayer.getCurrentLocation().getExitCollection().containsExit(argument.toString())) {
Exit theExit =

5
ITECH7201 Software Engineering: Analysis and Design Assignment 2

thePlayer.getCurrentLocation().getExitCollection().getExit((String)argument);
return new CommandResponse(theExit.getDescription());
}
}
return response;
}
}

6. Group part – List/Get/Drop item command

6.1 List command


The format of list command is: list
When the list command is entered, everything which is held by the player will be
displayed.
Source code:
package mazegame.control;

import mazegame.entity.Player;

/**
* Created by sola2 on 30/05/2017.
*/
public class ListCommand implements Command {

@Override
public CommandResponse execute(ParsedInput userInput, Player thePlayer) {
return new CommandResponse(thePlayer.getInventory().toString());
}
}

6.2 Get command


The format of get command is: get [item]/gold
When get [item] command is entered, system checks whether the item entered
existing at the location. If exists, the item will be added to the player’s inventory,
and removed from the location. When the [item] is gold, the gold which is placed
at the location will be added to the player’s inventory and removed from the location.
Source code:
package mazegame.control;

import mazegame.entity.Item;
import mazegame.entity.Player;

/**
* Created by sola2 on 31/05/2017.
*/
public class GetCommand implements Command {

@Override
public CommandResponse execute(ParsedInput userInput, Player thePlayer) {

String itemLabel = userInput.getFirstArg();


6
ITECH7201 Software Engineering: Analysis and Design Assignment 2

if (userInput.getArguments().size() == 0){
return new CommandResponse("What do you want to get?");
}

//get gold command


int gp = thePlayer.getCurrentLocation().getInventory().getGold();
if (itemLabel.equals("gold")){
if (gp == 0){
return new CommandResponse("There is no gold here...");
}
else{
thePlayer.getInventory().addMoney(gp);
thePlayer.getCurrentLocation().getInventory().removeMoney(gp);
return new CommandResponse("You got " + gp + " pieces of gold!");
}
}

//get items command


//find if there's the item which inputted by the user
Item itemOnLocation =
thePlayer.getCurrentLocation().getInventory().findItem(itemLabel);

if (itemOnLocation == null){
return new CommandResponse("There is no " + itemLabel + " at this place...");
}

//successfully added
boolean successAdd = thePlayer.getInventory().addItem(itemOnLocation);
if (successAdd){

thePlayer.getCurrentLocation().getInventory().removeItem(itemLabel);
return new CommandResponse("You picked up a " + itemLabel);
}

//failed to add
return new CommandResponse("It's too heavy!");
}
}

6.3 Drop command


The format of drop command is: drop [item]
When drop [item] command is entered, system checks whether the item entered
existing in player’s inventory. If exists, the item will be added to the location, and
removed from the player’s inventory.
Source code:
package mazegame.control;

import mazegame.entity.Item;
import mazegame.entity.Player;

/**
* Created by sola2 on 31/05/2017.
*/
public class DropCommand implements Command {

@Override

7
ITECH7201 Software Engineering: Analysis and Design Assignment 2

public CommandResponse execute(ParsedInput userInput, Player thePlayer) {

if (userInput.getArguments().size() == 0){
return new CommandResponse("What do you want to drop?");
}

//get user's input of the item


String itemLabel = userInput.getFirstArg();

//find if there's the item which inputted by the user


Item itemInInventory = thePlayer.getInventory().findItem(itemLabel);

if (itemInInventory == null){
return new CommandResponse("There is no " + itemLabel + " in your
inventory...");
}

//successfully dropped
boolean successDrop =
thePlayer.getCurrentLocation().getInventory().addItem(itemInInventory);

if(successDrop) {
thePlayer.getInventory().removeItem(itemLabel);
return new CommandResponse(itemLabel + " is dropped on the location");
}
return new CommandResponse(itemLabel + " not found..");

7. Group part – Buy/Sell item command

7.1 Buy command


The format of buy command is: buy [item]
Similar to get command, when buy [item] is entered, system checks whether the
item entered existing at the shop, and if the player’s money is sufficient. Then the
item will be added to the player’s inventory, and the money will also be subtracted
from the player’s inventory. However, the item will not be removed from the location
because the shop has infinite stocks.
Source code:
package mazegame.control;

import mazegame.entity.Item;
import mazegame.entity.Player;

public class BuyCommand implements Command {


public CommandResponse execute (ParsedInput userInput, Player thePlayer) {

String itemLabel = userInput.getFirstArg();

if (userInput.getArguments().size() == 0){
return new CommandResponse("What do you want to buy?");
}

//no such item


8
ITECH7201 Software Engineering: Analysis and Design Assignment 2

Item itemToBuy = thePlayer.getCurrentLocation().getInventory().findItem(itemLabel);


if (itemToBuy == null)
return new CommandResponse("There's no " + itemLabel + " in this shop");

//enough money
if (thePlayer.getInventory().getGold() >= itemToBuy.getValue())
{
boolean successBuy = thePlayer.getInventory().addItem(itemToBuy);

if (successBuy)
{
thePlayer.getInventory().removeMoney(itemToBuy.getValue());
return new CommandResponse("You bought " + itemLabel);
}
else
return new CommandResponse("It's too heavy!");

}
return new CommandResponse("Not enough money!");

}
}

7.2 Sell command


The format of sell command is: sell [item]
When sell [item] is entered, system checks the existence of the item. Then the item
will be removed from the player’s inventory, but not be added to the location. Also,
the money of the item’s value will be added to the player’s inventory.
Source code:
package mazegame.control;

import mazegame.entity.Item;
import mazegame.entity.Player;

public class SellCommand implements Command {


public CommandResponse execute (ParsedInput userInput, Player thePlayer) {

//Similar to buy command

if (userInput.getArguments().size() == 0)
return new CommandResponse("What do you want to sell?");

String itemLabel = userInput.getFirstArg();

Item itemToSell = thePlayer.getInventory().findItem(itemLabel);

if (itemToSell == null) {
return new CommandResponse("You do not have a " + itemLabel);
} else {
thePlayer.getInventory().removeItem(itemLabel);
thePlayer.getInventory().addMoney(itemToSell.getValue());
return new CommandResponse("You sold " + itemLabel + " and get " +
itemToSell.getValue() + " pieces of gold!");
}
}
}

9
ITECH7201 Software Engineering: Analysis and Design Assignment 2

8. Group part – Attack/Potion command

8.1 Attack command


The format of attack command is: attack [NPC name]
When attack [NPC name] is entered, system checks whether there is the NPC
exists at the location, and whether the NPC is a hostile. If the NPC is a hostile, the
NPC will lose life points, the number is the same to the player’s strength (10 in our
case). The NPC will fight back immediately, the player also loses life points, the
number is the same to the NPC’s strength (12 in our case). All the result will be
displayed, and wait for user’s next command.
Source code:
package mazegame.control;

import mazegame.entity.Player;

public class AttackCommand implements Command {


public CommandResponse execute (ParsedInput userInput, Player thePlayer) {

if (userInput.getArguments().size() == 0){
return new CommandResponse("Who do you want to attack?");
}

//check if there is an NPC


if (thePlayer.getCurrentLocation().getNpc() == null) {
return new CommandResponse("Nobody is here!");
}

//check if the NPC is a hostile


else if(thePlayer.getCurrentLocation().getNpc().getHostile() == false){
return new CommandResponse(thePlayer.getCurrentLocation().getNpc().getName() +
" is not a hostile!");
}

else
{
System.out.println("You attacked " +
thePlayer.getCurrentLocation().getNpc().getName() + " for "
+ thePlayer.getStrength() + " life points!");

//NPC lost life points

thePlayer.getCurrentLocation().getNpc().setLifePoints(thePlayer.getCurrentLocation().getNpc
().getLifePoints() -
thePlayer.getStrength());

//NPC is dead
if (thePlayer.getCurrentLocation().getNpc().getLifePoints() <= 0){
System.out.println(thePlayer.getCurrentLocation().getNpc().getName() + " is
dead...");
thePlayer.getCurrentLocation().removeNpc();
return new CommandResponse("");
}

//NPC is still alive


else{
//NPC fight back
System.out.println(thePlayer.getCurrentLocation().getNpc().getName() + "
attacked you for "
+ thePlayer.getCurrentLocation().getNpc().getStrength() + " life
10
ITECH7201 Software Engineering: Analysis and Design Assignment 2

points!");

thePlayer.setLifePoints(thePlayer.getLifePoints() -
thePlayer.getCurrentLocation().getNpc().getStrength());

System.out.println("Your current life point: " +


thePlayer.getLifePoints());

System.out.println(thePlayer.getCurrentLocation().getNpc().getName() + "'s
current life point: " +
thePlayer.getCurrentLocation().getNpc().getLifePoints());

//player died
if (thePlayer.getLifePoints() <= 0){
return new CommandResponse("You died!",true);
}
return new CommandResponse("");
}
}
// return new CommandResponse ("You entered the attack command");
}
}

8.2 Potion command


The format of potion command is: potion
When potion is entered, system checks whether the player still has potions on hand.
If the player has potions, 1 potion will be used and 10 life points will be restored to
the player.
Source code:
package mazegame.control;

import mazegame.entity.Player;

/**
* Created by sola2 on 1/06/2017.
*/
public class PotionCommand implements Command {
@Override
public CommandResponse execute(ParsedInput userInput, Player thePlayer) {

if(thePlayer.getPotions() == 0){
return new CommandResponse("You do not have any more potions!");
}
else{
thePlayer.setLifePoints(thePlayer.getLifePoints() + 10);
thePlayer.usePotions();
System.out.println("You use a potion to restore 10 life points!");
return new CommandResponse("Your current life point: " +
thePlayer.getLifePoints());
}

}
}

11
ITECH7201 Software Engineering: Analysis and Design Assignment 2

9. Group part – Unit tests


Unit testing is for special area or unit that tests certain functions and codes. This allows us to
validate our functional work, as expected, and it avoids the problem of testing in traditional
tests where the entire code takes extra time and effort to rewrite the problem. In this
assingment, we have also applied unit tests.

Test section:
Test with Junit4 based on the Junit framework.

Test item:
lookCommand class and attackCommand class in control package.

Operation process:
In the same package, selected the chosen class, right click and choose new test class, input
test condition preset code in the setup method of @before identification, such as set the
location, player, command. test methods corresponding to the class under test method in the
input identification method of @test, finally,through running those test classes, in test
interface we can see the relevant test result.

Test code:
LookCommand:

package mazegame.control;

import static org.junit.Assert.*;


import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import junit.framework.Assert;
import mazegame.control.*;
import mazegame.entity.*;

public class LookCommandTest {

private ParsedInput playerInput;


private Player thePlayer;
private CommandHandler handler;
private LookCommand lookcommand;
private Exit northExit;
private Location location2;
private ArrayList argument1=new ArrayList();

@Before
public void setUp() throws Exception {
playerInput = new ParsedInput("look", argument1);
thePlayer = new Player("tester");
location2 = new Location("a lecture room", "Room202");
12
ITECH7201 Software Engineering: Analysis and Design Assignment 2

Location testarea = new Location("this place is used for test", "testarea");


northExit = new Exit("you see a mound of paper to the south", testarea);
location2.getExitCollection().addExit("north", northExit);
thePlayer.setCurrentLocation(location2);
handler = new CommandHandler();
lookcommand=new LookCommand();

@Test
public void testlookifNoA() {

assertSame(location2, thePlayer.getCurrentLocation());
CommandResponse response = lookcommand.execute(playerInput, thePlayer);
assertFalse(response.isFinishedGame());
assertTrue(response.getMessage().contains(location2.getDescription()));

@Test
public void testlookifcannotfind(){

argument1.add("randomletterfortest");
playerInput.setArguments(argument1);
CommandResponse response = lookcommand.execute(playerInput, thePlayer);
assertFalse(response.isFinishedGame());
assertTrue(response.getMessage().contains("Can't find that to look at here!"));

}
@Test
public void testlookiflookatNorth()
{
argument1.add("north");
playerInput.setArguments(argument1);
CommandResponse response = lookcommand.execute(playerInput, thePlayer);
assertFalse(response.isFinishedGame());
assertTrue(response.getMessage().contains(northExit.getDescription()));
}
@Test
public void TestLookHandler()
{

CommandResponse response = handler.processTurn("look to the north", thePlayer);


assertFalse(response.isFinishedGame());
assertTrue(response.getMessage().contains(northExit.getDescription()));
}

AttackCommand:
package mazegame.control;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import junit.framework.Assert;
import mazegame.control.*;
import mazegame.entity.*;
public class AttackCommandTest {
private ParsedInput playerInput;
13
ITECH7201 Software Engineering: Analysis and Design Assignment 2

private Player thePlayer;


private CommandHandler handler;
private AttackCommand attack;
private Exit northExit;
private NonPlayerCharacter npc;
private ArrayList argument1=new ArrayList();
private Location location2;

@Before
public void setUp() throws Exception {
playerInput = new ParsedInput("attack", argument1);
thePlayer = new Player("tester");
location2 = new Location("a lecture room", "Room202");
Location testarea = new Location("this place is used for test", "testarea");
northExit = new Exit("you see a mound of paper to the south", testarea);
location2.getExitCollection().addExit("north", northExit);
thePlayer.setCurrentLocation(location2);
handler = new CommandHandler();
attack =new AttackCommand();
}

@Test
public void testifNoTarget() {

CommandResponse response = attack.execute(playerInput, thePlayer);


assertFalse(response.isFinishedGame());
assertTrue(response.getMessage().contains("Who do you want to attack?"));
}
@Test
public void testifNobody(){

location2.setupNpc(null, 0);
CommandResponse response = attack.execute(playerInput, thePlayer);
assertFalse(response.isFinishedGame());
assertTrue(response.getMessage().contains("Nobody is here!"));
}
@Test
public void testifnohostile(){

location2.setupNpc("Iamnotenemy",20);
argument1.add("Iamnotenemy");
CommandResponse response = attack.execute(playerInput, thePlayer);
assertFalse(response.isFinishedGame());

assertTrue(response.getMessage().contains((thePlayer.getCurrentLocation().getNpc().getName(
) + " is not a hostile!")));

}
@Test
public void testifhostile(){

location2.setupNpc("PleaseHitme",20,"hostile");
argument1.add("PleaseHitme");
CommandResponse response = attack.execute(playerInput, thePlayer);

assertTrue(response.getMessage().contains(("You attacked " +


thePlayer.getCurrentLocation().getNpc().getName() + " for "
+ thePlayer.getStrength() + " life points!")));

}
@Test
public void testifplayerdied(){

14
ITECH7201 Software Engineering: Analysis and Design Assignment 2

location2.setupNpc("PleaseHitme",100,"hostile");
argument1.add("PleaseHitme");
CommandResponse response = attack.execute(playerInput, thePlayer);

assertTrue(response.getMessage().contains("You died!"));
}
}

Test result:

lookCommand:

picture : test result of LookCommand

According to the picture ,4 test methods have been run,there is no error in the test
methods, and all the method passed.It means lookMethod Class is no issues.

Attackmethod:

picture : test result of AttackCommand

According to the picture ,5 test methods have been run,there is no error in the test
methods, 3 test methods passed and 2 test methods failed. The failed method is testifhostile
and testifNobody. The type of failed is assrtionError, this may be due to the fact that the
statement of conditional judgment is not valid and that the problem is caused by nonstandard
output.

15
ITECH7201 Software Engineering: Analysis and Design Assignment 2

16
ITECH7201 Software Engineering: Analysis and Design Assignment 2

10. Individual part – User stories

ID STORY

001 Named: player can be named themselves, use the name


which their input.

002 Look: player can input look command to get information about
the current location, props, information, and directions for
moving

003 Look +direction: player can get information about typed


directs, like item, money.

004 Move + direction: player can move to one direction, get the
information about the new place. The system describes the
location of the player movement, and hints at the relevant
operations that player can do, as well as hints about the
direction of the starting central region. If just input
move ,system will ask player which direction he/she will go.

005 Get item: every location may have items on the ground. Player
can pick up it. money also can get as items.

006 List item: player can list all the item he have.

007 Drop item: players can drop their item

008 Buy item: players can buy some item, like weapon ,if they have
enough money. This command only available in shop.

009 Sell item: players can sell their items. This command only
available in shop.

010 Attack Npc: player can attack NPC when they look at them in
one location. If the type of Npc is not hostile, players can not
attack; if the type of Npc is hostile, player can attack the npc,
a battle description will be showed.

011 Potion: if players hurted, can use potion to Restore HP

012

013

014

17
ITECH7201 Software Engineering: Analysis and Design Assignment 2

11. Individual part – Class diagrams for Lab 7 & 8

11.1 Lab 7 Class Diagram


Main structure:

Lab 7 and Lab 8 both have this main structure.

18
ITECH7201 Software Engineering: Analysis and Design Assignment 2

All of class in Lab 7:

19
ITECH7201 Software Engineering: Analysis and Design Assignment 2

Control package in lab7:

Boundary package in Lab7:

20
ITECH7201 Software Engineering: Analysis and Design Assignment 2

Entity package in Lab 7

Mazgegame package in Lab7:

Those two files: HardCodeData.java and SimpleConsoleClient were showed in “All of class in
Lab 7”

21
ITECH7201 Software Engineering: Analysis and Design Assignment 2

11.2 Lab 8 Class Diagram


All of class in Lab 8 (1):

22
ITECH7201 Software Engineering: Analysis and Design Assignment 2

All of class in Lab 8 (2):

23
ITECH7201 Software Engineering: Analysis and Design Assignment 2

Control package in Lab 8:

24
ITECH7201 Software Engineering: Analysis and Design Assignment 2

Boundary package in Lab 8:

Entity package in lab 8:

25
ITECH7201 Software Engineering: Analysis and Design Assignment 2

12. Individual part – Sequence diagrams

12.1 Sequence Diagram for “sell” command

26
ITECH7201 Software Engineering: Analysis and Design Assignment 2

12.2 Sequence Diagram for “attack” command

27
ITECH7201 Software Engineering: Analysis and Design Assignment 2

13. Individual part – Punchtime report

28
ITECH7201 Software Engineering: Analysis and Design Assignment 2

References
There is no specific reference in this report.

29

Вам также может понравиться