diff --git a/pom.xml b/pom.xml index 91157ba..7ad73d5 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 1.0.8 chess-utils - 0.0.4-SNAPSHOT + 0.0.5-SNAPSHOT chess-utils Some helpful piece of code to implement chess engines. diff --git a/src/main/java/com/fathzer/chess/utils/evaluators/utils/AbstractPieceSquareTableEvaluator.java b/src/main/java/com/fathzer/chess/utils/evaluators/utils/AbstractPieceSquareTableEvaluator.java index 21539c8..9374b4a 100644 --- a/src/main/java/com/fathzer/chess/utils/evaluators/utils/AbstractPieceSquareTableEvaluator.java +++ b/src/main/java/com/fathzer/chess/utils/evaluators/utils/AbstractPieceSquareTableEvaluator.java @@ -1,7 +1,6 @@ package com.fathzer.chess.utils.evaluators.utils; -import static com.fathzer.chess.utils.Pieces.KING; -import static com.fathzer.chess.utils.Pieces.ROOK; +import static com.fathzer.chess.utils.Pieces.*; import java.util.concurrent.atomic.AtomicInteger; @@ -50,27 +49,24 @@ private int getRawEvaluation(BoardExplorer explorer) { @Override public void prepareMove(MoveData moveData) { final boolean isBlack = moveData.getMovingPiece()<0; - int moving = Math.abs(moveData.getMovingPiece()); + int pieceType = Math.abs(moveData.getMovingPiece()); final int movingIndex = moveData.getMovingIndex(); int inc = 0; - if (moving==KING) { - // Be cautious with castling - int rookIndex = moveData.getCastlingRookIndex(); - if (rookIndex>=0) { - // It's a castling move, update rook positions values - inc = getPositionValue(ROOK, isBlack, moveData.getCastlingRookDestinationIndex()) - getPositionValue(ROOK, isBlack, rookIndex); - } + final int rookIndex = moveData.getCastlingRookIndex(); + if (rookIndex>=0) { + // It's a castling move, update rook positions values + inc = getPositionValue(ROOK, isBlack, moveData.getCastlingRookDestinationIndex()) - getPositionValue(ROOK, isBlack, rookIndex); } // Remove the old position value of the moving piece - inc -= getPositionValue(moving, isBlack, movingIndex); + inc -= getPositionValue(pieceType, isBlack, movingIndex); final int promoType = moveData.getPromotionType(); if (promoType!=0) { // If promotion, update the moving piece - moving = promoType; + pieceType = promoType; } - // Adds the new position value of the - inc += getPositionValue(moving, isBlack, moveData.getMovingDestination()); - int captured = moveData.getCapturedType(); + // Adds the value of the new position of the piece + inc += getPositionValue(pieceType, isBlack, moveData.getMovingDestination()); + final int captured = moveData.getCapturedType(); if (captured!=0) { // If the move is a capture add its position value inc -= getPositionValue(captured, !isBlack, moveData.getCapturedIndex()); diff --git a/src/main/java/com/fathzer/chess/utils/evaluators/utils/ComposableEvaluator.java b/src/main/java/com/fathzer/chess/utils/evaluators/utils/ComposableEvaluator.java index c670cec..a3726bd 100644 --- a/src/main/java/com/fathzer/chess/utils/evaluators/utils/ComposableEvaluator.java +++ b/src/main/java/com/fathzer/chess/utils/evaluators/utils/ComposableEvaluator.java @@ -9,6 +9,6 @@ public interface ComposableEvaluator> extends Zero @Override default void prepareMove(B board, M move) { - throw new IllegalStateException("Prepare move is not implemented"); + throw new UnsupportedOperationException("Prepare move is not implemented"); } } \ No newline at end of file diff --git a/src/test/java/com/fathzer/chess/utils/adapters/chesslib/ChessLibMoveData.java b/src/test/java/com/fathzer/chess/utils/adapters/chesslib/ChessLibMoveData.java index 2e9ad2b..744b13e 100644 --- a/src/test/java/com/fathzer/chess/utils/adapters/chesslib/ChessLibMoveData.java +++ b/src/test/java/com/fathzer/chess/utils/adapters/chesslib/ChessLibMoveData.java @@ -1,104 +1,103 @@ package com.fathzer.chess.utils.adapters.chesslib; -import static com.github.bhlangonijr.chesslib.PieceType.*; import static com.github.bhlangonijr.chesslib.CastleRight.*; +import static com.fathzer.chess.utils.Pieces.*; + import com.fathzer.chess.utils.adapters.MoveData; -import com.github.bhlangonijr.chesslib.Piece; -import com.github.bhlangonijr.chesslib.PieceType; + import com.github.bhlangonijr.chesslib.Side; import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.game.GameContext; import com.github.bhlangonijr.chesslib.move.Move; public class ChessLibMoveData implements MoveData { - private Square movingIndex; - private Piece movingPiece; - private Square movingDestination; - private Square capturedIndex; - private PieceType captured; - private PieceType promotion; - private Square castlingRookIndex; - private Square castlingRookDestinationIndex; + private int movingIndex; + private int movingPiece; + private int movingDestination; + private int capturedIndex; + private int capturedType; + private int promotionType; + private int castlingRookIndex; + private int castlingRookDestinationIndex; @Override public int getMovingIndex() { - return getIndex(movingIndex); + return movingIndex; } @Override public int getMovingPiece() { - return ChessLibBoardExplorer.toPiece(movingPiece); + return movingPiece; } @Override public int getMovingDestination() { - return getIndex(movingDestination); + return movingDestination; } @Override public int getCapturedType() { - return ChessLibBoardExplorer.fromPieceType(captured); + return capturedType; } @Override public int getCapturedIndex() { - return getIndex(capturedIndex); + return capturedIndex; } @Override public int getPromotionType() { - return ChessLibBoardExplorer.fromPieceType(promotion); + return promotionType; } @Override public int getCastlingRookIndex() { - return castlingRookIndex==null ? -1 : getIndex(castlingRookIndex); + return castlingRookIndex; } @Override public int getCastlingRookDestinationIndex() { - return getIndex(castlingRookDestinationIndex); + return castlingRookDestinationIndex; } @Override public boolean update(Move move, ChessLibMoveGenerator board) { - this.movingIndex = move.getFrom(); - this.movingPiece = board.getBoard().getPiece(movingIndex); - PieceType movingType = movingPiece.getPieceType(); - if (movingType==null) { + this.movingIndex = getIndex(move.getFrom()); + this.movingPiece = ChessLibBoardExplorer.toPiece(board.getBoard().getPiece(move.getFrom())); + int movingType = Math.abs(movingPiece); + if (movingType==0) { return false; } else if (movingType==KING) { - this.promotion = null; + this.promotionType = 0; final GameContext context = board.getBoard().getContext(); if (context.isCastleMove(move)) { final Side side = board.getBoard().getSideToMove(); - this.captured = null; - this.movingDestination = move.getTo(); - final Move rookMove = context.getRookCastleMove(side, context.isKingSideCastle(move) ? KING_SIDE : - QUEEN_SIDE); - this.castlingRookIndex = rookMove.getFrom(); - this.castlingRookDestinationIndex = rookMove.getTo(); + this.capturedType = 0; + this.movingDestination = getIndex(move.getTo()); + final Move rookMove = context.getRookCastleMove(side, context.isKingSideCastle(move) ? KING_SIDE : QUEEN_SIDE); + this.castlingRookIndex = getIndex(rookMove.getFrom()); + this.castlingRookDestinationIndex = getIndex(rookMove.getTo()); } else { - this.castlingRookIndex = null; - this.movingDestination = move.getTo(); - this.captured = board.getBoard().getPiece(movingDestination).getPieceType(); - if (this.captured!=null) { + this.castlingRookIndex = -1; + this.movingDestination = getIndex(move.getTo()); + this.capturedType = ChessLibBoardExplorer.fromPieceType(board.getBoard().getPiece(move.getTo()).getPieceType()); + if (this.capturedType!=0) { this.capturedIndex = this.movingDestination; } } } else { // Not a king move => no castling - this.castlingRookIndex=null; - this.movingDestination = move.getTo(); - if (movingType==PAWN && movingDestination==board.getBoard().getEnPassant()) { - this.captured = PAWN; - this.capturedIndex = board.getBoard().getEnPassantTarget(); - this.promotion = null; + this.castlingRookIndex=-1; + this.movingDestination = getIndex(move.getTo()); + if (movingType==PAWN && move.getTo()==board.getBoard().getEnPassant()) { + this.capturedType = PAWN; + this.capturedIndex = getIndex(board.getBoard().getEnPassantTarget()); + this.promotionType = 0; } else { - this.promotion = move.getPromotion().getPieceType(); - this.captured = board.getBoard().getPiece(movingDestination).getPieceType(); - if (this.captured!=null) { + this.promotionType = ChessLibBoardExplorer.fromPieceType(move.getPromotion().getPieceType()); + this.capturedType = ChessLibBoardExplorer.fromPieceType(board.getBoard().getPiece(move.getTo()).getPieceType()); + if (this.capturedType!=0) { this.capturedIndex = this.movingDestination; } } diff --git a/src/test/java/com/fathzer/chess/utils/evaluators/simplified/AbstractIncrementalSimplifiedEvaluatorTest2.java b/src/test/java/com/fathzer/chess/utils/evaluators/simplified/AbstractIncrementalSimplifiedEvaluatorTest2.java index cd3d89e..cdf8cf6 100644 --- a/src/test/java/com/fathzer/chess/utils/evaluators/simplified/AbstractIncrementalSimplifiedEvaluatorTest2.java +++ b/src/test/java/com/fathzer/chess/utils/evaluators/simplified/AbstractIncrementalSimplifiedEvaluatorTest2.java @@ -35,8 +35,9 @@ public MyEval(int state) { @Override public void prepareMove(ChessLibMoveGenerator board, Move move) { - moveData.update(move, board); - prepareMove(moveData); + if (moveData.update(move, board)) { + prepareMove(moveData); + } } @Override @@ -50,7 +51,7 @@ protected PiecesOnlySquareTable fork(int state) { } } - private int getStaticEval(ChessLibMoveGenerator board) { + int getStaticEval(ChessLibMoveGenerator board) { MyEval eval = new MyEval(); eval.init(board); return eval.evaluateAsWhite(board); @@ -62,9 +63,8 @@ void testIncrementalThings() { // Start with black queen and rook vs a pawn, a knight and a bishop for black => Middle game MyEval ev = new MyEval(); ChessLibMoveGenerator board = FENUtils.from("3qk3/7P/8/8/8/N7/B4r2/4K3 w - - 0 1"); -/* ev.init(board); + ev.init(board); int expected = 150+290+320-895-510; -System.out.println(expected); assertEquals(expected, ev.evaluateAsWhite(board)); MyEval forked = (MyEval) ev.fork(); @@ -107,18 +107,13 @@ void testIncrementalThings() { assertEquals(forkedExpected1, forked.evaluateAsWhite(forkedMg)); forked.unmakeMove(); assertEquals(expected, forked.evaluateAsWhite(forkedMg)); -*/ + // Other tests on another board board = FENUtils.from("r3k3/8/8/8/8/8/1p6/R3KQ2 b q - 1 1"); ev.init(board); assertEquals(740, ev.evaluateAsWhite(board)); // Test castling mv = new Move(E8, C8); - { - ChessLibMoveGenerator b = (ChessLibMoveGenerator) board.fork(); - b.makeMove(mv, MoveConfidence.UNSAFE); - assertEquals(740-5, getStaticEval(b)); - } ev.prepareMove(board, mv); ev.commitMove(); assertEquals(740-5, ev.evaluateAsWhite(board)); @@ -128,7 +123,7 @@ void testIncrementalThings() { mv = new Move(B2, A1, Piece.BLACK_QUEEN); ev.prepareMove(board, mv); ev.commitMove(); - assertEquals(745+150-880-500, ev.evaluateAsWhite(board)); + assertEquals(740+150-880-500, ev.evaluateAsWhite(board)); // Test that illegal move does not throw exception ev.unmakeMove();