Compare commits

...

2 Commits

Author SHA1 Message Date
mimie 9f2106ba2d Merge branch 'master' of https://gitarero.ecam.fr/aurele.wittke/OOP_Groupe_1A5_Project.git 2025-05-06 16:32:53 +02:00
mimie 0cf7e16c45 en passant OK 2025-05-06 16:32:38 +02:00
1 changed files with 40 additions and 0 deletions

View File

@ -11,6 +11,10 @@ public class Board {
private int x;
private int y;
private int turnNumber;
private boolean lastTurnPawnTwo = false;//for en passant
private int xTwo;
private int yTwo;
private boolean enPassant = false;
public Board(int colNum, int lineNum) {
this.colNum = colNum;
@ -162,9 +166,25 @@ public class Board {
}
}
if(((this.positionOccupied(x, y) == false || pieces.get(whatPiece(x,y)).isWhite() != pieces.get(whatPiece(this.x,this.y)).isWhite())) && (highlights)) {//moves the piece only if the position selected is not occupied by a piece of the same color
if (enPassant) {
this.pieces.remove(whatPiece(xTwo,yTwo));
enPassant = false;
}
int indexPiece = this.whatPiece(this.x, this.y);
if (pieces.get(indexPiece).getType() == PieceType.Pawn && Math.abs(this.y-y)==2) {//save the number of the turn at which a pawn moved by two
lastTurnPawnTwo = true;
xTwo = x;
yTwo = y;
}
else {
lastTurnPawnTwo = false;
}
pieces.get(indexPiece).setX(x);
pieces.get(indexPiece).setY(y);
this.turnNumber +=1;
}
@ -388,13 +408,24 @@ public class Board {
boolean highlight = false;
if (positionOccupied(this.x,this.y)) { //check if we have selected a position with a piece
int indexPieceSelect = whatPiece(this.x,this.y);
if (pieces.get(indexPieceSelect).getType() == PieceType.Pawn) {//highlight for pawns
if (pieces.get(indexPieceSelect).isWhite()) {//white pawns
if (lastTurnPawnTwo) {//en passant
if(yTwo == this.y && Math.abs(xTwo - this.x) == 1 && x == xTwo && y == this.y - 1) {
highlight = true;
enPassant = true;
}
}
if(y == this.y-1) {
if((x == this.x) && (positionOccupied(x,y)==false)) {//advance one case
highlight = true;
}
if((Math.abs(x-this.x) == 1) && (positionOccupied(x,y)) && pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) {//highlight the piece in diagonal, if existing and of another color
highlight = true;
}
@ -406,9 +437,18 @@ public class Board {
if (pieces.get(indexPieceSelect).isWhite() == false) {//black pawns
if(y == this.y+1) {
if (lastTurnPawnTwo) {//en passant
if(yTwo == this.y && Math.abs(xTwo - this.x) == 1 && x == xTwo && y == this.y+1) {
highlight = true;
enPassant = true;
}
}
if((x == this.x) && (positionOccupied(x,y)==false)) {
highlight = true;
}
if((Math.abs(x-this.x) == 1) && (positionOccupied(x,y)) && pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) {
highlight = true;
}