Castling my leugueu

This commit is contained in:
lrave 2025-05-13 20:13:48 +02:00
parent 60ac37cd2f
commit 4c6d6e3af3
3 changed files with 65 additions and 1 deletions

View File

@ -187,16 +187,41 @@ public class Board {
if (valid) {
Piece pieceToMove = board[selectedX][selectedY];
if(pieceToMove.getType() == PieceType.King && Math.abs(x - selectedX)==2) {
y = selectedY;
if (x == 6) {
System.out.println("pipi au caca");
board[5][y] = board[7][y];
board[7][y] = null;
board[5][y].setMoved(true);
board[5][y].setX(5);
board[5][y].setY(y);
} else if (x == 2) {
System.out.println("pipi");
//System.out.println(board [3][y]);
//System.out.println(board [0][y]);
board [3][y] = board[0][y];
board [0][y] = null;
//System.out.println(board [3][y]);
//System.out.println(board [0][y]);
board[3][y].setMoved(true);
board[3][y].setX(3);
board[3][y].setY(y);
}
}
board[x][y] = new Piece(x, y, pieceToMove.getType(), pieceToMove.isWhite());
board[selectedX][selectedY] = null;
board[x][y].setMoved(true);
incrementTurn();
}
// reset selection
selectedX = -1;
selectedY = -1;
highlightedPositions.clear();
clearConsole();
//clearConsole();
System.out.println(toString());
}
}

View File

@ -7,5 +7,6 @@ import java.util.Optional;
* the moving piece, and an optional captured piece.
*/
public class Move {
}

View File

@ -31,6 +31,14 @@ public class Piece {
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public PieceType getType() {
@ -115,6 +123,12 @@ public class Piece {
}
if (!this.getHasMoved()) {
int yRow = this.isWhite() ? 7: 0; // if white, row = 7 else row = row = 0 (mapping of boolean 0,1 --> 7,0 rows)
if(canCastle(board, x, yRow, 7)) {
moves.add(new int[] {6, yRow});
}
if(canCastle(board, x, yRow, 0)) {
moves.add(new int[] {2, yRow});
}
}
break;
@ -140,6 +154,30 @@ public class Piece {
return moves;
}
private boolean canCastle(Board board, int kingX, int y, int rookX) {
if (rookX == 7) { // identify if castle on king-side
if (board.getPiece(rookX, y) != null && board.getPiece(rookX, y).getType() == PieceType.Rook && !board.getPiece(rookX, y).getHasMoved()) {
if (board.getPiece(5, y) == null && board.getPiece(6, y) == null) {
return true;
}
else {
return false;
}
}
}
else if (rookX == 0) {
if (board.getPiece(rookX, y) != null && board.getPiece(rookX, y).getType() == PieceType.Rook && !board.getPiece(rookX, y).getHasMoved()) {
if (board.getPiece(1, y) == null && board.getPiece(2, y) == null && board.getPiece(3, y) == null) {
return true;
}
else {
return false;
}
}
}
return false;
}
private void addLinearMoves(Board board, ArrayList<int[]> moves, int x, int y, int dx, int dy) {
int nx = x + dx;
int ny = y + dy;