check and checkmate dynamics, first steps
This commit is contained in:
parent
a3662f4561
commit
387a9c50b7
|
|
@ -186,6 +186,13 @@ public class Board {
|
||||||
if (isKingInCheck(false)) {
|
if (isKingInCheck(false)) {
|
||||||
System.out.println("Black is in check!");
|
System.out.println("Black is in check!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isCheckmate(true)) {
|
||||||
|
System.out.println("White is in checkmate!");
|
||||||
|
}
|
||||||
|
if (isCheckmate(false)) {
|
||||||
|
System.out.println("Black is in checkmate!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
public boolean isSelected(int x, int y) {
|
||||||
|
|
@ -239,8 +246,21 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Board(Board board) {
|
public Board(Board board) {
|
||||||
//TODO
|
this.colNum = board.colNum;
|
||||||
|
this.lineNum = board.lineNum;
|
||||||
|
this.turnNumber = board.turnNumber;
|
||||||
|
this.isWhiteTurn = board.isWhiteTurn;
|
||||||
|
|
||||||
|
this.pieces = new ArrayList<>();
|
||||||
|
for (int i = 0; i < board.pieces.size(); i++) {
|
||||||
|
Piece original = board.pieces.get(i);
|
||||||
|
Piece copy = new Piece(original.getX(), original.getY(), original.isWhite(), original.getType());
|
||||||
|
this.pieces.add(copy);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.selectedX = -1;
|
||||||
|
this.selectedY = -1;
|
||||||
|
this.highlightedSquares = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
|
|
@ -271,28 +291,82 @@ public class Board {
|
||||||
|
|
||||||
public boolean isKingInCheck(boolean whiteKing) {
|
public boolean isKingInCheck(boolean whiteKing) {
|
||||||
Piece king = null;
|
Piece king = null;
|
||||||
for (Piece p : pieces) {
|
|
||||||
|
for (int i = 0; i < pieces.size(); i++) {
|
||||||
|
Piece p = pieces.get(i);
|
||||||
if (p.getType() == PieceType.King && p.isWhite() == whiteKing) {
|
if (p.getType() == PieceType.King && p.isWhite() == whiteKing) {
|
||||||
king = p;
|
king = p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean inCheck = false;
|
||||||
|
|
||||||
|
if (king != null) {
|
||||||
int kingX = king.getX();
|
int kingX = king.getX();
|
||||||
int kingY = king.getY();
|
int kingY = king.getY();
|
||||||
|
for (int i = 0; i < pieces.size(); i++) {
|
||||||
for (Piece p : pieces) {
|
Piece p = pieces.get(i);
|
||||||
if (p.isWhite() != whiteKing) {
|
if (p.isWhite() != whiteKing) {
|
||||||
ArrayList<int[]> enemyMoves = getValidMoves(p);
|
ArrayList<int[]> moves = getValidMoves(p);
|
||||||
|
|
||||||
for (int[] move : enemyMoves) {
|
for (int j = 0; j < moves.size(); j++) {
|
||||||
|
int[] move = moves.get(j);
|
||||||
if (move[0] == kingX && move[1] == kingY) {
|
if (move[0] == kingX && move[1] == kingY) {
|
||||||
return true;
|
inCheck = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return inCheck;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCheckmate(boolean whiteKing) {
|
||||||
|
boolean kingInCheck = isKingInCheck(whiteKing);
|
||||||
|
boolean hasEscape = false;
|
||||||
|
|
||||||
|
if (kingInCheck) {
|
||||||
|
for (int i = 0; i < pieces.size(); i++) {
|
||||||
|
Piece piece = pieces.get(i);
|
||||||
|
|
||||||
|
if (piece.isWhite() == whiteKing) {
|
||||||
|
ArrayList<int[]> moves = getValidMoves(piece);
|
||||||
|
|
||||||
|
for (int j = 0; j < moves.size(); j++) {
|
||||||
|
int[] move = moves.get(j);
|
||||||
|
int newX = move[0];
|
||||||
|
int newY = move[1];
|
||||||
|
|
||||||
|
// Simulate the board
|
||||||
|
Board simBoard = new Board(this);
|
||||||
|
Piece simPiece = simBoard.getPieceAt(piece.getX(), piece.getY());
|
||||||
|
Piece captured = simBoard.getPieceAt(newX, newY);
|
||||||
|
|
||||||
|
if (captured != null) {
|
||||||
|
simBoard.getPieces().remove(captured);
|
||||||
|
}
|
||||||
|
|
||||||
|
simPiece.setX(newX);
|
||||||
|
simPiece.setY(newY);
|
||||||
|
|
||||||
|
boolean stillInCheck = simBoard.isKingInCheck(whiteKing);
|
||||||
|
|
||||||
|
if (!stillInCheck) {
|
||||||
|
hasEscape = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean checkmate = false;
|
||||||
|
|
||||||
|
if (kingInCheck && !hasEscape) {
|
||||||
|
checkmate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return checkmate;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue