-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
212 lines (199 loc) · 4.05 KB
/
Main.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <utility>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include "Game.h"
#include "Util.h"
#include "Test.h"
#include "Engine.h"
using namespace std;
void test();
void perft(Game & game, int depth);
void menu();
int main(int argc, char ** argv)
{
string fen;
Game game;
Move move;
int depth;
int thinkingTime;
int pvSort;
// If there are any command line arguments
if (argc > 1)
{
string arg(argv[1]);
if (arg == "-t")
{
test();
return 0;
}
else if (arg == "-f" && argc > 2)
{
fen = argv[2];
}
else if (arg == "-m" && argc > 4)
{
thinkingTime = stoi(argv[2]);
pvSort = stoi(argv[3]);
fen = argv[4];
game.init(fen);
Engine engine(&game, {static_cast<bool>(pvSort), true});
engine.think(thinkingTime * 1000);
move = engine.move();
cout << getMoveString(move);
return 0;
}
else if (arg == "-p" && argc > 3)
{
depth = stoi(argv[2]);
fen = argv[3];
game.init(fen);
if (depth > 0)
perft(game, depth);
else
cout << "Depth must be > 0\n";
return 0;
}
else if (arg == "-e" && argc > 3)
{
thinkingTime = stoi(argv[2]);
fen = argv[3];
game.init(fen); // Always remember to call game.init()!!!!!
Engine engine(&game, {true, false}); // Book is turned off because we always want an evaluation.
engine.think(thinkingTime * 1000);
move = engine.move();
tt_entry e;
engine.tt.get(game.pos.hash, e);
cout << (game.pos.side == white ? e.eval : -e.eval) << ' ';
cout << getMoveString(move);
return 0;
}
else
{
cout << "Unrecognized command line argument\n";
cout << "Valid options are:\n\n";
cout << "\t-t: run tests\n";
cout << "\t-m <time> <pvsort> \"<fen>\": get engine move\n";
cout << "\t-p <depth> \"<fen>\": run perft test\n";
cout << "\t-f \"<fen>\": load game from FEN string\n\n";
cout << "\t-e <time> \"<fen>\": get evaluation and engine move\n";
return 0;
}
}
string s;
if (fen.empty())
game.init();
else
game.init(fen);
Engine engine(&game, &cout);
cout << "Type m to print menu\n";
while (1)
{
cout << "> ";
cin >> s;
if (s == "q")
{
cout << "Bye!\n";
exit(0);
}
else if (s == "d")
{
game.display(cout);
}
else if (s == "m")
{
menu();
}
else if (s == "b")
{
if (!game.takeBack())
{
cout << "Unable to take back move. Try something else.\n";
continue;
}
}
else if (s == "p")
{
cout << "depth = ";
cin >> depth;
while (depth < 1)
{
cout << "Depth must be > 1. Try again: ";
cin >> depth;
}
perft(game, depth);
}
else if (s == "i")
{
printPosition(cout, game.pos);
}
else if (s == "e")
{
cout << "static evaluation = " << game.Evaluation() << '\n';
}
else if (s == "c")
{
engine.think(5000);
move = engine.move();
cout << getMoveString(move) << '\n';
}
else
{
move.x = parseMove(s);
if (move.m.mtype & 128)
{
cout << "Invalid command or move string. Try again.\n";
continue;
}
move = game.getMove(move.x);
if (!game.makeMove(move))
cout << "Illegal move. Try again.\n";
}
}
return 0;
}
void test()
{
testEval();
// testPerft();
testBoardIsAttacked();
testIntegration();
testFENParser();
testInCheck();
testThreeMoveRepetition();
testTranspositionTable();
}
void perft(Game & game, int depth)
{
Move move;
cout << "Perft " << depth << '\n';
vector<int> moves;
moves.reserve(40);
game.genMoves(moves);
long nodes, totalNodes = 0;
for (int x: moves)
{
move.x = x;
if (game.makeMove(move))
{
nodes = game.Perft(depth - 1);
cout << getMoveString(move) << ' ' << nodes << '\n';
totalNodes += nodes;
game.takeBack();
}
}
cout << "Total nodes = " << totalNodes << '\n';
}
void menu()
{
cout << "\n\tEnter a move (e.g. e2e4)\n";
cout << "\tm to print menu\n";
cout << "\tb to take a move back\n";
cout << "\tp to run perft test\n";
cout << "\ti to print info on current position\n";
cout << "\te to print static evaluation\n";
cout << "\td to display board\n";
cout << "\tc to get computer move\n";
cout << "\tq to quit\n";
}