-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
73 lines (54 loc) · 1.69 KB
/
common.py
File metadata and controls
73 lines (54 loc) · 1.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
class Hex:
directions = [
(+1, 0), (+1, -1), ( 0, -1),
(-1, 0), (-1, +1), ( 0, +1)
]
def __init__(self, q, r):
self.q = q
self.r = r
def __repr__(self):
return 'Hex({}, {})'.format(self.q, self.r)
def __add__(self, other):
return Hex(self.q+other.q, self.r+other.r)
def __eq__(self, other):
return self.q == other.q and self.r == other.r
def to_cube(self):
x = self.q
z = self.r
y = -x-z
return Cube(x, y, z)
def neighbor(self, direction):
return self + Hex(*self.directions[direction])
def to_cart(self):
x = self.q
y = (not (self.q&1)) + (2 * int(self.r + (self.q + (self.q&1)) / 2))
return Cart(x, y)
class Cube:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return 'Cube({}, {}, {})'.format(self.x, self.y, self.z)
def __add__(self, other):
return Cube(self.x+other.x, self.y+other.y, self.z+other.z)
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
def to_hex(self):
q = self.x
r = self.z
return Hex(q, r)
def to_cart(self):
x = self.x
y = (not (self.x&1)) + (2 * int(self.z + (self.x + (self.x&1)) / 2))
return Cart(x, y)
class Cart:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return 'Cart({}, {})'.format(self.x, self.y)
def __add__(self, other):
return Cart(self.x+other.x, self.y+other.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y