Implemented freemovehandling

This commit is contained in:
id101010
2016-06-17 11:28:09 +02:00
parent effc25dac0
commit 89d7ffdd29

View File

@@ -17,6 +17,8 @@ public class Game {
private int score; private int score;
private int size; private int size;
private int freeBlocks; private int freeBlocks;
private int freeMoves;
private int numUndos;
private Random rand; private Random rand;
public Game(){ public Game(){
@@ -86,7 +88,6 @@ public class Game {
ArrayList<Vertex> vertices = new ArrayList<Vertex>(); // List of vertices ArrayList<Vertex> vertices = new ArrayList<Vertex>(); // List of vertices
vertices.add(new Vertex(0, src)); vertices.add(new Vertex(0, src));
// Get a verticies list from the field data // Get a verticies list from the field data
@@ -97,9 +98,9 @@ public class Game {
} }
} }
} }
ArrayList<Vertex> allVerticies = new ArrayList<Vertex>(vertices); // List of vertices ArrayList<Vertex> allVerticies = new ArrayList<Vertex>(vertices); // List of vertices
while(!vertices.isEmpty()){ // As long as there are vertices while(!vertices.isEmpty()){ // As long as there are vertices
final Vertex u = findNearestVertex(vertices); final Vertex u = findNearestVertex(vertices);
vertices.remove(u); // Remove u from the set of vertices vertices.remove(u); // Remove u from the set of vertices
@@ -134,7 +135,7 @@ public class Game {
} }
public boolean doUndo(){ public boolean doUndo(){
return false; return false;
} }
public boolean doFreeMove(Point src, Point dst){ public boolean doFreeMove(Point src, Point dst){
@@ -149,6 +150,9 @@ public class Game {
field[dst.x][dst.y] = field[src.x][src.y]; field[dst.x][dst.y] = field[src.x][src.y];
field[src.x][src.y] = 0; field[src.x][src.y] = 0;
freeMoves--;
nextStep(dst); //cleanup rows or add new blocks nextStep(dst); //cleanup rows or add new blocks
return true; return true;
@@ -157,11 +161,11 @@ public class Game {
} }
public int getAvailFreeMoves(){ public int getAvailFreeMoves(){
return 0; return freeMoves;
} }
public int getAvailUndo(){ public int getAvailUndo(){
return 0; return numUndos;
} }
public void reset(){ public void reset(){
@@ -175,6 +179,8 @@ public class Game {
field = new int[size][size]; field = new int[size][size];
level = 1; level = 1;
score = 0; score = 0;
numUndos = 0;
freeMoves = 0;
// Populate game field // Populate game field
this.populateField(); this.populateField();
@@ -212,6 +218,13 @@ public class Game {
return null; return null;
} }
/**
*
* @param vertices
* @param src
* @param dst
* @return
*/
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>();
path.add(dst); path.add(dst);
@@ -230,18 +243,33 @@ 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
* @param lastPoint
*/ */
private void nextStep(final Point lastPoint) { private void nextStep(final Point lastPoint) {
if(!checkRemoveBlocks(lastPoint)){
populateField(); //add new blocks
}
}
/**
* Collision detection and block removal if there are 4 or more blocks in a row in any direction.
*
* @author
* @param lastPoint
*/
private boolean checkRemoveBlocks(final Point lastPoint){
final Point[] offsets = { final Point[] offsets = {
new Point(0,1), // -> new Point(0,1), // right
new Point(0,-1), // <- new Point(0,-1), // left
new Point(1,0), // v new Point(1,0), // bottom
new Point(-1,0), // ^ new Point(-1,0), // top
new Point(1,1), // \. new Point(1,1), // bottom right
new Point(-1,-1), // '\ new Point(-1,-1),// top left
new Point(-1,1), // ./ new Point(-1,1), // bottom left
new Point(1,-1) // /' new Point(1,-1) // top right
}; };
int matches[] = new int[8]; int matches[] = new int[8];
@@ -264,12 +292,12 @@ public class Game {
matches[i] = matchcount; matches[i] = matchcount;
} }
boolean matched = false; int distinctmatches = 0;
for(int i = 0; i < 4; i++){ for(int i = 0; i < 4; i++){
int totalmatches = 1 + matches[i*2] + matches[i*2+1]; int totalmatches = 1 + matches[i*2] + matches[i*2+1];
if(totalmatches >= 4){ if(totalmatches >= 4){
matched = true; distinctmatches++;
for(int j = 0; j < 2; j++){ for(int j = 0; j < 2; j++){
Point offset = offsets[j+i*2]; Point offset = offsets[j+i*2];
Point current = new Point(lastPoint); Point current = new Point(lastPoint);
@@ -283,12 +311,19 @@ public class Game {
} }
} }
if(!matched){ if(distinctmatches > 0){
populateField(); //add new blocks
}else{
field[lastPoint.x][lastPoint.y] = 0; field[lastPoint.x][lastPoint.y] = 0;
freeBlocks++; freeBlocks++;
if(distinctmatches < 1){
freeMoves++;
}
return true;
} }
return false;
} }
/** /**
@@ -306,6 +341,7 @@ public class Game {
if(field[x][y] == 0){ if(field[x][y] == 0){
field[x][y] = nextBlocks.remove(0); // fill with the first element of nextBlocks field[x][y] = nextBlocks.remove(0); // fill with the first element of nextBlocks
freeBlocks--; freeBlocks--;
checkRemoveBlocks(new Point(x,y));
} }
} }