-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.py
105 lines (81 loc) · 2.65 KB
/
clock.py
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
from enum import Enum
# 24 +2 which is for the buffer below
moves_to_go_default = 26
buffer_millis = 200
buffer_moves = 2
class State(Enum):
IDLE = 1
SEARCH = 2
# PONDER = 3
state: State = State.IDLE
class TimingMode(Enum):
DEPTH = 1
NODES = 2
MOVETIME = 3
TC = 4
INFINITE = 5
class SearchLimiter:
def __init__(self, mode: TimingMode, depth=0, nodes=0, movetime=0):
self.mode = mode
self.depth = depth
self.nodes = nodes
self.movetime = movetime
lim: SearchLimiter
def from_fixed(movetime: int):
if movetime > buffer_millis:
return movetime - buffer_millis
else:
return 0
def lim_from_fixed(movetime: int):
return SearchLimiter(TimingMode.MOVETIME, movetime=from_fixed(movetime))
def from_tc(is_white: bool, wtime, btime, winc, binc, movestogo):
if movestogo > 0:
moves_to_go_actual = movestogo + buffer_moves
else:
moves_to_go_actual = moves_to_go_default
if is_white:
movetime = winc + wtime / moves_to_go_actual
else:
movetime = binc + btime / moves_to_go_actual
return from_fixed(movetime)
def lim_from_tc(is_white: bool, wtime, btime, winc, binc, movestogo):
movetime = from_tc(is_white, wtime, btime, winc, binc, movestogo)
return SearchLimiter(TimingMode.TC, movetime=movetime)
def lim_from_tc_from_str(is_stm_white, commands):
wtime = 0
btime = 0
winc = 0
binc = 0
movestogo = 0
pairs = [commands[i * 2 : (i + 1) * 2] for i in range(int(len(commands) / 2))]
for [key, value] in pairs:
match key:
case "wtime":
wtime = int(value)
case "btime":
btime = int(value)
case "winc":
winc = int(value)
case "binc":
binc = int(value)
case "movestogo":
movestogo = int(value)
return lim_from_tc(is_stm_white, wtime, btime, winc, binc, movestogo)
def from_cmd(is_stm_white, commands):
mode = ""
if len(commands) > 0:
mode = commands.pop(0)
match mode:
case "depth":
value = int(commands.pop(0))
return SearchLimiter(TimingMode.DEPTH, depth=value)
case "nodes":
value = int(commands.pop(0))
return SearchLimiter(TimingMode.NODES, nodes=value)
case "movetime":
value = int(commands.pop(0))
return lim_from_fixed(value)
case item if item in ["wtime", "btime", "winc", "binc", "movestogo"]:
return lim_from_tc_from_str(is_stm_white, [mode, *commands])
case _:
return SearchLimiter(TimingMode.INFINITE)