Skip to content

Commit

Permalink
Code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Dec 18, 2024
1 parent c2f5284 commit 1508db1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 72 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0.8</version>
</parent>
<artifactId>chess-utils</artifactId>
<version>0.0.4-SNAPSHOT</version>
<version>0.0.5-SNAPSHOT</version>

<name>chess-utils</name>
<description>Some helpful piece of code to implement chess engines.</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -50,27 +49,24 @@ private int getRawEvaluation(BoardExplorer explorer) {
@Override
public void prepareMove(MoveData<M, B> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public interface ComposableEvaluator<M, B extends MoveGenerator<M>> 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");
}
}
Original file line number Diff line number Diff line change
@@ -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<Move, ChessLibMoveGenerator> {
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,7 +51,7 @@ protected PiecesOnlySquareTable<Move, ChessLibMoveGenerator> fork(int state) {
}
}

private int getStaticEval(ChessLibMoveGenerator board) {
int getStaticEval(ChessLibMoveGenerator board) {
MyEval eval = new MyEval();
eval.init(board);
return eval.evaluateAsWhite(board);
Expand All @@ -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();
Expand Down Expand Up @@ -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));
Expand All @@ -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();
Expand Down

0 comments on commit 1508db1

Please sign in to comment.