def box_to_col_check(self, boxnum, change): # Determines if two columns are solved in a particular box, the unsolved columns must contain the missing values # Thus, you can remove these missing values from the cells located in this columns in other boxes box = utils.get_box(boxnum, self.sudoku) errorcode = 0 incomplete_cols = set() for i, local_col in box.iteritems(): col_complete = True for cell in local_col.values: if cell.value == 0: col_complete = False if not col_complete: incomplete_cols.add(i) if len(incomplete_cols) == 1: column = self.sudoku.loc[:, incomplete_cols.pop()] return self.remove_from_other_boxes(column, box, boxnum, change) else: return change, errorcode
def box_to_row_check(self, boxnum, change): # Determines if two rows are solved in a particular box, the unsolved row must contain the missing values # Thus, you can remove these missing values from the cells located in this row in other boxes box = utils.get_box(boxnum, self.sudoku) errorcode = 0 # Check to see if two rows are completed within the box incomplete_rows = set() for i, local_row in enumerate(box.itertuples(index=False)): row_complete = True for cell in local_row: if cell.value == 0: row_complete = False if not row_complete: incomplete_rows.add(i) if len(incomplete_rows) == 1: row = self.sudoku.loc[utils.eval_row(boxnum, incomplete_rows.pop())] return self.remove_from_other_boxes(row, box, boxnum, change) else: return change, errorcode
def int_check_box(self, boxnum, change): # Slice the board into the correct box box = utils.get_box(boxnum, self.sudoku) found = utils.check_values_box(box) errorcode = 0 # Start checking the cells in the box. Cannot use int_check_region due needing to use the iter_box generator for cell in utils.iter_box(box): if not cell.solved: # Create a tempoarty list of possible values to check for possible = cell.possible.difference(found) # Loop through the other cells in the quardrant to find their possible values for checkcell in utils.iter_box(box): # Only check if the cell isn't solved and it isn't the cell we are comparing to if not checkcell.solved and cell != checkcell: for pos in checkcell.possible: # If there is a value that is in the source possible list, remove it if pos in possible: possible.remove(pos) # If there is only one possible solution left, that must be the solution for this cell if len(possible) == 1: cell.possible = possible.copy() change, errorcode = cell.check_solved(change, self.sudoku) if change: return change, errorcode return change, errorcode
def bas_check_box(self, boxnum, change): # Slice the board into the correct box box = utils.get_box(boxnum, self.sudoku) # Check which values exist in the box found = utils.check_values_box(box) # Assign possible values based on found, return if a cell has been solved or if there is an error return self.bas_assign_region(utils.iter_box(box), change, found)
def validate_boxes(self, solved): for boxnum in range(9): box = utils.get_box(boxnum, self.sudoku) solved, error = utils.validate_box(box, solved) if error: return solved, error return solved, error
def bt_check_box(sudoku, cell, value): box = utils.get_box(cell.box, sudoku) for check in utils.iter_box(box): if check != cell and check.value != 0: if check.value == value: return False return True
def build_box(self, boxnum): box = utils.get_box(boxnum, self.sudoku.sudoku) grid = BoxGrid() for cell in utils.iter_box(box): cell.gui = SudokuCell(cell) grid.add_widget(cell.gui) self.cell_list.append(cell.gui) return grid
def check_double(self, sudoku): # Check Row _, errorrow = utils.validate_region(sudoku.loc[self.row], True) # Check Column _, errorcol = utils.validate_region(sudoku.loc[:, self.column], True) # Check box box = utils.get_box(self.box, sudoku) _, errorbox = utils.validate_box(box, True) return errorrow or errorcol or errorbox
def col_to_box_check(self, colnum, change): # Determines, within a column, if 2 boxes have been completed. If they have, the outstanding values in that column must be placed in the remaining # cells in the column. Thus it is not possible to place them in any other location in that box, so remove them from the possible values of other cells in that box found = utils.check_values(self.sudoku.loc[:, colnum]) errorcode = 0 if len(found) > 5: incompleted_boxes = utils.eval_boxes(self.sudoku.loc[:, colnum]) if len(incompleted_boxes) == 1: remove_pos = {1, 2, 3, 4, 5, 6, 7, 8, 9}.difference(found) box = utils.get_box(incompleted_boxes.pop(), self.sudoku) for cell in utils.iter_box(box): if not cell.solved and cell.column != colnum: change, errorcode = cell.assign_possible( remove_pos, change, self.sudoku) return change, errorcode
def row_to_box_check(self, rownum, change): # Determines, within a row, if 2 boxes have been completed. If they have, the outstanding values in that row must be placed in the remaining # cells in the row. Thus it is not possible to place them in any other location in that box, so remove them from the possible values of other cells in that box found = utils.check_values(self.sudoku.loc[rownum]) errorcode = 0 # Only check if 6 or more values have been filled in, as any less means 2 boxes couldn't have been solved if len(found) > 5: incompleted_boxes = utils.eval_boxes(self.sudoku.loc[rownum]) if len(incompleted_boxes) == 1: remove_pos = {1, 2, 3, 4, 5, 6, 7, 8, 9}.difference(found) box = utils.get_box(incompleted_boxes.pop(), self.sudoku) for cell in utils.iter_box(box): if not cell.solved and cell.row != rownum: change, errorcode = cell.assign_possible( remove_pos, change, self.sudoku) return change, errorcode