Refactored, comments++
This commit is contained in:
@@ -11,7 +11,6 @@ public class Game {
|
||||
public void gameUpdate();
|
||||
}
|
||||
|
||||
|
||||
// Constants
|
||||
static final int numberOfColors = 5;
|
||||
static final int linesPerLevel = 40;
|
||||
@@ -46,23 +45,19 @@ public class Game {
|
||||
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(){
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public int getAvailFreeMoves(){
|
||||
return freeMoves;
|
||||
}
|
||||
|
||||
public int getAvailUndo(){
|
||||
return numUndos;
|
||||
}
|
||||
|
||||
public int getSize(){
|
||||
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.
|
||||
*
|
||||
* @author aaron
|
||||
* @param src
|
||||
* @param dst
|
||||
* @return
|
||||
* @return True if a move from src to dst is possible.
|
||||
*/
|
||||
public boolean doMove(Point src, Point 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
|
||||
* @param src
|
||||
* @param dst
|
||||
* @return
|
||||
* @return Shortest path between src and dst.
|
||||
*/
|
||||
public List<Point> getPath(final Point src, final Point dst){
|
||||
|
||||
@@ -187,7 +213,7 @@ public class Game {
|
||||
* @author aaron
|
||||
* @param src
|
||||
* @param dst
|
||||
* @return
|
||||
* @return True if freemove is posible.
|
||||
*/
|
||||
public boolean doFreeMove(Point src, Point dst){
|
||||
//move without path checking
|
||||
@@ -207,16 +233,6 @@ public class Game {
|
||||
nextStep(dst); //cleanup rows or add new blocks
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public int getAvailFreeMoves(){
|
||||
return freeMoves;
|
||||
}
|
||||
|
||||
public int getAvailUndo(){
|
||||
return numUndos;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -251,7 +267,7 @@ public class Game {
|
||||
*
|
||||
* @author aaron
|
||||
* @param vertices
|
||||
* @return
|
||||
* @return Nearest vertex to the fist element of the given vertices list.
|
||||
*/
|
||||
private Vertex findNearestVertex(final List<Vertex> vertices){
|
||||
Vertex tmp = vertices.get(0);
|
||||
@@ -260,7 +276,6 @@ public class Game {
|
||||
Vertex n = vertices.get(i);
|
||||
if(n.getDist() < tmp.getDist()) {
|
||||
tmp = n;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,11 +289,10 @@ public class Game {
|
||||
* @param x
|
||||
* @param y
|
||||
* @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){
|
||||
return findVertex(new Point(x,y), vertices);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -287,7 +301,7 @@ public class Game {
|
||||
* @author aaron
|
||||
* @param pos
|
||||
* @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){
|
||||
for (int i = 0; i < vertices.size(); i++) {
|
||||
@@ -306,7 +320,7 @@ public class Game {
|
||||
* @param vertices
|
||||
* @param src
|
||||
* @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){
|
||||
ArrayList<Point> path = new ArrayList<Point>();
|
||||
@@ -350,6 +364,7 @@ public class Game {
|
||||
*
|
||||
* @author aaron
|
||||
* @param lastPoint
|
||||
* @return True if 4 or more blocks got removed.
|
||||
*/
|
||||
private boolean checkRemoveBlocks(final Point lastPoint){
|
||||
|
||||
@@ -427,8 +442,9 @@ public class Game {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds n new blocks to random positions on the field,
|
||||
* according to the level number.
|
||||
* Adds n new blocks to random positions on the field, according to the level number.
|
||||
*
|
||||
* @author aaron
|
||||
*/
|
||||
private void populateField(){
|
||||
|
||||
|
||||
Reference in New Issue
Block a user