-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_bot.py
More file actions
executable file
·58 lines (45 loc) · 1.51 KB
/
test_bot.py
File metadata and controls
executable file
·58 lines (45 loc) · 1.51 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
#!/usr/bin/env python3
import sys
import logging
import re
movenum = 0
moves = ['C10', 'C9', 'C8', 'C7', 'C6', 'pass']
def handle_cmd(cmd, args):
global movenum
if cmd == 'name':
return False, 'test_bot'
elif cmd == 'list_commands':
return False, 'name\nfrisbee-reg_genmove\nfrisbee-play\nlist_commands\nkomi\nfrisbee-epsilon\nboardsize\nclear_board'
elif cmd == 'frisbee-reg_genmove':
movenum, move = movenum + 1, moves[movenum%len(moves)]
return False, move
elif cmd in ['frisbee-play', 'boardsize', 'komi', 'frisbee-epsilon']:
return False, ''
return True, 'unknown cmd'
logging.basicConfig(format="test_bot %(levelname)s: %(message)s", level=logging.DEBUG)
for raw_line in sys.stdin:
if raw_line and raw_line[-1] != '\n':
logging.warn("missing newline at the end")
#logging.debug("got cmd %s" % (repr(raw_line)))
line = re.sub(r'\s+', ' ', raw_line)
line = re.sub(r'#.*', '', line)
cmdline = line.strip().split()
if not cmdline:
continue
cmdid = ''
if re.match('\d+', cmdline[0]):
cmdid = cmdline[0]
cmdline = cmdline[1:]
cmd, args = cmdline[0].lower(), cmdline[1:]
try:
err, ret = handle_cmd(cmd, args)
except:
err, ret = True, "exception occured"
raise
if err:
output = '?%s %s\n\n'%(cmdid, ret)
else:
output = '=%s %s\n\n'%(cmdid, ret)
#logging.debug("returning %s"%(repr(output)))
print(output, end='')
sys.stdout.flush()