finished chessRules that are somewhat global and necessary for castling.

FInished the isCastling possible check. Need to implment the actual
castling now and update the highlight  so that it hsows the castlign
possibilty.
Also added line 154 in board class the fact that it increments the move
turn counter when a piece is set down.
This commit is contained in:
Juliette 2025-05-21 21:33:16 +02:00
parent 42d91fa63d
commit e63064501b
3 changed files with 72 additions and 40 deletions

View File

@ -151,6 +151,8 @@ public class Board {
selectedPiece.setX(x); selectedPiece.setX(x);
selectedPiece.setY(y); selectedPiece.setY(y);
selectedPiece.moveNumberAdd(); // added this to count the turn number
turnNumber++; turnNumber++;
isWhiteTurn = !isWhiteTurn; isWhiteTurn = !isWhiteTurn;
} }

View File

@ -45,7 +45,7 @@ public class Piece {
return color; return color;
} }
public int getmoveNumber() { public int getMoveNumber() {
return moveNumber; return moveNumber;
} }

View File

@ -5,31 +5,6 @@ public class chessRulesCheck {
//The basic rules needed for CASTLING!!!!!!!!! useful for the rest of course ;) //The basic rules needed for CASTLING!!!!!!!!! useful for the rest of course ;)
public boolean isInCheck (Board board, boolean isWhite){ //is under attack + is king
int kx = -1, ky = -1;
for (Piece p : board.getPieces()) {
if (p.getType() == PieceType.King && p.isWhite() == isWhite) {
kx = p.getX();
ky = p.getY();
break;
}
}
if (kx < 0)
return false;
for (Piece p : board.getPieces()) {
if (p.isWhite() == isWhite) continue;
for (Board.Position pos : board.findMoves(p)) {
if (pos.x == kx && pos.y == ky)
return true;
}
}
return false;
}
public boolean isThreatened (Board board, int x, int y, boolean byWhite){ public boolean isThreatened (Board board, int x, int y, boolean byWhite){
for (Piece p : board.getPieces()) { for (Piece p : board.getPieces()) {
@ -42,7 +17,35 @@ public class chessRulesCheck {
return false; return false;
} }
public boolean nothingInBetween (Board board, boolean isWhite, boolean kingSide) { public boolean isGoingToBeThreatenedIfItGoesThere (Board board, Piece piece, int toX, int toY ) {
int fromX = piece.getX();
int fromY = piece.getY();
Piece target = board.getPieceAt(toX, toY);
if (target != null) {
board.getPieces().remove(target);
}
piece.setX(toX);
piece.setY(toY);
boolean threatened = isThreatened(board, toX, toY, !piece.isWhite());
piece.setX(fromX);
piece.setY(fromY);
if (target != null) {
board.getPieces().add(target);
}
return threatened;
}
public boolean nothingInBetweenKorN (Board board, boolean isWhite, boolean kingSide) { //castling specific
int rank = isWhite ? 7 : 0; int rank = isWhite ? 7 : 0;
int kx = 4; int kx = 4;
int rx = kingSide ? 7 : 0; int rx = kingSide ? 7 : 0;
@ -56,18 +59,45 @@ public class chessRulesCheck {
} }
public boolean hasKOrNMoved (Board board, boolean isWhite, boolean kingSide) { //castling specific public boolean hasItMoved(Piece p) {
return p.getMoveNumber() > 0;
}
public boolean canItCastle ( Board board, boolean isWhite, boolean kingSide ) {
Piece king = null, rook = null; Piece king = null, rook = null;
for (Piece p : board.getPieces()) { for (Piece p : board.getPieces()) {
if (p.getType() == PieceType.King && p.isWhite() == isWhite) king = p; if (p.getType() == PieceType.King && p.isWhite() == isWhite) {
king = p;
}
if (p.getType() == PieceType.Rook && p.isWhite() == isWhite) { if (p.getType() == PieceType.Rook && p.isWhite() == isWhite) {
if (kingSide && p.getX() == 7) rook = p; if (kingSide && p.getX() == 7) rook = p;
if (!kingSide && p.getX() == 0) rook = p; if (!kingSide && p.getX() == 0) rook = p;
} }
} }
if (king == null || rook == null) return true; if (king == null || rook == null)
return king.hasMoved() || rook.hasMoved(); return false;
if (hasItMoved(king) || hasItMoved(rook))
return false;
int kx = king.getX(), ky = king.getY();
if (isThreatened(board, kx, ky, !isWhite))
return false;
if (!nothingInBetweenKorN(board, isWhite, kingSide))
return false;
int dir = kingSide ? +1 : -1;
for (int i = 1; i <= 2; i++) {
int tx = kx + dir * i;
int ty = ky;
if (isGoingToBeThreatenedIfItGoesThere(board, king, tx, ty)) {
return false;
}
} }
return true;
} }
}