This commit is contained in:
2022-12-23 23:57:29 +01:00
parent 87adca7e66
commit cb0fcc4702
14 changed files with 208 additions and 52 deletions

View File

@@ -1,8 +1,11 @@
#include <iostream>
#include "Search.hpp"
#include "Move.hpp"
#include "Board.hpp"
int Search::search(Node *node, const Board &board, unsigned depth) {
#include <algorithm>
int Search::search(Node *node, const Board &board, unsigned depth, int alpha, int beta) {
if (depth == 0) {
return evaluate(board);
@@ -13,6 +16,7 @@ int Search::search(Node *node, const Board &board, unsigned depth) {
if (moves.empty()) {
return evaluate(board);
}
std::stable_sort(moves.begin(), moves.end());
int maxValue = kNegInfinity;
for (auto &kMove: moves) {
@@ -20,13 +24,18 @@ int Search::search(Node *node, const Board &board, unsigned depth) {
nodeBoard.makeMove(kMove);
Node child(kMove);
child.value = search(&child, nodeBoard, depth - 1);
child.value = search(&child, nodeBoard, depth - 1, -beta, -alpha);
auto value = -child.value;
if (value > maxValue) {
maxValue = value;
node->next = std::make_unique<Node>(child);
}
alpha = std::max(alpha, value);
if (alpha >= beta) {
break;
}
}
node->value = maxValue;
@@ -39,9 +48,11 @@ int Search::evaluate(const Board &board) {
return board.evaluate();
}
PrincipalVariation Search::start(const Board &board) {
auto rootMoves = Board::MoveVec();
board.pseudoLegalMoves(rootMoves);
std::stable_sort(rootMoves.begin(), rootMoves.end());
if (rootMoves.size() <= 1) {
auto b = board;
@@ -56,7 +67,7 @@ PrincipalVariation Search::start(const Board &board) {
Board nodeBoard = board;
nodeBoard.makeMove(kMove);
auto value = -search(&node, nodeBoard, 4);
auto value = -search(&node, nodeBoard, 5, kNegInfinity, kPosInfinity);
if (value > best->value) {
best->child = std::make_unique<Node>(node);
best->board = nodeBoard;