| 1 | """A Gomocup match between two brains, as Piskvork runs one: the brains speak the Gomocup protocol on stdin and stdout |
| 2 | (START, INFO, BEGIN, TURN, BOARD ... DONE, END), the board is 15x15 and the rule is standard: exactly five in a row wins, |
| 3 | an overline does not. A match starts from a balanced opening of a few stones and is played twice with colours swapped, |
| 4 | as Gomocup does. A brain that runs out of time, answers nonsense or plays on a stone loses. Renju's forbidden moves are |
| 5 | not judged (ponytail: standard rule only; renju needs its own detector, the free ones are GPL). |
| 6 | """ |
| 7 | import queue, subprocess, threading, time |
| 8 | |
| 9 | D = [(1, 0), (0, 1), (1, 1), (1, -1)] |
| 10 | |
| 11 | |
| 12 | class Brain: |
| 13 | def __init__(self, argv, cwd): |
| 14 | self.p = subprocess.Popen(argv, cwd=cwd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, bufsize=1) |
| 15 | self.q = queue.Queue() |
| 16 | threading.Thread(target=lambda: [self.q.put(l) for l in self.p.stdout], daemon=True).start() |
| 17 | |
| 18 | def send(self, *lines): |
| 19 | self.p.stdin.write(''.join(l + '\n' for l in lines)) |
| 20 | self.p.stdin.flush() |
| 21 | |
| 22 | def reply(self, timeout): |
| 23 | end = time.monotonic() + timeout |
| 24 | while True: |
| 25 | l = self.q.get(timeout=max(0.001, end - time.monotonic())).strip() |
| 26 | if l.split(' ')[0].upper() not in ('MESSAGE', 'DEBUG', 'SUGGEST'): |
| 27 | return l |
| 28 | |
| 29 | |
| 30 | def run_of(board, x, y, c): |
| 31 | best, line = 0, None |
| 32 | for dx, dy in D: |
| 33 | cells = [(x, y)] |
| 34 | for s in (1, -1): |
| 35 | i = 1 |
| 36 | while board.get((x + s * i * dx, y + s * i * dy)) == c: |
| 37 | cells.append((x + s * i * dx, y + s * i * dy)) |
| 38 | i += 1 |
| 39 | if len(cells) > best: |
| 40 | best, line = len(cells), sorted(cells) |
| 41 | return best, line |
| 42 | |
| 43 | |
| 44 | def match(brains, opening, size=15, turn_ms=1000): |
| 45 | """brains: two (argv, cwd); the first plays black (moves first after the opening). Returns the replay.""" |
| 46 | live = [Brain(*b) for b in brains] |
| 47 | board, moves, out = {}, [], {'winner': None, 'reason': 'board full', 'five': None} |
| 48 | try: |
| 49 | for b in live: |
| 50 | b.send(f'START {size}', f'INFO timeout_turn {turn_ms}', 'INFO timeout_match 0', 'INFO rule 1') |
| 51 | for i, b in enumerate(live): |
| 52 | try: |
| 53 | if not b.reply(5).upper().startswith('OK'): |
| 54 | raise ValueError |
| 55 | except (queue.Empty, ValueError): |
| 56 | return {**base(size, opening, moves), 'winner': 1 - i, 'reason': 'did not start'} |
| 57 | for i, (x, y) in enumerate(opening): |
| 58 | board[(x, y)] = i % 2 |
| 59 | moves.append([x, y, 0]) |
| 60 | side = len(opening) % 2 |
| 61 | if opening: |
| 62 | live[side].send('BOARD', *[f'{x},{y},{1 if board[(x, y)] == side else 2}' for x, y, _ in moves], 'DONE') |
| 63 | else: |
| 64 | live[side].send('BEGIN') |
| 65 | told = [side == s or not opening for s in (0, 1)] |
| 66 | while True: |
| 67 | t = time.monotonic() |
| 68 | try: |
| 69 | x, y = map(int, live[side].reply(turn_ms / 1000 + 0.5).split(',')) |
| 70 | except (queue.Empty, ValueError): |
| 71 | out = {'winner': 1 - side, 'reason': 'out of time or no move', 'five': None} |
| 72 | break |
| 73 | if not (0 <= x < size and 0 <= y < size) or (x, y) in board: |
| 74 | out = {'winner': 1 - side, 'reason': f'illegal move {x},{y}', 'five': None} |
| 75 | break |
| 76 | board[(x, y)] = side |
| 77 | moves.append([x, y, int((time.monotonic() - t) * 1000)]) |
| 78 | n, line = run_of(board, x, y, side) |
| 79 | if n == 5: |
| 80 | out = {'winner': side, 'reason': 'five', 'five': line} |
| 81 | break |
| 82 | if len(board) == size * size: |
| 83 | break |
| 84 | side = 1 - side |
| 85 | if not told[side]: |
| 86 | live[side].send('BOARD', *[f'{a},{b},{1 if board[(a, b)] == side else 2}' for a, b, _ in moves], 'DONE') |
| 87 | told[side] = True |
| 88 | else: |
| 89 | live[side].send(f'TURN {x},{y}') |
| 90 | finally: |
| 91 | for b in live: |
| 92 | try: |
| 93 | b.send('END') |
| 94 | b.p.wait(1) |
| 95 | except Exception: |
| 96 | b.p.kill() |
| 97 | return {**base(size, opening, moves), **out} |
| 98 | |
| 99 | |
| 100 | def base(size, opening, moves): |
| 101 | return {'game': 'gomoku', 'rule': 'standard', 'size': size, 'opening': len(opening), 'moves': moves} |