searches return a result class instead of an int
This commit is contained in:
1 parent
22a9a692c5
commit
abe07eb844
2 files changed
+38
-26
No files matched your search
+21
-26
@@ -5,8 +5,6 @@
|
|||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
static inline int nodes = 0;
|
static inline int nodes = 0;
|
||||||
static inline Move bestMove = Move();
|
|
||||||
static inline int bestScore = -50000;
|
|
||||||
static inline int ply = 0;
|
static inline int ply = 0;
|
||||||
|
|
||||||
static int ScoreMove(Board &board, Move &move) {
|
static int ScoreMove(Board &board, Move &move) {
|
||||||
@@ -59,7 +57,7 @@ void ListScores(Board &board) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int Quiescence(Board board, int alpha, int beta) {
|
static SearchResults Quiescence(Board board, int alpha, int beta) {
|
||||||
nodes++;
|
nodes++;
|
||||||
|
|
||||||
int staticScore = Evaluate(board);
|
int staticScore = Evaluate(board);
|
||||||
@@ -76,12 +74,13 @@ static int Quiescence(Board board, int alpha, int beta) {
|
|||||||
|
|
||||||
SortMoves(board);
|
SortMoves(board);
|
||||||
|
|
||||||
|
SearchResults results;
|
||||||
|
|
||||||
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;
|
||||||
board.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
int score = -Quiescence(board, -beta, -alpha);
|
int score = -Quiescence(copy, -beta, -alpha).score;
|
||||||
board = copy;
|
|
||||||
|
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
return beta;
|
return beta;
|
||||||
@@ -89,17 +88,18 @@ static int Quiescence(Board board, int alpha, int beta) {
|
|||||||
|
|
||||||
if (score > alpha) {
|
if (score > alpha) {
|
||||||
alpha = score;
|
alpha = score;
|
||||||
|
results.bestMove = board.moveList[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return alpha;
|
results.score = alpha;
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int PVS(Board board, int depth, int alpha, int beta, int isRoot) {
|
static SearchResults PVS(Board board, int depth, int alpha, int beta) {
|
||||||
|
|
||||||
nodes++;
|
nodes++;
|
||||||
if (depth == 0) return Quiescence(board, alpha, beta);
|
if (depth == 0) return Quiescence(board, alpha, beta).score;
|
||||||
|
|
||||||
int score = -50000;
|
int score = -50000;
|
||||||
|
|
||||||
@@ -116,6 +116,8 @@ static int PVS(Board board, int depth, int alpha, int beta, int isRoot) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SearchResults results;
|
||||||
|
|
||||||
// For all moves
|
// For all moves
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
@@ -125,18 +127,18 @@ static int PVS(Board board, int depth, int alpha, int beta, int isRoot) {
|
|||||||
if (!i) {
|
if (!i) {
|
||||||
// Full search
|
// Full search
|
||||||
ply++;
|
ply++;
|
||||||
score = -PVS(copy, depth - 1, -beta, -alpha, false);
|
score = -PVS(copy, depth - 1, -beta, -alpha).score;
|
||||||
ply--;
|
ply--;
|
||||||
} else {
|
} else {
|
||||||
// Quick search
|
// Quick search
|
||||||
ply++;
|
ply++;
|
||||||
score = -PVS(copy, depth - 1, -alpha-1, -alpha, false);
|
score = -PVS(copy, depth - 1, -alpha-1, -alpha).score;
|
||||||
ply--;
|
ply--;
|
||||||
|
|
||||||
if (score > alpha && beta - alpha > 1) {
|
if (score > alpha && beta - alpha > 1) {
|
||||||
// We have to do full search
|
// We have to do full search
|
||||||
ply++;
|
ply++;
|
||||||
score = -PVS(copy, depth - 1, -beta, -alpha, false);
|
score = -PVS(copy, depth - 1, -beta, -alpha).score;
|
||||||
ply--;
|
ply--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,25 +149,18 @@ static int PVS(Board board, int depth, int alpha, int beta, int isRoot) {
|
|||||||
|
|
||||||
if (score > alpha) {
|
if (score > alpha) {
|
||||||
alpha = score;
|
alpha = score;
|
||||||
}
|
results.bestMove = board.moveList[i];
|
||||||
|
|
||||||
if (isRoot) {
|
|
||||||
if (alpha > bestScore) {
|
|
||||||
bestScore = alpha;
|
|
||||||
bestMove = copy.moveList[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return alpha;
|
results.score = alpha;
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SearchPosition(Board &board, int depth) {
|
void SearchPosition(Board &board, int depth) {
|
||||||
bestScore = -50000;
|
SearchResults results = PVS(board, depth, -50000, 50000);
|
||||||
bestMove = Move();
|
|
||||||
PVS(board, depth, -50000, 50000, true);
|
|
||||||
std::cout << "info depth " << depth << " nodes " << nodes << '\n';
|
std::cout << "info depth " << depth << " nodes " << nodes << '\n';
|
||||||
std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()]
|
std::cout << "bestmove " << squareCoords[results.bestMove.MoveFrom()]
|
||||||
<< squareCoords[bestMove.MoveTo()] << '\n';
|
<< squareCoords[results.bestMove.MoveTo()] << '\n';
|
||||||
nodes = 0;
|
nodes = 0;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
|
||||||
|
class SearchResults {
|
||||||
|
public:
|
||||||
|
int score = 0;
|
||||||
|
Move bestMove = Move();
|
||||||
|
|
||||||
|
SearchResults(){}
|
||||||
|
|
||||||
|
SearchResults(int pScore, Move pMove) {
|
||||||
|
score = pScore;
|
||||||
|
bestMove = pMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
SearchResults(int pScore) {
|
||||||
|
score = pScore;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void SearchPosition(Board &board, int depth);
|
void SearchPosition(Board &board, int depth);
|
||||||
|
|
||||||
constexpr int moveScoreTable[6][6] = {
|
constexpr int moveScoreTable[6][6] = {
|
||||||
|
|||||||
Reference in new issue
Block a user