-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconsole.py
More file actions
executable file
·70 lines (61 loc) · 2.11 KB
/
console.py
File metadata and controls
executable file
·70 lines (61 loc) · 2.11 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
65
66
67
68
69
#!/usr/bin/env python3
"""A python3 implementation of the Thud! boardgame
"""
__author__ = "William Dizon"
__license__ = "MIT License"
__version__ = "1.8.0"
__email__ = "wdchromium@gmail.com"
import sys
import re
from thud import *
from thudclasses import *
from itertools import cycle
if __name__ == '__main__':
ply_list = [l.strip('\n') for l in sys.stdin.readlines()]
newgame = Gameboard('classic')
if sys.argv[1] == 'captures':
last_move = ply_list.pop()
try:
turn = itertools.cycle(['dwarf', 'troll'])
for move in ply_list:
if ',' in move: continue
p = Ply.parse_string(move)
if p.token != next(turn):
raise RuntimeError(len(newgame.ply_list), move)
if not p:
if len(newgame.ply_list) == 0:
continue
break
valid = newgame.validate_move(p.origin, p.dest)
if valid[0] or valid[1]:
newgame.apply_ply(p)
newgame.ply_list.append(p)
else:
raise RuntimeError(len(newgame.ply_list), move)
except RuntimeError as e:
if sys.argv[1] == 'next_move':
print('{}:{}'.format(e[0], e[1]))
elif sys.argv[1] == 'validate':
print('False')
else:
if sys.argv[1] == 'next_move':
result = AIEngine.calculate_best_move(newgame, \
newgame.turn_to_act(), \
3)
print(result)
elif sys.argv[1] == 'validate':
print('True')
elif sys.argv[1] == 'turn':
print(newgame.turn_to_act())
elif sys.argv[1] == 'captures':
candidates = sorted([str(x) for x in list(newgame.find_caps(newgame.turn_to_act()))],
key=len,
reverse=True)
for p in candidates:
if last_move in p:
print(str(p))
break
else:
print()
finally:
exit(0)