From 0b30c851e437104fd2a32e25a01f0d52a83697d7 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Tue, 27 May 2025 01:02:45 -0400 Subject: [PATCH] h --- chess/src/chess/Board.java | 54 +++++++++++++++++++++++++++++++++++++- chess/src/chess/Util.java | 3 +++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/chess/src/chess/Board.java b/chess/src/chess/Board.java index 24221eb..0b4bbaa 100644 --- a/chess/src/chess/Board.java +++ b/chess/src/chess/Board.java @@ -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 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 getLineAbove(Coord coord, Util.Col col) { ArrayList line = new ArrayList<>(); @@ -286,10 +308,40 @@ class Board { } return line; } + + // Return all the posible move locations of a color. + ArrayList allMoves(Util.Col col) { + ArrayList 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 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 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 { diff --git a/chess/src/chess/Util.java b/chess/src/chess/Util.java index 3a96b28..caa9c85 100644 --- a/chess/src/chess/Util.java +++ b/chess/src/chess/Util.java @@ -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; + } }