-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain_core.hpp
118 lines (89 loc) · 3.09 KB
/
brain_core.hpp
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
/**
* @file brain_core.hpp
* @brief Core of the brain (protocol, etc.)
*/
#ifndef CORE_BRAIN_CORE_HPP
#define CORE_BRAIN_CORE_HPP
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
#ifdef DEBUG
#include <fstream>
#endif
#include "core/config.hpp"
#include "core/protocol.hpp"
namespace gmk {
using Arguments = std::vector<std::variant<std::int32_t, std::string>>;
class BrainCore {
public:
enum InfoType {
timeout_turn,
timeout_match,
max_memory,
time_left,
game_type,
rule,
folder,
evaluate,
};
BrainCore(const std::string &about = "");
virtual ~BrainCore();
//! NEED TO BE IMPLEMENTED BY THE BRAIN
// create the board and call sendOK() or sendError("msg"), return false if error
virtual bool brainInit() = 0;
// delete old board, create new board, call sendOK(), return false if error
virtual bool brainRestart() = 0;
// callback for info update
virtual void brainInfo(const InfoType &info) = 0;
// choose your move and call doMyMove(x,y), x < width, y < height
virtual void brainTurn() = 0;
// put your move to the boardn, return true if success
virtual bool brainMyMove(std::uint32_t x, std::uint32_t y) = 0;
// put opponent's move to the board, return true if success
virtual bool brainOpponentMove(std::uint32_t x, std::uint32_t y) = 0;
// square [x,y] belongs to a winning line (when info_continuous is 1), return true if success
virtual bool brainBlock(std::uint32_t x, std::uint32_t y) = 0;
// clear one square, return value: success, not supported, error
virtual void brainTakeback(std::uint32_t x, std::uint32_t y) = 0;
// delete temporary files, free resources
virtual void brainEnd() = 0;
//! BUILT-IN FUNCTIONS
// core
void run();
void executeCommand(const std::string &command);
Arguments parse_arguments(std::string const &verb, VerbConfig const &verb_config, std::string const &str_args);
Arguments parse_multiline_arguments(std::string const &verb, VerbConfig const &verb_config);
std::tuple<IncomingVerb, Arguments> parse_command(std::string const &command_);
void startThinkingThread();
void stopThinkingThread();
void thinkingThread();
void startThinking();
// callbacks
void doMyMove(std::uint32_t x, std::uint32_t y);
// communication
bool recv(std::string &buffer);
void send(const std::string &str);
void sendOK();
void sendUnknown();
void sendUnknown(const std::string &str);
void sendError();
void sendError(const std::string &str);
void sendMessage(const std::string &str);
void sendDebug(const std::string &str);
void sendSuggest(std::uint32_t x, std::uint32_t y);
protected:
// core
bool m_running;
const std::string m_about;
bool m_thinking_thread_running;
std::thread m_thinking_thread;
std::condition_variable m_need_thinking_cond;
std::condition_variable m_start_thinking_cond;
Config m_config;
#ifdef DEBUG
std::ofstream m_debug_file;
#endif
};
}
#endif /* CORE_BRAIN_CORE_HPP */