In class JPanelChessBoard is change the colour of the frame and the

colour of the posible move is also changed, now when the king is death
the game is over by adding method isGameover
This commit is contained in:
USer 2025-05-16 10:27:25 +02:00
parent 9536276f78
commit 228227f073
3 changed files with 31 additions and 6 deletions

View File

@ -134,7 +134,11 @@ public class Board {
public void userTouch(int x, int y) {
// 1) Find if you clicked on a piece at (x,y)
Piece clicked = null;
if (isGameOver()) {
System.out.println("Game Over!");
return;
}
Piece clicked = null;
for (Piece p : Pieces) {
if (p.getX() == x && p.getY() == y) {
clicked = p;
@ -198,6 +202,7 @@ public class Board {
Move move = new Move(toMove, x, y, captured); // captured can be null if no piece was taken
moveHistory.push(move);
System.out.println(this);
if (isGameOver()) return;
}
@ -429,7 +434,11 @@ public class Board {
public void playMove(Move move) {
// Remove the piece from the destination (if a capture)
if (isGameOver()) {
System.out.println("Game Over!");
return;
}
// Remove the piece from the destination (if a capture)
Pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
// Remove the moved piece from its old position
@ -461,5 +470,20 @@ public class Board {
selectedX = x;
selectedY = y;
}
public boolean isGameOver() {
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);
}
}

View File

@ -2,7 +2,7 @@
package backend;
public class Move {
private final int fromX;
private final int fromX;
private final int fromY;
private final int toX;
private final int toY;
@ -10,6 +10,7 @@ public class Move {
private final Piece capturedPiece;
// Constructor for a move (with or without capture)
public Move(Piece movedPiece, int toX, int toY, Piece capturedPiece) {
this.fromX = movedPiece.getX();
this.fromY = movedPiece.getY();

View File

@ -103,10 +103,10 @@ public class JPanelChessBoard extends JPanel {
boolean isSelect = myGame.isSelected(x,y);
boolean isHighlight = myGame.isHighlighted(x,y);
if(isSelect) {
g.setColor(Color.blue);
g.setColor(Color.cyan);
}
if(isHighlight) {
g.setColor(Color.yellow);
g.setColor(Color.lightGray);
}
if((x+y)%2==1 || isSelect || isHighlight) {
g.fillRect(
@ -123,7 +123,7 @@ public class JPanelChessBoard extends JPanel {
}
}
g.setColor(Color.gray);
g.setColor(Color.blue);
for(int x=0; x<myGame.getWidth();x++) {
int graphX = Math.round(x*cellWidth);
g.drawLine(graphX, 0, graphX, this.getHeight());