Implemented undo, refactored some code.
This commit is contained in:
@@ -96,6 +96,7 @@ public class FieldCanvas extends JPanel{
|
|||||||
public void doUndo() {
|
public void doUndo() {
|
||||||
if(game.getAvailUndo()>0) {
|
if(game.getAvailUndo()>0) {
|
||||||
game.doUndo();
|
game.doUndo();
|
||||||
|
repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,15 +109,11 @@ public class FieldCanvas extends JPanel{
|
|||||||
return new Point(globalPos.x/space,globalPos.y/space);
|
return new Point(globalPos.x/space,globalPos.y/space);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
|
|
||||||
g.setColor(Color.lightGray);
|
g.setColor(Color.lightGray);
|
||||||
|
|
||||||
|
|
||||||
g.translate(borderLeft, borderTop);
|
g.translate(borderLeft, borderTop);
|
||||||
int total = Math.min(this.getHeight()-borderTop-borderBottom,FieldCanvas.this.getWidth()-borderLeft-borderRight);
|
int total = Math.min(this.getHeight()-borderTop-borderBottom,FieldCanvas.this.getWidth()-borderLeft-borderRight);
|
||||||
int space = total/game.getSize();
|
int space = total/game.getSize();
|
||||||
@@ -156,18 +153,12 @@ public class FieldCanvas extends JPanel{
|
|||||||
g.setColor(c);
|
g.setColor(c);
|
||||||
g.fillRect(src.x*space+2+sSpace2, src.y*space+2+sSpace2, space -3 - 2* sSpace2, space -3 - 2* sSpace2);
|
g.fillRect(src.x*space+2+sSpace2, src.y*space+2+sSpace2, space -3 - 2* sSpace2, space -3 - 2* sSpace2);
|
||||||
|
|
||||||
|
|
||||||
for(int i=1; i<path.size() -1; i++) {
|
for(int i=1; i<path.size() -1; i++) {
|
||||||
Point p = path.get(i);
|
Point p = path.get(i);
|
||||||
g.fillRect(p.x*space+2+sSpace, p.y*space+2+sSpace, space -3 - 2* sSpace, space -3 - 2* sSpace);
|
g.fillRect(p.x*space+2+sSpace, p.y*space+2+sSpace, space -3 - 2* sSpace, space -3 - 2* sSpace);
|
||||||
}
|
}
|
||||||
|
|
||||||
g.fillRect(dst.x*space+2+sSpace2, dst.y*space+2+sSpace2, space -3 - 2* sSpace2, space -3 - 2* sSpace2);
|
g.fillRect(dst.x*space+2+sSpace2, dst.y*space+2+sSpace2, space -3 - 2* sSpace2, space -3 - 2* sSpace2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,9 @@ public class Game {
|
|||||||
// Private members
|
// Private members
|
||||||
private int[][] field;
|
private int[][] field;
|
||||||
private ArrayList<Integer> nextBlocks;
|
private ArrayList<Integer> nextBlocks;
|
||||||
|
private ArrayList<Point> oldMoves;
|
||||||
|
private ArrayList<Point> lastNewBlocks;
|
||||||
|
private ArrayList<Integer> numOflastNewBlocks;
|
||||||
private int level;
|
private int level;
|
||||||
private int score;
|
private int score;
|
||||||
private int size;
|
private int size;
|
||||||
@@ -85,7 +88,7 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean canMove(Point src, Point dst){
|
public boolean canMove(Point src, Point dst){
|
||||||
return getPath(src, dst)!=null;
|
return getPath(src, dst) != null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,14 +132,17 @@ public class Game {
|
|||||||
* @return True if a move from src to dst is possible.
|
* @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)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!canMove(src, dst)) {
|
if(!canMove(src, dst)){
|
||||||
return false; // checking if there is a path from src to dest
|
return false; // checking if there is a path from src to dst
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldMoves.add(src); // add src to the oldMove Stack
|
||||||
|
oldMoves.add(dst); // add dst to the oldMove Stack
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@@ -163,13 +169,13 @@ public class Game {
|
|||||||
for(int i= 0; i < size; i++){
|
for(int i= 0; i < size; i++){
|
||||||
for(int j = 0; j < size; j++){
|
for(int j = 0; j < size; j++){
|
||||||
if(field[i][j] == 0 && (src.x!=i || src.y!=j)){ //field empty and not src
|
if(field[i][j] == 0 && (src.x!=i || src.y!=j)){ //field empty and not src
|
||||||
vertices.add(new Vertex(Integer.MAX_VALUE, new Point(i, j)));
|
vertices.add(new Vertex(Integer.MAX_VALUE, new Point(i, j)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@@ -203,8 +209,34 @@ public class Game {
|
|||||||
return reconstructShortestPath(allVerticies, src,dst);
|
return reconstructShortestPath(allVerticies, src,dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undo the last move if there are enough available undos.
|
||||||
|
*
|
||||||
|
* @author aaron
|
||||||
|
* @return True if undo was possible.
|
||||||
|
*/
|
||||||
public boolean doUndo(){
|
public boolean doUndo(){
|
||||||
return false;
|
if(getAvailUndo() > 0 && oldMoves.size() > 0){
|
||||||
|
Point dst = oldMoves.remove(oldMoves.size() - 1); // pops the last dst
|
||||||
|
Point src = oldMoves.remove(oldMoves.size() - 1); // pops the last src
|
||||||
|
|
||||||
|
field[src.x][src.y] = field[dst.x][dst.y]; // Undo the last move
|
||||||
|
field[dst.x][dst.y] = 0; // reset the undone dst to zero
|
||||||
|
|
||||||
|
// get the number of blocks which got added last time and remove them
|
||||||
|
for(int n = numOflastNewBlocks.remove(numOflastNewBlocks.size() - 1); n > 0; n--){
|
||||||
|
Point tmp = lastNewBlocks.remove(lastNewBlocks.size() - 1);
|
||||||
|
field[tmp.x][tmp.y] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
numUndos--;
|
||||||
|
|
||||||
|
emitUpdateEvent();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -225,13 +257,14 @@ public class Game {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldMoves.add(src); // add src to the oldMove Stack
|
||||||
|
oldMoves.add(dst); // add dst to the oldMove Stack
|
||||||
|
|
||||||
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--;
|
freeMoves--;
|
||||||
|
|
||||||
nextStep(dst); //cleanup rows or add new blocks
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,8 +277,11 @@ public class Game {
|
|||||||
this.size = size;
|
this.size = size;
|
||||||
this.freeBlocks = size * size;
|
this.freeBlocks = size * size;
|
||||||
|
|
||||||
// Initialize new blocks
|
// Initialize new blocks and oldMove list
|
||||||
|
oldMoves = new ArrayList<Point>();
|
||||||
nextBlocks = new ArrayList<Integer>();
|
nextBlocks = new ArrayList<Integer>();
|
||||||
|
lastNewBlocks = new ArrayList<Point>();
|
||||||
|
numOflastNewBlocks = new ArrayList<Integer>();
|
||||||
nextBlocks.add(1);
|
nextBlocks.add(1);
|
||||||
nextBlocks.add(2);
|
nextBlocks.add(2);
|
||||||
nextBlocks.add(3);
|
nextBlocks.add(3);
|
||||||
@@ -254,8 +290,8 @@ public class Game {
|
|||||||
field = new int[size][size];
|
field = new int[size][size];
|
||||||
level = 1;
|
level = 1;
|
||||||
score = 0;
|
score = 0;
|
||||||
numUndos = 0;
|
numUndos = 100;
|
||||||
freeMoves = 0;
|
freeMoves = 100;
|
||||||
linesLeft=linesPerLevel;
|
linesLeft=linesPerLevel;
|
||||||
|
|
||||||
// Populate game field
|
// Populate game field
|
||||||
@@ -448,6 +484,8 @@ public class Game {
|
|||||||
*/
|
*/
|
||||||
private void populateField(){
|
private void populateField(){
|
||||||
|
|
||||||
|
numOflastNewBlocks.add(nextBlocks.size()); // add the numbers of blocks to add to the list
|
||||||
|
|
||||||
// while there are blocks left in nextBlocks
|
// while there are blocks left in nextBlocks
|
||||||
while((nextBlocks.size() > 0) && (freeBlocks > 0)){
|
while((nextBlocks.size() > 0) && (freeBlocks > 0)){
|
||||||
int x = rand.nextInt(size); // get random x position
|
int x = rand.nextInt(size); // get random x position
|
||||||
@@ -455,6 +493,7 @@ public class Game {
|
|||||||
|
|
||||||
// if the position is free
|
// if the position is free
|
||||||
if(field[x][y] == 0){
|
if(field[x][y] == 0){
|
||||||
|
lastNewBlocks.add(new Point(x,y)); // add the new block to the lastNewBlocks list
|
||||||
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));
|
checkRemoveBlocks(new Point(x,y));
|
||||||
|
|||||||
@@ -147,7 +147,5 @@ public class Window extends JFrame implements ActionListener{
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Window("7 not 7");
|
new Window("7 not 7");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user