-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the Map class as well as movement for the Player. Graph,
Vertex, and Edge class also added in its own package.
- Loading branch information
Showing
10 changed files
with
445 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.