win detection yay
This commit is contained in:
@@ -26,6 +26,9 @@ abstract class Actor {
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
// Set this actor the winner.
|
||||
void setWinner() { this.won = true; }
|
||||
|
||||
// Process a user click on the board if the active actor.
|
||||
void click(Coord coord) {}
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package chess;
|
||||
|
||||
import chess.Util.Col;
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -93,6 +94,15 @@ class Board {
|
||||
// Move a piece.
|
||||
void move(Coord targ, Coord dest) {
|
||||
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.set(dest, piece);
|
||||
this.game.changeActive();
|
||||
@@ -119,6 +129,18 @@ class Board {
|
||||
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.
|
||||
ArrayList<Coord> getLineAbove(Coord coord, Util.Col col) {
|
||||
ArrayList<Coord> line = new ArrayList<>();
|
||||
|
@@ -10,12 +10,14 @@ class Game extends World {
|
||||
Board board;
|
||||
WorldScene scene;
|
||||
Actor activeActor;
|
||||
Actor winner;
|
||||
|
||||
Game(Actor white, Actor black, Board board) {
|
||||
this.white = white;
|
||||
this.black = black;
|
||||
this.board = board;
|
||||
this.activeActor = this.white;
|
||||
this.winner = null;
|
||||
}
|
||||
|
||||
Game() {
|
||||
@@ -23,6 +25,7 @@ class Game extends World {
|
||||
this.white = new Player(Util.Col.WHITE, this.board);
|
||||
this.black = new Player(Util.Col.BLACK, this.board);
|
||||
this.activeActor = this.white;
|
||||
this.winner = null;
|
||||
|
||||
this.setup();
|
||||
}
|
||||
@@ -39,14 +42,26 @@ class Game extends World {
|
||||
|
||||
void draw() {
|
||||
this.scene = this.board.draw();
|
||||
this.scene.placeImageXY(
|
||||
new TextImage(
|
||||
(this.activeActor.equals(this.white) ? "white" : "black") +
|
||||
"'s turn",
|
||||
Util.scale / 2, Color.red
|
||||
),
|
||||
Util.scale * 2, 3 * Util.scale
|
||||
);
|
||||
|
||||
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(
|
||||
new TextImage(
|
||||
(this.activeActor.equals(this.white) ? "white" : "black") +
|
||||
"'s turn",
|
||||
Util.scale / 2, Color.red
|
||||
),
|
||||
Util.scale * 2, 3 * Util.scale
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
@@ -123,13 +138,26 @@ class Game extends World {
|
||||
}
|
||||
|
||||
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() {
|
||||
if (this.activeActor.equals(this.white)) this.activeActor = this.black;
|
||||
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; }
|
||||
}
|
||||
|
Reference in New Issue
Block a user