Refactored, comments++

This commit is contained in:
id101010
2016-06-22 20:44:56 +02:00
parent 3aa7195ce8
commit 0baf13ad43

View File

@@ -11,7 +11,6 @@ public class Game {
public void gameUpdate(); public void gameUpdate();
} }
// Constants // Constants
static final int numberOfColors = 5; static final int numberOfColors = 5;
static final int linesPerLevel = 40; static final int linesPerLevel = 40;
@@ -46,23 +45,19 @@ public class Game {
this.reset(size); this.reset(size);
} }
public void addUpdateListener(UpdateListener listener) {
updateListeners.add(listener);
}
public void removeUpdateListener(UpdateListener listener) {
updateListeners.remove(listener);
}
private void emitUpdateEvent() {
for(UpdateListener e: updateListeners) {
e.gameUpdate();
}
}
public boolean isGameOver(){ public boolean isGameOver(){
return false; return false;
} }
public int getAvailFreeMoves(){
return freeMoves;
}
public int getAvailUndo(){
return numUndos;
}
public int getSize(){ public int getSize(){
return size; return size;
} }
@@ -94,13 +89,44 @@ public class Game {
} }
/**
* Adds an update listener to the game object.
*
* @author aaron
* @param listener
*/
public void addUpdateListener(UpdateListener listener){
updateListeners.add(listener);
}
/**
* Removes the update listener from the game object.
*
* @author aaron
* @param listener
*/
public void removeUpdateListener(UpdateListener listener){
updateListeners.remove(listener);
}
/**
* Updatelistener callback, updates game when a listener gets triggered.
*
* @author aaron
*/
private void emitUpdateEvent(){
for(UpdateListener e: updateListeners) {
e.gameUpdate();
}
}
/** /**
* Check if there is a valid path from src to dst and move a block if possible. * Check if there is a valid path from src to dst and move a block if possible.
* *
* @author aaron * @author aaron
* @param src * @param src
* @param dst * @param dst
* @return * @return True if a move from src to dst is possible.
*/ */
public boolean doMove(Point src, Point dst){ public boolean doMove(Point src, Point dst){
if(field[src.x][src.y]==0 ||field[dst.x][dst.y] !=0 || src.equals(dst)) { if(field[src.x][src.y]==0 ||field[dst.x][dst.y] !=0 || src.equals(dst)) {
@@ -125,7 +151,7 @@ public class Game {
* @author aaron * @author aaron
* @param src * @param src
* @param dst * @param dst
* @return * @return Shortest path between src and dst.
*/ */
public List<Point> getPath(final Point src, final Point dst){ public List<Point> getPath(final Point src, final Point dst){
@@ -187,7 +213,7 @@ public class Game {
* @author aaron * @author aaron
* @param src * @param src
* @param dst * @param dst
* @return * @return True if freemove is posible.
*/ */
public boolean doFreeMove(Point src, Point dst){ public boolean doFreeMove(Point src, Point dst){
//move without path checking //move without path checking
@@ -207,16 +233,6 @@ public class Game {
nextStep(dst); //cleanup rows or add new blocks nextStep(dst); //cleanup rows or add new blocks
return true; return true;
}
public int getAvailFreeMoves(){
return freeMoves;
}
public int getAvailUndo(){
return numUndos;
} }
/** /**
@@ -251,7 +267,7 @@ public class Game {
* *
* @author aaron * @author aaron
* @param vertices * @param vertices
* @return * @return Nearest vertex to the fist element of the given vertices list.
*/ */
private Vertex findNearestVertex(final List<Vertex> vertices){ private Vertex findNearestVertex(final List<Vertex> vertices){
Vertex tmp = vertices.get(0); Vertex tmp = vertices.get(0);
@@ -260,7 +276,6 @@ public class Game {
Vertex n = vertices.get(i); Vertex n = vertices.get(i);
if(n.getDist() < tmp.getDist()) { if(n.getDist() < tmp.getDist()) {
tmp = n; tmp = n;
} }
} }
@@ -274,11 +289,10 @@ public class Game {
* @param x * @param x
* @param y * @param y
* @param vertices * @param vertices
* @return * @return Vertex with the given position out of a list of vertices.
*/ */
private Vertex findVertex(int x, int y, final List<Vertex> vertices){ private Vertex findVertex(int x, int y, final List<Vertex> vertices){
return findVertex(new Point(x,y), vertices); return findVertex(new Point(x,y), vertices);
} }
/** /**
@@ -287,7 +301,7 @@ public class Game {
* @author aaron * @author aaron
* @param pos * @param pos
* @param vertices * @param vertices
* @return * @return Vertex with the given position out of a list of vertices.
*/ */
private Vertex findVertex(final Point pos, final List<Vertex> vertices){ private Vertex findVertex(final Point pos, final List<Vertex> vertices){
for (int i = 0; i < vertices.size(); i++) { for (int i = 0; i < vertices.size(); i++) {
@@ -306,7 +320,7 @@ public class Game {
* @param vertices * @param vertices
* @param src * @param src
* @param dst * @param dst
* @return * @return Shortest path between two given points in a list of vertices.
*/ */
private List<Point> reconstructShortestPath(final List<Vertex> vertices, final Point src, final Point dst){ private List<Point> reconstructShortestPath(final List<Vertex> vertices, final Point src, final Point dst){
ArrayList<Point> path = new ArrayList<Point>(); ArrayList<Point> path = new ArrayList<Point>();
@@ -350,6 +364,7 @@ public class Game {
* *
* @author aaron * @author aaron
* @param lastPoint * @param lastPoint
* @return True if 4 or more blocks got removed.
*/ */
private boolean checkRemoveBlocks(final Point lastPoint){ private boolean checkRemoveBlocks(final Point lastPoint){
@@ -427,8 +442,9 @@ public class Game {
} }
/** /**
* Adds n new blocks to random positions on the field, * Adds n new blocks to random positions on the field, according to the level number.
* according to the level number. *
* @author aaron
*/ */
private void populateField(){ private void populateField(){