From 8a853bdc3a5ddfe6e07a06f367727e6b922b4c5f Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 20 Apr 2025 10:45:03 +0200 Subject: [PATCH] little more optimisation --- app/src/main/java/chess/model/ChessBoard.java | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/chess/model/ChessBoard.java b/app/src/main/java/chess/model/ChessBoard.java index ebc15e5..adaf385 100644 --- a/app/src/main/java/chess/model/ChessBoard.java +++ b/app/src/main/java/chess/model/ChessBoard.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -import chess.model.visitor.KingIdentifier; +import chess.model.pieces.King; import chess.model.visitor.PawnIdentifier; import chess.model.visitor.PiecePathChecker; @@ -31,6 +31,10 @@ public class ChessBoard { private Move lastMove; private Piece lastEjectedPiece; + private List cachedAllowedMoves = null; + + private final Coordinate kingPos[]; + public ChessBoard() { this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX]; for (int i = 0; i < Coordinate.VALUE_MAX; i++) { @@ -41,6 +45,7 @@ public class ChessBoard { this.lastVirtualMove = null; this.lastMove = null; this.lastEjectedPiece = null; + this.kingPos = new Coordinate[Color.values().length]; } public void applyMove(Move move) { @@ -98,6 +103,8 @@ public class ChessBoard { public void pieceComes(Piece piece, Coordinate coordinate) { cellAt(coordinate).setPiece(piece); + if (piece instanceof King) + this.kingPos[piece.getColor().ordinal()] = coordinate; } public void pieceLeaves(Coordinate coordinate) { @@ -105,18 +112,19 @@ public class ChessBoard { } public Coordinate findKing(Color color) { - KingIdentifier kingIdentifier = new KingIdentifier(color); - for (int i = 0; i < Coordinate.VALUE_MAX; i++) { - for (int j = 0; j < Coordinate.VALUE_MAX; j++) { - Coordinate coordinate = new Coordinate(i, j); - Piece piece = pieceAt(coordinate); - if (kingIdentifier.isKing(piece)) { - return coordinate; - } - } - } - assert false : "No king found ?!"; - return null; + // KingIdentifier kingIdentifier = new KingIdentifier(color); + // for (int i = 0; i < Coordinate.VALUE_MAX; i++) { + // for (int j = 0; j < Coordinate.VALUE_MAX; j++) { + // Coordinate coordinate = new Coordinate(i, j); + // Piece piece = pieceAt(coordinate); + // if (kingIdentifier.isKing(piece)) { + // return coordinate; + // } + // } + // } + // assert false : "No king found ?!"; + // return null; + return kingPos[color.ordinal()]; } public boolean isKingInCheck(Color color) { @@ -127,7 +135,7 @@ public class ChessBoard { for (int j = 0; j < Coordinate.VALUE_MAX; j++) { Coordinate attackCoords = new Coordinate(i, j); Piece attackPiece = pieceAt(attackCoords); - if (attackPiece == null) + if (attackPiece == null || attackPiece.getColor() == color) continue; PiecePathChecker checker = new PiecePathChecker(this, new Move(attackCoords, kingPos)); @@ -143,6 +151,10 @@ public class ChessBoard { } public List getAllowedMoves(Color player) { + if (this.cachedAllowedMoves != null) { + return this.cachedAllowedMoves; + } + List result = new ArrayList<>(); for (int x = 0; x < Coordinate.VALUE_MAX; x++) { @@ -159,6 +171,11 @@ public class ChessBoard { Coordinate destination = new Coordinate(i, j); Move move = new Move(start, destination); + Piece destPiece = pieceAt(destination); + + if (destPiece != null && destPiece.getColor() == player) + continue; + PiecePathChecker piecePathChecker = new PiecePathChecker(this, move); if (!piecePathChecker.isValid()) @@ -172,6 +189,7 @@ public class ChessBoard { } } } + this.cachedAllowedMoves = result; return result; } @@ -300,10 +318,11 @@ public class ChessBoard { public void setLastMove(Move lastMove) { this.lastMove = lastMove; + this.cachedAllowedMoves = null; } @Override - public boolean equals(Object obj){ + public boolean equals(Object obj) { if (obj instanceof ChessBoard board) return board.hashCode() == this.hashCode(); return false;