implemented Move Ordering (MVV-LVA)
This commit is contained in:
1 parent
328c9e93dc
commit
cb403943cb
3 files changed
+71
-5
No files matched your search
+58
-4
@@ -1,12 +1,61 @@
|
|||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "evaluate.h"
|
#include "evaluate.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
static inline int ply = 0;
|
static inline int ply = 0;
|
||||||
static inline Move bestMove = Move();
|
static inline Move bestMove = Move();
|
||||||
static inline int bestScore = -50000;
|
static inline int bestScore = -50000;
|
||||||
|
|
||||||
int Quiescence(Board board, int alpha, int beta) {
|
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];
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SortMoves(Board &board) {
|
||||||
|
std::array<int, 218> scores;
|
||||||
|
std::array<int, 218> indices;
|
||||||
|
|
||||||
|
// Initialize scores and indices
|
||||||
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
scores[i] = ScoreMove(board, board.moveList[i]);
|
||||||
|
indices[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort indices based on scores
|
||||||
|
std::sort(indices.begin(), indices.begin() + board.currentMoveIndex,
|
||||||
|
[&scores](int a, int b) {
|
||||||
|
return scores[a] > scores[b];
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a temporary move list and reorder the moveList based on the sorted indices
|
||||||
|
std::array<Move, 218> sortedMoves;
|
||||||
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
sortedMoves[i] = board.moveList[indices[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign the sorted moves back to the board's move list
|
||||||
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
board.moveList[i] = sortedMoves[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListScores(Board &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';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int Quiescence(Board board, int alpha, int beta) {
|
||||||
int staticScore = Evaluate(board);
|
int staticScore = Evaluate(board);
|
||||||
|
|
||||||
if (staticScore >= beta) {
|
if (staticScore >= beta) {
|
||||||
@@ -19,6 +68,8 @@ int Quiescence(Board board, int alpha, int beta) {
|
|||||||
|
|
||||||
GenerateMoves(board, board.sideToMove);
|
GenerateMoves(board, board.sideToMove);
|
||||||
|
|
||||||
|
SortMoves(board);
|
||||||
|
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
if (board.moveList[i].IsCapture()) {
|
if (board.moveList[i].IsCapture()) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
@@ -39,14 +90,17 @@ int Quiescence(Board board, int alpha, int beta) {
|
|||||||
return alpha;
|
return alpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
|
static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
|
||||||
if (depth == 0) {
|
if (depth == 0) {
|
||||||
return Quiescence(board, -50000, 50000);
|
return Quiescence(board, alpha, beta);
|
||||||
//return Evaluate(board);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GenerateMoves(board, board.sideToMove);
|
GenerateMoves(board, board.sideToMove);
|
||||||
|
|
||||||
|
SortMoves(board);
|
||||||
|
|
||||||
|
if (board.InCheck(board.sideToMove)) depth++;
|
||||||
|
|
||||||
if (board.currentMoveIndex == 0) {
|
if (board.currentMoveIndex == 0) {
|
||||||
if (board.InCheck(board.sideToMove)) { // checkmate
|
if (board.InCheck(board.sideToMove)) { // checkmate
|
||||||
return -49000 + ply;
|
return -49000 + ply;
|
||||||
|
|||||||
@@ -2,3 +2,15 @@
|
|||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
|
||||||
void SearchPosition(Board &board, int depth);
|
void SearchPosition(Board &board, int depth);
|
||||||
|
|
||||||
|
constexpr int moveScoreTable[6][6] = {
|
||||||
|
105, 205, 305, 405, 505, 605,
|
||||||
|
104, 204, 304, 404, 504, 604,
|
||||||
|
103, 203, 303, 403, 503, 603,
|
||||||
|
102, 202, 302, 402, 502, 602,
|
||||||
|
101, 201, 301, 401, 501, 601,
|
||||||
|
100, 200, 300, 400, 500, 600
|
||||||
|
};
|
||||||
|
|
||||||
|
void SortMoves(Board &board);
|
||||||
|
void ListScores(Board &board);
|
||||||
+1
-1
@@ -12,7 +12,7 @@ enum COLOR {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum PIECE {
|
enum PIECE {
|
||||||
Pawn,
|
Pawn = 0,
|
||||||
Knight,
|
Knight,
|
||||||
Bishop,
|
Bishop,
|
||||||
Rook,
|
Rook,
|
||||||
|
|||||||
Reference in new issue
Block a user