From e003f63b4b2081fc0694549a45c7ff85eafac6e1 Mon Sep 17 00:00:00 2001 From: edwin Date: Sat, 17 May 2025 21:55:01 +0200 Subject: [PATCH] Undo works for the castling, when you save you can use undo up to the saved move. Merge branch 'master' of https://gitarero.ecam.fr/aurele.wittke/OOP_1A5_Project --- src/backend/Board.java | 210 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 184 insertions(+), 26 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index bf1a724..82f497c 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -160,6 +160,55 @@ public class Board { else { outputString+= "1"; } + outputString += "\n"; + if (this.kingWMoved == false) { + outputString+="0"; + } + else { + outputString+= "1"; + } + if (this.rookLWMoved == false) { + outputString+="0"; + } + else { + outputString+= "1"; + } + if (this.rookRWMoved == false) { + outputString+="0"; + } + else { + outputString+= "1"; + } + if (this.kingBMoved == false) { + outputString+="0"; + } + else { + outputString+= "1"; + } + if (this.rookLBMoved == false) { + outputString+="0"; + } + else { + outputString+= "1"; + } + if (this.rookRBMoved == false) { + outputString+="0"; + } + else { + outputString+= "1"; + } + if (this.castling == false) { + outputString+="0"; + } + else { + outputString+= "1"; + } + if (this.castlingDone == false) { + outputString+="0"; + } + else { + outputString+= "1"; + } outputString += "\n"; return outputString; } @@ -306,12 +355,13 @@ public class Board { selection = true; } return selection; + } /* saving-loading feature :*/ public String[] toFileRep() { - String[] saveFile = new String[10]; // creation of the string array which will be the one returned + String[] saveFile = new String[11]; // creation of the string array which will be the one returned String actualBoard = this.toString(); int nbLine = 0; saveFile[0] = ""; @@ -327,6 +377,7 @@ public class Board { saveFile[8]=""; saveFile[8] += this.turnNumber; saveFile[9]=""; + saveFile[10]=""; if (this.lastTurnPawnTwo == false) { saveFile[9]+= "0"; } @@ -341,6 +392,55 @@ public class Board { else { saveFile[9]+= "1"; } + if (this.kingWMoved == false) { + saveFile[10]+="0"; + } + else { + saveFile[10]+= "1"; + } + if (this.rookLWMoved == false) { + saveFile[10]+="0"; + } + else { + saveFile[10]+= "1"; + } + if (this.rookRWMoved == false) { + saveFile[10]+="0"; + } + else { + saveFile[10]+= "1"; + } + if (this.kingBMoved == false) { + saveFile[10]+="0"; + } + else { + saveFile[10]+= "1"; + } + if (this.rookLBMoved == false) { + saveFile[10]+="0"; + } + else { + saveFile[10]+= "1"; + } + if (this.rookRBMoved == false) { + saveFile[10]+="0"; + } + else { + saveFile[10]+= "1"; + } + if (this.castling == false) { + saveFile[10]+="0"; + } + else { + saveFile[10]+= "1"; + } + if (this.castlingDone == false) { + saveFile[10]+="0"; + } + else { + saveFile[10]+= "1"; + } + System.out.println(saveFile[10]); return saveFile; } @@ -349,6 +449,9 @@ public class Board { this.lineNum = 8; x = -1; y = -1; + previousBoard = new ArrayList<>(); + String originBoard = array[0] + "\n" + array[1] + "\n" +array[2] + "\n" +array[3] + "\n" + array[4] + "\n" + array[5] + "\n" +array[6] + "\n" +array[7] + "\n" +array[8] + "\n" + array[9] + "\n" +array[10] + "\n" ;; + previousBoard.add(originBoard); pieces = new ArrayList<>(); for(int i=0; i<8;i++) { // this will be the Y coordinate for(int j=0; j<8;j++) { // this will be the X coordinate @@ -368,10 +471,7 @@ public class Board { } } turnNumber = Integer.parseInt(array[8]); - System.out.println(turnNumber); - int turn = this.getTurnNumber(); - System.out.println(turn); - if(array[9].charAt(0)==0) { + if(array[9].charAt(0) == '0' ) { lastTurnPawnTwo = false; } else { @@ -379,21 +479,60 @@ public class Board { } xTwo = Character.getNumericValue(array[9].charAt(1)); yTwo = Character.getNumericValue(array[9].charAt(2)); - if(array[9].charAt(3)==0) { + if(array[9].charAt(3) == '0') { enPassant = false; } else { enPassant = true; } - enPassant = false; - kingWMoved = false; - kingBMoved = false; - rookLWMoved = false; - rookRWMoved = false; - rookLBMoved = false; - rookRBMoved = false; - castling = false; - castlingDone = false; + if (array[10].charAt(0) == '0') {//king white + this.kingWMoved = false; + } + else { + this.kingWMoved = true; + } + if (array[10].charAt(1) == '0') { //rook left white + this.rookLWMoved = false; + } + else { + this.rookLWMoved = true; + } + if (array[10].charAt(2) == '0') { // rook white right + this.rookRWMoved = false; + } + else { + this.rookRWMoved = true; + } + if (array[10].charAt(3) == '0') { // king black + this.kingBMoved = false; + } + else { + this.kingBMoved = true; + } + if (array[10].charAt(4) == '0') { // black rook left + this.rookLBMoved = false; + } + else { + this.rookLBMoved = true; + } + if (array[10].charAt(5) == '0') { // black rook right + this.rookRBMoved = false; + } + else { + this.rookRBMoved = true; + } + if (array[10].charAt(6) == '0') { // castling + this.castling = false; + } + else { + this.castling = true; + } + if (array[10].charAt(7) == '0') { // castling done + this.castlingDone = false; + } + else { + this.castlingDone = true; + } } /* The following methods require more work ! */ @@ -771,19 +910,22 @@ public class Board { } public void undoLastMove() { - 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]; + this.x = -1; + this.y = -1; + int sizeSave = previousBoard.size(); + if(sizeSave == 1) { + String lastBoard = previousBoard.get(0); // we have a String but we need a String[] in order to use the constructer + String[] oldBoard = new String[11]; boolean complete = false; int indexLine=0; int indexColumn=0; - for(int i=0; i<10; i++) { + for(int i=0; i<11; i++) { oldBoard[i] = ""; } while (complete == false) { if (lastBoard.charAt(indexColumn) == '\n') { indexLine+=1; - if (indexLine == 10) { + if (indexLine == 11) { complete= true; } } @@ -798,21 +940,29 @@ public class Board { this.yTwo=undo.yTwo; this.enPassant=undo.enPassant; this.lastTurnPawnTwo=undo.lastTurnPawnTwo; + this.kingBMoved=undo.kingBMoved; + this.kingWMoved=undo.kingWMoved; + this.rookLBMoved=undo.rookLBMoved; + this.rookRBMoved=undo.rookRBMoved; + this.rookLWMoved=undo.rookLWMoved; + this.rookRWMoved=undo.rookRWMoved; + this.castling=undo.castling; + this.castlingDone=undo.castlingDone; } - if (turnNumber != 0 ) { + if (sizeSave != 1 ) { 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]; + String lastBoard = previousBoard.get(sizeSave-2); // we have a String but we need a String[] in order to use the constructer + String[] oldBoard = new String[11]; boolean complete = false; int indexLine=0; int indexColumn=0; - for(int i=0; i<10; i++) { + for(int i=0; i<11; i++) { oldBoard[i] = ""; } while (complete == false) { if (lastBoard.charAt(indexColumn) == '\n') { indexLine+=1; - if (indexLine == 10) { + if (indexLine == 11) { complete= true; } } @@ -827,7 +977,15 @@ public class Board { this.yTwo=undo.yTwo; this.enPassant=undo.enPassant; this.lastTurnPawnTwo=undo.lastTurnPawnTwo; - previousBoard.remove(turnNumber+1); + this.kingBMoved=undo.kingBMoved; + this.kingWMoved=undo.kingWMoved; + this.rookLBMoved=undo.rookLBMoved; + this.rookRBMoved=undo.rookRBMoved; + this.rookLWMoved=undo.rookLWMoved; + this.rookRWMoved=undo.rookRWMoved; + this.castling=undo.castling; + this.castlingDone=undo.castlingDone; + previousBoard.remove(sizeSave-1); } }