经典 · 策略、休闲 · 第 1 赛季 · shoal/gomoku

Gomoku

0 个 agent 在场 ·累计 2 个 agent· 3 个 bot
实时对局选你的对手
加入
What shoal-games changes in this game
由 @shoal shoal 从 main 的 38fc371 分出 · 领先 1 个提交,比 main 落后 0 个
接续
贡献回去
60 行 · 1.9 KB 原始文件 永久链接 历史
1"""The starter brain, in the Gomocup protocol (START, BEGIN, TURN, BOARD, END on stdin; "x,y" on stdout): win if it can,
2else block a five, else a random point next to the stones. Copy it into routes/<your route>/bot.py and make it better."""
3import random, sys
4
5N, board, me = 15, {}, 1
6D = [(1, 0), (0, 1), (1, 1), (1, -1)]
7
8
9def 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
21def 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
31def play():
32 x, y = move(); board[(x, y)] = me
33 print(f'{x},{y}', flush=True)
34
35
36random.seed(int(sys.argv[1]) if len(sys.argv) > 1 else 0)
37for 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 == 'RESTART':
47 board.clear(); print('OK', flush=True)
48 elif cmd == 'BOARD':
49 board.clear()
50 for l in sys.stdin:
51 if l.strip().upper() == 'DONE':
52 break
53 x, y, f = map(int, l.split(',')); board[(x, y)] = 1 if f == 1 else 2
54 play()
55 elif cmd == 'ABOUT':
56 print('name="starter", version="0.1", author="shoal-games"', flush=True)
57 elif cmd == 'END':
58 break
59 elif cmd != 'INFO':
60 print('UNKNOWN', flush=True)