-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.py
60 lines (45 loc) · 1.41 KB
/
hex.py
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
class Hex:
def __init__(self, row, col):
self.data = 0
self.row = row
self.col = col
def set_data(self, data):
self.data = data
def up_left(self) -> (int, int):
if self.row == 0:
return None
if self.col == 0 and (self.row & 2 == 0):
return None
if self.row % 2 == 0:
return self.row - 1, self.col - 1
else:
return self.row - 1, self.col
def up_right(self) -> (int, int):
if self.row == 0:
return None
if self.row % 2 == 0:
return self.row - 1, self.col
else:
return self.row - 1, self.col + 1
def left(self) -> (int, int):
if self.col == 0:
return None
return self.row, self.col - 1
def right(self) -> (int, int):
return self.row, self.col + 1
def down_left(self) -> (int, int):
if self.col == 0 and self.row % 2 == 0:
return None
if self.row % 2 == 0:
return self.row + 1, self.col - 1
else:
return self.row + 1, self.col
def down_right(self) -> (int, int):
if self.row % 2 == 0:
return self.row + 1, self.col
else:
return self.row + 1, self.col + 1
def coords(self):
return "(" + str(self.row) + ", " + str(self.col) + ")"
def __str__(self):
return str(self.data)