New method is added in JPanelChessBoard, so now when the black chess win

it shows W in chess board
This commit is contained in:
USer 2025-05-16 12:27:50 +02:00
parent 228227f073
commit b5aa58a2a6
2 changed files with 87 additions and 4 deletions

View File

@ -133,7 +133,11 @@ public class Board {
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
// 1) Find if you clicked on a piece at (x,y) if (isGameOver()) {
endGame(); // < this MUST be called
return;
}
// 1) Find if you clicked on a piece at (x,y)
if (isGameOver()) { if (isGameOver()) {
System.out.println("Game Over!"); System.out.println("Game Over!");
return; return;
@ -144,6 +148,10 @@ public class Board {
clicked = p; clicked = p;
break; break;
} }
if (isGameOver()) {
endGame(); // < also check again after making a move
return;
}
} }
// 2) If nothing is selected, select a piece of the correct color // 2) If nothing is selected, select a piece of the correct color
@ -203,6 +211,10 @@ public class Board {
moveHistory.push(move); moveHistory.push(move);
System.out.println(this); System.out.println(this);
if (isGameOver()) return; if (isGameOver()) return;
if (isGameOver()) {
endGame();
return;
}
} }
@ -434,9 +446,13 @@ public class Board {
public void playMove(Move move) { public void playMove(Move move) {
if (isGameOver()) { if (isGameOver()) {
System.out.println("Game Over!"); endGame(); // Make sure this is here
return; return;
}
if (isGameOver()) {
System.out.println("Game Over!");
return;
} }
// Remove the piece from the destination (if a capture) // Remove the piece from the destination (if a capture)
Pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY()); Pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
@ -457,6 +473,10 @@ public class Board {
// Clear selection // Clear selection
hasSelection = false; hasSelection = false;
if (isGameOver()) {
endGame(); // check again after applying move
return;
}
} }
Piece getPieceAt(int x, int y) { Piece getPieceAt(int x, int y) {
@ -483,7 +503,47 @@ public class Board {
return !(whiteKingAlive && blackKingAlive); return !(whiteKingAlive && blackKingAlive);
} }
private void endGame() {
boolean whiteAlive = false;
boolean blackAlive = false;
for (Piece p : Pieces) {
if (p.getType() == PieceType.King) {
if (p.isWhite()) whiteAlive = true;
else blackAlive = true;
}
}
Pieces.clear(); // Remove all remaining pieces
hasSelection = false;
if (whiteAlive && !blackAlive) {
showWinText(true); // White wins
} else if (!whiteAlive && blackAlive) {
showWinText(false); // Black wins
}
System.out.println("Game Over!");
}
private void showWinText(boolean isWhite) {
// W
Pieces.add(new Piece(1, 2, isWhite, PieceType.Queen));
Pieces.add(new Piece(1, 3, isWhite, PieceType.Queen));
Pieces.add(new Piece(2, 4, isWhite, PieceType.Queen));
Pieces.add(new Piece(3, 2, isWhite, PieceType.Queen));
Pieces.add(new Piece(3, 3, isWhite, PieceType.Queen));
Pieces.add(new Piece(4, 4, isWhite, PieceType.Queen));
Pieces.add(new Piece(5, 2, isWhite, PieceType.Queen));
Pieces.add(new Piece(5, 3, isWhite, PieceType.Queen));
}
} }

View File

@ -192,5 +192,28 @@ public class JPanelChessBoard extends JPanel {
public boolean isPieceAdderMode() { public boolean isPieceAdderMode() {
return pieceAdderMode; return pieceAdderMode;
} }
private void drawWinMessage(Graphics g, boolean isWhite) {
PieceType type = PieceType.Queen;
Color color = isWhite ? Color.white : Color.black;
// Draw 'W'
drawSinglePiece(g, 0, 2, type, isWhite);
drawSinglePiece(g, 0, 3, type, isWhite);
drawSinglePiece(g, 1, 4, type, isWhite);
drawSinglePiece(g, 2, 3, type, isWhite);
drawSinglePiece(g, 2, 2, type, isWhite);
}
private void drawSinglePiece(Graphics g, int boardX, int boardY, PieceType type, boolean isWhite) {
g.drawImage(
getChessPieceImageFromType(type, isWhite),
MARGIN + xCoordFromGame(boardX),
MARGIN + yCoordFromGame(boardY),
null
);
}
} }