-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
216 lines (181 loc) · 5.28 KB
/
main.cpp
File metadata and controls
216 lines (181 loc) · 5.28 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
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
212
213
214
215
216
#include "ai.hpp"
#include "board.hpp"
#include "board_stats.hpp"
#include "figure_moves.hpp"
#include "timer.hpp"
#include <iostream>
namespace {
constexpr auto COMPUTER_PLAY_TIME = 2000u;
inline bool pointValid(const Point& p)
{
return Board::validIndex(p.x, p.y);
}
bool detectCastling(const Point& from, const Point& to, const Board& board)
{
if (figure(board.get(from.x, from.y)) != Figure::KING_IDLE) {
return false;
}
if (to.x == 6 && figure(board.get(7, to.y)) == Figure::ROOK_IDLE) {
return true;
}
if (to.x == 2 && figure(board.get(0, to.y)) == Figure::ROOK_IDLE) {
return true;
}
return false;
}
bool detectEnPassantCapture(Figure fig, const Point& from, const Point& to, const Board& board)
{
return fig == Figure::PAWN && from.x != to.x && (from.y == 3 || from.y == 4) && figure(board.get(to.x, to.y)) == Figure::NONE;
}
std::optional<Move> parseMove(const std::string& str, const Board& board)
{
const Point from = { str[0] - 'a', str[1] - '1' };
const Point to = { str[2] - 'a', str[3] - '1' };
if (!pointValid(from) || !pointValid(to)) {
return {};
}
const auto sq = board.get(from.x, from.y);
const auto fig = figure(sq);
const auto col = color(sq);
Figure resultFigure;
if (fig == Figure::KING_IDLE) {
resultFigure = Figure::KING;
} else if (fig == Figure::ROOK_IDLE) {
resultFigure = Figure::ROOK;
} else if (fig == Figure::PAWN_IDLE) {
resultFigure = std::abs(from.y - to.y) == 2 ? Figure::PAWN_EN_PASSANT : Figure::PAWN;
} else if (fig == Figure::PAWN_EN_PASSANT) {
resultFigure = Figure::PAWN;
} else if (fig == Figure::PAWN) {
resultFigure = (to.y == 0 || to.y == Board::SIZE - 1) ? Figure::QUEEN : Figure::PAWN;
} else {
resultFigure = fig;
}
MoveType moveType = MoveType::STANDARD;
if (detectCastling(from, to, board)) {
moveType = MoveType::CASTLING;
} else if (detectEnPassantCapture(fig, from, to, board)) {
moveType = MoveType::EN_PASSANT;
}
return Move { from, to, square(resultFigure, col), moveType };
}
void playerPlays(Board& board, BoardStats& boardStats, Color col)
{
while (true) {
std::cout << "Enter valid move: ";
std::string input;
std::cin >> input;
const auto m = parseMove(input, board);
if (!m || !figureMoveValid(*m, board, col)) {
continue;
}
board.applyMove(*m);
boardStats.visit(board);
break;
}
}
Move computerPlays(Board& board, BoardStats& boardStats, Color col)
{
AI ai(board, col, boardStats);
Timer timer(COMPUTER_PLAY_TIME, [&] {
ai.stop();
});
ai.run();
timer.stop();
// Wait for AI to finish
timer.join();
const auto m = ai.bestMove();
if (!m) {
throw std::runtime_error("Computer - no move available");
}
board.applyMove(*m);
board.clearUndoMoves();
boardStats.visit(board);
std::cout << "Score: " << board.score() << std::endl;
std::cout << "My move: " << *m << std::endl;
std::cout << std::endl;
}
enum class GameStatus {
CONTINUE,
DRAW,
WIN_LOSS
};
template <typename Fn>
void forEachMove(Board& b, Color c, Fn&& f)
{
for (auto generator = b.moveGenerator(c); generator.hasMoves();) {
for (const auto& m : generator.movesChunk()) {
const auto undos = b.applyMove(m);
f(m);
b.undoMove(undos);
}
}
}
GameStatus gameStatus(Board& board, Color col)
{
if (board.kingCaptured()) {
return GameStatus::WIN_LOSS;
}
const auto ecol = enemyColor(col);
bool draw = true;
forEachMove(board, col, [&](const auto&) {
if (!draw || board.kingCaptured()) {
return;
}
bool kingAlive = true;
forEachMove(board, ecol, [&](const auto&) {
kingAlive = kingAlive && !board.kingCaptured();
});
if (kingAlive) {
draw = false;
}
});
if (draw) {
return GameStatus::DRAW;
}
return GameStatus::CONTINUE;
}
bool resolveGameStatus(Board& board, Color col, bool computerTurn, GameStatus status)
{
if (status == GameStatus::WIN_LOSS) {
if (computerTurn) {
std::cout << "You have lost!" << std::endl;
} else {
std::cout << "You have won!" << std::endl;
}
return true;
}
if (status == GameStatus::DRAW) {
std::cout << "Draw!" << std::endl;
return true;
}
return false;
}
} // namespace
int main()
{
Board board;
BoardStats boardStats;
auto color = Color::WHITE;
bool computerTurn = true;
while (true) {
try {
std::cout << board;
const auto status = gameStatus(board, color);
if (resolveGameStatus(board, color, computerTurn, status)) {
break;
}
if (computerTurn) {
computerPlays(board, boardStats, color);
} else {
playerPlays(board, boardStats, color);
}
computerTurn = !computerTurn;
color = enemyColor(color);
} catch (const std::exception& ex) {
std::cerr << "FATAL: " << ex.what() << std::endl;
return -1;
}
}
return 0;
}