Compare commits
1 Commits
15fc3a6851
...
5d5c023f1a
| Author | SHA1 | Date |
|---|---|---|
|
|
5d5c023f1a |
|
|
@ -11,7 +11,8 @@ public class Board {
|
||||||
private boolean turnWhite=true; // True if it's White's turn, False if it's Black's turn
|
private boolean turnWhite=true; // True if it's White's turn, False if it's Black's turn
|
||||||
private ArrayList<int[]> highlightedPositions = new ArrayList<>(); // List containing all board positions
|
private ArrayList<int[]> highlightedPositions = new ArrayList<>(); // List containing all board positions
|
||||||
private ArrayList<MoveRecord> moveHistory = new ArrayList<>(); // List to store move history
|
private ArrayList<MoveRecord> moveHistory = new ArrayList<>(); // List to store move history
|
||||||
|
private String winnerMessage;
|
||||||
|
private boolean suppressCheckmateCheck = false;
|
||||||
private boolean inBounds(int x, int y) {
|
private boolean inBounds(int x, int y) {
|
||||||
// Verify the bounds of the board
|
// Verify the bounds of the board
|
||||||
return x >= 0 && y >= 0 && x < col && y < line;
|
return x >= 0 && y >= 0 && x < col && y < line;
|
||||||
|
|
@ -453,7 +454,62 @@ public class Board {
|
||||||
this.selectedPosition = null;
|
this.selectedPosition = null;
|
||||||
this.highlightedPositions.clear();
|
this.highlightedPositions.clear();
|
||||||
|
|
||||||
|
if (!suppressCheckmateCheck && isCheckmate(!turnWhite)) {
|
||||||
|
winnerMessage = turnWhite ? "White is the winner" : "Black is the winner";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
public boolean isKingInCheck(boolean kingIsWhite) {
|
||||||
|
int kingX = -1, kingY = -1;
|
||||||
|
|
||||||
|
//Find the king
|
||||||
|
for (Piece p : getPieces()) {
|
||||||
|
if (p.getType() == PieceType.King && p.isWhite() == kingIsWhite) {
|
||||||
|
kingX = p.getX();
|
||||||
|
kingY = p.getY();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (kingX == -1)
|
||||||
|
return false;
|
||||||
|
for (Piece p : getPieces()) {
|
||||||
|
if (p.isWhite() != kingIsWhite) {
|
||||||
|
for (int[] m : getValidMoves(p)) {
|
||||||
|
if (m[0] == kingX && m[1] == kingY) {
|
||||||
|
return true; //King in check
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public boolean isCheckmate(boolean playerWhite) {
|
||||||
|
if (!isKingInCheck(playerWhite))
|
||||||
|
return false;
|
||||||
|
for (Piece p : getPieces()) {
|
||||||
|
if (p.isWhite() == playerWhite) {
|
||||||
|
for (int[] move : getValidMoves(p)) {
|
||||||
|
//simulate the move
|
||||||
|
Board copy = new Board(this);
|
||||||
|
copy.suppressCheckmateCheck = true;
|
||||||
|
Piece simPiece = copy.board[p.getY()][p.getX()];
|
||||||
|
Move m = new Move(simPiece, move[0], move[1]);
|
||||||
|
copy.playMove(m);
|
||||||
|
|
||||||
|
if (!copy.isKingInCheck(playerWhite)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public String getWinnerMessage() {
|
||||||
|
return winnerMessage;
|
||||||
|
}
|
||||||
|
public void setWinnerMessage(String message) {
|
||||||
|
this.winnerMessage = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,22 @@ public class Game extends Thread {
|
||||||
|
|
||||||
private void aiPlayerTurn() {
|
private void aiPlayerTurn() {
|
||||||
if(isAITurn()) {
|
if(isAITurn()) {
|
||||||
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
Move move = aiPlayer.computeBestMove(new Board(board));
|
||||||
|
|
||||||
|
if (move != null) {
|
||||||
|
board.playMove(move);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (board.isKingInCheck(board.isTurnWhite())) {
|
||||||
|
System.out.println((board.isTurnWhite() ? "White" : "Black") + "is checkmated!");
|
||||||
|
board.setWinnerMessage((!board.isTurnWhite() ? "White" : "Black") + "wins by checkmate!");
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("It's a draw");
|
||||||
|
board.setWinnerMessage("Draw!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue