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:
parent
42d91fa63d
commit
e63064501b
|
|
@ -150,6 +150,8 @@ public class Board {
|
|||
pieces.removeIf(p -> p.getX() == x && p.getY() == y);
|
||||
selectedPiece.setX(x);
|
||||
selectedPiece.setY(y);
|
||||
|
||||
selectedPiece.moveNumberAdd(); // added this to count the turn number
|
||||
|
||||
turnNumber++;
|
||||
isWhiteTurn = !isWhiteTurn;
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public class Piece {
|
|||
return color;
|
||||
}
|
||||
|
||||
public int getmoveNumber() {
|
||||
public int getMoveNumber() {
|
||||
return moveNumber;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,31 +5,6 @@ public class chessRulesCheck {
|
|||
//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){
|
||||
for (Piece p : board.getPieces()) {
|
||||
|
|
@ -42,7 +17,35 @@ public class chessRulesCheck {
|
|||
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 kx = 4;
|
||||
int rx = kingSide ? 7 : 0;
|
||||
|
|
@ -56,18 +59,45 @@ public class chessRulesCheck {
|
|||
|
||||
}
|
||||
|
||||
public boolean hasKOrNMoved (Board board, boolean isWhite, boolean kingSide) { //castling specific
|
||||
Piece king = null, rook = null;
|
||||
for (Piece p : board.getPieces()) {
|
||||
if (p.getType() == PieceType.King && p.isWhite() == isWhite) king = p;
|
||||
if (p.getType() == PieceType.Rook && p.isWhite() == isWhite) {
|
||||
if (kingSide && p.getX() == 7) rook = p;
|
||||
if (!kingSide && p.getX() == 0) rook = p;
|
||||
}
|
||||
}
|
||||
if (king == null || rook == null) return true;
|
||||
return king.hasMoved() || rook.hasMoved();
|
||||
|
||||
public boolean hasItMoved(Piece p) {
|
||||
return p.getMoveNumber() > 0;
|
||||
}
|
||||
|
||||
public boolean canItCastle ( Board board, boolean isWhite, boolean kingSide ) {
|
||||
Piece king = null, rook = null;
|
||||
for (Piece p : board.getPieces()) {
|
||||
if (p.getType() == PieceType.King && p.isWhite() == isWhite) {
|
||||
king = p;
|
||||
}
|
||||
if (p.getType() == PieceType.Rook && p.isWhite() == isWhite) {
|
||||
if (kingSide && p.getX() == 7) rook = p;
|
||||
if (!kingSide && p.getX() == 0) rook = p;
|
||||
}
|
||||
}
|
||||
if (king == null || rook == null)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue