-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.py
277 lines (203 loc) · 9.42 KB
/
table.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from os import system, name
from time import sleep
from cell import *
class Table():
def __init__(self, level: int, row: int, col: int):
"""
Intializes a new table.
Parameters:
level (int): Difficulty of the table. 1 to 3.
row (int): Number of rows of the table.
col (int): Numer of columns of the table.
"""
self.__row = row
self.__col = col
self.__level = level
self.__positions = []
self.__selected = False
self.__grid = self.initGrid()
# Getters Start
def getRow(self):
"""
Gets the number of rows of the table (if reset).
Returns:
int: Number of rows of the table.
"""
return self.__row
def getCol(self):
"""
Gets the number of columns of the table (if reset).
Returns:
int: Number of columns of the table.
"""
return self.__col
def getGrid(self):
"""
Gets the grid representing the table.
Returns:
matrice: List of lists of cells (object).
"""
return self.__grid
def getPositions(self):
"""
Gets the positions of the last saved and checked neighbors.
Returns:
matrice: List of tuples containing x,y positions of cells
"""
return self.__positions
def getSelected(self):
"""
Gets if Cells have been selected or not.
Returns:
bool: Selected or not (cells highlighted or not).
"""
return self.__selected
# End
# Setters Start
def setGrid(self, grid: list[list[Cell]]):
"""
Sets the grid to a new grid.
Parameters:
grid (matrice): List of lists of cells (object).
"""
self.__grid = grid
def setRow(self, row: int):
"""
Sets the number of rows of the grid (if reset).
Parameters:
row (int): Number of rows if grid is reset.
"""
self.__row = row
def setCol(self, col: int):
"""
Sets the number of cols of the grid (if reset).
Parameters:
col (int): Number of cols if grid is reset.
"""
self.__col = col
def setPositions(self, positions: list[tuple[int, int]]):
"""
Sets new (x,y) positions of neighbors.
Parameters:
positions (matrice): List of tuples containing x,y positions of cells
"""
self.__positions = positions
def setSelected(self, selected: bool):
"""
Sets if cells are selected or not.
Parameters:
selected (bool): Selected or not.
"""
self.__selected = selected
# End
# Methods Start
def initGrid(self):
"""
Creates a new grid and replaces the previous one with it.
Returns:
matrice: List of lists of cells (object).
"""
grid = [[Cell(self.__level) for col in range(self.__col)]
for row in range(self.__row)] # inline for loop to create a 2D array of cells
return grid
def checkNeighbors(self, x: int, y: int):
"""
Checks the neighbors of the clicked cell with the same value
and gets their positions in a recursive way.
Parameters:
x (int): X position of the current cell.
y (int): Y position of the current cell.
Returns:
matrice: List of tuples containing x,y positions of cells.
"""
tab = [(x, y)] # Creates the list of neighbors of the current cell including the current cell
self.getGrid()[y][x].setChecked(True) # Sets the current cell checked value to True to ignore it once checked
top, bottom, left, right = [], [], [], [] # Creates temporary arrays to add the positions of neighbors in the recursion
# Recursion Start
# If the cell is not on the first row, if the cell above has the same value, if the cell above hasn't been checked yet, then
if y != 0 and self.getGrid()[y - 1][x].getState() == self.getGrid()[y][x].getState() and self.getGrid()[y - 1][x].getChecked() == False:
top = self.checkNeighbors(x, y - 1) # Add the position of this cell to Top and proceed to check its own neighbors
# If the cell is not on the last row, if the cell below has the same value, if the cell below hasn't been checked yet, then
if y != self.getRow() - 1 and self.getGrid()[y + 1][x].getState() == self.getGrid()[y][x].getState() and self.getGrid()[y + 1][x].getChecked() == False:
bottom = self.checkNeighbors(x, y + 1) # Add the position of this cell to Bottom and proceed to check its own neighbors
# If the cell is not on the first column, if the left cell has the same value, if the left cell hasn't been checked yet, then
if x != 0 and self.getGrid()[y][x - 1].getState() == self.getGrid()[y][x].getState() and self.getGrid()[y][x - 1].getChecked() == False:
left = self.checkNeighbors(x - 1, y) # Add the position of this cell to Left and proceed to check its own neighbors
# If the cell is not on the last column, if the right cell has the same value, if the right cell hasn't been checked yet, then
if x != self.getCol() - 1 and self.getGrid()[y][x + 1].getState() == self.getGrid()[y][x].getState() and self.getGrid()[y][x + 1].getChecked() == False:
right = self.checkNeighbors(x + 1, y) # Add the position of this cell to Right and proceed to check its own neighbors
# Recursion End
self.getGrid()[y][x].setChecked(False)
# After having checked every neighbors of the current cell, put the current cell back to unchecked
# Add every tuple (x,y) to tab if not in tab and return it
# This is to have a simple 2D array and avoid having a linked list which is harder to deal with
for i in top:
if i not in tab:
tab.append(i)
for i in bottom:
if i not in tab:
tab.append(i)
for i in left:
if i not in tab:
tab.append(i)
for i in right:
if i not in tab:
tab.append(i)
return tab # Becomes top, bottom, left and right arrays within the recursion
def updateNeighborsPos(self, x: int, y: int):
"""
Updates positions of previously selected cell's neighbors to the newly selected cell's neighbors.
Parameters:
x (int): X position of the original cell.
y (int): Y position of the original cell.
Returns:
matrice: List of tuples containing x,y positions of cells.
"""
self.__positions = self.checkNeighbors(x, y) # Saves the new neighbors's positions
return self.__positions
def displayTable(self):
"""
Prints the grid with values/states in the console.
"""
print() # Skip line
display = [[None for col in range(self.__col)]
for row in range(self.__row)] # Creates a 2D array (col x row) with None as default values
for row in range(self.__row):
for col in range(self.__col):
display[row][col] = str(self.__grid[row][col].getState()) # Changes the None to the values of the respective positions (x, y) in the grid
# skip a line for every row (items), and join the values within each rows with a space
for item in display:
print(" ".join(item))
def gravity(self):
"""
Updates the positions of the cells within the grid by applying gravity
by moving cells above downwards if current cell is empty.
"""
empty = False # Consider there are no empty cells in grid
for y in range(self.__row):
for x in range(self.__col): # For every cell in grid
if self.__grid[y][x].getState() == 0: # if cell is empty (0)
empty = True # Set empty to True
if self.__grid[y-1][x].getState() != 0 and y > 0:
# If cell above current cell is not empty, if current cell is not on first row
self.__grid[y][x].setState(self.__grid[y-1][x].getState()) # Move every cell downwards if cell below empty
self.__grid[y-1][x].setState(0) # Changed previous position of moved cell to empty (0)
if y == 0:
# If no cells with value above empty cells
self.__grid[y][x].randomState(self.__level) # Randomize the state of the cell
if empty: # If empty cell(s) in grid
self.gravity()
# Restart the function to move the rest of the cells downwards and randomize the rest of the empty cells
def win(self):
"""
Checks the winning conditions and returns True or False accordingly.
Returns:
bool: Win or not.
"""
for i in self.__grid:
for j in i: # For every cell in grid
if j.getState() == 10: # If value of any cell is equal to 10
return True
# If no cell value is equal to 10
return False
# End