-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex.py
More file actions
169 lines (138 loc) · 5.12 KB
/
hex.py
File metadata and controls
169 lines (138 loc) · 5.12 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from math import cos, pi, sin
from typing import (
List,
Tuple,
)
from graphics import (
Window,
Point,
Line,
Text,
)
class Hex:
def __init__(self, col: int, row: int, win: Window = None, walls: int = 6) -> None:
self._col, self._row = col, row
self.q, self.r = oddr_to_axial(self._col, self._row)
self.s = -self.q - self.r
self._cx, self._cy = 0, 0
self.walls = [True] * walls
self.visited = False
self.__win = win
def __repr__(self) -> str:
return f"(Hex(({self._col}, {self._row}), {self.walls}, {self.visited})"
def __eq__(self, other: "Hex"):
return self.q == other.q and self.r == other.r and self.s == other.s
def __nq__(self, other: "Hex"):
return not (self == other)
def __add__(self, other: "Hex") -> "Hex":
axial = axial_to_oddr(self.q + other.q, self.r + other.r)
return Hex(*axial)
def __sub__(self, other: "Hex") -> "Hex":
axial = axial_to_oddr(self.q + other.q, self.r + other.r)
return Hex(*axial)
def corners(self, width: int, height: int) -> List[Tuple[float, float]]:
walls = len(self.walls)
offsetAngle = pi / walls
corners = []
for i in range(walls):
x = self._cx + (width / 2) * cos(offsetAngle - i * 2 * pi / walls)
y = self._cy + (height / 2) * sin(offsetAngle - i * 2 * pi / walls)
corners.append((x, y))
return corners
def direction(self, direction: int) -> "Hex":
return hex_directions[(6 + (direction % 6)) % 6]
def neighbor(self, direction: int) -> "Hex":
return self + self.direction(direction)
def neighbors(self) -> List[Point]:
neighbors = []
for i in range(6):
neighbor: Hex = self.neighbor(i)
coords = Point(*axial_to_oddr(neighbor.q, neighbor.r))
neighbors.append(coords)
return neighbors
def bounded_neighbors(self, cols: int, rows: int) -> List[Point]:
filtered_neighbors = filter(
lambda p: p.x >= 0 and p.x < cols and p.y >= 0 and p.y < rows,
self.neighbors(),
)
return list(filtered_neighbors)
def draw(self, cx: float, cy: float, width: int, height: int) -> None:
self._cx, self._cy = cx, cy
corners = self.corners(width, height)
corners = [Point(*corner) for corner in corners]
lines = [Line(p1, p2) for p1, p2 in zip(corners, corners[1:] + [corners[0]])]
for wall, line in zip(self.walls, lines):
self.__win.draw(
line,
"black" if wall else "#3199b7",
2 if wall else 3,
True if wall else False,
)
"""
text = Text(
f"c{self._col}, r{self._row}\n\nq{self.q}, r{self.r}\n s{self.s}",
Point(self._cx, self._cy),
)
self.__win.draw_text(text)
"""
def draw_move(self, other: "Hex", undo: bool = False):
origin = Point(int(self._cx), int(self._cy))
destination = Point(other._cx, other._cy)
line = Line(origin, destination)
self.__win.draw(line, "#e5b068" if undo else "#cffb95", 2, False)
def break_wall(self, direction: int):
self.walls[direction] = False
def wall_towards(self, direction: int):
return self.walls[direction]
def break_between(self, other: "Hex"):
delta_q = other.q - self.q
delta_r = other.r - self.r
if delta_q == 1 and delta_r == 0: # E
self.break_wall(0)
return
if delta_q == 1 and delta_r == -1: # NE
self.break_wall(1)
return
if delta_q == 0 and delta_r == -1: # NW
self.break_wall(2)
return
if delta_q == -1 and delta_r == 0: # W
self.break_wall(3)
return
if delta_q == -1 and delta_r == 1: # SW
self.break_wall(4)
return
if delta_q == 0 and delta_r == 1: # SE
self.break_wall(5)
return
def wall_between(self, other: "Hex"):
delta_q = other.q - self.q
delta_r = other.r - self.r
if delta_q == 1 and delta_r == 0: # E
return self.wall_towards(0)
if delta_q == 1 and delta_r == -1: # NE
return self.wall_towards(1)
if delta_q == 0 and delta_r == -1: # NW
return self.wall_towards(2)
if delta_q == -1 and delta_r == 0: # W
return self.wall_towards(3)
if delta_q == -1 and delta_r == 1: # SW
return self.wall_towards(4)
if delta_q == 0 and delta_r == 1: # SE
return self.wall_towards(5)
def axial_to_oddr(q: int, r: int):
col = q + int((r - (r & 1)) / 2)
row = r
return col, row
def oddr_to_axial(col: int, row: int):
q = col - int((row - (row & 1)) / 2)
r = row
return q, r
hex_directions = [
Hex(*axial_to_oddr(1, 0)), # 0 E
Hex(*axial_to_oddr(1, -1)), # 1 NE
Hex(*axial_to_oddr(0, -1)), # 2 NW
Hex(*axial_to_oddr(-1, 0)), # 3 W
Hex(*axial_to_oddr(-1, 1)), # 4 SW
Hex(*axial_to_oddr(0, 1)), # 5 SE
]