Check Text Show up
This commit is contained in:
parent
e50739c834
commit
2365f7a0ef
|
|
@ -12,6 +12,10 @@ private int width;
|
||||||
private int height;
|
private int height;
|
||||||
private ArrayList<Piece> Pieces;
|
private ArrayList<Piece> Pieces;
|
||||||
private Stack<Move> moveHistory = new Stack<>();
|
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 ───────────────────────────────────────
|
// ── NEW FIELDS FOR userTouch ───────────────────────────────────────
|
||||||
private boolean hasSelection = false; // did we already click to pick up a piece?
|
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;
|
turnWhite = !turnWhite;
|
||||||
hasSelection = false;
|
hasSelection = false;
|
||||||
System.out.println(this);
|
System.out.println(this);
|
||||||
// 🧠 CHECK & CHECKMATE logic
|
|
||||||
|
// Reset previous check flags
|
||||||
checkedKingX = -1;
|
checkedKingX = -1;
|
||||||
checkedKingY = -1;
|
checkedKingY = -1;
|
||||||
checkmateFlag = false;
|
|
||||||
|
|
||||||
boolean opponentInCheck = isInCheck(turnWhite);
|
// 🧠 CHECK detection
|
||||||
if (opponentInCheck) {
|
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);
|
Piece king = getKing(turnWhite);
|
||||||
if (king != null) {
|
if (king != null) {
|
||||||
checkedKingX = king.getX();
|
checkedKingX = king.getX();
|
||||||
checkedKingY = king.getY();
|
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;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
public Piece getKing(boolean white) {
|
public Piece getKing(boolean isWhite) {
|
||||||
for (Piece p : Pieces) {
|
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;
|
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 "";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -41,6 +41,10 @@ public class Game extends Thread {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
while(true) {
|
while(true) {
|
||||||
|
if (board.isGameOver()) {
|
||||||
|
mjf.update(board.getTurnNumber(), board.isTurnWhite()); // update GUI one last time
|
||||||
|
break; // 💣 exit game loop
|
||||||
|
}
|
||||||
aiPlayerTurn();
|
aiPlayerTurn();
|
||||||
mjf.update(board.getTurnNumber(), board.isTurnWhite());
|
mjf.update(board.getTurnNumber(), board.isTurnWhite());
|
||||||
try {
|
try {
|
||||||
|
|
@ -56,12 +60,14 @@ public class Game extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void aiPlayerTurn() {
|
private void aiPlayerTurn() {
|
||||||
|
if (board.isGameOver()) return;
|
||||||
if(isAITurn()) {
|
if(isAITurn()) {
|
||||||
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clickCoords(int x, int y) {
|
public void clickCoords(int x, int y) {
|
||||||
|
if (board.isGameOver()) return;
|
||||||
int width = this.getWidth();
|
int width = this.getWidth();
|
||||||
int height = this.getHeight();
|
int height = this.getHeight();
|
||||||
if(0>x || 0>y || x>width || y>height) {
|
if(0>x || 0>y || x>width || y>height) {
|
||||||
|
|
|
||||||
|
|
@ -257,7 +257,12 @@ public class MyInterface extends JFrame {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(int turnCount, boolean turnIsWhite) {
|
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":
|
actionLabel.setText(panelDraw.isPieceAdderMode()?"Adding Piece":
|
||||||
(panelDraw.isPieceSelectorMode()?"Selecting Piece to Add":
|
(panelDraw.isPieceSelectorMode()?"Selecting Piece to Add":
|
||||||
"Playing"));
|
"Playing"));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue