-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk2.h
108 lines (93 loc) · 3.75 KB
/
k2.h
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
#include "engine.h"
#include <memory>
#include <map>
#include <thread>
class k2 : public engine
{
public:
k2() : commands(), force(false), quit(false), silent_mode(false),
xboard(false), uci(false), max_depth(max_ply), search_moves(),
not_search_moves(), time_for_move(0), time_per_time_control(60),
time_inc(0), current_clock(60), moves_per_time_control(0),
moves_to_go(0), move_cr(0), use_thread(USE_THREAD), thr() {
std::srand(unsigned(time(nullptr)));
}
void start();
void main_search();
typedef void(k2::*method_ptr)(const std::string &);
std::map<std::string, std::pair<method_ptr, bool>> commands;
void new_command(const std::string &in);
void force_command(const std::string &in);
void quit_command(const std::string &in);
void perft_command(const std::string &in);
void go_command(const std::string &in);
void setboard_command(const std::string &in);
void memory_command(const std::string &in);
void help_command(const std::string &in);
void post_command(const std::string &in);
void nopost_command(const std::string &in);
void eval_command(const std::string &in);
void sd_command(const std::string &in);
void protover_command(const std::string &in);
void sn_command(const std::string &in);
void st_command(const std::string &in);
void level_command(const std::string &in);
void uci_command(const std::string &in);
void isready_command(const std::string &in);
void position_command(const std::string &in);
void setoption_command(const std::string &in);
void void_command(const std::string &in);
void execute_search();
protected:
bool force, quit, silent_mode, xboard, uci;
i8 max_depth;
std::set<std::string> search_moves, not_search_moves;
double time_for_move, time_per_time_control,
time_inc, current_clock;
int moves_per_time_control, moves_to_go, move_cr;
bool use_thread;
std::thread thr;
const double time_margin = 0.02;
bool execute_command(const std::string &in);
void uci_go_command(const std::string &in);
void uci_go_infinite(const std::string &in);
void uci_go_ponder(const std::string &in);
void uci_go_wtime(const std::string &in);
void uci_go_btime(const std::string &in);
void uci_go_winc(const std::string &in);
void uci_go_binc(const std::string &in);
void uci_go_moves(const std::string &in);
void uci_go_movestogo(const std::string &in);
void uci_go_movetime(const std::string &in);
void uci_go_searchmoves(const std::string &in);
bool looks_like_move(const std::string &in) const;
move_s root_search(const i8 depth, int &alpha_orig, const int beta,
std::vector<move_s> &moves);
void print_search_iteration_result(i8 dpt, int val);
std::string uci_score(int val) const;
void timer_start() {
t_beg = std::chrono::high_resolution_clock::now();
}
void update_clock() {
if (uci)
return;
current_clock += time_inc - time_elapsed();
move_cr++;
if (moves_per_time_control && move_cr >= moves_per_time_control) {
move_cr = 0;
current_clock += time_per_time_control;
}
}
void set_time_for_move() {
if (!uci)
moves_to_go = moves_per_time_control ?
moves_per_time_control - move_cr : 0;
int mov2go = moves_to_go ? moves_to_go : 30;
double k_branch = mov2go <= 4 ? 1 : 2;
time_for_move = current_clock/mov2go/k_branch + time_inc;
double k_max = mov2go <= 4 ? 1 : 3;
max_time_for_move = k_max*time_for_move - time_margin;
if (max_time_for_move >= current_clock)
max_time_for_move = current_clock - time_margin;
}
};