Super IA (#5)
Reviewed-on: #5 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-committed-by: Persson-dev <sim16.prib@gmail.com>
This commit was merged in pull request #5.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -70,24 +70,27 @@ public class Game {
|
||||
playerTurn = Color.getEnemy(playerTurn);
|
||||
}
|
||||
|
||||
public GameStatus checkGameStatus() {
|
||||
final Color enemy = Color.getEnemy(getPlayerTurn());
|
||||
|
||||
// this is the bottleneck of algorithms using this chess engine
|
||||
public GameStatus checkGameStatus(Color color) {
|
||||
if (checkDraw())
|
||||
return GameStatus.Draw;
|
||||
|
||||
if (this.board.isKingInCheck(enemy))
|
||||
if (this.board.hasAllowedMoves(enemy))
|
||||
if (this.board.isKingInCheck(color))
|
||||
if (this.board.hasAllowedMoves(color))
|
||||
return GameStatus.Check;
|
||||
else
|
||||
return GameStatus.CheckMate;
|
||||
|
||||
if (!board.hasAllowedMoves(enemy))
|
||||
if (!board.hasAllowedMoves(color))
|
||||
return GameStatus.Pat;
|
||||
|
||||
return GameStatus.OnGoing;
|
||||
}
|
||||
|
||||
public GameStatus checkGameStatus() {
|
||||
return checkGameStatus(Color.getEnemy(getPlayerTurn()));
|
||||
}
|
||||
|
||||
public void addAction(PlayerCommand command) {
|
||||
this.movesHistory.add(command);
|
||||
}
|
||||
@@ -119,4 +122,8 @@ public class Game {
|
||||
return this.movesHistory;
|
||||
}
|
||||
|
||||
public boolean pawnShouldBePromoted() {
|
||||
return this.board.pawnShouldBePromoted();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
20
app/src/main/java/chess/model/PermissiveGame.java
Normal file
20
app/src/main/java/chess/model/PermissiveGame.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package chess.model;
|
||||
|
||||
public class PermissiveGame extends Game {
|
||||
|
||||
@Override
|
||||
public GameStatus checkGameStatus(Color color) {
|
||||
return GameStatus.OnGoing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undoTraitPiecesPos() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveTraitPiecesPos() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user