-
Notifications
You must be signed in to change notification settings - Fork 2
/
victory.py
151 lines (121 loc) · 4.73 KB
/
victory.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
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
def transpose_grid(grid):
grid_t = []
grid_line = []
for i in range(len(grid[0])):
for row in grid:
grid_line.append(row[i])
grid_t.append(grid_line)
grid_line = []
return grid_t
def verify_row(row, necessary_length):
if len(row) < necessary_length:
return False, None
actual_length = 0
pawn_to_verify = row[0]
for element in row:
# Analyse each row
# Count repeated elements in a row
if element == pawn_to_verify and element != 0: # Do not consider 0 an element: it's considered an empty space
actual_length += 1
# If the length of these repeated elements reaches the necessary length to victory, stop
if actual_length == necessary_length:
# Return true and the winner element
return True, element
else:
actual_length = 1
pawn_to_verify = element
return False, None
def verify_grid_rows(grid, necessary_length):
row_number = 0
for row in grid:
is_winner_row, element = verify_row(row, necessary_length)
if is_winner_row:
return True, element, row_number
row_number += 1
return False, None, -1
def get_backslash_diagonal(grid, uppermost_coordinates):
"""
Gets the backslash-type diagonal of a grid based on the coordinates of the uppermost element.
:param grid: The base matrix
:param uppermost_coordinates: Tuple with the coordinates of the uppermost element of the form (row, column)
:return: A list of the diagonal elements, starting from the uppermost
"""
i = uppermost_coordinates[0]
j = uppermost_coordinates[1]
diagonal_elements = []
while i <= len(grid)-1 and j <= len(grid[0])-1:
# Put it in the list
diagonal_elements.append(grid[i][j])
# Go to next element:
i += 1
j += 1
return diagonal_elements
def get_forward_slash_diagonal(grid, uppermost_coordinates):
"""
Gets the forward slash-type diagonal of a grid based on the coordinates of the uppermost element.
:param grid: The base matrix
:param uppermost_coordinates: Tuple with the coordinates of the uppermost element of the form (row, column)
:return: A list of the diagonal elements, starting from the uppermost
"""
i = uppermost_coordinates[0]
j = uppermost_coordinates[1]
diagonal_elements = []
while i <= len(grid)-1 and j >= 0:
# Put it in the list
diagonal_elements.append(grid[i][j])
# Go to next element:
i += 1
j -= 1
return diagonal_elements
def tic_tac_toe_victory(grid, necessary_length):
# Horizontal verification
exists_winner, element, row_number = verify_grid_rows(grid, necessary_length)
if exists_winner:
print("Row victory")
return True, element, row_number
# Vertical verification
grid_t = transpose_grid(grid)
exists_winner, element, column_number = verify_grid_rows(grid_t, necessary_length)
if exists_winner:
print("Column victory")
return True, element, column_number
# Diagonal verification:
# Back slash type diagonals: (\)
# print("Backslash diagonals")
# Lower half of the diagonals
for i in range(len(grid)-1, -1, -1):
diagonal = get_backslash_diagonal(grid, (i, 0))
exists_winner, element = verify_row(diagonal, necessary_length)
if exists_winner:
print("Diagonal victory")
return True, element, 0
# print(diagonal)
# Upper half of the diagonals
for j in range(1, len(grid[0])):
diagonal = get_backslash_diagonal(grid, (0,j))
exists_winner, element = verify_row(diagonal, necessary_length)
if exists_winner:
print("Diagonal victory")
return True, element, 0
# print(diagonal)
# Forward slash type diagonals (/)
# print("Forward slash diagonals")
# Upper half of the diagonals
for j in range(len(grid[0])):
diagonal = get_forward_slash_diagonal(grid, (0, j))
exists_winner, element = verify_row(diagonal, necessary_length)
if exists_winner:
print("Diagonal victory")
return True, element, 0
# print(diagonal)
# Lower half of the diagonals
for i in range(1,len(grid)):
diagonal = get_forward_slash_diagonal(grid, (i, len(grid[0])-1))
exists_winner, element = verify_row(diagonal, necessary_length)
if exists_winner:
print("Diagonal victory")
return True, element, 0
# print(diagonal)
return False, None, -1
#grid = [[' ',' ',' ','B',' '],[' ',' ','X','A',' '],[' ','X',' ','A',' '],['X',' ',' ','A','B']]
#print(victory(grid, 4))