-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
211 lines (182 loc) · 5.52 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
#include "ChessBoard.h"
#include "Piece.h"
#include "Move.h"
#include "Util.h"
#include "MoveGenerator.h"
#include "Evaluator.h"
#include "Search.h"
#include "MoveParser.h"
#include <cstring>
#include <string>
#ifndef wasm
#include "CLI.h"
#endif
#ifdef wasm
#include <emscripten/em_macros.h>
// The main board object for the web app.
ChessBoard board;
// The main search object for the web app.
Search search = Search(board);
extern "C" {
EMSCRIPTEN_KEEPALIVE
void init() {
board.hashCodes.initialize();
board.setStartingPosition();
Search::tt.loadOpenings(board);
}
EMSCRIPTEN_KEEPALIVE
char *move(const int start, const int end, int flag, int promotionType, int player) {
board.makeMove({
static_cast<int_fast8_t>(start), static_cast<int_fast8_t>(end),
static_cast<Pieces::Type>(promotionType),
static_cast<MoveFlag>(flag), static_cast<Pieces::Color>(player)
});
const std::string fen = board.fen();
const int length = fen.length();
char *chararray = new char[length + 1];
strcpy(chararray, fen.c_str());
return chararray;
}
EMSCRIPTEN_KEEPALIVE
char *unmove() {
board.unMakeMove();
const std::string fen = board.fen();
const int length = fen.length();
char *chararray = new char[length + 1];
strcpy(chararray, fen.c_str());
return chararray;
}
EMSCRIPTEN_KEEPALIVE
char *listPieces() {
std::string string = "White pieces: ";
for (Piece &piece: board.whitePieces) {
string += "[";
string += Util::pieceToString(piece.type, Pieces::WHITE);
string += ", ";
string += std::to_string(piece.position);
string += "] ";
}
string += "\nBlack pieces:";
for (Piece &piece: board.blackPieces) {
string += "[";
string += Util::pieceToString(piece.type, Pieces::BLACK);
string += ", ";
string += std::to_string(piece.position);
string += "] ";
}
string += "\n";
const int length = string.length();
char *chararray = new char[length + 1];
strcpy(chararray, string.c_str());
return chararray;
}
EMSCRIPTEN_KEEPALIVE
char *getMoves() {
std::string string;
std::vector<Move> moves = MoveGenerator::pseudoLegalMoves(board);
bool empty = true;
string += "{";
string += R"("moves" : [)";
for (const Move move: moves) {
if (MoveGenerator::isLegalMove(board, move)) {
empty = false;
string += "{";
string += R"("start":")";
string += Util::positionToString(move.start);
string += R"(","end":")";
string += Util::positionToString(move.end);
string += R"(","promotionType":")";
string += std::to_string(move.promotionType);
string += R"(","flag":")";
string += std::to_string(move.flag);
string += R"(","player":")";
string += std::to_string(move.player);
string += "\"},";
}
}
if (!empty) {
string.pop_back();
}
string += R"(],"state":)";
if (empty) {
if (MoveGenerator::inCheck(board, board.sideToMove)) string += R"("checkmate")";
else string += R"("stalemate")";
} else string += R"("normal")";
string += "}";
const int length = string.length();
char *chararray = new char[length + 1];
strcpy(chararray, string.c_str());
return chararray;
}
EMSCRIPTEN_KEEPALIVE
char *getAttacks() {
std::string attackedSquares;
for (int_fast8_t i = 0; i < 64; ++i) {
if (MoveGenerator::isSquareAttacked(board, i, board.sideToMove)) {
attackedSquares += Util::positionToString(i);
attackedSquares += " ";
}
}
if (!attackedSquares.empty()) attackedSquares.pop_back();
const int length = attackedSquares.length();
char *chararray = new char[length + 1];
strcpy(chararray, attackedSquares.c_str());
return chararray;
}
EMSCRIPTEN_KEEPALIVE
int eval() {
int score;
if (Search::tt.contains(board.hashCode)) {
TranspositionTable::Entry entry = Search::tt.getEntry(board.hashCode, 0);
score = entry.score;
} else {
score = Evaluator::evaluate(board);
}
return score;
}
EMSCRIPTEN_KEEPALIVE
void startSearch() {
search.reset();
search.doSearch();
}
EMSCRIPTEN_KEEPALIVE
//timeout in milliseconds
char *stopSearch(const int timeout) {
const Move bestMove = search.endSearch(timeout);
std::string bestMoveJSON = R"({"start":")";
bestMoveJSON += Util::positionToString(bestMove.start);
bestMoveJSON += R"(","end":")";
bestMoveJSON += Util::positionToString(bestMove.end);
bestMoveJSON += R"(","promotionType":")";
bestMoveJSON += std::to_string(bestMove.promotionType);
bestMoveJSON += R"(","flag":")";
bestMoveJSON += std::to_string(bestMove.flag);
bestMoveJSON += R"(","player":")";
bestMoveJSON += std::to_string(bestMove.player);
bestMoveJSON += "\"}";
const int length = bestMoveJSON.length();
char *chararray = new char[length + 1];
strcpy(chararray, bestMoveJSON.c_str());
return chararray;
}
EMSCRIPTEN_KEEPALIVE
int setFen(char *fen) {
std::string fenString(fen);
board.setPosition(fenString);
return board.sideToMove;
}
EMSCRIPTEN_KEEPALIVE
int runPerft(int depth, const char *fen) {
ChessBoard perftBoard;
perftBoard.setPosition(fen);
return MoveGenerator::perft(depth, perftBoard);
}
}
#endif
int main() {
#ifndef wasm
Interface::CLI interface;
interface.start();
#endif
return 0;
}