Skip to content

Commit 4a5a3dc

Browse files
exposed search stopping
1 parent 4b22d74 commit 4a5a3dc

File tree

5 files changed

+171
-178
lines changed

5 files changed

+171
-178
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (WASM)
3434
COMPILE_FLAGS "-pthread"
3535
)
3636
set_target_properties(ChessEngine PROPERTIES
37-
LINK_FLAGS "-O3 --closure 1 -sMODULARIZE -sEXPORT_ES6=1 -sEXPORT_NAME=Engine -sENVIRONMENT=web,worker -pthread -sPTHREAD_POOL_SIZE=8 --embed-file ../assets@/ -sINITIAL_MEMORY=512MB -sEXPORTED_FUNCTIONS=['_main','_init','_move','_parseandmove','_unmove','_listPieces','_getMoves','_getAttacks','_setFen','_runPerft','_eval','_getBestMove'] -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap']"
37+
LINK_FLAGS "-O3 --closure 1 -sMODULARIZE -sEXPORT_ES6=1 -sEXPORT_NAME=Engine -sENVIRONMENT=web,worker -pthread -sPTHREAD_POOL_SIZE=8 --embed-file ../assets@/ -sINITIAL_MEMORY=512MB -sEXPORTED_FUNCTIONS=['_main','_init','_move','_unmove','_listPieces','_getMoves','_getAttacks','_setFen','_runPerft','_eval','_startSearch','_stopSearch'] -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap']"
3838
COMPILE_FLAGS "-pthread"
3939
)
4040
#-s ENVIRONMENT=web / -s ENVIRONMENT=node

main.cpp

+140-144
Original file line numberDiff line numberDiff line change
@@ -14,202 +14,198 @@
1414
#endif
1515

1616
#ifdef wasm
17+
1718
#include <emscripten/em_macros.h>
1819

1920
// The main board object for the web app.
2021
ChessBoard board;
22+
// The main search object for the web app.
23+
Search search = Search(board);
2124

2225
extern "C" {
2326
EMSCRIPTEN_KEEPALIVE
2427
void init() {
25-
board.hashCodes.initialize();
26-
board.setStartingPosition();
28+
board.hashCodes.initialize();
29+
board.setStartingPosition();
2730
Search::tt.loadOpenings(board);
2831
}
2932

3033
EMSCRIPTEN_KEEPALIVE
31-
char* move(const int start, const int end, int flag, int promotionType, int player) {
32-
board.makeMove({
33-
static_cast<int_fast8_t>(start), static_cast<int_fast8_t>(end), static_cast<Pieces::Type>(promotionType),
34-
static_cast<MoveFlag>(flag), static_cast<Pieces::Color>(player)
35-
});
36-
37-
const std::string fen = board.fen();
38-
const int length = fen.length();
39-
char* chararray = new char[length + 1];
40-
strcpy(chararray, fen.c_str());
41-
return chararray;
42-
}
34+
char *move(const int start, const int end, int flag, int promotionType, int player) {
35+
board.makeMove({
36+
static_cast<int_fast8_t>(start), static_cast<int_fast8_t>(end),
37+
static_cast<Pieces::Type>(promotionType),
38+
static_cast<MoveFlag>(flag), static_cast<Pieces::Color>(player)
39+
});
4340

44-
EMSCRIPTEN_KEEPALIVE
45-
char* parseandmove(const char* movestring){
46-
Move move = parseMove(movestring, board);
47-
board.makeMove(move);
4841
const std::string fen = board.fen();
4942
const int length = fen.length();
50-
char* chararray = new char[length + 1];
43+
char *chararray = new char[length + 1];
5144
strcpy(chararray, fen.c_str());
5245
return chararray;
5346
}
5447

5548
EMSCRIPTEN_KEEPALIVE
56-
char* unmove() {
57-
board.unMakeMove();
58-
59-
const std::string fen = board.fen();
60-
const int length = fen.length();
61-
char* chararray = new char[length + 1];
62-
strcpy(chararray, fen.c_str());
63-
return chararray;
49+
char *unmove() {
50+
board.unMakeMove();
51+
52+
const std::string fen = board.fen();
53+
const int length = fen.length();
54+
char *chararray = new char[length + 1];
55+
strcpy(chararray, fen.c_str());
56+
return chararray;
6457
}
6558

6659
EMSCRIPTEN_KEEPALIVE
67-
char* listPieces() {
68-
std::string string = "White pieces: ";
69-
for (Piece&piece: board.whitePieces) {
70-
string += "[";
71-
string += Util::pieceToString(piece.type, Pieces::WHITE);
72-
string += ", ";
73-
string += std::to_string(piece.position);
74-
string += "] ";
75-
}
76-
string += "\nBlack pieces:";
77-
for (Piece&piece: board.blackPieces) {
78-
string += "[";
79-
string += Util::pieceToString(piece.type, Pieces::BLACK);
80-
string += ", ";
81-
string += std::to_string(piece.position);
82-
string += "] ";
83-
}
84-
string += "\n";
85-
86-
const int length = string.length();
87-
char* chararray = new char[length + 1];
88-
strcpy(chararray, string.c_str());
89-
return chararray;
60+
char *listPieces() {
61+
std::string string = "White pieces: ";
62+
for (Piece &piece: board.whitePieces) {
63+
string += "[";
64+
string += Util::pieceToString(piece.type, Pieces::WHITE);
65+
string += ", ";
66+
string += std::to_string(piece.position);
67+
string += "] ";
68+
}
69+
string += "\nBlack pieces:";
70+
for (Piece &piece: board.blackPieces) {
71+
string += "[";
72+
string += Util::pieceToString(piece.type, Pieces::BLACK);
73+
string += ", ";
74+
string += std::to_string(piece.position);
75+
string += "] ";
76+
}
77+
string += "\n";
78+
79+
const int length = string.length();
80+
char *chararray = new char[length + 1];
81+
strcpy(chararray, string.c_str());
82+
return chararray;
9083
}
9184

9285
EMSCRIPTEN_KEEPALIVE
93-
char* getMoves() {
94-
std::string string;
95-
std::vector<Move> moves = MoveGenerator::pseudoLegalMoves(board);
96-
97-
bool empty = true;
98-
99-
string += "{";
100-
string += R"("moves" : [)";
101-
for (const Move move: moves) {
102-
if (MoveGenerator::isLegalMove(board, move)) {
103-
empty = false;
104-
string += "{";
105-
string += R"("start":")";
106-
string += Util::positionToString(move.start);
107-
string += R"(","end":")";
108-
string += Util::positionToString(move.end);
109-
string += R"(","promotionType":")";
110-
string += std::to_string(move.promotionType);
111-
string += R"(","flag":")";
112-
string += std::to_string(move.flag);
113-
string += R"(","player":")";
114-
string += std::to_string(move.player);
115-
string += "\"},";
116-
}
117-
}
118-
if (!empty) {
119-
string.pop_back();
120-
}
121-
string += R"(],"state":)";
122-
123-
if (empty) {
124-
if (MoveGenerator::inCheck(board, board.sideToMove)) string += R"("checkmate")";
125-
else string += R"("stalemate")";
126-
}
127-
else string += R"("normal")";
128-
129-
string += "}";
130-
131-
const int length = string.length();
132-
char* chararray = new char[length + 1];
133-
strcpy(chararray, string.c_str());
134-
return chararray;
86+
char *getMoves() {
87+
std::string string;
88+
std::vector<Move> moves = MoveGenerator::pseudoLegalMoves(board);
89+
90+
bool empty = true;
91+
92+
string += "{";
93+
string += R"("moves" : [)";
94+
for (const Move move: moves) {
95+
if (MoveGenerator::isLegalMove(board, move)) {
96+
empty = false;
97+
string += "{";
98+
string += R"("start":")";
99+
string += Util::positionToString(move.start);
100+
string += R"(","end":")";
101+
string += Util::positionToString(move.end);
102+
string += R"(","promotionType":")";
103+
string += std::to_string(move.promotionType);
104+
string += R"(","flag":")";
105+
string += std::to_string(move.flag);
106+
string += R"(","player":")";
107+
string += std::to_string(move.player);
108+
string += "\"},";
109+
}
110+
}
111+
if (!empty) {
112+
string.pop_back();
113+
}
114+
string += R"(],"state":)";
115+
116+
if (empty) {
117+
if (MoveGenerator::inCheck(board, board.sideToMove)) string += R"("checkmate")";
118+
else string += R"("stalemate")";
119+
} else string += R"("normal")";
120+
121+
string += "}";
122+
123+
const int length = string.length();
124+
char *chararray = new char[length + 1];
125+
strcpy(chararray, string.c_str());
126+
return chararray;
135127
}
136128

137129
EMSCRIPTEN_KEEPALIVE
138-
char* getAttacks() {
139-
std::string attackedSquares;
140-
for (int_fast8_t i = 0; i < 64; ++i) {
141-
if (MoveGenerator::isSquareAttacked(board, i, board.sideToMove)) {
142-
attackedSquares += Util::positionToString(i);
143-
attackedSquares += " ";
144-
}
145-
}
146-
if (!attackedSquares.empty()) attackedSquares.pop_back();
147-
148-
const int length = attackedSquares.length();
149-
char* chararray = new char[length + 1];
150-
strcpy(chararray, attackedSquares.c_str());
151-
return chararray;
130+
char *getAttacks() {
131+
std::string attackedSquares;
132+
for (int_fast8_t i = 0; i < 64; ++i) {
133+
if (MoveGenerator::isSquareAttacked(board, i, board.sideToMove)) {
134+
attackedSquares += Util::positionToString(i);
135+
attackedSquares += " ";
136+
}
137+
}
138+
if (!attackedSquares.empty()) attackedSquares.pop_back();
139+
140+
const int length = attackedSquares.length();
141+
char *chararray = new char[length + 1];
142+
strcpy(chararray, attackedSquares.c_str());
143+
return chararray;
152144
}
153145

154146

155147
EMSCRIPTEN_KEEPALIVE
156148
int eval() {
157-
int score;
158-
if (Search::tt.contains(board.hashCode)) {
159-
TranspositionTable::Entry entry = Search::tt.getEntry(board.hashCode, 0);
160-
score = entry.score;
161-
}
162-
else {
163-
score = Evaluator::evaluate(board);
164-
}
165-
return score;
149+
int score;
150+
if (Search::tt.contains(board.hashCode)) {
151+
TranspositionTable::Entry entry = Search::tt.getEntry(board.hashCode, 0);
152+
score = entry.score;
153+
} else {
154+
score = Evaluator::evaluate(board);
155+
}
156+
return score;
166157
}
167158

168159

169160
EMSCRIPTEN_KEEPALIVE
170-
char* getBestMove(const int milliseconds) {
171-
Search search = Search(board);
161+
void startSearch() {
162+
search.reset();
172163
search.doSearch();
173-
const Move bestMove = search.endSearch(milliseconds);
174-
std::string bestMoveJSON = R"({"start":")";
175-
bestMoveJSON += Util::positionToString(bestMove.start);
176-
bestMoveJSON += R"(","end":")";
177-
bestMoveJSON += Util::positionToString(bestMove.end);
178-
bestMoveJSON += R"(","promotionType":")";
179-
bestMoveJSON += std::to_string(bestMove.promotionType);
180-
bestMoveJSON += R"(","flag":")";
181-
bestMoveJSON += std::to_string(bestMove.flag);
182-
bestMoveJSON += R"(","player":")";
183-
bestMoveJSON += std::to_string(bestMove.player);
184-
bestMoveJSON += "\"}";
185-
const int length = bestMoveJSON.length();
186-
char* chararray = new char[length + 1];
187-
strcpy(chararray, bestMoveJSON.c_str());
188-
return chararray;
189164
}
190165

191166
EMSCRIPTEN_KEEPALIVE
192-
int setFen(char* fen) {
193-
std::string fenString(fen);
194-
board.setPosition(fenString);
195-
return board.sideToMove;
167+
//timeout in milliseconds
168+
char *stopSearch(const int timeout) {
169+
const Move bestMove = search.endSearch(timeout);
170+
std::string bestMoveJSON = R"({"start":")";
171+
bestMoveJSON += Util::positionToString(bestMove.start);
172+
bestMoveJSON += R"(","end":")";
173+
bestMoveJSON += Util::positionToString(bestMove.end);
174+
bestMoveJSON += R"(","promotionType":")";
175+
bestMoveJSON += std::to_string(bestMove.promotionType);
176+
bestMoveJSON += R"(","flag":")";
177+
bestMoveJSON += std::to_string(bestMove.flag);
178+
bestMoveJSON += R"(","player":")";
179+
bestMoveJSON += std::to_string(bestMove.player);
180+
bestMoveJSON += "\"}";
181+
const int length = bestMoveJSON.length();
182+
char *chararray = new char[length + 1];
183+
strcpy(chararray, bestMoveJSON.c_str());
184+
return chararray;
185+
}
186+
187+
EMSCRIPTEN_KEEPALIVE
188+
int setFen(char *fen) {
189+
std::string fenString(fen);
190+
board.setPosition(fenString);
191+
return board.sideToMove;
196192
}
197193

198194

199195
EMSCRIPTEN_KEEPALIVE
200-
int runPerft(int depth, const char* fen) {
201-
ChessBoard perftBoard;
202-
perftBoard.setPosition(fen);
196+
int runPerft(int depth, const char *fen) {
197+
ChessBoard perftBoard;
198+
perftBoard.setPosition(fen);
203199

204-
return MoveGenerator::perft(depth, perftBoard);
200+
return MoveGenerator::perft(depth, perftBoard);
205201
}
206202
}
207203
#endif
208204

209205
int main() {
210206
#ifndef wasm
211-
Interface::CLI interface;
212-
interface.start();
207+
Interface::CLI interface;
208+
interface.start();
213209
#endif
214-
return 0;
210+
return 0;
215211
}

0 commit comments

Comments
 (0)