Merge branch 'master' of

https://gitarero.ecam.fr/lucie.gauthier/OOP_3B2_Project.git
This commit is contained in:
aelga 2025-05-14 16:16:48 +02:00
commit 4b05c6bdc7
1 changed files with 94 additions and 67 deletions

View File

@ -31,25 +31,7 @@ public class Board {
this.colNum = colNum; this.colNum = colNum;
this.lineNum = lineNum; this.lineNum = lineNum;
this.board = new Piece[lineNum][colNum]; this.board = new Piece[lineNum][colNum];
this.x = -1; initialise();
this.y = -1;
turns = 0;
whiteTurn = true;
enPassant = false;
pawnX = -1;
pawnY = -1;
enPassantX = -1;
enPassantY = -1;
whiteRookLeft = false;
whiteRookRight = false;
blackRookLeft = false;
blackRookRight = false;
whiteKing = false;
blackKing = false;
castlingRight = false;
castlingLeft = false;
states = new ArrayList<>();
} }
// GETTERS // GETTERS
@ -69,10 +51,29 @@ public class Board {
return whiteTurn; return whiteTurn;
} }
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
for (int i = 0; i < colNum; i++) {
for (int j = 0; j < lineNum; j++) {
if (board[j][i] != null) {
pieces.add(board[j][i]);
}
}
}
return pieces;
}
public Piece getPieceAt(int x, int y) {
if (inBoard(x,y)) {
return board[y][x];
}
return null;
}
// INITIALISE THE BOARD // INITIALISE THE BOARD
public void initialise() { public void initialise() {
x = -1; this.x = -1;
y = -1; this.y = -1;
turns = 0; turns = 0;
whiteTurn = true; whiteTurn = true;
@ -148,40 +149,29 @@ public class Board {
} }
str += "\n"; str += "\n";
} }
String turn = "B"; String end = "B";
if (isTurnWhite()) { if (isTurnWhite()) {
turn = "W"; end = "W";
} }
str += turn; Boolean[] booleans = {enPassant, whiteRookLeft, whiteRookRight, blackRookLeft, blackRookRight, whiteKing, blackKing};
for (int k = 0; k < 7; k++) {
end += Boolean.toString(booleans[k]).charAt(0);
}
int[] integers = {pawnX, pawnY, enPassantX, enPassantY};
for (int n = 0; n < 4; n++) {
end += Integer.toString(integers[n]).charAt(0);
}
str += end;
return str; return str;
} }
// LIST THE PIECES OF THE BOARD
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
for (int i = 0; i < colNum; i++) {
for (int j = 0; j < lineNum; j++) {
if (board[j][i] != null) {
pieces.add(board[j][i]);
}
}
}
return pieces;
}
public boolean inBoard(int x, int y) { public boolean inBoard(int x, int y) {
if (x > -1 && x < colNum && y > -1 && y < lineNum) { if (x > -1 && x < colNum && y > -1 && y < lineNum) {
return true; return true;
} }
return false; return false;
} }
public Piece getPieceAt(int x, int y) {
if (inBoard(x,y)) {
return board[y][x];
}
return null;
}
// PROCESS THE USER CLICK // PROCESS THE USER CLICK
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
@ -203,16 +193,18 @@ public class Board {
this.y = -1; this.y = -1;
} }
// the new location is highlighted, meaning it is a legal displacement // the new location is highlighted, meaning it is a legal displacement
else if (isHighlighted(x,y)) { else if (isHighlighted(x,y)) {
Piece piece = board[this.y][this.x];
// handling en passant // handling en passant
if (this.enPassant == true && board[this.y][this.x].getType() == PieceType.Pawn && x == enPassantX && y == enPassantY) { if (this.enPassant == true && piece.getType() == PieceType.Pawn && x == enPassantX && y == enPassantY) {
board[pawnY][pawnX] = null; board[pawnY][pawnX] = null;
playMove(move); playMove(move);
this.states.add(toString());
} }
// castling to the left // castling to the left
else if (this.castlingLeft && x == this.x-2) { else if (this.castlingLeft && x == this.x-2) {
if (board[this.y][this.x].isWhite()) { if (piece.isWhite()) {
// rook displacement (white left) // rook displacement (white left)
playMove(new Move(0,7,x+1,y)); playMove(new Move(0,7,x+1,y));
} }
@ -222,11 +214,12 @@ public class Board {
} }
// king displacement // king displacement
playMove(move); playMove(move);
this.states.add(toString());
} }
// castling to the right // castling to the right
else if (this.castlingRight && x == this.x+2) { else if (this.castlingRight && x == this.x+2) {
if (board[this.y][this.x].isWhite()) { if (piece.isWhite()) {
// rook displacement (white right) // rook displacement (white right)
playMove(new Move(7,7,x-1,y)); playMove(new Move(7,7,x-1,y));
} }
@ -236,50 +229,54 @@ public class Board {
} }
// king displacement // king displacement
playMove(move); playMove(move);
this.states.add(toString());
} }
// basic legal move // basic legal move
else { else {
// verify if the pawn goes two steps forward (to know if en passant will be possible for the opponent) // verify if the pawn goes two steps forward (to know if en passant will be possible for the opponent)
if (board[this.y][this.x].getType() == PieceType.Pawn && (y == this.y+2 || y == this.y-2)) { if (piece.getType() == PieceType.Pawn && (y == this.y+2 || y == this.y-2)) {
// sets that en passant can be done next turn
enPassant = true; enPassant = true;
// coordinates of the pawn that can be taken
pawnX = x; pawnX = x;
pawnY = y; pawnY = y;
} }
else { else {
enPassant = false; enPassant = false;
pawnX = -1; pawnX = -1;
pawnX = -1; pawnY = -1;
} }
// check if rooks or kings are beeing moved to enable (or not) castling later // check if rooks or kings are beeing moved to enable (or not) castling later
if (board[this.y][this.x].getType() == PieceType.Rook) { playMove(move);
this.states.add(toString());
if (piece.getType() == PieceType.Rook) {
if (this.x == 0) { if (this.x == 0) {
if (board[this.y][this.x].isWhite() && whiteRookLeft == false) { if (piece.isWhite()) {
whiteRookLeft = true; whiteRookLeft = true;
} }
else if (board[this.y][this.x].isWhite() == false && blackRookLeft == false) { else {
blackRookLeft = true; blackRookLeft = true;
} }
} }
else if (this.x == 7) { else if (this.x == 7) {
if (board[this.y][this.x].isWhite() && whiteRookRight == false) { if (piece.isWhite()) {
whiteRookRight = true; whiteRookRight = true;
} }
else if (board[this.y][this.x].isWhite() == false && blackRookRight == false) { else {
blackRookRight = true; blackRookRight = true;
} }
} }
} }
else if (board[this.y][this.x].getType() == PieceType.King) { else if (piece.getType() == PieceType.King) {
if (board[this.y][this.x].isWhite()) { if (piece.isWhite()) {
whiteKing = true; whiteKing = true;
} }
else if (board[this.y][this.x].isWhite() == false) { else {
blackKing = true; blackKing = true;
} }
} }
playMove(move);
} }
turns += 1; turns += 1;
this.x = -1; this.x = -1;
@ -328,12 +325,10 @@ public class Board {
} }
} }
} }
if (array[lineNum] == "B") { whiteTurn = true;
if (array[lineNum].charAt(0) == 'B') {
whiteTurn = false; whiteTurn = false;
} }
else {
whiteTurn = true;
}
} }
@ -382,12 +377,44 @@ public class Board {
Board newBoard = new Board(undo); Board newBoard = new Board(undo);
this.board = newBoard.board; this.board = newBoard.board;
if (undo[lineNum] == "B" || turns == 0) { if (turns != 0) {
whiteTurn = true; whiteTurn = !whiteTurn;
} }
else { else {
whiteTurn = false; whiteTurn = true;
} }
Boolean[] booleans = new Boolean[7];
for (int k = 1; k < 8; k++) {
if (undo[lineNum].charAt(k) == 'f') {
booleans[k-1] = false;
}
else {
booleans[k-1] = true;
}
}
int[] integers = new int[4];
for (int n = 8; n < 12; n++) {
if (undo[lineNum].charAt(n) == '-') {
integers[n-8] = -1;
}
else {
integers[n-8] = Character.getNumericValue(undo[lineNum].charAt(n));
}
}
this.enPassant = booleans[0];
this.whiteRookLeft = booleans[1];
this.whiteRookRight = booleans[2];
this.blackRookLeft = booleans[3];
this.blackRookRight = booleans[4];
this.whiteKing = booleans[5];
this.blackKing = booleans[6];
this.pawnX = integers[0];
this.pawnY = integers[1];
this.enPassantX = integers[2];
this.enPassantY = integers[3];
} }
} }
@ -415,7 +442,7 @@ public class Board {
canPromote(x, y); canPromote(x, y);
} }
} }
this.states.add(toString()); //this.states.add(toString());
} }
ArrayList<Piece> promoPieces= new ArrayList<>(); ArrayList<Piece> promoPieces= new ArrayList<>();