-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_rules.py
More file actions
52 lines (44 loc) · 1.68 KB
/
Copy pathgo_rules.py
File metadata and controls
52 lines (44 loc) · 1.68 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
"""Small deterministic Go rules. Positional superko; passes exempt."""
from copy import deepcopy
def neighbors(board, x, y):
n = len(board)
return [(a, b) for a, b in ((x-1,y),(x+1,y),(x,y-1),(x,y+1)) if 0 <= a < n and 0 <= b < n]
def group(board, x, y):
color = board[y][x]
if not color:
return set(), set()
stones, liberties, pending = set(), set(), [(x,y)]
while pending:
p = pending.pop()
if p in stones:
continue
stones.add(p)
for a,b in neighbors(board,*p):
if board[b][a] == 0:
liberties.add((a,b))
elif board[b][a] == color and (a,b) not in stones:
pending.append((a,b))
return stones, liberties
def key(board):
return ''.join(str(c) for row in board for c in row)
def play(board, x, y, color, seen=()):
if type(x) is not int or type(y) is not int or not (0 <= x < len(board) and 0 <= y < len(board)):
raise ValueError('落子坐标不在棋盘内。')
if board[y][x]:
raise ValueError('这里已经有棋子,请选空交叉点。')
out = deepcopy(board)
out[y][x] = color
removed = set()
for a,b in neighbors(out,x,y):
if out[b][a] == 3-color:
stones, libs = group(out,a,b)
if not libs:
removed |= stones
for c,d in stones:
out[d][c] = 0
stones, libs = group(out,x,y)
if not libs:
raise ValueError('这一步会让自己的棋没有气,不能下。')
if key(out) in seen:
raise ValueError('不能立即还原已出现的局面(本练习采用全局同形禁着规则)。')
return out, len(removed)