Skip to content

Commit

Permalink
Added conditional for null inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Feb 28, 2015
1 parent eb2b0f7 commit 466d78e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
24 changes: 13 additions & 11 deletions MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Map {

private Graph _mapGraph; // Graph representation of the map
private int _mapSize; // The n*n dimension of the map
private Player _player;

public Map(int size) {
_mapSize = size;
Expand All @@ -20,6 +21,7 @@ public Map(int size) {
* @param y The y coordinate of the player's location
*/
public void initializePlayer(Player player, int x, int y) {
_player = player;
int vertexNum = y*_mapSize + x;
player.setX(x);
player.setY(y);
Expand All @@ -44,16 +46,16 @@ public void initializeMerchant(Merchant merchant, int x, int y) {
* @param player The player to be moved
* @param String The direction to be moved (north, west, east, or south)
*/
public void movePlayer(Player player, String direction) {
int currentX = player.getX();
int currentY = player.getY();
public void movePlayer(String direction) {
int currentX = _player.getX();
int currentY = _player.getY();

if (direction.equals("north")) {
if (currentY != 0) {
if (!isOccupied(currentX, currentY - 1)) {
_mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null);
_mapGraph.getVertex((currentY - 1)*_mapSize + currentX).setOccupant(player);
player.setY(currentY - 1);
_mapGraph.getVertex((currentY - 1)*_mapSize + currentX).setOccupant(_player);
_player.setY(currentY - 1);
}
}
}
Expand All @@ -62,8 +64,8 @@ public void movePlayer(Player player, String direction) {
if (currentX != _mapSize - 1) {
if (!isOccupied(currentX + 1, currentY)) {
_mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null);
_mapGraph.getVertex(currentY*_mapSize + (currentX + 1)).setOccupant(player);
player.setX(currentX + 1);
_mapGraph.getVertex(currentY*_mapSize + (currentX + 1)).setOccupant(_player);
_player.setX(currentX + 1);
}
}
}
Expand All @@ -72,8 +74,8 @@ public void movePlayer(Player player, String direction) {
if (currentY != _mapSize - 1) {
if (!isOccupied(currentX, currentY + 1)) {
_mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null);
_mapGraph.getVertex((currentY + 1)*_mapSize + currentX).setOccupant(player);
player.setY(currentY + 1);
_mapGraph.getVertex((currentY + 1)*_mapSize + currentX).setOccupant(_player);
_player.setY(currentY + 1);
}
}
}
Expand All @@ -82,8 +84,8 @@ public void movePlayer(Player player, String direction) {
if (currentX != 0) {
if (!isOccupied(currentX - 1, currentY)) {
_mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null);
_mapGraph.getVertex(currentY*_mapSize + (currentX - 1)).setOccupant(player);
player.setX(currentX - 1);
_mapGraph.getVertex(currentY*_mapSize + (currentX - 1)).setOccupant(_player);
_player.setX(currentX - 1);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion MerchantRPGCSE2102/src/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public Player(String playerName, int startingCash, ArrayList<String> startingInv
{
_name = playerName;
_playerCash = startingCash;
generatePlayerInventory(startingInventory);
if (startingInventory != null)
generatePlayerInventory(startingInventory);
}

/**
Expand Down
32 changes: 16 additions & 16 deletions MerchantRPGCSE2102/src/tests/TestMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ public void testMovePlayer() {
Merchant merchant = new Merchant("TestMerchant", 0, null);
map.initializePlayer(player, 3, 3);
map.initializeMerchant(merchant, 0, 2);
map.movePlayer(player, "west");
map.movePlayer(player, "south");
map.movePlayer(player, "north");
map.movePlayer(player, "east");
map.movePlayer(player, "east");
map.movePlayer(player, "north");
map.movePlayer(player, "north");
map.movePlayer(player, "north");
map.movePlayer(player, "west");
map.movePlayer(player, "west");
map.movePlayer(player, "west");
map.movePlayer(player, "west");
map.movePlayer(player, "south");
map.movePlayer(player, "south");
map.movePlayer(player, "south");
map.movePlayer(player, "south");
map.movePlayer("west");
map.movePlayer("south");
map.movePlayer("north");
map.movePlayer("east");
map.movePlayer("east");
map.movePlayer("north");
map.movePlayer("north");
map.movePlayer("north");
map.movePlayer("west");
map.movePlayer("west");
map.movePlayer("west");
map.movePlayer("west");
map.movePlayer("south");
map.movePlayer("south");
map.movePlayer("south");
map.movePlayer("south");
assertEquals(player.getX(), 0);
assertEquals(player.getY(), 1);
assertEquals(map.isOccupied(player.getX(), player.getY()), true);
Expand Down

0 comments on commit 466d78e

Please sign in to comment.