Merge remote-tracking branch 'origin/master'

This commit is contained in:
t-moe
2016-06-17 13:18:25 +02:00

View File

@@ -31,6 +31,12 @@ public class Game {
this(7); this(7);
} }
/**
* Constructor.
*
* @author aaron
* @param size
*/
public Game (int size) { public Game (int size) {
/* Initialize variables */ /* Initialize variables */
this.size = size; this.size = size;
@@ -87,6 +93,14 @@ public class Game {
} }
/**
* Check if there is a valid path from src to dst and move a block if possible.
*
* @author aaron
* @param src
* @param dst
* @return
*/
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)) {
return false; return false;
@@ -106,6 +120,11 @@ public class Game {
/** /**
* Pathfinding of shortest path between src and dst. * Pathfinding of shortest path between src and dst.
*
* @author aaron
* @param src
* @param dst
* @return
*/ */
public List<Point> getPath(final Point src, final Point dst){ public List<Point> getPath(final Point src, final Point dst){
@@ -161,6 +180,14 @@ public class Game {
return false; return false;
} }
/**
* Do a free move if freeMoves < 0.
*
* @author aaron
* @param src
* @param dst
* @return
*/
public boolean doFreeMove(Point src, Point dst){ public boolean doFreeMove(Point src, Point dst){
//move without path checking //move without path checking
if(getAvailFreeMoves() <= 0 ) { if(getAvailFreeMoves() <= 0 ) {
@@ -191,6 +218,11 @@ public class Game {
return numUndos; return numUndos;
} }
/**
* Reset game score, field and state.
*
* @author aaron
*/
public void reset(){ public void reset(){
// Initialize new blocks // Initialize new blocks
nextBlocks = new ArrayList<Integer>(); nextBlocks = new ArrayList<Integer>();
@@ -211,6 +243,10 @@ public class Game {
/** /**
* Finds the nearest vertex to start * Finds the nearest vertex to start
*
* @author aaron
* @param vertices
* @return
*/ */
private Vertex findNearestVertex(final List<Vertex> vertices){ private Vertex findNearestVertex(final List<Vertex> vertices){
Vertex tmp = vertices.get(0); Vertex tmp = vertices.get(0);
@@ -226,11 +262,28 @@ public class Game {
return tmp; return tmp;
} }
/**
* Helper function for pathfinding. Finds a vertex corresponding to the given coordinate.
*
* @author aaron
* @param x
* @param y
* @param vertices
* @return
*/
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);
} }
/**
* Helper function for pathfinding. Finds a vertex corresponding to the given point.
*
* @author aaron
* @param pos
* @param vertices
* @return
*/
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++) {
Vertex n = vertices.get(i); Vertex n = vertices.get(i);
@@ -242,7 +295,9 @@ public class Game {
} }
/** /**
* Helper function for pathfinding. Returns shortest path between src and dst in a given set of vertices.
* *
* @author aaron
* @param vertices * @param vertices
* @param src * @param src
* @param dst * @param dst
@@ -267,7 +322,7 @@ public class Game {
/** /**
* Calculates the next game step. This method will either call populateField, or it will cleanup blocks * Calculates the next game step. This method will either call populateField, or it will cleanup blocks
* *
* @author * @author aaron
* @param lastPoint * @param lastPoint
*/ */
private void nextStep(final Point lastPoint) { private void nextStep(final Point lastPoint) {
@@ -280,7 +335,7 @@ public class Game {
/** /**
* Collision detection and block removal if there are 4 or more blocks in a row in any direction. * Collision detection and block removal if there are 4 or more blocks in a row in any direction.
* *
* @author * @author aaron
* @param lastPoint * @param lastPoint
*/ */
private boolean checkRemoveBlocks(final Point lastPoint){ private boolean checkRemoveBlocks(final Point lastPoint){
@@ -343,6 +398,14 @@ public class Game {
freeMoves++; freeMoves++;
} }
int sum = 0;
for( Integer i : matches ) sum += i;
score += (1 + distinctmatches * sum);
System.out.println("Score: " + score);
return true; return true;
} }