Super IA #5
@@ -4,7 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import chess.model.visitor.KingIdentifier;
|
import chess.model.pieces.King;
|
||||||
import chess.model.visitor.PawnIdentifier;
|
import chess.model.visitor.PawnIdentifier;
|
||||||
import chess.model.visitor.PiecePathChecker;
|
import chess.model.visitor.PiecePathChecker;
|
||||||
|
|
||||||
@@ -31,6 +31,10 @@ public class ChessBoard {
|
|||||||
private Move lastMove;
|
private Move lastMove;
|
||||||
private Piece lastEjectedPiece;
|
private Piece lastEjectedPiece;
|
||||||
|
|
||||||
|
private List<Move> cachedAllowedMoves = null;
|
||||||
|
|
||||||
|
private final Coordinate kingPos[];
|
||||||
|
|
||||||
public ChessBoard() {
|
public ChessBoard() {
|
||||||
this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
|
this.cells = new Cell[Coordinate.VALUE_MAX][Coordinate.VALUE_MAX];
|
||||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
@@ -41,6 +45,7 @@ public class ChessBoard {
|
|||||||
this.lastVirtualMove = null;
|
this.lastVirtualMove = null;
|
||||||
this.lastMove = null;
|
this.lastMove = null;
|
||||||
this.lastEjectedPiece = null;
|
this.lastEjectedPiece = null;
|
||||||
|
this.kingPos = new Coordinate[Color.values().length];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void applyMove(Move move) {
|
public void applyMove(Move move) {
|
||||||
@@ -98,6 +103,8 @@ public class ChessBoard {
|
|||||||
|
|
||||||
public void pieceComes(Piece piece, Coordinate coordinate) {
|
public void pieceComes(Piece piece, Coordinate coordinate) {
|
||||||
cellAt(coordinate).setPiece(piece);
|
cellAt(coordinate).setPiece(piece);
|
||||||
|
if (piece instanceof King)
|
||||||
|
this.kingPos[piece.getColor().ordinal()] = coordinate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pieceLeaves(Coordinate coordinate) {
|
public void pieceLeaves(Coordinate coordinate) {
|
||||||
@@ -105,18 +112,19 @@ public class ChessBoard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Coordinate findKing(Color color) {
|
public Coordinate findKing(Color color) {
|
||||||
KingIdentifier kingIdentifier = new KingIdentifier(color);
|
// KingIdentifier kingIdentifier = new KingIdentifier(color);
|
||||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
// for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
// for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
Coordinate coordinate = new Coordinate(i, j);
|
// Coordinate coordinate = new Coordinate(i, j);
|
||||||
Piece piece = pieceAt(coordinate);
|
// Piece piece = pieceAt(coordinate);
|
||||||
if (kingIdentifier.isKing(piece)) {
|
// if (kingIdentifier.isKing(piece)) {
|
||||||
return coordinate;
|
// return coordinate;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
assert false : "No king found ?!";
|
// assert false : "No king found ?!";
|
||||||
return null;
|
// return null;
|
||||||
|
return kingPos[color.ordinal()];
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isKingInCheck(Color color) {
|
public boolean isKingInCheck(Color color) {
|
||||||
@@ -127,7 +135,7 @@ public class ChessBoard {
|
|||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
Coordinate attackCoords = new Coordinate(i, j);
|
Coordinate attackCoords = new Coordinate(i, j);
|
||||||
Piece attackPiece = pieceAt(attackCoords);
|
Piece attackPiece = pieceAt(attackCoords);
|
||||||
if (attackPiece == null)
|
if (attackPiece == null || attackPiece.getColor() == color)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
PiecePathChecker checker = new PiecePathChecker(this, new Move(attackCoords, kingPos));
|
PiecePathChecker checker = new PiecePathChecker(this, new Move(attackCoords, kingPos));
|
||||||
@@ -143,6 +151,10 @@ public class ChessBoard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Move> getAllowedMoves(Color player) {
|
public List<Move> getAllowedMoves(Color player) {
|
||||||
|
if (this.cachedAllowedMoves != null) {
|
||||||
|
return this.cachedAllowedMoves;
|
||||||
|
}
|
||||||
|
|
||||||
List<Move> result = new ArrayList<>();
|
List<Move> result = new ArrayList<>();
|
||||||
|
|
||||||
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
for (int x = 0; x < Coordinate.VALUE_MAX; x++) {
|
||||||
@@ -159,6 +171,11 @@ public class ChessBoard {
|
|||||||
Coordinate destination = new Coordinate(i, j);
|
Coordinate destination = new Coordinate(i, j);
|
||||||
Move move = new Move(start, destination);
|
Move move = new Move(start, destination);
|
||||||
|
|
||||||
|
Piece destPiece = pieceAt(destination);
|
||||||
|
|
||||||
|
if (destPiece != null && destPiece.getColor() == player)
|
||||||
|
continue;
|
||||||
|
|
||||||
PiecePathChecker piecePathChecker = new PiecePathChecker(this,
|
PiecePathChecker piecePathChecker = new PiecePathChecker(this,
|
||||||
move);
|
move);
|
||||||
if (!piecePathChecker.isValid())
|
if (!piecePathChecker.isValid())
|
||||||
@@ -172,6 +189,7 @@ public class ChessBoard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.cachedAllowedMoves = result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,10 +318,11 @@ public class ChessBoard {
|
|||||||
|
|
||||||
public void setLastMove(Move lastMove) {
|
public void setLastMove(Move lastMove) {
|
||||||
this.lastMove = lastMove;
|
this.lastMove = lastMove;
|
||||||
|
this.cachedAllowedMoves = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj){
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof ChessBoard board)
|
if (obj instanceof ChessBoard board)
|
||||||
return board.hashCode() == this.hashCode();
|
return board.hashCode() == this.hashCode();
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user