-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseboard.py
64 lines (51 loc) · 1.91 KB
/
parseboard.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
import json, sys, os
'''
Generates board configurations from a board text file
'''
DEFAULT_BOARD_TEXT_FILE = "defaultboard.txt"
# Pick player - (use same letter for board design)
PLAYER = 'red'
# PLAYER = 'green'
# PLAYER = 'blue'
##############################################################################
boardcoords = (
( 0,-3),( 1,-3),( 2,-3),( 3,-3),
(-1,-2),( 0,-2),( 1,-2),( 2,-2),( 3,-2),
(-2,-1),(-1,-1),( 0,-1),( 1,-1),( 2,-1),( 3,-1),
(-3, 0),(-2, 0),(-1, 0),( 0, 0),( 1, 0),( 2, 0),( 3, 0),
(-3, 1),(-2, 1),(-1, 1),( 0, 1),( 1, 1),( 2, 1),
(-3, 2),(-2, 2),(-1, 2),( 0, 2),( 1, 2),
(-3, 3),(-2, 3),(-1, 3),( 0, 3))
PLAYER_TILES = "rgbRGB"
BLOCK_TILES = "xX"
EMPTY_TILE = '-'
def parseboardinput():
if len(sys.argv)==1:
# Get the default board config returned by parseboard
board_text_file = DEFAULT_BOARD_TEXT_FILE
elif len(sys.argv)==2:
try:
# determine if the input is a txt file or json file
board_file = sys.argv[1]
file_ext = os.path.splitext(board_file)[1]
if file_ext == ".json":
with open(board_file) as file_data:
return json.load(file_data)
else:
board_text_file = board_file
except:
print("# bad input", file=sys.stderr)
exit()
else:
print("# too many arguments (expected up to 1)", file=sys.stderr)
exit()
template = {'colour': PLAYER, 'pieces': [], 'blocks': []}
with open(board_text_file) as board_text:
for tile, coord in zip(board_text.read().split(), boardcoords):
if tile in PLAYER_TILES:
template["pieces"].append(coord)
elif tile in BLOCK_TILES:
template["blocks"].append(coord)
elif tile != EMPTY_TILE:
print("bad parse", file=sys.stderr)
return template