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,18 +59,10 @@ 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]
|
||||||
|
|
||||||
row_check = np.where(self.number_grid[row, :] == val_check)[0]
|
if not prev_num == val_check or val_check == 0:
|
||||||
col_check = np.where(self.number_grid[:, col] == val_check)[0]
|
|
||||||
local_grid_row = int(row / 3) * 3
|
|
||||||
local_grid_col = int(col / 3) * 3
|
|
||||||
local_grid_check_row, local_grid_check_col = np.where(
|
|
||||||
self.number_grid[local_grid_row:local_grid_row + 3, local_grid_col:local_grid_col + 3] == val_check)
|
|
||||||
|
|
||||||
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]:
|
while self.offending_cells[row][col]:
|
||||||
r, c = self.offending_cells[row][col].pop()
|
r, c = self.offending_cells[row][col].pop()
|
||||||
try:
|
try:
|
||||||
|
@ -79,31 +71,42 @@ class SudokuSystem:
|
||||||
print('No such cell found')
|
print('No such cell found')
|
||||||
if not self.offending_cells[r][c]:
|
if not self.offending_cells[r][c]:
|
||||||
self.change_cell_status(r, c, VALID)
|
self.change_cell_status(r, c, VALID)
|
||||||
print('Completion?', self.completion_check())
|
|
||||||
|
|
||||||
else:
|
if not val_check == 0:
|
||||||
self.cell_status[row, col] = INVALID
|
|
||||||
bad_cells = []
|
|
||||||
if not len(row_check) == 1:
|
|
||||||
for c in row_check:
|
|
||||||
if not c == col:
|
|
||||||
bad_cells.append((row, c))
|
|
||||||
self.offending_cells[row][c].append((row, col))
|
|
||||||
self.change_cell_status(row, c, INVALID)
|
|
||||||
if not len(col_check) == 1:
|
|
||||||
for r in col_check:
|
|
||||||
if not r == row:
|
|
||||||
bad_cells.append((r, col))
|
|
||||||
self.offending_cells[r][col].append((row, col))
|
|
||||||
self.change_cell_status(r, col, INVALID)
|
|
||||||
if not len(local_grid_check_row) == 1:
|
|
||||||
for r, c in zip(local_grid_check_row + local_grid_row, local_grid_check_col + local_grid_col):
|
|
||||||
if not (c == col or r == row):
|
|
||||||
bad_cells.append((r, c))
|
|
||||||
self.offending_cells[r][c].append((row, col))
|
|
||||||
self.change_cell_status(r, c, INVALID)
|
|
||||||
|
|
||||||
self.offending_cells[row][col] = bad_cells
|
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
|
||||||
|
local_grid_col = int(col / 3) * 3
|
||||||
|
local_grid_check_row, local_grid_check_col = np.where(
|
||||||
|
self.number_grid[local_grid_row:local_grid_row + 3, local_grid_col:local_grid_col + 3] == val_check)
|
||||||
|
|
||||||
|
if len(row_check) == 1 and len(col_check) == 1 and len(local_grid_check_row) == 1:
|
||||||
|
self.cell_status[row, col] = VALID
|
||||||
|
print('Completion?', self.completion_check())
|
||||||
|
else:
|
||||||
|
self.cell_status[row, col] = INVALID
|
||||||
|
bad_cells = []
|
||||||
|
if not len(row_check) == 1:
|
||||||
|
for c in row_check:
|
||||||
|
if not c == col:
|
||||||
|
bad_cells.append((row, c))
|
||||||
|
self.offending_cells[row][c].append((row, col))
|
||||||
|
self.change_cell_status(row, c, INVALID)
|
||||||
|
if not len(col_check) == 1:
|
||||||
|
for r in col_check:
|
||||||
|
if not r == row:
|
||||||
|
bad_cells.append((r, col))
|
||||||
|
self.offending_cells[r][col].append((row, col))
|
||||||
|
self.change_cell_status(r, col, INVALID)
|
||||||
|
if not len(local_grid_check_row) == 1:
|
||||||
|
for r, c in zip(local_grid_check_row + local_grid_row, local_grid_check_col + local_grid_col):
|
||||||
|
if not (c == col or r == row):
|
||||||
|
bad_cells.append((r, c))
|
||||||
|
self.offending_cells[r][c].append((row, col))
|
||||||
|
self.change_cell_status(r, c, INVALID)
|
||||||
|
|
||||||
|
self.offending_cells[row][col] = bad_cells
|
||||||
|
|
||||||
def generate_test_board(self):
|
def generate_test_board(self):
|
||||||
with open(test_dir, 'r') as f:
|
with open(test_dir, 'r') as f:
|
||||||
|
|
Loading…
Reference in New Issue