"castling done for white pawns with kind and queen side + enable if rook

or kind already moved"
This commit is contained in:
martinbst 2025-04-17 17:14:01 +02:00
parent b4849591d0
commit 48631a1d96
3 changed files with 53 additions and 10 deletions

View File

@ -125,7 +125,37 @@ public class Board {
if (select) {
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
Piece pieceToMove = this.board.get(this.ym).get(this.xm);
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
//check for castling mode:
if (isCastlingMove) {
//kingside
if (x > xm) {
Piece rook = board.get(ym).get(7);
board.get(ym).set(5, rook);
board.get(ym).set(7, null);
if (rook != null) {
rook.x = 5;
rook.setMoved(true);
}
}
if (x < xm) {
Piece rook = board.get(ym).get(0);
board.get(ym).set(3, rook);
board.get(ym).set(0, null);
if (rook != null) {
rook.x = 3;
rook.setMoved(true);
}
}
}
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
Piece movedPiece = this.getPiece(x, y);
if (movedPiece != null) {
movedPiece.setMoved(true);
}
board.get(this.ym).set(this.xm,null);
}
}

View File

@ -45,14 +45,23 @@ public class King extends Piece {
}
}
//kingside method
if (x+3 < 8) {
Piece Rook = board.get(y).get(x+3);
if (Rook != null && Rook.getType()== PieceType.Rook && !Rook.movePiece() && Rook.isWhite()== this.isWhite) {
if(board.get(y).get(x+1) == null && board.get(y).get(x+2) == null) {
moves.get(y).set(x+2,true);
if (!this.hasMoved() && x + 3 < 8) {
Piece rook = board.get(y).get(x +3);
if (rook != null && rook.getType() == PieceType.Rook && !rook.hasMoved() && rook.isWhite() == this.isWhite) {
if (board.get(y).get(x + 1) == null && board.get(y).get(x + 2) == null) {
moves.get(y).set(x + 2, true);
}
}
}
}
//queensside method
if (!this.hasMoved() && x - 4 >= 0) {
Piece rook = board.get(y).get(x -4);
if (rook != null && rook.getType() == PieceType.Rook && !rook.hasMoved() && rook.isWhite() == this.isWhite) {
if (board.get(y).get(x - 1) == null && board.get(y).get(x - 2) == null && board.get(y).get(x - 3) == null) {
moves.get(y).set(x - 2, true);
}
}
}
return moves;
}
}

View File

@ -36,9 +36,13 @@ public abstract class Piece {
public String toString() {
return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]";
}
public boolean movePiece() {
boolean moved = false;
return moved;
private boolean hasMoved = false;
public boolean hasMoved() {
return hasMoved;
}
public void setMoved(boolean moved) {
this.hasMoved = moved;
}
}