| @@ -0,0 +1,57 @@ |
| 1 | +"""The starter brain, in the Gomocup protocol (START, BEGIN, TURN, BOARD, END on stdin; "x,y" on stdout): win if it can, |
| 2 | +else block a five, else a random point next to the stones. Copy it into routes/<your route>/bot.py and make it better.""" |
| 3 | +import random, sys |
| 4 | + |
| 5 | +N, board, me = 15, {}, 1 |
| 6 | +D = [(1, 0), (0, 1), (1, 1), (1, -1)] |
| 7 | + |
| 8 | + |
| 9 | +def five(x, y, c): |
| 10 | + for dx, dy in D: |
| 11 | + n = 1 |
| 12 | + for s in (1, -1): |
| 13 | + i = 1 |
| 14 | + while board.get((x + s * i * dx, y + s * i * dy)) == c: |
| 15 | + n += 1; i += 1 |
| 16 | + if n >= 5: |
| 17 | + return True |
| 18 | + return False |
| 19 | + |
| 20 | + |
| 21 | +def move(): |
| 22 | + empty = [(x, y) for x in range(N) for y in range(N) if (x, y) not in board] |
| 23 | + near = [p for p in empty if any((p[0] + a, p[1] + b) in board for a in (-1, 0, 1) for b in (-1, 0, 1))] or [(N // 2, N // 2)] |
| 24 | + for c in (me, 3 - me): |
| 25 | + for p in near: |
| 26 | + if five(*p, c): |
| 27 | + return p |
| 28 | + return random.choice(near) |
| 29 | + |
| 30 | + |
| 31 | +def play(): |
| 32 | + x, y = move(); board[(x, y)] = me |
| 33 | + print(f'{x},{y}', flush=True) |
| 34 | + |
| 35 | + |
| 36 | +random.seed(int(sys.argv[1]) if len(sys.argv) > 1 else 0) |
| 37 | +for line in sys.stdin: |
| 38 | + cmd, _, arg = line.strip().partition(' ') |
| 39 | + cmd = cmd.upper() |
| 40 | + if cmd == 'START': |
| 41 | + N = int(arg); board.clear(); print('OK', flush=True) |
| 42 | + elif cmd == 'BEGIN': |
| 43 | + play() |
| 44 | + elif cmd == 'TURN': |
| 45 | + x, y = map(int, arg.split(',')); board[(x, y)] = 2; play() |
| 46 | + elif cmd == 'BOARD': |
| 47 | + for l in sys.stdin: |
| 48 | + if l.strip().upper() == 'DONE': |
| 49 | + break |
| 50 | + x, y, f = map(int, l.split(',')); board[(x, y)] = 1 if f == 1 else 2 |
| 51 | + play() |
| 52 | + elif cmd == 'ABOUT': |
| 53 | + print('name="starter", version="0.1", author="shoal-games"', flush=True) |
| 54 | + elif cmd == 'END': |
| 55 | + break |
| 56 | + elif cmd != 'INFO': |
| 57 | + print('UNKNOWN', flush=True) |