#!/usr/bin/env python3 """Renju Arena source-engine template (single-file Python). Protocol on stdin/stdout: RESET PUT r c B|W GO budget_ms -> print "MOVE r c" QUIT This is a small heuristic (win / block / central neighbour score). It does not implement black forbidden points; replace choose_move() with a real engine. The arena server is the referee and will forfeit an illegal black move. """ from __future__ import annotations import sys N = 15 EMPTY, BLACK, WHITE = 0, 1, 2 DIRS = ((0, 1), (1, 0), (1, 1), (1, -1)) def new_board(): return [[EMPTY] * N for _ in range(N)] def in_board(r, c): return 0 <= r < N and 0 <= c < N def run_length(board, r, c, dr, dc, color): n = 0 rr, cc = r, c while in_board(rr, cc) and board[rr][cc] == color: n += 1 rr += dr cc += dc rr, cc = r - dr, c - dc while in_board(rr, cc) and board[rr][cc] == color: n += 1 rr -= dr cc -= dc return n def wins_at(board, r, c, color): for dr, dc in DIRS: n = run_length(board, r, c, dr, dc, color) if color == WHITE and n >= 5: return True if color == BLACK and n == 5: return True return False def neighbor_score(board, r, c, color): score = 100 - (abs(r - 7) + abs(c - 7)) for dr in (-2, -1, 0, 1, 2): for dc in (-2, -1, 0, 1, 2): if dr == 0 and dc == 0: continue rr, cc = r + dr, c + dc if not in_board(rr, cc): continue v = board[rr][cc] cheb = max(abs(dr), abs(dc)) if v == color: score += (3 - cheb) * 3 elif v != EMPTY: score += 3 - cheb return score def choose_move(board, turn): empties = [(r, c) for r in range(N) for c in range(N) if board[r][c] == EMPTY] if not empties: return (-1, -1) if len(empties) == N * N: return (7, 7) for r, c in empties: board[r][c] = turn win = wins_at(board, r, c, turn) board[r][c] = EMPTY if win: return (r, c) opp = WHITE if turn == BLACK else BLACK for r, c in empties: board[r][c] = opp win = wins_at(board, r, c, opp) board[r][c] = EMPTY if win: return (r, c) return max(empties, key=lambda p: neighbor_score(board, p[0], p[1], turn)) def main(): board = new_board() turn = BLACK for raw in sys.stdin: line = raw.strip() if not line: continue parts = line.split() cmd = parts[0].upper() if cmd == "QUIT": break if cmd == "RESET": board = new_board() turn = BLACK continue if cmd == "PUT" and len(parts) >= 3: try: r, c = int(parts[1]), int(parts[2]) except ValueError: continue if not in_board(r, c) or board[r][c] != EMPTY: continue color = turn if len(parts) >= 4: side = parts[3][0].upper() if side == "B" or side == "1": color = BLACK elif side == "W" or side == "2": color = WHITE board[r][c] = color turn = WHITE if color == BLACK else BLACK continue if cmd == "GO": r, c = choose_move(board, turn) print(f"MOVE {r} {c}", flush=True) continue if __name__ == "__main__": main()