-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
139 lines (123 loc) · 4.22 KB
/
search.cpp
File metadata and controls
139 lines (123 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <limits>
#include <map>
#include "chess.hpp"
using namespace chess;
double evaluate(const Board& board) {
if (board.isGameOver().second == chess::GameResult::WIN) {
return (board.sideToMove() == Color::WHITE)
? 10000.0
: -10000.0;
}
if (board.isGameOver().second == GameResult::DRAW) {
return 0.0;
}
double material = 0.0;
const std::map<PieceType, double> values = {
{PieceType::PAWN, 1.0},
{PieceType::KNIGHT, 3.0},
{PieceType::BISHOP, 3.1},
{PieceType::ROOK, 5.0},
{PieceType::QUEEN, 9.0}
};
for (const auto& [piece, val] : values) {
material += val * board.pieces(piece, Color::WHITE).count();
material -= val * board.pieces(piece, Color::BLACK).count();
}
return material;
}
double alpha_beta(Board& board, int depth, double alpha, double beta) {
if (depth == 0 || !(board.isGameOver().second == GameResult::NONE)) {
return evaluate(board);
}
Movelist moves;
movegen::legalmoves(moves, board);
if (board.sideToMove() == Color::WHITE) {
double max_eval = -std::numeric_limits<double>::infinity();
for (const auto& move : moves) {
board.makeMove(move);
double eval = alpha_beta(board, depth - 1, alpha, beta);
board.unmakeMove(move);
max_eval = std::max(max_eval, eval);
alpha = std::max(alpha, eval);
if (beta <= alpha) break;
}
return max_eval;
} else {
double min_eval = std::numeric_limits<double>::infinity();
for (const auto& move : moves) {
board.makeMove(move);
double eval = alpha_beta(board, depth - 1, alpha, beta);
board.unmakeMove(move);
min_eval = std::min(min_eval, eval);
beta = std::min(beta, eval);
if (beta <= alpha) break;
}
return min_eval;
}
}
Move get_best_move(Board& board, int depth) {
Movelist moves;
movegen::legalmoves(moves, board);
if (moves.empty()) {
return Move::NO_MOVE;
}
double alpha = -std::numeric_limits<double>::infinity();
double beta = std::numeric_limits<double>::infinity();
double best_value;
Move best_move = moves[0];
if (board.sideToMove() == Color::WHITE) {
best_value = -std::numeric_limits<double>::infinity();
for (const auto& move : moves) {
board.makeMove(move);
double value = alpha_beta(board, depth - 1, alpha, beta);
board.unmakeMove(move);
if (value > best_value) {
best_value = value;
best_move = move;
}
alpha = std::max(alpha, best_value);
if (beta <= alpha) break;
}
} else {
best_value = std::numeric_limits<double>::infinity();
for (const auto& move : moves) {
board.makeMove(move);
double value = alpha_beta(board, depth - 1, alpha, beta);
board.unmakeMove(move);
if (value < best_value) {
best_value = value;
best_move = move;
}
beta = std::min(beta, best_value);
if (beta <= alpha) break;
}
}
return best_move;
}
int main() {
Board board = Board();
int depth = 6;
while (board.isGameOver().second == GameResult::NONE) {
std::cout << "Current Board:\n" << board << "\n";
if (board.sideToMove() == Color::WHITE) {
// Human player (White)
std::string move_str;
std::cout << "Enter your move (e.g. e2e4): ";
std::cin >> move_str;
try {
Move move = uci::uciToMove(board, move_str);
board.makeMove(move);
} catch (const std::exception& e) {
std::cout << "Invalid move! Try again.\n";
continue;
}
} else {
// Engine player (Black)
Move best_move = get_best_move(board, depth);
std::cout << "Engine plays: " << uci::moveToUci(best_move) << "\n";
board.makeMove(best_move);
}
}
std::cout << "Final Board:\n" << board << "\n";
return 0;
}