-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers.py
71 lines (62 loc) · 1.79 KB
/
parsers.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
import argparse
import os
def parse_map(file):
maps = []
with open(file, 'r') as f:
lines = f.readlines()
i = 0
while i < len(lines):
j = 1
if lines[i].startswith("***"):
maze = {
'width': 0,
'height': 0,
'walls': [],
'goals': [],
'player': (0, 0),
'boxes': [],
}
while j + i < len(lines) and not lines[j + i].startswith("***"):
line = lines[j + i]
if j == 3:
maze['width'] = int(line.split(':')[1])
if j == 4:
maze['height'] = int(line.split(':')[1])
if j >= 8:
for x, c in enumerate(line):
pos = (j - 7, x + 1)
if c == 'X':
maze['walls'].append(pos)
elif c == '@':
maze['player'] = pos
elif c == '*':
maze['boxes'].append(pos)
elif c == '.':
maze['goals'].append(pos)
j += 1
if maze['player'] != (0, 0):
maps.append(maze)
i = j + i
return maps
def save_map(maps, prefix, output):
i = 1
if not os.path.exists(output) or not os.path.isdir(output):
os.mkdir(output)
for m in maps:
with open('%s/%s%02d.txt' % (output, prefix, i), 'w') as f:
f.writelines([
'%d %d' % (m['width'], m['height']) + '\n',
'%d ' % len(m['walls']) + ' '.join(['%d %d' % t for t in m['walls']]) + '\n',
'%d ' % len(m['boxes']) + ' '.join(['%d %d' % t for t in m['boxes']]) + '\n',
'%d ' % len(m['goals']) + ' '.join(['%d %d' % t for t in m['goals']]) + '\n',
'%d %d' % tuple(m['player']),
])
i += 1
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Parsing map file to input format")
parser.add_argument("--src")
parser.add_argument("--output", "-o", default="inputs")
parser.add_argument("--prefix", "-p")
args = parser.parse_args()
maps = parse_map(args.src)
save_map(maps, args.prefix, args.output)