Repository navigation
Expand file tree
/
Copy pathGameSession.cpp
More file actions
84 lines (70 loc) · 1.46 KB
/
Copy pathGameSession.cpp
File metadata and controls
84 lines (70 loc) · 1.46 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
#include "GameSession.h"
namespace gomoku {
GameSession::GameSession(EvaluationTablesPtr evaluationTables) : game(std::move(evaluationTables))
{
}
bool GameSession::placePiece(int x, int y)
{
if (game.checkWinner() != Piece::EMPTY || game.isBoardFull() || !game.placePiece(x, y)) {
return false;
}
moves.push_back({x, y});
return true;
}
std::pair<int, int> GameSession::placeComputerMove(SearchOptions options)
{
if (game.checkWinner() != Piece::EMPTY || game.isBoardFull()) {
return {-1, -1};
}
const auto move = game.placePiece(options);
if (move.first < 0 || move.second < 0) {
return move;
}
moves.push_back({move.first, move.second});
return move;
}
int GameSession::undo(int movesToUndo)
{
int undone = 0;
while (undone < movesToUndo && !moves.empty()) {
moves.pop_back();
++undone;
}
if (undone) {
rebuild();
}
return undone;
}
void GameSession::reset()
{
moves.clear();
rebuild();
}
Piece GameSession::getPiece(int x, int y) const
{
return game.getPiece(x, y);
}
Piece GameSession::currentTurn() const
{
return game.currentTurn();
}
Piece GameSession::checkWinner() const
{
return game.checkWinner();
}
bool GameSession::isBoardFull() const
{
return game.isBoardFull();
}
int GameSession::moveCount() const
{
return static_cast<int>(moves.size());
}
void GameSession::rebuild()
{
game.reset();
for (const Move move : moves) {
(void)game.placePiece(move.x, move.y);
}
}
} // namespace gomoku