|
14 | 14 | #endif
|
15 | 15 |
|
16 | 16 | #ifdef wasm
|
| 17 | + |
17 | 18 | #include <emscripten/em_macros.h>
|
18 | 19 |
|
19 | 20 | // The main board object for the web app.
|
20 | 21 | ChessBoard board;
|
| 22 | +// The main search object for the web app. |
| 23 | +Search search = Search(board); |
21 | 24 |
|
22 | 25 | extern "C" {
|
23 | 26 | EMSCRIPTEN_KEEPALIVE
|
24 | 27 | void init() {
|
25 |
| - board.hashCodes.initialize(); |
26 |
| - board.setStartingPosition(); |
| 28 | + board.hashCodes.initialize(); |
| 29 | + board.setStartingPosition(); |
27 | 30 | Search::tt.loadOpenings(board);
|
28 | 31 | }
|
29 | 32 |
|
30 | 33 | 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 | + }); |
43 | 40 |
|
44 |
| -EMSCRIPTEN_KEEPALIVE |
45 |
| -char* parseandmove(const char* movestring){ |
46 |
| - Move move = parseMove(movestring, board); |
47 |
| - board.makeMove(move); |
48 | 41 | const std::string fen = board.fen();
|
49 | 42 | const int length = fen.length();
|
50 |
| - char* chararray = new char[length + 1]; |
| 43 | + char *chararray = new char[length + 1]; |
51 | 44 | strcpy(chararray, fen.c_str());
|
52 | 45 | return chararray;
|
53 | 46 | }
|
54 | 47 |
|
55 | 48 | 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; |
64 | 57 | }
|
65 | 58 |
|
66 | 59 | 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; |
90 | 83 | }
|
91 | 84 |
|
92 | 85 | 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; |
135 | 127 | }
|
136 | 128 |
|
137 | 129 | 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; |
152 | 144 | }
|
153 | 145 |
|
154 | 146 |
|
155 | 147 | EMSCRIPTEN_KEEPALIVE
|
156 | 148 | 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; |
166 | 157 | }
|
167 | 158 |
|
168 | 159 |
|
169 | 160 | EMSCRIPTEN_KEEPALIVE
|
170 |
| -char* getBestMove(const int milliseconds) { |
171 |
| - Search search = Search(board); |
| 161 | +void startSearch() { |
| 162 | + search.reset(); |
172 | 163 | 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; |
189 | 164 | }
|
190 | 165 |
|
191 | 166 | 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; |
196 | 192 | }
|
197 | 193 |
|
198 | 194 |
|
199 | 195 | 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); |
203 | 199 |
|
204 |
| - return MoveGenerator::perft(depth, perftBoard); |
| 200 | + return MoveGenerator::perft(depth, perftBoard); |
205 | 201 | }
|
206 | 202 | }
|
207 | 203 | #endif
|
208 | 204 |
|
209 | 205 | int main() {
|
210 | 206 | #ifndef wasm
|
211 |
| - Interface::CLI interface; |
212 |
| - interface.start(); |
| 207 | + Interface::CLI interface; |
| 208 | + interface.start(); |
213 | 209 | #endif
|
214 |
| - return 0; |
| 210 | + return 0; |
215 | 211 | }
|
0 commit comments