diff --git a/MerchantRPGCSE2102/rpgclass diagram.mgc b/MerchantRPGCSE2102/rpgclass diagram.mgc
new file mode 100644
index 0000000..59536ad
--- /dev/null
+++ b/MerchantRPGCSE2102/rpgclass diagram.mgc
@@ -0,0 +1,44 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/MerchantRPGCSE2102/src/graph/Edge.java b/MerchantRPGCSE2102/src/graph/Edge.java
new file mode 100644
index 0000000..f560ced
--- /dev/null
+++ b/MerchantRPGCSE2102/src/graph/Edge.java
@@ -0,0 +1,35 @@
+package graph;
+
+
+public class Edge {
+ private Vertex _v;
+ private Vertex _w;
+
+
+ public Vertex getV() {
+ return _v;
+ }
+
+ public void setV(Vertex v) {
+ _v = v;
+ }
+
+ public Vertex getW() {
+ return _w;
+ }
+
+ public void setW(Vertex w) {
+ _w = w;
+ }
+
+ public Vertex opposite(Vertex v) {
+ if (v == _v)
+ return _w;
+ else if (v == _w)
+ return _v;
+ return null;
+
+ }
+
+
+}
diff --git a/MerchantRPGCSE2102/src/graph/Graph.java b/MerchantRPGCSE2102/src/graph/Graph.java
new file mode 100644
index 0000000..1acff8b
--- /dev/null
+++ b/MerchantRPGCSE2102/src/graph/Graph.java
@@ -0,0 +1,79 @@
+package graph;
+
+public class Graph
+{
+ private Vertex[] _vertexList;
+ private int _size;
+
+ public Graph(int size)
+ {
+ _size = size;
+ _vertexList = new Vertex[size*size];
+ }
+
+ public void initializeGraph() {
+ initializeVertices();
+ initializeEdges();
+ }
+
+ public void initializeVertices() {
+ for (int row = 0; row < _size; row++) {
+ for (int col = 0; col < _size; col++) {
+ int cellNum = row*_size + col;
+ Vertex v = new Vertex(cellNum, _size);
+ setVertex(cellNum, v);
+ }
+ }
+ }
+
+ public void initializeEdges() {
+ for (int row = 0; row < _size; row++) {
+ for (int col = 0; col < _size; col++) {
+
+ if (row !=0)
+ connectWithEdge(getVertex(_size*row + col), getVertex(_size*(row - 1) + col));
+
+ if (col != _size - 1)
+ connectWithEdge(getVertex(_size*row + col), getVertex(_size*row + col + 1));
+ }
+ }
+ }
+
+ public void connectWithEdge(Vertex v, Vertex w) {
+ Edge edge = new Edge();
+ v.addEdge(edge);
+ w.addEdge(edge);
+ edge.setV(v);
+ edge.setW(w);
+ }
+
+ public int getSize()
+ {
+ return _size;
+ }
+
+ public Vertex getVertex(int vertexNum)
+ {
+ return _vertexList[vertexNum];
+ }
+
+ public void setVertex(int vertexNum, Vertex vertex) {
+ _vertexList[vertexNum] = vertex;
+ }
+
+ /**
+ * Checks if two vertices are adjacent
+ */
+ public boolean areAdjacent(Vertex v, Vertex w) {
+ for (Edge edge : v.getIncidentList()) {
+ if (edge.getV() == w || edge.getW() == w)
+ return true;
+ }
+ return false;
+ }
+
+ public Vertex[] getVertexList() {
+ return _vertexList;
+ }
+
+}
diff --git a/MerchantRPGCSE2102/src/graph/Vertex.java b/MerchantRPGCSE2102/src/graph/Vertex.java
new file mode 100644
index 0000000..4b69807
--- /dev/null
+++ b/MerchantRPGCSE2102/src/graph/Vertex.java
@@ -0,0 +1,53 @@
+package graph;
+
+import java.util.LinkedList;
+
+public class Vertex
+{
+ private int _cellNum;
+ private int _row;
+ private int _col;
+ private Object _occupant = null;
+ private LinkedList _incidentEdges = new LinkedList();
+
+
+ public Vertex(int cellNum, int n) {
+ _cellNum = cellNum;
+ _row = 0;
+ int temp = _cellNum;
+ while (temp - n >= 0) { // Calculates the row of the vertex
+ temp = temp - n;
+ _row++;
+ }
+ _col = _cellNum - _row*n; // Calculates the column of the vertex
+ }
+
+ public void addEdge(Edge edge) {
+ _incidentEdges.add(edge);
+ }
+
+ public int getCellNum() {
+ return _cellNum;
+ }
+
+ public int getRow() {
+ return _row;
+ }
+
+ public int getCol() {
+ return _col;
+ }
+
+ public LinkedList getIncidentList() {
+ return _incidentEdges;
+ }
+
+ public Object getOccupant() {
+ return _occupant;
+ }
+
+ public void setOccupant(Object occupant) {
+ _occupant = occupant;
+ }
+
+}
diff --git a/MerchantRPGCSE2102/src/model/Map.java b/MerchantRPGCSE2102/src/model/Map.java
new file mode 100644
index 0000000..7c61a0b
--- /dev/null
+++ b/MerchantRPGCSE2102/src/model/Map.java
@@ -0,0 +1,110 @@
+package model;
+
+import graph.Graph;
+
+public class Map {
+
+ private Graph _mapGraph; // Graph representation of the map
+ private int _mapSize; // The n*n dimension of the map
+
+ public Map(int size) {
+ _mapSize = size;
+ _mapGraph = new Graph(_mapSize);
+ _mapGraph.initializeGraph();
+ }
+
+ /**
+ * This method stores the sepcified instance of Player at the specified position on the map
+ * @param player The player to be initialized
+ * @param x The x coordinate of the player's location
+ * @param y The y coordinate of the player's location
+ */
+ public void initializePlayer(Player player, int x, int y) {
+ int vertexNum = y*_mapSize + x;
+ player.setX(x);
+ player.setY(y);
+ _mapGraph.getVertex(vertexNum).setOccupant(player);
+ }
+
+ /**
+ * This method stores the sepcified instance of Merchant at the specified position on the map
+ * @param player The merchant to be initialized
+ * @param x The x coordinate of the merchant's location
+ * @param y The y coordinate of the merchant's location
+ */
+ public void initializeMerchant(Merchant merchant, int x, int y) {
+ int vertexNum = y*_mapSize + x;
+ merchant.setX(x);
+ merchant.setY(y);
+ _mapGraph.getVertex(vertexNum).setOccupant(merchant);
+ }
+
+ /**
+ * 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();
+
+ 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);
+ }
+ }
+ }
+
+ if (direction.equals("east")) {
+ 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);
+ }
+ }
+ }
+
+ if (direction.equals("south")) {
+ 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);
+ }
+ }
+ }
+
+ if (direction.equals("west")) {
+ 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);
+ }
+ }
+ }
+ }
+
+ /**
+ * Returns the graph representation of the map
+ * @return graph
+ */
+ public Graph getGraph() {
+ return _mapGraph;
+ }
+
+ /**
+ * Checks if a location on the map is occupied by a merchant or player
+ * @param x x coordinate to check
+ * @param y y coordinate to check
+ * @return Boolean value
+ */
+ public boolean isOccupied(int x, int y) {
+ return _mapGraph.getVertex(y*_mapSize + x).getOccupant() != null;
+ }
+
+}
diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java
index 1e2e4af..17997b5 100644
--- a/MerchantRPGCSE2102/src/model/Merchant.java
+++ b/MerchantRPGCSE2102/src/model/Merchant.java
@@ -10,6 +10,7 @@ public class Merchant
private int _baseCash;
private int _dailyRandomPercent;
private Item[] _merchantInventory;
+ private int _x, _y;
public Merchant(String name, int cashLimit, ArrayList inventory)
@@ -17,7 +18,8 @@ public Merchant(String name, int cashLimit, ArrayList inventory)
_name = name;
_currentCash = cashLimit;
_baseCash = cashLimit;
- generateMerchantInventory(inventory);
+ if (inventory != null)
+ generateMerchantInventory(inventory);
_dailyRandomPercent = 0; //placeholder percentage
}
@@ -200,4 +202,20 @@ public Item[] getInventory()
{
return _merchantInventory;
}
+
+ public int getX() {
+ return _x;
+ }
+
+ public void setX(int x) {
+ _x = x;
+ }
+
+ public int getY() {
+ return _y;
+ }
+
+ public void setY(int y) {
+ _y = y;
+ }
}
diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java
deleted file mode 100644
index 90f481f..0000000
--- a/MerchantRPGCSE2102/src/model/Player.java
+++ /dev/null
@@ -1,166 +0,0 @@
-package model;
-
-import java.util.ArrayList;
-
-public class Player
-{
- private String _name; // player's name
- private int _playerCash; // current amount of cash the player has
- private Item[] _playerInventory; // the player's current inventory
-
-
- public Player(String playerName, int startingCash, ArrayList startingInventory)
- {
- _name = playerName;
- _playerCash = startingCash;
- generatePlayerInventory(startingInventory);
- }
-
- /**
- * Read's the player's item list provided by RPGame and store's instances of each item into the player's inventory
- *
- * @param itemList an ArrayList of strings of items to be supplied to the player
- */
- private void generatePlayerInventory(ArrayList itemList)
- {
- _playerInventory = new Item[itemList.size()]; // inventory size will be the size of the list of items
- String[] nameAndPrice; // a two string array of both the item name and price
- String name;
- String price;
-
- for(int i=0; i < itemList.size(); i++)
- {
- nameAndPrice = itemList.get(i).split("\\s+"); //splits the string into the string name and string price
- name = nameAndPrice[0];
- price = nameAndPrice[1];
- _playerInventory[i] = new Item(name, Integer.parseInt(price), 0); // stores a new instance of the item in the player's inventory (quantity begins at 0)
- }
- }
-
- /**
- * Allows player to buy an item from a target merchant
- *
- * @param itemName string containing the name of the item to be purchased
- * @param targetMerchant the merchant who the player is purchasing from
- * @param amount the amount of the item that the player is buying
- * @return returns true if transaction successful, false otherwise
- */
- public boolean buy(String itemName, Merchant targetMerchant, int amount)
- {
- int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items
-
- if(totalPrice > getPlayerCash())
- {
- System.out.println("You do not have sufficent cash"); //will output error message if total price is over the player's cash amount
- return false;
- }
- else
- {
- deductCash(totalPrice);
- Item targetItem = getItem(itemName);
- targetItem.increaseQuantity(amount);
- return true;
- }
- }
-
- /**
- * Allows player to sell an item to a target merchant
- *
- * @param itemName string containing the name of the item to be sold
- * @param targetMerchant the merchant who the player is selling to
- * @param amount the amount of the item that the player is selling
- * @return returns true if transaction successful, false otherwise
- */
- public boolean sell(String itemName, Merchant targetMerchant, int amount)
- {
- int totalPrice = targetMerchant.getItemPrice(itemName)* amount; //calculates the total price of the items
- Item targetItem = getItem(itemName);
-
- if(targetMerchant.getCurrentCash() >= totalPrice)
- {
- if(targetItem.decreaseQuantity(amount))
- {
- increaseCash(totalPrice);
- targetMerchant.subtractCash(amount);
- return true;
- }
- else
- return false;
- }
- else
- return false;
- }
-
- /**
- * Searches through the player's inventory for the item corresponding to the specified item name
- *
- * @param itemName string containing the name of the item
- * @return the item matching the specified item name
- */
- public Item getItem(String itemName)
- {
- for(int i = 0; i < _playerInventory.length; i++)
- {
- if(_playerInventory[i].getItemName().equals(itemName))
- return _playerInventory[i];
- }
-
- System.out.println("No such item exists"); // item was not found by searching the inventory
- return null;
- }
-
- /**
- * Decreases the player's cash by the specified amount
- *
- * @param deductAmount amount to decrease cash by
- */
- public void deductCash(int deductAmount)
- {
- if(deductAmount > _playerCash)
- {
- System.out.println("ERROR: Player does not have sufficient cash");
- }
- else
- _playerCash -= deductAmount;
- }
-
- /**
- * Increase's the player's cash by the specified amount
- *
- * @param increaseAmount amount to increase cash by
- */
- public void increaseCash(int increaseAmount)
- {
- _playerCash += increaseAmount;
- }
-
- /**
- * Returns a string containing the player's name
- *
- * @return player's name
- */
- public String getName()
- {
- return _name;
- }
-
- /**
- * Returns the current amount of cash the player has
- *
- * @return player's cash amount
- */
- public int getPlayerCash()
- {
- return _playerCash;
- }
-
- /**
- * Returns the player's current inventory
- *
- * @return player's inventory array
- */
- public Item[] getInventory()
- {
- return _playerInventory;
- }
-}
diff --git a/MerchantRPGCSE2102/src/tests/TestMap.java b/MerchantRPGCSE2102/src/tests/TestMap.java
new file mode 100644
index 0000000..4560aa1
--- /dev/null
+++ b/MerchantRPGCSE2102/src/tests/TestMap.java
@@ -0,0 +1,71 @@
+package tests;
+
+import graph.Graph;
+import graph.Vertex;
+import graph.Edge;
+import model.Map;
+import model.Merchant;
+import model.Player;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestMap {
+
+ Map map;
+
+ @Test
+ public void test() {
+ setup();
+ testMapInit();
+ testOccupancy();
+ testMovePlayer();
+ }
+
+ public void setup() {
+ map = new Map(4);
+ }
+
+ public void testMapInit() {
+ Graph graph = map.getGraph();
+ for (Vertex v : graph.getVertexList()) {
+ System.out.print("Vertex " + v.getCellNum() + " is adjacent with ");
+ for (Edge edge : v.getIncidentList())
+ System.out.print(edge.opposite(v).getCellNum() + " ");
+ System.out.println();
+ }
+ }
+
+ public void testOccupancy() {
+ for (Vertex v : map.getGraph().getVertexList()) {
+ assertEquals(v.getOccupant(), null);
+ }
+ }
+
+ public void testMovePlayer() {
+ Player player = new Player("TestPlayer", 0, null);
+ 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");
+ assertEquals(player.getX(), 0);
+ assertEquals(player.getY(), 1);
+ assertEquals(map.isOccupied(player.getX(), player.getY()), true);
+ }
+
+}
diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java
index 050891f..da54a73 100644
--- a/MerchantRPGCSE2102/src/tests/TestPlayer.java
+++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java
@@ -30,6 +30,7 @@ public void testPlayerCashSystem()
}
public void testPlayerInventory() {
+ // tests that the player is able to load their inventory which the RPGame read from the text file
setup();
Item[] inventory = p.getInventory();
for (Item item : inventory) {
diff --git a/MerchantRPGCSE2102/src/tests/TestRPGame.java b/MerchantRPGCSE2102/src/tests/TestRPGame.java
index 0bc5f45..7c56296 100644
--- a/MerchantRPGCSE2102/src/tests/TestRPGame.java
+++ b/MerchantRPGCSE2102/src/tests/TestRPGame.java
@@ -14,6 +14,7 @@ public void setup()
public void testFile()
{
+ // tests that the RPGame is able to read from the text file correctly
setup();
game.inventoryFromFile();
assertEquals("water 3", game.getMerchantInventoryList(1).get(0));