-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
186 lines (144 loc) · 4.9 KB
/
grid.py
File metadata and controls
186 lines (144 loc) · 4.9 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
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
class Cell:
def __init__(self, row, col, content=None):
self.fixed = content is not None # if init with content, not allowed to modify it
self.content = content
self.pos = (row, col)
@property
def is_fixed(self):
return self.fixed
@property
def val(self):
return self.content
@property
def position(self):
return self.pos
def is_empty(self):
return self.content is None
def fill(self, content):
assert not self.is_fixed # cannot modify fixed cell
self.content = content
def __str__(self):
return str(self.content) if self.content is not None else str(0)
def __eq__(self, other):
if isinstance(other, Cell):
return self.val == other.val
if isinstance(other, int):
return self.val == other
return False
class Grid:
def __init__(self, grid):
self.size = len(grid)
self.grid = []
for i, row in enumerate(grid):
new_row = []
for j, cell in enumerate(row):
content = None if cell == 0 else cell
new_row.append(Cell(i, j, content))
self.grid.append(new_row)
def is_solved(self):
errors = 0
nums = list(range(1, self.size+1))
# check rows
for row in self.grid:
for n in nums:
if n not in row:
errors += 1
# return False
# check columns
for col in range(self.size):
col_values = [self.grid[row][col] for row in range(self.size)]
for n in nums:
if n not in col_values:
errors += 1
# return False
# check squares
square_size = int(self.size**0.5)
for row in range(square_size): # iterate square by square
for col in range(square_size):
# gather the cells in a given square
square = []
for i in range(square_size):
for j in range(square_size):
cell = self.grid[row*square_size+i][col*square_size+j]
square.append(cell)
for n in nums:
if n not in square:
errors += 1
# return False
return errors == 0
def fill_cell(self, row, col, content):
assert 0 <= row < self.size and 0 <= col < self.size
self.grid[row][col].fill(content)
def columns(self):
return GridColIterator(self.grid)
def rows(self):
return GridRowIterator(self.grid)
def squares(self):
return GridSquareIterator(self.grid)
def __iter__(self):
return GridCellIterator(self.grid)
def __getitem__(self, index):
return self.grid[index]
def __str__(self):
s = ''
for row in self.grid:
s += f"{' '.join([str(cell) for cell in row])}\n"
return s
class GridCellIterator:
def __init__(self, grid):
self.grid = grid
self.row = 0
self.col = 0
def __next__(self):
row, col = self.row, self.col
self.col += 1
if self.col == len(self.grid[0]):
self.col = 0
self.row += 1
if row >= len(self.grid) or col >= len(self.grid[0]):
raise StopIteration
return self.grid[row][col]
def __iter__(self):
return self
class GridRowIterator:
def __init__(self, grid):
self.grid = grid
self.row = 0
def __next__(self):
row = self.row
self.row += 1
if row >= len(self.grid):
raise StopIteration
return self.grid[row]
def __iter__(self):
return self
class GridColIterator:
def __init__(self, grid):
self.grid = grid
self.col = 0
def __next__(self):
col = self.col
self.col += 1
if col >= len(self.grid[0]):
raise StopIteration
return [self.grid[row][col] for row in range(len(self.grid))]
def __iter__(self):
return self
class GridSquareIterator:
def __init__(self, grid):
self.grid = grid
self.square_size = int(len(self.grid)**0.5) # assumes grid is a sudoku
self.row = 0
self.col = 0
def __next__(self):
row, col = self.row, self.col
self.col += 1
if self.col == self.square_size:
self.col = 0
self.row += 1
if row >= self.square_size or col >= self.square_size:
raise StopIteration
return [self.grid[row*self.square_size+i][col*self.square_size+j] \
for i in range(self.square_size) for j in range(self.square_size)]
def __iter__(self):
return self