Skip to content

Commit

Permalink
Fucking rebase ->
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Jan 6, 2025
2 parents e2c25bb + b8c33bd commit 9e826dc
Show file tree
Hide file tree
Showing 28 changed files with 898 additions and 231 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<version>3.4.1</version>
<configuration>
<finalName>${customized.jar.name}</finalName>
<minimizeJar>true</minimizeJar>
<minimizeJar>false</minimizeJar>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
package com.fathzer.jchess.chesslib;

import static com.github.bhlangonijr.chesslib.Square.*;
import static com.github.bhlangonijr.chesslib.Square.A1;
import static com.github.bhlangonijr.chesslib.Square.A2;
import static com.github.bhlangonijr.chesslib.Square.A3;
import static com.github.bhlangonijr.chesslib.Square.A4;
import static com.github.bhlangonijr.chesslib.Square.A5;
import static com.github.bhlangonijr.chesslib.Square.A6;
import static com.github.bhlangonijr.chesslib.Square.A7;
import static com.github.bhlangonijr.chesslib.Square.A8;
import static com.github.bhlangonijr.chesslib.Square.B1;
import static com.github.bhlangonijr.chesslib.Square.B2;
import static com.github.bhlangonijr.chesslib.Square.B3;
import static com.github.bhlangonijr.chesslib.Square.B4;
import static com.github.bhlangonijr.chesslib.Square.B5;
import static com.github.bhlangonijr.chesslib.Square.B6;
import static com.github.bhlangonijr.chesslib.Square.B7;
import static com.github.bhlangonijr.chesslib.Square.B8;
import static com.github.bhlangonijr.chesslib.Square.C1;
import static com.github.bhlangonijr.chesslib.Square.C2;
import static com.github.bhlangonijr.chesslib.Square.C3;
import static com.github.bhlangonijr.chesslib.Square.C4;
import static com.github.bhlangonijr.chesslib.Square.C5;
import static com.github.bhlangonijr.chesslib.Square.C6;
import static com.github.bhlangonijr.chesslib.Square.C7;
import static com.github.bhlangonijr.chesslib.Square.C8;
import static com.github.bhlangonijr.chesslib.Square.D1;
import static com.github.bhlangonijr.chesslib.Square.D2;
import static com.github.bhlangonijr.chesslib.Square.D3;
import static com.github.bhlangonijr.chesslib.Square.D4;
import static com.github.bhlangonijr.chesslib.Square.D5;
import static com.github.bhlangonijr.chesslib.Square.D6;
import static com.github.bhlangonijr.chesslib.Square.D7;
import static com.github.bhlangonijr.chesslib.Square.D8;
import static com.github.bhlangonijr.chesslib.Square.E1;
import static com.github.bhlangonijr.chesslib.Square.E2;
import static com.github.bhlangonijr.chesslib.Square.E3;
import static com.github.bhlangonijr.chesslib.Square.E4;
import static com.github.bhlangonijr.chesslib.Square.E5;
import static com.github.bhlangonijr.chesslib.Square.E6;
import static com.github.bhlangonijr.chesslib.Square.E7;
import static com.github.bhlangonijr.chesslib.Square.E8;
import static com.github.bhlangonijr.chesslib.Square.F1;
import static com.github.bhlangonijr.chesslib.Square.F2;
import static com.github.bhlangonijr.chesslib.Square.F3;
import static com.github.bhlangonijr.chesslib.Square.F4;
import static com.github.bhlangonijr.chesslib.Square.F5;
import static com.github.bhlangonijr.chesslib.Square.F6;
import static com.github.bhlangonijr.chesslib.Square.F7;
import static com.github.bhlangonijr.chesslib.Square.F8;
import static com.github.bhlangonijr.chesslib.Square.G1;
import static com.github.bhlangonijr.chesslib.Square.G2;
import static com.github.bhlangonijr.chesslib.Square.G3;
import static com.github.bhlangonijr.chesslib.Square.G4;
import static com.github.bhlangonijr.chesslib.Square.G5;
import static com.github.bhlangonijr.chesslib.Square.G6;
import static com.github.bhlangonijr.chesslib.Square.G7;
import static com.github.bhlangonijr.chesslib.Square.G8;
import static com.github.bhlangonijr.chesslib.Square.H1;
import static com.github.bhlangonijr.chesslib.Square.H2;
import static com.github.bhlangonijr.chesslib.Square.H3;
import static com.github.bhlangonijr.chesslib.Square.H4;
import static com.github.bhlangonijr.chesslib.Square.H5;
import static com.github.bhlangonijr.chesslib.Square.H6;
import static com.github.bhlangonijr.chesslib.Square.H7;
import static com.github.bhlangonijr.chesslib.Square.H8;

import com.fathzer.chess.utils.adapters.BoardExplorer;
import com.github.bhlangonijr.chesslib.Board;
Expand All @@ -9,7 +72,7 @@
import com.github.bhlangonijr.chesslib.Side;
import com.github.bhlangonijr.chesslib.Square;

class ChessLibBoardExplorer implements BoardExplorer {
public class ChessLibBoardExplorer implements BoardExplorer {
static final Square[] SQUARES;

static {
Expand All @@ -29,7 +92,7 @@ class ChessLibBoardExplorer implements BoardExplorer {
private int index;
private int piece;

ChessLibBoardExplorer(Board board) {
public ChessLibBoardExplorer(Board board) {
this.board = board;
this.index = -1;
next();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.fathzer.jchess.chesslib;

import static com.github.bhlangonijr.chesslib.PieceType.*;
import static com.github.bhlangonijr.chesslib.CastleRight.*;
import static com.github.bhlangonijr.chesslib.CastleRight.KING_SIDE;
import static com.github.bhlangonijr.chesslib.CastleRight.QUEEN_SIDE;
import static com.github.bhlangonijr.chesslib.PieceType.KING;
import static com.github.bhlangonijr.chesslib.PieceType.PAWN;

import com.fathzer.chess.utils.adapters.MoveData;
import com.github.bhlangonijr.chesslib.Piece;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.util.List;
import java.util.function.Function;

import com.fathzer.games.MoveGenerator;
import com.fathzer.games.HashProvider;
import com.fathzer.games.MoveGenerator;
import com.fathzer.games.Status;
import com.fathzer.games.util.MoveList;
import com.fathzer.games.util.SelectiveComparator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.fathzer.games.ai.evaluation.ZeroSumEvaluator;
import com.fathzer.games.util.Stack;
import com.fathzer.jchess.chesslib.ChessLibMoveGenerator;
import com.github.bhlangonijr.chesslib.Board;
import com.github.bhlangonijr.chesslib.move.Move;

/** An incremental implementation of the simplified evaluator described at <a href="https://www.chessprogramming.org/Simplified_Evaluation_Function">https://www.chessprogramming.org/Simplified_Evaluation_Function</a>
* <br>This only works with 8*8 games and exactly one king per Color.
Expand Down Expand Up @@ -57,7 +59,16 @@ public void init(B board) {
public void prepareMove(B board, M move) {
if (moveData.update(move, board)) {
buildToCommit();
toCommit.update(moveData);
ChessLibMoveGenerator lChessLibMoveGenerator = (ChessLibMoveGenerator)board;
Board boardBeforeMove = lChessLibMoveGenerator.getBoard();
Board bordAfterMove = boardBeforeMove.clone();
Move lMove = (Move)move;
bordAfterMove.doMove(lMove, true);


toCommit.update(moveData, bordAfterMove);


}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,98 +1,63 @@
package com.fathzer.jchess.chesslib.ai.eval.hbpg2;

import static com.fathzer.chess.utils.Pieces.KING;


import com.fathzer.chess.utils.adapters.BoardExplorer;
import com.fathzer.jchess.chesslib.ai.eval.hbpg2.additional.ChessEvalAdditionalElems;
import com.github.bhlangonijr.chesslib.Board;

/** The state of the evaluator.
*/
class Hb2BasicState extends Hb2FastPhaseDetector {
int pointsMg;
int pointsEg;
int pointsPosMg;
int pointsPosEg;
int whiteKingIndex;
int blackKingIndex;
int computedPhase;
Board board;

class Hb2BasicState extends Hb2ElementaryBasicState {
protected ChessEvalAdditionalElems chessEvalAdditionalElems;

Hb2BasicState() {
super();
}

void copyTo(Hb2BasicState other) {
public void copyTo(Hb2BasicState other) {
super.copyTo(other);
other.pointsMg = pointsMg;
other.pointsEg= pointsEg;
other.pointsPosMg = pointsPosMg;
other.pointsPosEg= pointsPosEg;
other.blackKingIndex = blackKingIndex;
other.whiteKingIndex = whiteKingIndex;
other.computedPhase = computedPhase;
other.board = board;
other.chessEvalAdditionalElems = new ChessEvalAdditionalElems(this.chessEvalAdditionalElems);




}



Hb2BasicState(BoardExplorer explorer, Board board) {
this.board = board;
this.pointsMg = 0;
this.pointsEg = 0;
this.pointsPosMg = 0;
this.pointsPosEg = 0;
this.computedPhase = 0;
do {
final int p = explorer.getPiece();
// add(p);
final int kind = Math.abs(p);
final int index = explorer.getIndex();
final boolean isBlack = p<0;
if (kind!=KING) {
int incMg = Hb2SimplifiedEvaluatorBase.getRawValueMg(kind);
int incEg = Hb2SimplifiedEvaluatorBase.getRawValueEg(kind);
if (isBlack) {
pointsMg -= incMg;
pointsEg -= incEg;
} else {
pointsMg += incMg;
pointsEg += incEg;
}
} else if (isBlack) {
this.blackKingIndex = index;
} else {
this.whiteKingIndex = index;
}



if (kind!=KING) {
int incPosMg = Hb2SimplifiedEvaluatorBase.getPositionValueMg(kind, isBlack, index);
int incPosEg = Hb2SimplifiedEvaluatorBase.getPositionValueEg(kind, isBlack, index);
if (isBlack) {
pointsPosMg -= incPosMg;
pointsPosEg -= incPosEg;
} else {
pointsPosMg += incPosMg;
pointsPosEg += incPosEg;
}
}
computedPhase += Hb2Phase.getPhaseValue(kind);
} while (explorer.next());
super(explorer, board);
chessEvalAdditionalElems = new ChessEvalAdditionalElems( board);


}

public ChessEvalAdditionalElems getChessEvalAdditionalElems() {
return chessEvalAdditionalElems;
}

public void setChessEvalAdditionalElems(ChessEvalAdditionalElems chessEvalAdditionalElems) {
this.chessEvalAdditionalElems = chessEvalAdditionalElems;
}


int evaluateAsWhite() {

// pointsMg = material only! The white material minus the black material in the middlegame.
// pointsEg = material only! The white material minus the black material in the endgame.
int phase = getPhaseForTaperedEval(computedPhase);
// int phase = Hb2Phase.getPhaseForTaperedEval(computedPhase);
// gets the borned phase: necessary for the tapered evaluation
int phase= (computedPhase > Hb2Phase.PHASE_UPPER_BOUND?Hb2Phase.PHASE_UPPER_BOUND:computedPhase);
// int pointsPosMg = Hb2SimplifiedEvaluatorBase.getPositionValueMg(board);
// int pointsPosEg = Hb2SimplifiedEvaluatorBase.getPositionValueEg(board);
int evalMg = pointsMg + pointsPosMg + Hb2SimplifiedEvaluatorBase.getKingPositionsValueMg(whiteKingIndex, blackKingIndex);
int evalEg = pointsEg + pointsPosEg+ Hb2SimplifiedEvaluatorBase.getKingPositionsValueEg(whiteKingIndex, blackKingIndex);
int posKingMg = Hb2SimplifiedEvaluatorBase.getKingPositionsValueMg(whiteKingIndex, blackKingIndex);
int posKingEg = Hb2SimplifiedEvaluatorBase.getKingPositionsValueEg(whiteKingIndex, blackKingIndex);
int evalMg = pointsMg + pointsPosMg + posKingMg + chessEvalAdditionalElems.getContribMg();
int evalEg = pointsEg + pointsPosEg+ posKingEg + chessEvalAdditionalElems.getContribEg();

return ((evalMg * phase + evalEg * (Hb2Phase.NB_INCR_PHASE-phase)) / Hb2Phase.NB_INCR_PHASE);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.fathzer.jchess.chesslib.ai.eval.hbpg2;

public class Hb2BitboardsUtils {

// This class is built mainly thanks to shameful plagiarism from https://github.com/Luecx/Chess.git (GNU Public License)
// That said, the guy took probably his inspiration from the C++ code described here: https://habr.com/ru/articles/682122/

public static final long H_FILE = 0x8080808080808080L;
public static final long G_FILE = H_FILE >>> 1;
public static final long F_FILE = H_FILE >>> 2;
public static final long E_FILE = H_FILE >>> 3;
public static final long D_FILE = H_FILE >>> 4;
public static final long C_FILE = H_FILE >>> 5;
public static final long B_FILE = H_FILE >>> 6;
public static final long A_FILE = H_FILE >>> 7;

public static final long RANK_1 = 0x00000000000000FFL;
public static final long RANK_2 = RANK_1 << 8;
public static final long RANK_3 = RANK_1 << 16;
public static final long RANK_4 = RANK_1 << 24;
public static final long RANK_5 = RANK_1 << 32;
public static final long RANK_6 = RANK_1 << 40;
public static final long RANK_7 = RANK_1 << 48;
public static final long RANK_8 = RANK_1 << 56;

public static final long NOT_A_FILE = ~A_FILE;
public static final long NOT_H_FILE = ~H_FILE;
public static final long NOT_RANK_1 = ~RANK_1;
public static final long NOT_RANK_8 = ~RANK_8;

public static final long[] WHITE_PASSED_PAWNS_MASK = new long[] { 0x0303030303030300L, 0x0707070707070700L,
0x0e0e0e0e0e0e0e00L, 0x1c1c1c1c1c1c1c00L, 0x3838383838383800L, 0x7070707070707000L, 0xe0e0e0e0e0e0e000L,
0xc0c0c0c0c0c0c000L, 0x0303030303030000L, 0x0707070707070000L, 0x0e0e0e0e0e0e0000L, 0x1c1c1c1c1c1c0000L,
0x3838383838380000L, 0x7070707070700000L, 0xe0e0e0e0e0e00000L, 0xc0c0c0c0c0c00000L, 0x0303030303000000L,
0x0707070707000000L, 0x0e0e0e0e0e000000L, 0x1c1c1c1c1c000000L, 0x3838383838000000L, 0x7070707070000000L,
0xe0e0e0e0e0000000L, 0xc0c0c0c0c0000000L, 0x0303030300000000L, 0x0707070700000000L, 0x0e0e0e0e00000000L,
0x1c1c1c1c00000000L, 0x3838383800000000L, 0x7070707000000000L, 0xe0e0e0e000000000L, 0xc0c0c0c000000000L,
0x0303030000000000L, 0x0707070000000000L, 0x0e0e0e0000000000L, 0x1c1c1c0000000000L, 0x3838380000000000L,
0x7070700000000000L, 0xe0e0e00000000000L, 0xc0c0c00000000000L, 0x0303000000000000L, 0x0707000000000000L,
0x0e0e000000000000L, 0x1c1c000000000000L, 0x3838000000000000L, 0x7070000000000000L, 0xe0e0000000000000L,
0xc0c0000000000000L, 0x0300000000000000L, 0x0700000000000000L, 0x0e00000000000000L, 0x1c00000000000000L,
0x3800000000000000L, 0x7000000000000000L, 0xe000000000000000L, 0xc000000000000000L, 0x0000000000000000L,
0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L,
0x0000000000000000L, 0x0000000000000000L };

public static final long[] BLACK_PASSED_PAWNS_MASK = new long[] {

0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L,
0x0000000000000000L, 0x0000000000000000L, 0x0000000000000000L, 0x0000000000000003L, 0x0000000000000007L,
0x000000000000000eL, 0x000000000000001cL, 0x0000000000000038L, 0x0000000000000070L, 0x00000000000000e0L,
0x00000000000000c0L, 0x0000000000000303L, 0x0000000000000707L, 0x0000000000000e0eL, 0x0000000000001c1cL,
0x0000000000003838L, 0x0000000000007070L, 0x000000000000e0e0L, 0x000000000000c0c0L, 0x0000000000030303L,
0x0000000000070707L, 0x00000000000e0e0eL, 0x00000000001c1c1cL, 0x0000000000383838L, 0x0000000000707070L,
0x0000000000e0e0e0L, 0x0000000000c0c0c0L, 0x0000000003030303L, 0x0000000007070707L, 0x000000000e0e0e0eL,
0x000000001c1c1c1cL, 0x0000000038383838L, 0x0000000070707070L, 0x00000000e0e0e0e0L, 0x00000000c0c0c0c0L,
0x0000000303030303L, 0x0000000707070707L, 0x0000000e0e0e0e0eL, 0x0000001c1c1c1c1cL, 0x0000003838383838L,
0x0000007070707070L, 0x000000e0e0e0e0e0L, 0x000000c0c0c0c0c0L, 0x0000030303030303L, 0x0000070707070707L,
0x00000e0e0e0e0e0eL, 0x00001c1c1c1c1c1cL, 0x0000383838383838L, 0x0000707070707070L, 0x0000e0e0e0e0e0e0L,
0x0000c0c0c0c0c0c0L, 0x0003030303030303L, 0x0007070707070707L, 0x000e0e0e0e0e0e0eL, 0x001c1c1c1c1c1c1cL,
0x0038383838383838L, 0x0070707070707070L, 0x00e0e0e0e0e0e0e0L, 0x00c0c0c0c0c0c0c0L, };

public static final long shiftWest(long b) {
return (b >>> 1) & NOT_H_FILE;
}

public static final long shiftEast(long b) {
return (b << 1) & NOT_A_FILE;
}

public static final long shiftSouth(long b) {
return b >>> 8;
}

public static final long shiftNorth(long b) {
return b << 8;
}

public static final long shiftNorthEast(long b) {
return (b << 9) & NOT_A_FILE;
}

public static final long shiftSouthEast(long b) {
return (b >>> 7) & NOT_A_FILE;
}

public static final long shiftSouthWest(long b) {
return (b >>> 9) & NOT_H_FILE;
}

public static final long shiftNorthWest(long b) {
return (b << 7) & NOT_H_FILE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fathzer.jchess.chesslib.ai.eval.hbpg2;

public abstract class Hb2ChessConstants {
public static final int NB_RANKS = 8;
public static final int NB_FILES = 8;
public static final int INDEX_MAX_RANK = NB_RANKS-1;
public static final int INDEX_MAX_FILE = NB_FILES-1;



public static final int NB_MAX_PAWNS_TAKEN_INTO_ACCOUNT_FOR_MALUS_DOUBLED_PAWNS = 2; // Beyond, it could be counterproductive...according to many famous chess engines specs

}
Loading

0 comments on commit 9e826dc

Please sign in to comment.