def solve(self): self.solvedSDKdict = sudokuS.solve(self.getSDKString()) if self.solvedSDKdict != False: for i in range(9): for j in range(9): self.solvedSDKarray[i][j] = self.solvedSDKdict[ self.arrToDict(i, j)]
def check(self): gr = self.getGrid() self.getStringGrid() flag = 1 v1 = '' v2 = '' for k1 in gr.keys(): for k2 in sudokuS.peers[k1]: if (gr[k1].text == gr[k2].text) and (gr[k1].text != ''): v1, v2 = k1, k2 flag = 0 break if flag == 0: break if flag == 1: if sudokuS.solve(self.grid) == False: self.label.text = 'This Sudoku does not have solution' for k in gr.keys(): gr[k].background_color = (225, 0.5, 0.5, 0.65) else: self.label.text = 'This Sudoku is valid and has solution' for k in gr.keys(): gr[k].background_color = (0.6, 225, 1, 1) else: for k in gr.keys(): gr[k].background_color = (1, 1, 1, 1) gr[v1].background_color = (225, 0.5, 0.5, 0.65) gr[v2].background_color = (225, 0.5, 0.5, 0.65) self.label.text = 'This Sudoku input is invalid.'
def solve(self): gr = self.getGrid() self.getStringGrid() flag = 1 v1 = '' v2 = '' for k1 in gr.keys(): for k2 in sudokuS.peers[k1]: if (gr[k1].text == gr[k2].text) and (gr[k1].text != ''): v1, v2 = k1, k2 flag = 0 break if flag == 0: break if flag == 1: if sudokuS.solve(self.grid) == False: self.label.text = 'This Sudoku does not have solution' for k in gr.keys(): gr[k].background_color = (225, 0.5, 0.5, 0.65) else: t1 = time.clock() SolvedGrid = sudokuS.solve(self.grid) t2 = time.clock() for k in gr.keys(): gr[k].background_color = (1, 1, 1, 1) for k in gr.keys(): if gr[k].text == '': gr[k].background_color = (0.5, 0.5, 255, 0.8) # Assign back for k1, v1 in gr.items(): for k2, v2 in SolvedGrid.items(): if k1 == k2: gr[k1].text = SolvedGrid[k2] break t = t2 - t1 self.label.text = 'Time:' + str(t) else: for k in gr.keys(): gr[k].background_color = (1, 1, 1, 1) gr[v1].background_color = (225, 0.5, 0.5, 0.65) gr[v2].background_color = (225, 0.5, 0.5, 0.65) self.label.text = 'This Sudoku input is invalid.'
def hint(self): gr = self.getGrid() self.getStringGrid() flag = 1 v1 = '' v2 = '' for k1 in gr.keys(): for k2 in sudokuS.peers[k1]: if (gr[k1].text == gr[k2].text) and (gr[k1].text != ''): v1, v2 = k1, k2 flag = 0 break if flag == 0: break if flag == 1: # SolvedGrid = sudokuS.solve(self.grid) # if SolvedGrid == False if sudokuS.solve(self.grid) == False: self.label.text = 'This Sudoku does not have solution' for k in gr.keys(): gr[k].background_color = (225, 0.5, 0.5, 0.65) else: #hint hintCell = '' SolvedGrid = sudokuS.solve(self.grid) for k in gr.keys(): if gr[k].text == '': hintCell = k break if hintCell == '': self.label.text = 'This Sudoku has been solved' for k in gr.keys(): gr[k].background_color = (0.5, 0.5, 255, 0.8) else: for k in gr.keys(): gr[k].background_color = (1, 1, 1, 1) gr[hintCell].background_color = (255, 255, 0, 0.65) gr[hintCell].text = SolvedGrid[hintCell] self.label.text = 'Hint!' else: for k in gr.keys(): gr[k].background_color = (1, 1, 1, 1) gr[v1].background_color = (225, 0.5, 0.5, 0.65) gr[v2].background_color = (225, 0.5, 0.5, 0.65) self.label.text = 'This Sudoku input is invalid.'