Compare commits

..

2 Commits

Author SHA1 Message Date
jefei de3721d00c Check Message and Checkmate ends the game 2025-05-18 00:03:01 +02:00
jefei 2365f7a0ef Check Text Show up 2025-05-18 00:00:00 +02:00
3 changed files with 85 additions and 10 deletions

View File

@ -12,6 +12,10 @@ private int width;
private int height;
private ArrayList<Piece> Pieces;
private Stack<Move> moveHistory = new Stack<>();
private boolean gameOver = false;
private String winnerText = null;
public boolean isGameOver() { return gameOver; }
public String getWinnerText() { return winnerText; }
// NEW FIELDS FOR userTouch
private boolean hasSelection = false; // did we already click to pick up a piece?
@ -315,26 +319,52 @@ public void userTouch(int x, int y) {
turnWhite = !turnWhite;
hasSelection = false;
System.out.println(this);
// 🧠 CHECK & CHECKMATE logic
// Reset previous check flags
checkedKingX = -1;
checkedKingY = -1;
checkmateFlag = false;
boolean opponentInCheck = isInCheck(turnWhite);
if (opponentInCheck) {
// 🧠 CHECK detection
boolean opponentColor = !turnWhite;
if (isInCheck(opponentColor)) {
Piece king = getKing(opponentColor);
if (king != null) {
checkedKingX = king.getX();
checkedKingY = king.getY();
System.out.println("⚠️ " + (opponentColor ? "White" : "Black") + " king is in CHECK!");
}
} else {
checkedKingX = -1;
checkedKingY = -1;
}
if (isInCheck(turnWhite)) {
Piece king = getKing(turnWhite);
if (king != null) {
checkedKingX = king.getX();
checkedKingY = king.getY();
System.out.println("⚠️ " + (turnWhite ? "White" : "Black") + " king is in check!");
System.out.println("⚠️ " + (turnWhite ? "White" : "Black") + " king is in CHECK!");
}
}
if (isCheckmate(turnWhite)) {
// CHECKMATE detection
if (captured != null && captured.getType() == PieceType.King) {
winnerText = (captured.isWhite() ? "Black" : "White") + " wins by king capture!";
gameOver = true;
System.out.println("♟️ " + winnerText);
return;
}if (isCheckmate(turnWhite)) {
checkmateFlag = true;
System.out.println("♟️ " + (!turnWhite ? "White" : "Black") + " wins by checkmate!");
winnerText = (!turnWhite ? "White" : "Black") + " wins by checkmate!";
gameOver = true;
System.out.println("♟️ " + winnerText);
return;
}
else {
checkmateFlag = false;
}
}
}
@ -736,11 +766,45 @@ public boolean isCheckmate(boolean white) {
}
return true;
}
public Piece getKing(boolean white) {
public Piece getKing(boolean isWhite) {
for (Piece p : Pieces) {
if (p.getType() == PieceType.King && p.isWhite() == white) return p;
if (p.getType() == PieceType.King && p.isWhite() == isWhite) {
return p;
}
}
return null;
}
public boolean isGameOver() {
// Checkmate
if (isCheckmate(true) || isCheckmate(false)) return true;
// King capture
boolean whiteKingAlive = false;
boolean blackKingAlive = false;
for (Piece p : Pieces) {
if (p.getType() == PieceType.King) {
if (p.isWhite()) whiteKingAlive = true;
else blackKingAlive = true;
}
}
return !(whiteKingAlive && blackKingAlive); // game over if any king is missing
}
public String getWinnerText() {
boolean whiteKingAlive = false;
boolean blackKingAlive = false;
for (Piece p : Pieces) {
if (p.getType() == PieceType.King) {
if (p.isWhite()) whiteKingAlive = true;
else blackKingAlive = true;
}
}
if (!whiteKingAlive && !blackKingAlive) return "Both Kings Captured!";
if (!whiteKingAlive) return "Black Wins!";
if (!blackKingAlive) return "White Wins!";
if (isCheckmate(true)) return "Black Wins by Checkmate!";
if (isCheckmate(false)) return "White Wins by Checkmate!";
return "";
}
//hi
}

View File

@ -41,6 +41,10 @@ public class Game extends Thread {
public void run() {
while(true) {
if (board.isGameOver()) {
mjf.update(board.getTurnNumber(), board.isTurnWhite()); // update GUI one last time
break; // 💣 exit game loop
}
aiPlayerTurn();
mjf.update(board.getTurnNumber(), board.isTurnWhite());
try {
@ -56,12 +60,14 @@ public class Game extends Thread {
}
private void aiPlayerTurn() {
if (board.isGameOver()) return;
if(isAITurn()) {
board.playMove(aiPlayer.computeBestMove(new Board(board)));
}
}
public void clickCoords(int x, int y) {
if (board.isGameOver()) return;
int width = this.getWidth();
int height = this.getHeight();
if(0>x || 0>y || x>width || y>height) {

View File

@ -257,7 +257,12 @@ public class MyInterface extends JFrame {
}
public void update(int turnCount, boolean turnIsWhite) {
turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black"));
if (game.getBoard().isGameOver()) {
actionLabel.setText(game.getBoard().getWinnerText()); // 🏁 Show who won
turnLabel.setText("Game Over");
} else {
turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black"));
}
actionLabel.setText(panelDraw.isPieceAdderMode()?"Adding Piece":
(panelDraw.isPieceSelectorMode()?"Selecting Piece to Add":
"Playing"));