Skip to content

Commit

Permalink
Added player as a member variable in the Map class and adjusted return
Browse files Browse the repository at this point in the history
value of the scaleAdjPrice to void in the Item class
  • Loading branch information
john committed Feb 28, 2015
1 parent 81a96e6 commit 41656b2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 38 deletions.
3 changes: 1 addition & 2 deletions MerchantRPGCSE2102/src/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Item(String itemName, int basePrice, int quantity)
* @return adjusted price integer
*
*/
public int scaleAdjPrice(double merchantPercent)
public void scaleAdjPrice(double merchantPercent)
{
int calculatedPrice = (int) Math.floor((merchantPercent / 100) * _basePrice); //will find the floor of the price to prevent decimals

Expand All @@ -48,7 +48,6 @@ else if(calculatedPrice < _minPrice)
_adjustedPrice = _minPrice;
else
_adjustedPrice = calculatedPrice;
return _adjustedPrice;
}

/**
Expand Down
25 changes: 13 additions & 12 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 @@ -41,19 +43,18 @@ public void initializeMerchant(Merchant merchant, int x, int y) {

/**
* Moves the specified instance of Player in the specified direction on the map
* @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 +63,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 +73,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 +83,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 @@ -15,7 +15,8 @@ public Player(String playerName, int startingCash, ArrayList<String> startingInv
{
_name = playerName;
_playerCash = startingCash;
generatePlayerInventory(startingInventory);
if (startingInventory != null)
generatePlayerInventory(startingInventory);
}

/**
Expand Down
18 changes: 11 additions & 7 deletions MerchantRPGCSE2102/src/tests/TestItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ public void setup()
public void testAdjustedPrice()
{
setup();
int testPrice = i.scaleAdjPrice(80);
i.scaleAdjPrice(80);
int testPrice = i.getAdjustedPrice();
assertEquals(80, testPrice);
testPrice = i.scaleAdjPrice(120);
assertEquals(120, testPrice);
testPrice = i.scaleAdjPrice(200);
assertEquals(150, testPrice);
testPrice = i.scaleAdjPrice(0);
assertEquals(50, testPrice);
i.scaleAdjPrice(120);
int testPrice2 = i.getAdjustedPrice();
assertEquals(120, testPrice2);
i.scaleAdjPrice(180);
int testPrice3 = i.getAdjustedPrice();
assertEquals(150, testPrice3);
i.scaleAdjPrice(40);
int testPrice4 = i.getAdjustedPrice();
assertEquals(50, testPrice4);
}
}
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 41656b2

Please sign in to comment.