-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_gui.cpp
More file actions
88 lines (72 loc) · 2.77 KB
/
main_gui.cpp
File metadata and controls
88 lines (72 loc) · 2.77 KB
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
#include "Renderer.h"
#include "position.h"
#include "movegen.h"
#include "search.h"
#include "helpers.h"
#include <optional>
#include <vector>
#include <thread>
#include <atomic>
#include <mutex>
constexpr int SEARCH_DEPTH = 5; // increase for stronger play
constexpr bool HUMAN_IS_WHITE = true; // human plays white
int main() {
Renderer renderer;
if (!renderer.init()) return -1;
Position pos;
pos.setStartingPosition();
Search search;
std::optional<Square> selected;
std::vector<Square> highlights;
Colour humanColour = HUMAN_IS_WHITE ? Colour::White : Colour::Black;
while (renderer.isOpen()) {
if (!renderer.pollEvents()) break;
// ── Engine turn ───────────────────────────────────────────────
if (pos.sideToMove != humanColour) {
SearchResult result = search.findBestMove(pos, SEARCH_DEPTH);
if (!result.bestMove.isNull()) {
Piece moving = pos.pieceOn(result.bestMove.from());
uint8_t prevC = pos.castlingRights;
Square prevE = pos.enPassantSquare;
int prevH = pos.halfMoveClock;
pos.makeMove(result.bestMove);
}
selected.reset();
highlights.clear();
}
// ── Human turn ────────────────────────────────────────────────
if (renderer.lastClick.has_value() &&
pos.sideToMove == humanColour) {
Square clicked = renderer.lastClick.value();
if (selected.has_value()) {
Square from = selected.value();
if (tryMove(pos, from, clicked)) {
selected.reset();
highlights.clear();
}
else {
// Reselect if clicking own piece
Piece p = pos.pieceOn(clicked);
if (!p.isEmpty() && p.colour == humanColour) {
selected = clicked;
highlights = legalDestinations(pos, clicked);
}
else {
selected.reset();
highlights.clear();
}
}
}
else {
// Select a piece
Piece p = pos.pieceOn(clicked);
if (!p.isEmpty() && p.colour == humanColour) {
selected = clicked;
highlights = legalDestinations(pos, clicked);
}
}
}
renderer.render(pos, selected, highlights);
}
return 0;
}