-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_brain.cpp
134 lines (118 loc) · 3.27 KB
/
random_brain.cpp
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
#include "random_brain.hpp"
namespace gmk::randbrain {
#define ABOUT_NAME "RandomBrain"
#define ABOUT_VERSION "1.0"
#define ABOUT_AUTHOR "Skyf0l"
#define ABOUT_COUNTRY "FR"
#define ABOUT_WWW "https://github.com/Skyf0l"
#define ABOUT_DESCRIPTION "Random move brain"
static const std::string ABOUT = "name=\"" ABOUT_NAME "\" version=\"" ABOUT_VERSION "\" author=\"" ABOUT_AUTHOR "\" country=\"" ABOUT_COUNTRY
"\" www=\"" ABOUT_WWW "\" description=\"" ABOUT_DESCRIPTION "\"";
RandomBrain::RandomBrain()
: BrainCore(ABOUT)
{
srand(time(nullptr));
}
RandomBrain::~RandomBrain()
{
m_board.clear();
}
bool RandomBrain::isFree(std::uint32_t x, std::uint32_t y)
{
if (x >= m_board.size() || y >= m_board[x].size())
return false;
return m_board[x][y] == EMPTY;
}
bool RandomBrain::resetBoard()
{
if (m_config.board_width < 5 || m_config.board_height < 5) {
sendError("board size is 5x5");
return false;
}
m_board.resize(m_config.board_height);
for (std::uint32_t i = 0; i < m_config.board_height; ++i) {
m_board[i].resize(m_config.board_width);
for (std::uint32_t j = 0; j < m_config.board_width; ++j) {
m_board[i][j] = EMPTY;
}
}
return true;
}
// create the board and call sendOK() or sendError("msg"), return false if error
bool RandomBrain::brainInit()
{
if (resetBoard()) {
sendOK();
return true;
}
return false;
}
// delete old board, create new board, call pipeOut("OK"), return false if error
bool RandomBrain::brainRestart()
{
if (resetBoard()) {
sendOK();
return true;
}
return false;
}
// callback for info update
void RandomBrain::brainInfo(const InfoType &info)
{
}
// choose your move and call doMyMove(x,y), 0 <= x < width, 0 <= y < height
void RandomBrain::brainTurn()
{
std::uint32_t x = rand() % m_config.board_width;
std::uint32_t y = rand() % m_config.board_height;
while (!isFree(x, y)) {
x = rand() % m_config.board_width;
y = rand() % m_config.board_height;
}
doMyMove(x, y);
}
// put your move to the board, return true if success
bool RandomBrain::brainMyMove(std::uint32_t x, std::uint32_t y)
{
if (isFree(x, y)) {
m_board[x][y] = ME;
return true;
}
sendError("Invalid move");
return false;
}
// put opponent's move to the board, return true if success
bool RandomBrain::brainOpponentMove(std::uint32_t x, std::uint32_t y)
{
if (isFree(x, y)) {
m_board[x][y] = OPPONENT;
return true;
}
sendError("Invalid opponent move");
return false;
}
// square [x,y] belongs to a winning line (when info_continuous is 1), return true if success
bool RandomBrain::brainBlock(std::uint32_t x, std::uint32_t y)
{
if (isFree(x, y)) {
m_board[x][y] = BLOCKED;
return true;
}
sendError("Invalid block");
return false;
}
// clear one square and call call sendOK() or sendError("msg")
void RandomBrain::brainTakeback(std::uint32_t x, std::uint32_t y)
{
if (x < m_board.size() && y < m_board[x].size() && !isFree(x, y)) {
m_board[x][y] = EMPTY;
sendOK();
} else {
sendError("Invalid takeback");
}
}
// delete temporary files, free resources
void RandomBrain::brainEnd()
{
}
}