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 9bab251..d4005ec 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
}
@@ -199,4 +201,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
index b31793a..400e4f6 100644
--- a/MerchantRPGCSE2102/src/model/Player.java
+++ b/MerchantRPGCSE2102/src/model/Player.java
@@ -7,15 +7,17 @@ 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
-
-
+ private int _x, _y;
+
+
public Player(String playerName, int startingCash, ArrayList startingInventory)
{
_name = playerName;
_playerCash = startingCash;
- generatePlayerInventory(startingInventory);
+ if (startingInventory != null)
+ generatePlayerInventory(startingInventory);
}
-
+
/**
* Read's the player's item list provided by RPGame and store's instances of each item into the player's inventory
*
@@ -36,7 +38,7 @@ private void generatePlayerInventory(ArrayList itemList)
_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
*
@@ -45,9 +47,9 @@ private void generatePlayerInventory(ArrayList itemList)
*/
public void buy(String itemName, Merchant targetMerchant)
{
-
+
}
-
+
/**
* Allows player to sell an item to a target merchant
*
@@ -56,7 +58,7 @@ public void buy(String itemName, Merchant targetMerchant)
*/
public void sell(String itemName, Merchant targetMerchant)
{
-
+
}
/**
@@ -73,7 +75,7 @@ public void deductCash(int deductAmount)
else
_playerCash -= deductAmount;
}
-
+
/**
* Increase's the player's cash by the specified amount
*
@@ -83,7 +85,7 @@ public void increaseCash(int increaseAmount)
{
_playerCash += increaseAmount;
}
-
+
/**
* Returns a string containing the player's name
*
@@ -93,7 +95,7 @@ public String getName()
{
return _name;
}
-
+
/**
* Returns the current amount of cash the player has
*
@@ -103,7 +105,7 @@ public int getPlayerCash()
{
return _playerCash;
}
-
+
/**
* Returns the player's current inventory
*
@@ -113,4 +115,22 @@ public Item[] getInventory()
{
return _playerInventory;
}
+
+ 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/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 1f77d92..3fd56da 100644
--- a/MerchantRPGCSE2102/src/tests/TestPlayer.java
+++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java
@@ -27,6 +27,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));