Skip to content

Commit

Permalink
Implemented the Map class as well as movement for the Player. Graph,
Browse files Browse the repository at this point in the history
Vertex, and Edge class also added in its own package.
  • Loading branch information
john committed Feb 23, 2015
1 parent 4dbf552 commit c92c00a
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 13 deletions.
44 changes: 44 additions & 0 deletions MerchantRPGCSE2102/rpgclass diagram.mgc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<ClassDiagram>
<Class name="model.Player" x="873" y="252">
<AttributesDisplayFilter public-accepted="true"
private-accepted="true" protected-accepted="true"
default-accepted="true" static-accepted="false"/>
<MethodDisplayFilter public-accepted="true"
private-accepted="false" protected-accepted="false"
default-accepted="false" static-accepted="false"
accessors-accepted="false" constructors-accepted="false"/>
<Association destinationClassName="model.Item"
endpointName="_playerInventory" kind="Simple"/>
</Class>
<Class name="controller.RPGame" x="598" y="68">
<AttributesDisplayFilter public-accepted="true"
private-accepted="true" protected-accepted="true"
default-accepted="true" static-accepted="false"/>
<MethodDisplayFilter public-accepted="true"
private-accepted="false" protected-accepted="false"
default-accepted="false" static-accepted="false"
accessors-accepted="false" constructors-accepted="false"/>
</Class>
<Class name="model.Item" x="635" y="566">
<AttributesDisplayFilter public-accepted="true"
private-accepted="true" protected-accepted="true"
default-accepted="true" static-accepted="false"/>
<MethodDisplayFilter public-accepted="true"
private-accepted="false" protected-accepted="false"
default-accepted="false" static-accepted="false"
accessors-accepted="false" constructors-accepted="false"/>
</Class>
<Class name="model.Merchant" x="375" y="243">
<AttributesDisplayFilter public-accepted="true"
private-accepted="true" protected-accepted="true"
default-accepted="true" static-accepted="false"/>
<MethodDisplayFilter public-accepted="true"
private-accepted="false" protected-accepted="false"
default-accepted="false" static-accepted="false"
accessors-accepted="false" constructors-accepted="false"/>
<Association destinationClassName="model.Item"
endpointName="_merchantInventory" kind="Simple"/>
<Communication destinationClassName="model.Item"/>
</Class>
</ClassDiagram>
35 changes: 35 additions & 0 deletions MerchantRPGCSE2102/src/graph/Edge.java
Original file line number Diff line number Diff line change
@@ -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;

}


}
79 changes: 79 additions & 0 deletions MerchantRPGCSE2102/src/graph/Graph.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
53 changes: 53 additions & 0 deletions MerchantRPGCSE2102/src/graph/Vertex.java
Original file line number Diff line number Diff line change
@@ -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<Edge> _incidentEdges = new LinkedList<Edge>();


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<Edge> getIncidentList() {
return _incidentEdges;
}

public Object getOccupant() {
return _occupant;
}

public void setOccupant(Object occupant) {
_occupant = occupant;
}

}
110 changes: 110 additions & 0 deletions MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
20 changes: 19 additions & 1 deletion MerchantRPGCSE2102/src/model/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ public class Merchant
private int _baseCash;
private int _dailyRandomPercent;
private Item[] _merchantInventory;
private int _x, _y;


public Merchant(String name, int cashLimit, ArrayList<String> inventory)
{
_name = name;
_currentCash = cashLimit;
_baseCash = cashLimit;
generateMerchantInventory(inventory);
if (inventory != null)
generateMerchantInventory(inventory);
_dailyRandomPercent = 0; //placeholder percentage
}

Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit c92c00a

Please sign in to comment.