Skip to content

Commit 4b22d74

Browse files
stoppable search
1 parent e3b7b71 commit 4b22d74

File tree

7 files changed

+199
-165
lines changed

7 files changed

+199
-165
lines changed

.idea/misc.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <iostream>
21
#include "ChessBoard.h"
32
#include "Piece.h"
43
#include "Move.h"
@@ -10,11 +9,14 @@
109
#include <cstring>
1110
#include <string>
1211

12+
#ifndef wasm
1313
#include "CLI.h"
14+
#endif
1415

1516
#ifdef wasm
1617
#include <emscripten/em_macros.h>
1718

19+
// The main board object for the web app.
1820
ChessBoard board;
1921

2022
extern "C" {
@@ -166,7 +168,9 @@ int eval() {
166168

167169
EMSCRIPTEN_KEEPALIVE
168170
char* getBestMove(const int milliseconds) {
169-
const Move bestMove = Search::search(board, milliseconds);
171+
Search search = Search(board);
172+
search.doSearch();
173+
const Move bestMove = search.endSearch(milliseconds);
170174
std::string bestMoveJSON = R"({"start":")";
171175
bestMoveJSON += Util::positionToString(bestMove.start);
172176
bestMoveJSON += R"(","end":")";

src/CLI.cpp

+33-17
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ namespace Interface {
1717
std::getline(std::cin, input);
1818
currentInstruction = interpret(input);
1919

20-
std::unique_lock lk(m);
21-
cv.wait(lk, [this] { return ready; });
22-
ready = false;
20+
readySemaphore.acquire();
21+
22+
if (currentInstruction.command != ucinewgame && currentInstruction.command != position){
23+
readySemaphore.release();
24+
}
25+
2326
std::thread thread(&CLI::handleInstruction, this, currentInstruction);
2427
thread.detach();
2528
}
@@ -47,14 +50,15 @@ namespace Interface {
4750
std::cout << "id author " << "Exclusivefrog28" << std::endl;
4851
std::cout << "uciok" << std::endl;
4952
break;
50-
case isready:
53+
case isready: {
5154
std::cout << "readyok" << std::endl;
55+
}
5256
break;
53-
case ucinewgame:
54-
ready = false;
57+
case ucinewgame: {
5558
Search::tt.clear();
5659
board = ChessBoard();
57-
ready = true;
60+
readySemaphore.release();
61+
}
5862
break;
5963
case position: {
6064
int startIndex = 1;
@@ -76,10 +80,11 @@ namespace Interface {
7680
board.makeMove(parseMove(instr.args[i], board));
7781
}
7882
}
79-
break;
83+
readySemaphore.release();
8084
}
85+
break;
8186
case go: {
82-
int timeOut = 3000;
87+
int timeOut = 0;
8388

8489
if (!instr.args.empty()) {
8590
const std::string arg = instr.args[0];
@@ -97,20 +102,31 @@ namespace Interface {
97102
}
98103
}
99104

100-
const Move bestMove = Search::search(board, timeOut);
101-
board.makeMove(bestMove);
105+
searching = true;
106+
search.reset();
107+
search.doSearch();
102108

103-
std::cout << "bestmove " << bestMove << std::endl;
109+
if (timeOut > 0) {
110+
Move bestMove = search.endSearch(timeOut);
111+
searching = false;
112+
board.makeMove(bestMove);
104113

114+
std::cout << "bestmove " << bestMove << std::endl;
115+
}
116+
}
105117
break;
118+
case stop: {
119+
if (searching) {
120+
Move bestMove = search.endSearch(0);
121+
searching = false;
122+
board.makeMove(bestMove);
123+
124+
std::cout << "bestmove " << bestMove << std::endl;
125+
}
106126
}
127+
break;
107128
default:
108129
break;
109130
}
110-
{
111-
std::lock_guard lk(m);
112-
ready = true;
113-
}
114-
cv.notify_one();
115131
}
116132
}

src/CLI.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include <unordered_map>
55
#include <vector>
66
#include <condition_variable>
7+
#include <semaphore>
78

89
#include "ChessBoard.h"
10+
#include "Search.h"
911

1012
//Universal Chess Interface implementation
1113
namespace Interface {
@@ -39,18 +41,19 @@ namespace Interface {
3941

4042
class CLI {
4143
public:
42-
void start();
44+
void start();
4345

4446
private:
4547
ChessBoard board;
4648

47-
std::mutex m;
48-
std::condition_variable cv;
49-
bool ready = true;
49+
std::binary_semaphore readySemaphore{1};
5050

5151
static Instruction interpret(const std::string&string);
5252

5353
void handleInstruction(const Instruction&instr);
54+
55+
Search search = Search(board);
56+
bool searching = false;
5457
};
5558
}
5659

src/Move.h

+74-73
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,97 @@
11
#ifndef CHESSENGINE_MOVE_H
22
#define CHESSENGINE_MOVE_H
3+
34
#include <cstdint>
45
#include <format>
56
#include <iostream>
67
#include "Piece.h"
78
#include "Util.h"
89

910
namespace Moves {
10-
using Pieces::Color, Pieces::Type;
11+
using Pieces::Color, Pieces::Type;
1112

12-
enum MoveFlag {
13-
QUIET = 0,
14-
CAPTUREPAWN = 1,
15-
CAPTUREKNIGHT = 2,
16-
CAPTUREBISHOP = 3,
17-
CAPTUREROOK = 4,
18-
CAPTUREQUEEN = 5,
19-
ENPASSANT = 6,
20-
DOUBLEPAWNPUSH = 7,
21-
CASTLEKINGSIDE = 8,
22-
CASTLEQUEENSIDE = 9,
23-
};
13+
enum MoveFlag {
14+
QUIET = 0,
15+
CAPTUREPAWN = 1,
16+
CAPTUREKNIGHT = 2,
17+
CAPTUREBISHOP = 3,
18+
CAPTUREROOK = 4,
19+
CAPTUREQUEEN = 5,
20+
ENPASSANT = 6,
21+
DOUBLEPAWNPUSH = 7,
22+
CASTLEKINGSIDE = 8,
23+
CASTLEQUEENSIDE = 9,
24+
};
2425

25-
struct Move {
26-
int8_t start : 8;
27-
int8_t end : 8;
28-
Type promotionType : 4;
29-
MoveFlag flag : 4;
30-
Color player : 1;
26+
struct Move {
27+
int8_t start: 8;
28+
int8_t end: 8;
29+
Type promotionType: 4;
30+
MoveFlag flag: 4;
31+
Color player: 1;
3132

32-
bool operator==(const Move&other) const {
33-
return (start == other.start && end == other.end && promotionType == other.promotionType &&
34-
flag == other.flag && player == other.player);
35-
}
33+
bool operator==(const Move &other) const {
34+
return (start == other.start && end == other.end && promotionType == other.promotionType &&
35+
flag == other.flag && player == other.player);
36+
}
3637

37-
std::string toString() const {
38-
std::string str = Util::positionToString(start);
39-
str += Util::positionToString(end);
40-
if (promotionType != Pieces::EMPTY) str += Util::pieceToString(promotionType, Color::BLACK);
41-
return str;
42-
}
38+
std::string toString() const {
39+
std::string str = Util::positionToString(start);
40+
str += Util::positionToString(end);
41+
if (promotionType != Pieces::EMPTY) str += Util::pieceToString(promotionType, Color::BLACK);
42+
return str;
43+
}
4344

44-
bool tactical() const {
45-
return (flag > 0 && flag < 7) || promotionType != Pieces::EMPTY;
46-
}
47-
};
45+
bool tactical() const {
46+
return (flag > 0 && flag < 7) || promotionType != Pieces::EMPTY;
47+
}
48+
};
4849

49-
inline std::ostream& operator<<(std::ostream&os, const Move&move) {
50-
os << move.toString();
51-
return os;
52-
}
50+
inline std::ostream &operator<<(std::ostream &os, const Move &move) {
51+
os << move.toString();
52+
return os;
53+
}
5354

5455

55-
constexpr Move NULL_MOVE = Move{0, 0, Pieces::EMPTY, QUIET, Color::WHITE};
56+
constexpr Move NULL_MOVE = Move{0, 0, Pieces::EMPTY, QUIET, Color::WHITE};
5657

57-
constexpr int8_t MAILBOX[120] = {
58-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60-
-1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
61-
-1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
62-
-1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
63-
-1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
64-
-1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
65-
-1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
66-
-1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
67-
-1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
68-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69-
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
70-
};
58+
constexpr int8_t MAILBOX[120] = {
59+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61+
-1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
62+
-1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
63+
-1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
64+
-1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
65+
-1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
66+
-1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
67+
-1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
68+
-1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
69+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
71+
};
7172

72-
constexpr int8_t MAILBOX64[64] = {
73-
21, 22, 23, 24, 25, 26, 27, 28,
74-
31, 32, 33, 34, 35, 36, 37, 38,
75-
41, 42, 43, 44, 45, 46, 47, 48,
76-
51, 52, 53, 54, 55, 56, 57, 58,
77-
61, 62, 63, 64, 65, 66, 67, 68,
78-
71, 72, 73, 74, 75, 76, 77, 78,
79-
81, 82, 83, 84, 85, 86, 87, 88,
80-
91, 92, 93, 94, 95, 96, 97, 98
81-
};
73+
constexpr int8_t MAILBOX64[64] = {
74+
21, 22, 23, 24, 25, 26, 27, 28,
75+
31, 32, 33, 34, 35, 36, 37, 38,
76+
41, 42, 43, 44, 45, 46, 47, 48,
77+
51, 52, 53, 54, 55, 56, 57, 58,
78+
61, 62, 63, 64, 65, 66, 67, 68,
79+
71, 72, 73, 74, 75, 76, 77, 78,
80+
81, 82, 83, 84, 85, 86, 87, 88,
81+
91, 92, 93, 94, 95, 96, 97, 98
82+
};
8283

83-
constexpr bool SLIDE[7] = {false, false, false, true, true, true, false};
84-
constexpr int8_t OFFSETS[7] = {0, 4, 8, 4, 4, 8, 8};
85-
constexpr int8_t OFFSET[7][8] = {
86-
{0, 0, 0, 0, 0, 0, 0, 0},
87-
{8, 9, 11, 16, 0, 0, 0, 0},
88-
{-21, -19, -12, -8, 8, 12, 19, 21},
89-
{-11, -9, 9, 11, 0, 0, 0, 0},
90-
{-10, -1, 1, 10, 0, 0, 0, 0},
91-
{-11, -10, -9, -1, 11, 10, 9, 1},
92-
{-11, -10, -9, -1, 11, 10, 9, 1}
93-
};
84+
constexpr bool SLIDE[7] = {false, false, false, true, true, true, false};
85+
constexpr int8_t OFFSETS[7] = {0, 4, 8, 4, 4, 8, 8};
86+
constexpr int8_t OFFSET[7][8] = {
87+
{0, 0, 0, 0, 0, 0, 0, 0},
88+
{8, 9, 11, 16, 0, 0, 0, 0},
89+
{-21, -19, -12, -8, 8, 12, 19, 21},
90+
{-11, -9, 9, 11, 0, 0, 0, 0},
91+
{-10, -1, 1, 10, 0, 0, 0, 0},
92+
{-11, -10, -9, -1, 11, 10, 9, 1},
93+
{-11, -10, -9, -1, 11, 10, 9, 1}
94+
};
9495
}
9596

9697
#endif //CHESSENGINE_MOVE_H

0 commit comments

Comments
 (0)