the undo button works

This commit is contained in:
edwin 2025-05-16 11:24:07 +02:00
parent 31fbc1debd
commit beb7f07d84
1 changed files with 69 additions and 3 deletions

View File

@ -157,7 +157,8 @@ public class Board {
}
else {
outputString+= "1";
}
}
outputString += "\n";
return outputString;
}
@ -382,6 +383,15 @@ public class Board {
else {
enPassant = true;
}
enPassant = false;
kingWMoved = false;
kingBMoved = false;
rookLWMoved = false;
rookRWMoved = false;
rookLBMoved = false;
rookRBMoved = false;
castling = false;
castlingDone = false;
}
/* The following methods require more work ! */
@ -753,8 +763,64 @@ public class Board {
}
public void undoLastMove() {
//TODO
if(turnNumber == 0) {
String lastBoard = previousBoard.get(turnNumber); // we have a String but we need a String[] in order to use the constructer
String[] oldBoard = new String[10];
boolean complete = false;
int indexLine=0;
int indexColumn=0;
for(int i=0; i<10; i++) {
oldBoard[i] = "";
}
while (complete == false) {
if (lastBoard.charAt(indexColumn) == '\n') {
indexLine+=1;
if (indexLine == 10) {
complete= true;
}
}
else {
oldBoard[indexLine] += lastBoard.charAt(indexColumn);
}
indexColumn+=1;
}
Board undo = new Board(oldBoard);
this.pieces = undo.pieces;
this.xTwo=undo.xTwo;
this.yTwo=undo.yTwo;
this.enPassant=undo.enPassant;
this.lastTurnPawnTwo=undo.lastTurnPawnTwo;
}
if (turnNumber != 0 ) {
turnNumber -= 1;
String lastBoard = previousBoard.get(turnNumber); // we have a String but we need a String[] in order to use the constructer
String[] oldBoard = new String[10];
boolean complete = false;
int indexLine=0;
int indexColumn=0;
for(int i=0; i<10; i++) {
oldBoard[i] = "";
}
while (complete == false) {
if (lastBoard.charAt(indexColumn) == '\n') {
indexLine+=1;
if (indexLine == 10) {
complete= true;
}
}
else {
oldBoard[indexLine] += lastBoard.charAt(indexColumn);
}
indexColumn+=1;
}
Board undo = new Board(oldBoard);
this.pieces = undo.pieces;
this.xTwo=undo.xTwo;
this.yTwo=undo.yTwo;
this.enPassant=undo.enPassant;
this.lastTurnPawnTwo=undo.lastTurnPawnTwo;
previousBoard.remove(turnNumber+1);
}
}