-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWthor.py
More file actions
64 lines (52 loc) · 1.53 KB
/
Wthor.py
File metadata and controls
64 lines (52 loc) · 1.53 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
import othelloDriver as OD
# .wtb file parser - reads the human games and processes them into agent friendly format
def wtbToMove(i):
if i == 0:
return -1
y = (i//10)-1
x = (i%10)-1
return y*8+x
def checkValidMove(i):
return i <= 88
def playGame(moves):
episode = []
board = '.'*27 + "ox......xo" + '.'*27
token = 'x'
for move in moves:
if move == -1:
return episode
episode.append((board, token))
episode.append(move)
tpl = OD.playMoveTpl(board, token, move)
if not tpl:
return False
board, token = tpl
return episode
def parseGame(bytes):
if not (0 <= bytes[6] <= 64 and 0 <= bytes[7] <= 64):
return False
moves = []
for i in bytes[8:]:
if not checkValidMove(i):
return False
moves.append(wtbToMove(i))
episode = playGame(moves)
return episode
def generateEpisodes(cnt):
print("Parsing Wthor files...")
episodes = []
for year in range(2016, 1977, -1):
with open(f"HumanGames/WTH_{year}.wtb", mode="rb") as f:
contents = f.read()
game = 0
while True:
if 16+game*68+68 >= len(contents):
break
if game%50 == 0:
print("*", end="", flush=True)
if not (episode:=parseGame(contents[16+game*68:16+game*68+68])):
break
episodes.append(episode)
if len(episodes) >= cnt:
return episodes
game += 1