-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgames.py
More file actions
26 lines (23 loc) · 782 Bytes
/
games.py
File metadata and controls
26 lines (23 loc) · 782 Bytes
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
# Load the moves.txt file into a dictionary
import re
import os
class Games(dict):
default_file='fixed_moves.txt'
def __init__(self, filename=default_file):
if not os.path.exists(filename):
print(f'Could not open "{filename}" -- no games loaded')
return
fd = open(filename)
moves = ''
game = None
for i in fd.readlines():
# Recognize a game header line, e.g. "#29596, longest at 53 moves"
game_no = re.search(r'#(\d*)', i)
if game_no:
if game is not None:
self[game] = moves.split()
moves = ''
game = int(game_no.group(1))
else:
moves += i
self[game] = moves.split()