-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
110 lines (80 loc) · 2.69 KB
/
game.py
File metadata and controls
110 lines (80 loc) · 2.69 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import numpy as np
from brange import brange
# Player colors
BLACK = -1
WHITE = 1
class Game:
def __init__(self, width, height):
# Who's the current player
self.turn = np.random.choice([BLACK, WHITE])
# Dimensionality
self.width = width
self.height = height
# Board
self.board = np.zeros((self.width, self.height))
self.available = (None, set(), set())
def __getitem__(self, xy):
x, y = xy
return self.board[x][y]
def __setitem__(self, xy, value):
x, y = xy
self.update(x, y, value)
def update(self, x, y, value):
self.board[x][y] = value
# Do nothing if removing piece
# TODO: Consider this later!
if value == 0:
return
# Loop through all ---
def is_blocked(self, a, b):
""" Check if two tiles are blocked by the other player """
# Separate each component
ax, ay = a
bx, by = b
color = self[ax, ay]
if color == 0:
raise RuntimeError("Can't check if path is blocked without a defined color")
# Distance between tiles
dx = bx - ax
dy = by - ay
# Polarity
plx = 1 if dx > 0 else -1
ply = 1 if dy > 0 else -1
if abs(dx) == abs(dy) or abs(dx) not in (1, 2) or abs(dy) not in (1, 2):
raise ValueError('Tiles must be a Horse move apart')
# Pick the two lines determining the connection
if abs(dx) > abs(dy):
u = self[ax:bx - plx, by]
v = self[ax + plx:bx, ay]
else:
u = self[bx, ay:by - ply]
v = self[ax, ay + ply:by]
# Check whether either of the lines
# is blocked by a piece of the opponent's color
u_blocked = any(i == -color for i in u)
v_blocked = any(j == -color for j in v)
if u_blocked and v_blocked:
return True
return False
def is_connected(self, x, y):
for px, py in self.get_hooks(x, y):
if self[px, py] == self[px, py]:
return True
return False
# Areas / Regions
def get_hooks(self, x, y):
""" Get all 8 possible Horse moves """
for i, j in (
(1, 2), (-1, 2), (1, -2), (-1, -2),
(2, 1), (2, -1), (-2, 1), (-1, -2)
):
px = x + i
py = y + j
if 0 <= px < self.width and 0 <= py < self.height:
yield px, py
def affected(self, x, y):
n = 3
for i in range(-n, n + 1):
spread = n - abs(i)
for j in range(-spread, spread + 1):
yield x + i, y + j