def test_coordsFromIndexAgainstValue(self): self.sudokuBoard.clearBoard() self.sudokuBoard.loadBoard('preloads/sudoku1.txt') for index in range(81): valByIndex = self.sudokuBoard.getDigit(index) coords = SudokuBoard.coordsFromIndex(index) index2 = SudokuBoard.indexFromCoords(coords) valByIndex2 = self.sudokuBoard.getDigit(index2) self.assertTrue(valByIndex == valByIndex2)
def runAlg(self, coords): # Get digit at coords n = self.board.getDigit(SudokuBoard.indexFromCoords(coords)) # Should we solve it? if n is not None: return None # Get candidates at the square cands = self.getCand(coords) if len(cands) != 1: return None else: return cands.pop()
def test_indexFromCoords(self): self.assertTrue(SudokuBoard.indexFromCoords((0,0)) == 0) self.assertTrue(SudokuBoard.indexFromCoords((1,1)) == 10) self.assertTrue(SudokuBoard.indexFromCoords((8,8)) == 80)
def runAlg(self, layer): # Get numbers in each layer self.row = [] self.row.append(list(self.board.getRow(3 * layer + 0))) self.row.append(list(self.board.getRow(3 * layer + 1))) self.row.append(list(self.board.getRow(3 * layer + 2))) # loop all numbers for target in range(1,10): #print("Analyzing target {0}".format(target)) # Analyze the situation self.analyzePositions(target) # For now implement only when a single row is missing a target # Get a row that is missing a target missingRow = self.rowMissingTarget() #print("Missing row {0}".format(missingRow)) if missingRow is None: continue # Get the third that should contain the missing number allThirds = set(range(0,3)) if missingRow == 0: allThirds.remove(self.row1TargetThird) allThirds.remove(self.row2TargetThird) elif missingRow == 1: allThirds.remove(self.row0TargetThird) allThirds.remove(self.row2TargetThird) else: allThirds.remove(self.row0TargetThird) allThirds.remove(self.row1TargetThird) targetThird = allThirds.pop() #print("Target third {0}".format(targetThird)) # Get the numbers in the target third in targer row slice = [] if targetThird == 0: slice = self.row[missingRow][0:3] elif targetThird == 1: slice = self.row[missingRow][3:6] else: slice = self.row[missingRow][6:9] #print("Target slice: {0}".format(slice)) # Get the number of Nones in the slice numNone = slice.count(None) #print("Count none {0}".format(numNone)) # If only one - we can return now if numNone == 1: noneIndex = slice.index(None) #print("None index {0}".format(noneIndex)) index = SudokuBoard.indexFromCoords(((targetThird * 3 + noneIndex), layer * 3 + missingRow)) return (index, target) # Nothing found return None