-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinate.py
90 lines (81 loc) · 3.57 KB
/
coordinate.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
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
class Coordinate:
@classmethod
def coords_right(cls, start_coord, spaces, board_size):
new_coords = [num for num in range(start_coord[1], start_coord[1] + spaces) if num > -1 and num < board_size]
old_coords = [start_coord[0] for val in range(spaces + 1)]
coords = zip(old_coords, new_coords)
coords = list(coords)
return coords
@classmethod
def coords_left(cls, start_coord, spaces, board_size):
new_coords = [num for num in range(start_coord[1], start_coord[1] - spaces, -1) if num > -1 and num < board_size]
old_coords = [start_coord[0] for val in range(spaces + 1)]
coords = zip(old_coords, new_coords)
coords = list(coords)
return coords
@classmethod
def coords_up(cls, start_coord, spaces, board_size):
new_coords = [num for num in range(start_coord[0], start_coord[0] - spaces, -1) if num > -1 and num < board_size]
old_coords = [start_coord[1] for val in range(spaces + 1)]
coords = zip(new_coords, old_coords)
coords = list(coords)
return coords
@classmethod
def coords_down(cls, start_coord, spaces, board_size):
new_coords = [num for num in range(start_coord[0], start_coord[0] + spaces) if num > -1 and num < board_size]
old_coords = [start_coord[1] for val in range(spaces + 1)]
coords = zip(new_coords, old_coords)
coords = list(coords)
return coords
@classmethod
def user_to_index(cls, user_coord):
index = int(user_coord[1:]) - 1, ord(user_coord[0]) - ord("A")
return index
@classmethod
def index_to_user(cls, index):
coords = chr(index[1] + ord('A')) + str(index[0] + 1)
return coords
@classmethod
def all_adjacent(cls, coord, board_size):
adjacent_coords = []
up = (coord[0]-1, coord[1])
if up[0] < board_size and up[0] > -1:
adjacent_coords.append(up)
down = (coord[0]+1, coord[1])
if down[0] < board_size and down[0] > -1:
adjacent_coords.append(down)
right = (coord[0], coord[1]+1)
if right[1] < board_size and right[1] > -1:
adjacent_coords.append(right)
left = (coord[0], coord[1]-1)
if left[1] < board_size and left[1] > -1:
adjacent_coords.append(left)
return adjacent_coords
@classmethod
def dir_adjacent(cls, direction, coord, board_size):
if direction == 'horizontal':
horizontal_coords = []
right_coord = (coord[0], coord[1]+1)
if right_coord[1] < board_size and right_coord[1] > -1:
horizontal_coords.append(right_coord)
left_coord = (coord[0], coord[1]-1)
if left_coord[1] < board_size and left_coord[1] > -1:
horizontal_coords.append(left_coord)
return horizontal_coords
elif direction == 'vertical':
vertical_coords = []
up_coord = (coord[0]-1, coord[1])
if up_coord[0] < board_size and up_coord[0] > -1:
vertical_coords.append(up_coord)
down_coord = (coord[0]+1, coord[1])
if down_coord[0] < board_size and down_coord[0] > -1:
vertical_coords.append(down_coord)
return vertical_coords
@classmethod
def find_orientation(cls, coord, coord2):
if coord[0] == coord2[0] and coord[1] != coord2[1]:
return 'horizontal'
elif coord[1] == coord2[1] and coord[0] != coord2[0]:
return 'vertical'
else:
print('not on same row or column')