Fix Soduku invalid cell check
parent
12af3a279a
commit
a4d95ee35c
|
@ -37,10 +37,10 @@ class SudokuSystem:
|
||||||
self.offending_cells[i][j].pop()
|
self.offending_cells[i][j].pop()
|
||||||
|
|
||||||
def replace_cell_number(self, row, col, val):
|
def replace_cell_number(self, row, col, val):
|
||||||
|
prev_val = self.number_grid[row, col]
|
||||||
self.number_grid[row, col] = int(val)
|
self.number_grid[row, col] = int(val)
|
||||||
if not val == 0:
|
self.invalid_cell_check(row, col, prev_val)
|
||||||
self.invalid_cell_check(row, col)
|
if val == 0:
|
||||||
else:
|
|
||||||
self.change_cell_status(row, col, EMPTY)
|
self.change_cell_status(row, col, EMPTY)
|
||||||
|
|
||||||
def get_cell_number(self, row, col):
|
def get_cell_number(self, row, col):
|
||||||
|
@ -59,9 +59,21 @@ class SudokuSystem:
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def invalid_cell_check(self, row, col):
|
def invalid_cell_check(self, row, col, prev_num):
|
||||||
val_check = self.number_grid[row, col]
|
val_check = self.number_grid[row, col]
|
||||||
|
|
||||||
|
if not prev_num == val_check or val_check == 0:
|
||||||
|
while self.offending_cells[row][col]:
|
||||||
|
r, c = self.offending_cells[row][col].pop()
|
||||||
|
try:
|
||||||
|
self.offending_cells[r][c].remove((row, col))
|
||||||
|
except ValueError:
|
||||||
|
print('No such cell found')
|
||||||
|
if not self.offending_cells[r][c]:
|
||||||
|
self.change_cell_status(r, c, VALID)
|
||||||
|
|
||||||
|
if not val_check == 0:
|
||||||
|
|
||||||
row_check = np.where(self.number_grid[row, :] == val_check)[0]
|
row_check = np.where(self.number_grid[row, :] == val_check)[0]
|
||||||
col_check = np.where(self.number_grid[:, col] == val_check)[0]
|
col_check = np.where(self.number_grid[:, col] == val_check)[0]
|
||||||
local_grid_row = int(row / 3) * 3
|
local_grid_row = int(row / 3) * 3
|
||||||
|
@ -71,16 +83,7 @@ class SudokuSystem:
|
||||||
|
|
||||||
if len(row_check) == 1 and len(col_check) == 1 and len(local_grid_check_row) == 1:
|
if len(row_check) == 1 and len(col_check) == 1 and len(local_grid_check_row) == 1:
|
||||||
self.cell_status[row, col] = VALID
|
self.cell_status[row, col] = VALID
|
||||||
while self.offending_cells[row][col]:
|
|
||||||
r, c = self.offending_cells[row][col].pop()
|
|
||||||
try:
|
|
||||||
self.offending_cells[r][c].remove((row, col))
|
|
||||||
except ValueError:
|
|
||||||
print('No such cell found')
|
|
||||||
if not self.offending_cells[r][c]:
|
|
||||||
self.change_cell_status(r, c, VALID)
|
|
||||||
print('Completion?', self.completion_check())
|
print('Completion?', self.completion_check())
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.cell_status[row, col] = INVALID
|
self.cell_status[row, col] = INVALID
|
||||||
bad_cells = []
|
bad_cells = []
|
||||||
|
|
Loading…
Reference in New Issue