win detection yay

This commit is contained in:
Jacob Signorovitch
2025-05-26 23:59:00 -04:00
parent 83bc3d192a
commit 5574f330e9
3 changed files with 63 additions and 10 deletions
+3
View File
@@ -26,6 +26,9 @@ abstract class Actor {
this.board = board; this.board = board;
} }
// Set this actor the winner.
void setWinner() { this.won = true; }
// Process a user click on the board if the active actor. // Process a user click on the board if the active actor.
void click(Coord coord) {} void click(Coord coord) {}
+22
View File
@@ -1,5 +1,6 @@
package chess; package chess;
import chess.Util.Col;
import java.awt.Color; import java.awt.Color;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -93,6 +94,15 @@ class Board {
// Move a piece. // Move a piece.
void move(Coord targ, Coord dest) { void move(Coord targ, Coord dest) {
Piece piece = this.get(targ); Piece piece = this.get(targ);
Piece capturedPiece = this.get(dest);
// Check if king's captured.
if (capturedPiece != null && capturedPiece.type == PieceType.KING) {
// Set active actor as winner.
this.game.activeActor.setWinner();
this.game.setWinner(this.game.activeActor);
}
this.drop(targ); this.drop(targ);
this.set(dest, piece); this.set(dest, piece);
this.game.changeActive(); this.game.changeActive();
@@ -119,6 +129,18 @@ class Board {
return piece != null && piece.col != col; return piece != null && piece.col != col;
} }
// Check if a king of the specified color exists on the board.
boolean hasKing(Col col) {
for (Map.Entry<Coord, Piece> entry : this.board.entrySet()) {
Piece piece = entry.getValue();
if (piece != null && piece.type == PieceType.KING &&
piece.col == col) {
return true;
}
}
return false;
}
// Return the line of free spaces above. // Return the line of free spaces above.
ArrayList<Coord> getLineAbove(Coord coord, Util.Col col) { ArrayList<Coord> getLineAbove(Coord coord, Util.Col col) {
ArrayList<Coord> line = new ArrayList<>(); ArrayList<Coord> line = new ArrayList<>();
+30 -2
View File
@@ -10,12 +10,14 @@ class Game extends World {
Board board; Board board;
WorldScene scene; WorldScene scene;
Actor activeActor; Actor activeActor;
Actor winner;
Game(Actor white, Actor black, Board board) { Game(Actor white, Actor black, Board board) {
this.white = white; this.white = white;
this.black = black; this.black = black;
this.board = board; this.board = board;
this.activeActor = this.white; this.activeActor = this.white;
this.winner = null;
} }
Game() { Game() {
@@ -23,6 +25,7 @@ class Game extends World {
this.white = new Player(Util.Col.WHITE, this.board); this.white = new Player(Util.Col.WHITE, this.board);
this.black = new Player(Util.Col.BLACK, this.board); this.black = new Player(Util.Col.BLACK, this.board);
this.activeActor = this.white; this.activeActor = this.white;
this.winner = null;
this.setup(); this.setup();
} }
@@ -39,6 +42,17 @@ class Game extends World {
void draw() { void draw() {
this.scene = this.board.draw(); this.scene = this.board.draw();
if (this.winner != null) {
this.scene.placeImageXY(
new TextImage(
(this.winner.equals(this.white) ? "WHITE" : "BLACK") +
" WINS",
Util.scale, Color.red
),
Util.scale * 4, 3 * Util.scale
);
} else {
this.scene.placeImageXY( this.scene.placeImageXY(
new TextImage( new TextImage(
(this.activeActor.equals(this.white) ? "white" : "black") + (this.activeActor.equals(this.white) ? "white" : "black") +
@@ -48,6 +62,7 @@ class Game extends World {
Util.scale * 2, 3 * Util.scale Util.scale * 2, 3 * Util.scale
); );
} }
}
void setup() { void setup() {
this.board.board.put(new Coord(0, 0), new Rook(Util.Col.BLACK)); this.board.board.put(new Coord(0, 0), new Rook(Util.Col.BLACK));
@@ -123,13 +138,26 @@ class Game extends World {
} }
public void onMouseClicked(Posn posn) { public void onMouseClicked(Posn posn) {
this.activeActor.click(this.where(posn)); // Process clicks if game not over.
if (this.winner == null) this.activeActor.click(this.where(posn));
} }
public void onKeyEvent(String key) { this.activeActor.key(key); } public void onKeyEvent(String key) {
// Process keys if game is not over.
if (this.winner == null) this.activeActor.key(key);
}
void changeActive() { void changeActive() {
if (this.activeActor.equals(this.white)) this.activeActor = this.black; if (this.activeActor.equals(this.white)) this.activeActor = this.black;
else this.activeActor = this.white; else this.activeActor = this.white;
} }
void setWinner(Actor winner) {
this.winner = winner;
System.out.println(
(winner.equals(this.white) ? "WHITE" : "BLACK") + " WINS!"
);
}
boolean isGameOver() { return this.winner != null; }
} }