added killer moves to move ordering

This commit is contained in:
rektdie committed 2024-06-22 05:50:19 +02:00
1 parent cb403943cb
commit 8c4a157797
5 files changed
+37 -8

No files matched your search

+1 -2
View File
@@ -49,8 +49,7 @@ int Bitboard::getLS1BIndex() const {
return -1;
}
Bitboard Bitboard::getOccupancy(int index, Bitboard attackMask) {
Bitboard occupancy;
Bitboard Bitboard::getOccupancy(int index, Bitboard attackMask) { Bitboard occupancy;
int squareCount = attackMask.PopCount();
for (int count = 0; count < squareCount; count++) {
+4
View File
@@ -58,4 +58,8 @@ public:
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
std::cout << " (" << moveTypes[GetFlags()] << ")\n";
}
operator uint16_t() {
return m_move;
}
};
+30 -5
View File
@@ -3,22 +3,35 @@
#include "evaluate.h"
#include <algorithm>
static inline int nodes = 0;
static inline int ply = 0;
static inline Move bestMove = Move();
static inline int bestScore = -50000;
// killer moves [id][ply]
static inline Move killerMoves[2][64];
static int ScoreMove(Board &board, Move &move) {
const int attackerType = board.GetPieceType(move.MoveFrom());
const int targetType = board.GetPieceType(move.MoveTo());
if (move.IsCapture()) {
return moveScoreTable[attackerType][targetType];
return moveScoreTable[attackerType][targetType] + 10000;
} else {
return 0;
// 1st killer move
if (killerMoves[0][ply] == move) {
return 9000;
}
// 2nd killer move
else if (killerMoves[1][ply] == move) {
return 8000;
}
}
return 0;
}
void SortMoves(Board &board) {
static void SortMoves(Board &board) {
std::array<int, 218> scores;
std::array<int, 218> indices;
@@ -47,15 +60,18 @@ void SortMoves(Board &board) {
}
void ListScores(Board &board) {
SortMoves(board);
for (int i = 0; i < board.currentMoveIndex; i++) {
Move currentMove = board.moveList[i];
std::cout << squareCoords[currentMove.MoveFrom()] << squareCoords[currentMove.MoveTo()];
std::cout << ": " << ScoreMove(board, currentMove) << '\n';
std::cout << squareCoords[currentMove.MoveFrom()] << squareCoords[currentMove.MoveTo()]; std::cout << ": " << ScoreMove(board, currentMove) << '\n';
}
}
static int Quiescence(Board board, int alpha, int beta) {
nodes++;
int staticScore = Evaluate(board);
if (staticScore >= beta) {
@@ -95,6 +111,8 @@ static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
return Quiescence(board, alpha, beta);
}
nodes++;
GenerateMoves(board, board.sideToMove);
SortMoves(board);
@@ -121,6 +139,10 @@ static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
ply--;
if (score >= beta) {
// store killer moves
killerMoves[1][ply] = killerMoves[0][ply];
killerMoves[0][ply] = board.moveList[i];
return beta;
}
@@ -142,10 +164,13 @@ static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
return max;
}
void SearchPosition(Board &board, int depth) {
bestScore = -50000;
bestMove = Move();
NegaMax(board, depth, -50000, 50000, true);
std::cout << "info depth " << depth << " nodes " << nodes << '\n';
std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()]
<< squareCoords[bestMove.MoveTo()] << '\n';
}
-1
View File
@@ -12,5 +12,4 @@ constexpr int moveScoreTable[6][6] = {
100, 200, 300, 400, 500, 600
};
void SortMoves(Board &board);
void ListScores(Board &board);
+2
View File
@@ -5,6 +5,8 @@
using U64 = uint64_t;
constexpr char StartingFen[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
constexpr char KiwiPeteFen[] = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -";
constexpr char KillerFen[] = "rnbqkb1r/pp1p1pPp/8/2p1pP2/1P1P4/3P3P/P1P1P3/RNBQKBNR w KQkq e6 0 1";
enum COLOR {
White,