Fix Soduku invalid cell check

master
En Yi 2018-07-16 19:48:14 +08:00
parent 12af3a279a
commit a4d95ee35c
1 changed files with 39 additions and 36 deletions

View File

@ -37,10 +37,10 @@ class SudokuSystem:
self.offending_cells[i][j].pop()
def replace_cell_number(self, row, col, val):
prev_val = self.number_grid[row, col]
self.number_grid[row, col] = int(val)
if not val == 0:
self.invalid_cell_check(row, col)
else:
self.invalid_cell_check(row, col, prev_val)
if val == 0:
self.change_cell_status(row, col, EMPTY)
def get_cell_number(self, row, col):
@ -59,9 +59,21 @@ class SudokuSystem:
else:
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]
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]
col_check = np.where(self.number_grid[:, col] == val_check)[0]
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:
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())
else:
self.cell_status[row, col] = INVALID
bad_cells = []