New method is added in JPanelChessBoard, so now when the black chess win
it shows W in chess board
This commit is contained in:
parent
228227f073
commit
b5aa58a2a6
|
|
@ -133,6 +133,10 @@ public class Board {
|
|||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
if (isGameOver()) {
|
||||
endGame(); // <— this MUST be called
|
||||
return;
|
||||
}
|
||||
// 1) Find if you clicked on a piece at (x,y)
|
||||
if (isGameOver()) {
|
||||
System.out.println("Game Over!");
|
||||
|
|
@ -144,6 +148,10 @@ public class Board {
|
|||
clicked = p;
|
||||
break;
|
||||
}
|
||||
if (isGameOver()) {
|
||||
endGame(); // <— also check again after making a move
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 2) If nothing is selected, select a piece of the correct color
|
||||
|
|
@ -203,6 +211,10 @@ public class Board {
|
|||
moveHistory.push(move);
|
||||
System.out.println(this);
|
||||
if (isGameOver()) return;
|
||||
if (isGameOver()) {
|
||||
endGame();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -434,6 +446,10 @@ public class Board {
|
|||
|
||||
|
||||
public void playMove(Move move) {
|
||||
if (isGameOver()) {
|
||||
endGame(); // Make sure this is here
|
||||
return;
|
||||
}
|
||||
if (isGameOver()) {
|
||||
System.out.println("Game Over!");
|
||||
return;
|
||||
|
|
@ -457,6 +473,10 @@ public class Board {
|
|||
|
||||
// Clear selection
|
||||
hasSelection = false;
|
||||
if (isGameOver()) {
|
||||
endGame(); // check again after applying move
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Piece getPieceAt(int x, int y) {
|
||||
|
|
@ -485,5 +505,45 @@ public class Board {
|
|||
}
|
||||
|
||||
|
||||
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));
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -193,4 +193,27 @@ public class JPanelChessBoard extends JPanel {
|
|||
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
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue