Super IA #5

Merged
Persson-dev merged 15 commits from deepmind into main 2025-04-30 18:28:02 +00:00
Showing only changes of commit 8a853bdc3a - Show all commits

View File

@@ -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<Move> 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<Move> getAllowedMoves(Color player) {
if (this.cachedAllowedMoves != null) {
return this.cachedAllowedMoves;
}
List<Move> 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;