-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.cpp
More file actions
142 lines (120 loc) · 4.49 KB
/
tester.cpp
File metadata and controls
142 lines (120 loc) · 4.49 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
140
141
142
// This will be the C++ file that the engine will be stored in!
// We will be using a traditional search algorithm called Minimax
// I have already tested MCTS algorithm but there is too much RAM and computtion powre needed for that algorithm
// This will be the C++ engine that will be called from the main.py
#include <iostream>
#include <string>
#include <limits>
#include <cctype>
#include <vector>
#include "chess.hpp" // Will be using Disservin's Chess.hpp
TYPE YES: Fanum
PLEASE TRY AGAIN TYPE YES: Tax
PLEASE TRY AGAIN TYPE YES: Ohio
PLEASE TRY AGAIN TYPE YES:
// Now we will use a class for the engine
using namespace chess;
class ChessEngine {
public:
ChessEngine(int depth) : maxDepth(depth) {}
// EVALUATES THE PROVIDED FEN STRING
// STOPS AFTER THE FIRST SPLIT OF THE STRING BECAUSE I DONT CARE ABOUT CASTLING
int evaluate(const std::string& fen, bool isMaximizingPlayer) {
static constexpr int pieceValues[12] = {
100, 320, 330, 510, 880, 20000, // White pieces
-100, -320, -330, -510, -880, -20000 // Black pieces
};
int materialScore = 0;
for (size_t i = 0; i < fen.size(); ++i) {
char ch = fen[i];
if (std::isspace(ch)) break; // Stop processing pieces
if (std::isdigit(ch)) continue; // Empty squares
int pieceValueIndex;
if (std::isupper(ch)) {
pieceValueIndex = ch - 'P';
} else {
pieceValueIndex = ch - 'p' + 6;
}
if (pieceValueIndex >= 0 && pieceValueIndex < 12) {
materialScore += pieceValues[pieceValueIndex];
}
}
// std::cout<<materialScore<<std::endl;
return materialScore;
}
// GLORIOUS MINIMAX WITH ALPHA_BETA
int minimax(Board& board, int depth, bool isMaximizingPlayer, int alpha, int beta) {
if (depth == 0) {
return evaluate(board.getFen(), isMaximizingPlayer);
}
Movelist moves;
movegen::legalmoves(moves, board);
if (isMaximizingPlayer) {
int maxEval = std::numeric_limits<int>::min();
for (const auto& move : moves) {
Board nextBoard(board);
nextBoard.makeMove(move);
int eval = minimax(nextBoard, depth - 1, false, alpha, beta);
maxEval = std::max(maxEval, eval);
alpha = std::max(alpha, eval);
if (beta <= alpha) {
break;
}
}
return maxEval;
} else {
int minEval = std::numeric_limits<int>::max();
for (const auto& move : moves) {
Board nextBoard(board);
nextBoard.makeMove(move);
int eval = minimax(nextBoard, depth - 1, true, alpha, beta);
minEval = std::min(minEval, eval);
beta = std::min(beta, eval);
if (beta <= alpha) {
break;
}
}
return minEval;
}
}
std::string getBestMove(const std::string& fen, bool isMaximizingPlayer) {
Board board(fen);
Movelist moves;
movegen::legalmoves(moves, board);
int bestEval = isMaximizingPlayer ? std::numeric_limits<int>::min()
: std::numeric_limits<int>::max();
std::string bestMove;
for (const auto& move : moves) {
Board nextBoard(board);
nextBoard.makeMove(move);
int eval = minimax(nextBoard, maxDepth - 1, !isMaximizingPlayer,
std::numeric_limits<int>::min(),
std::numeric_limits<int>::max());
if (isMaximizingPlayer) {
if (eval > bestEval) {
bestEval = eval;
bestMove = uci::moveToUci(move);
}
} else {
if (eval < bestEval) {
bestEval = eval;
bestMove = uci::moveToUci(move);
}
}
}
return bestMove;
}
private:
int maxDepth;
};
int main(int argc, char *argv[]) {
if (argc < 3) {
std::cerr << "Usage: ./a.out <FEN> <isWhite>" << std::endl;
return 1;
}
std::string fen = argv[1];
bool isWhite = std::string(argv[2]) == "1";
ChessEngine engine(3);
std::cout << engine.getBestMove(fen, isWhite) << std::endl;
return 0;
}