h
This commit is contained in:
@@ -10,6 +10,7 @@ import javalib.worldimages.EmptyImage;
|
||||
import javalib.worldimages.OutlineMode;
|
||||
import javalib.worldimages.OverlayImage;
|
||||
import javalib.worldimages.RectangleImage;
|
||||
import javalib.worldimages.TextImage;
|
||||
import javalib.worldimages.WorldImage;
|
||||
|
||||
class Board {
|
||||
@@ -46,6 +47,8 @@ class Board {
|
||||
Util.scale, Util.scale, OutlineMode.SOLID,
|
||||
new Color(200, 50, 200, 150)
|
||||
);
|
||||
else if (info == Info.CHECK)
|
||||
infoImage = new TextImage("!", Util.scale, Color.RED);
|
||||
|
||||
scene.placeImageXY(
|
||||
new OverlayImage(
|
||||
@@ -136,6 +139,13 @@ class Board {
|
||||
}
|
||||
|
||||
this.game.changeActive();
|
||||
|
||||
// Check if in check after last move.
|
||||
if (this.inCheck()) {
|
||||
this.info.put(this.activeKing(), Info.CHECK);
|
||||
} else {
|
||||
this.info.remove(this.activeKing());
|
||||
}
|
||||
}
|
||||
|
||||
// Get the valid moves for a piece.
|
||||
@@ -196,6 +206,18 @@ class Board {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the coords of the active actor's king.
|
||||
Coord activeKing() {
|
||||
for (Map.Entry<Coord, Piece> entry : this.board.entrySet()) {
|
||||
Piece piece = entry.getValue();
|
||||
if (piece != null && piece.type == PieceType.KING &&
|
||||
piece.col == this.game.activeActor.col) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Return the line of free spaces above.
|
||||
ArrayList<Coord> getLineAbove(Coord coord, Util.Col col) {
|
||||
ArrayList<Coord> line = new ArrayList<>();
|
||||
@@ -286,10 +308,40 @@ class Board {
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
// Return all the posible move locations of a color.
|
||||
ArrayList<Coord> allMoves(Util.Col col) {
|
||||
ArrayList<Coord> moves = new ArrayList<>();
|
||||
for (int x = 0; x < Util.boardW; x++)
|
||||
for (int y = 0; y < Util.boardW; y++) {
|
||||
Coord coord = new Coord(x, y);
|
||||
Piece piece = this.get(coord);
|
||||
if (piece != null && piece.col.equals(col))
|
||||
moves.addAll(piece.moves(coord, this));
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
// Check if list of coords contains the king (check).
|
||||
boolean check(ArrayList<Coord> coords) {
|
||||
for (Coord coord : coords) {
|
||||
Piece piece = this.get(coord);
|
||||
if (piece != null && piece.type.equals(PieceType.KING)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if active actor is in check.
|
||||
boolean inCheck() {
|
||||
ArrayList<Coord> allEnemyMoves =
|
||||
allMoves(Util.reverse(this.game.activeActor.col));
|
||||
return check(allEnemyMoves);
|
||||
}
|
||||
}
|
||||
|
||||
// Information layered over the board.
|
||||
enum Info { CANMOVE, WARN, SELECTED, PROMOTION }
|
||||
enum Info { CANMOVE, WARN, SELECTED, PROMOTION, CHECK }
|
||||
|
||||
// Measured from top left.
|
||||
class Coord {
|
||||
|
@@ -16,4 +16,7 @@ class Util {
|
||||
|
||||
// The color a piece can be.
|
||||
enum Col { WHITE, BLACK }
|
||||
static Col reverse(Col col) {
|
||||
return col == Col.WHITE ? Col.BLACK : Col.WHITE;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user