def test_print_board(capfd): """Test print_board function""" cells = sudoku.make_board('E') changed_cells, immut = sudoku.modify_board(cells, True) sudoku.print_board(changed_cells, immut, [], [], False, True) out, err = capfd.readouterr() assert out.strip() == """-------------------------------------
def main(): board = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7, 8, 9, 1], [5, 6, 7, 8, 9, 1, 2, 3, 4], [8, 9, 1, 2, 3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8, 9, 1, 2], [6, 7, 8, 9, 1, 2, 3, 4, 5], [9, 1, 2, 3, 4, 5, 6, 7, 8]] r = randomise_board(board) print_board("Randomised Sudoku Board:", r) s = Sudoku(r) s.solve() print_board("Completed Sudoku Board:", s.get_board())
def test_solve(self): board = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7, 8, 9, 1], [5, 6, 7, 8, 9, 1, 2, 3, 4], [8, 9, 1, 2, 3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8, 9, 1, 2], [6, 7, 8, 9, 1, 2, 3, 4, 5], [9, 1, 2, 3, 4, 5, 6, 7, 8]] r = randomise_board(board) print_board("Randomised Sudoku Board:", r) s = Sudoku(r) s.solve() print_board("Completed Sudoku Board:", s.get_board()) self.assertListEqual(board, s.get_board())
def main(): #create an empty board board = [[0] * 9 for _ in range(9)] valid_sudoku = False while not valid_sudoku: try: generate(board) solve(board) valid_sudoku = True except RecursionError: valid_sudoku = False print_board(board)
def createFullPuzzle(board): for i in range(0, 9): for j in range(0, 9): tmp = ['1','2','3','4','5','6','7','8','9'] while True: board[i][j] = random.choice(tmp) if sudoku.validateCell(board, i, j): break else: tmp.remove(board[i][j]) if len(tmp) == 0: sudoku.print_board(board) return board sudoku.print_board(board) return board
def generateBoard2(difficulty=4): board = [['0' for x in xrange(9)] for x in xrange(9)] LB = LEVELS[difficulty][0] UB = LEVELS[difficulty][1] placedCtr = 0 while True: if placedCtr > UB: placedCtr = 0 board = [['0' for x in xrange(9)] for x in xrange(9)] if addElement(board) != None: placedCtr += 1 sudoku.print_board(board) result = sudoku.solve(board) if sudoku.checkIfDone(result): # there is a solution print "nyan" break else: print "delai" sudoku.print_board(result) else: placedCtr = 0 board = [['0' for x in xrange(9)] for x in xrange(9)] sudoku.print_board(board)
def generateBoard(difficulty=4): # generate a full puzzle, then remove the items to create different difficulty level board = [['0' for x in xrange(9)] for x in xrange(9)] while not sudoku.validateBoard(board): board = createFullPuzzle(board) sudoku.print_board(board) result = board lb = LEVELS[difficulty][0] ub = LEVELS[difficulty][1] num = LOWERBOUNDS[difficulty] removedCtr = 0 while (81 - removedCtr) >= lb: i = random.randrange(0, 9) j = random.randrange(0, 9) if board[i][j] != '0': # check row if (9 - board[i].count('0')) > num: continue buf = [] # check col for k in range(0, 9): buf.append(board[i][k]) if (9 - buf.count('0')) > num: continue tmp = board[i][j] board[i][j] = '0' tmpboard = sudoku.solve(board) if checkSolution(tmpboard, result): removedCtr += 1 if (81 - removedCtr) <= ub and random.randrange(0, 10) < 4: break else: board[i][j] = tmp sudoku.print_board(board) sudoku.print_board(result)
else: y, x = sudoku.next_xy(board) valid_nums = sudoku.valid_for_cell(board, y, x) for num in valid_nums: newboard = copy.deepcopy(board) newboard[y][x] = num nxt = find_solution(newboard) if nxt is not None: return nxt def solve_hard_puzzle(): board = [ [4, 0, 0, 0, 0, 0, 0, 0, 2], [1, 0, 7, 0, 0, 2, 5, 0, 0], [6, 0, 0, 5, 0, 0, 0, 8, 0], [0, 0, 0, 0, 9, 4, 6, 7, 0], [0, 0, 0, 0, 2, 0, 0, 0, 0], [0, 3, 6, 8, 7, 0, 0, 0, 0], [0, 8, 0, 0, 0, 7, 0, 0, 5], [0, 0, 1, 9, 0, 0, 3, 0, 6], [2, 0, 0, 0, 0, 0, 0, 0, 7], ] return find_solution(board) if __name__ == "__main__": sudoku.print_board(solve_hard_puzzle())
#!/usr/bin/python import sudoku import sys ## main. try: arg = sys.argv[1] except (IndexError): arg = "board" board = sudoku.read_board(arg) done = sudoku.solve(board, 0) sudoku.print_board(done)
temp = int(temp[0]) else: temp = 0 ans.append(temp) x += dx x = xc if (j % 3 == 0): y += dy + 12 else: y += dy + 8 grid.append(ans) touch.append(t) orig_grid = copy.deepcopy(grid) solve(grid) print_board(grid) def click(i, j): device.shell(f'input touchscreen tap {touch[i][j][0]} {touch[i][j][1]}') def select(n): arr = [123, 212, 314, 410, 515, 623, 724, 839, 937] device.shell(f'input touchscreen tap {arr[n-1]} 1453') for i in range(len(grid)): for j in range(len(grid[0])): if (orig_grid[i][j] == 0): select(grid[i][j])
temp = int(temp[0]) if temp[0].isnumeric() else 0 grid_row.append(temp) x += dx x = xcopy # En nuestra aplicación cada 3 filas se presenta una línea más # gruesa, entonces: ## y += dy + (11 if j % 3 == 0 else 6) mat.append(grid_row) touch_mat.append(t_row) # Resolver e imprimir en pantalla el sudoku si es que este se puede resolver, # caso contrario se imprime en pantalla el mensaje que indica que el sudoku # no es solucionable y cambia el valor de "impr" a 1 para que ninguna acción # sea ralizada en pantalla: print_board(mat) print("_________________________") orig_grid = copy.deepcopy(mat) if sudoku_solver(mat): print("Sudoku Resuelto: ") print_board(mat) else: print("No existe solución") impr = 1 # TO MOD # Función que permite al programa detectar las coordenadas de cada casilla y # y realizar un "click" sobre las mismas: def click(i, j): device.shell(f'input touchscreen tap {touch_mat[i][j][0]} {touch_mat[i][j][1]}')
def test_is_empty_top_left(self): print_board(self.board) self.assertEqual(find_next_empty(self.board), (0, 0))
sudoku_solutions = open('data/sudokus_finish.txt').readlines() ################################################################################ # DEBUGGING # ################################################################################ string = sudoku_start[10] # string = list(solvable.keys())[1] sudoku = sudoku_board(string) csp = csp_sudoku(sudoku) csp.domain # AC3(csp) csp.domain a = Backtracking_Search(csp) sudoku.solved_board.update(a) sudoku.print_board('board') print() sudoku.print_board('solved_board') solution = solvable[string] sudoku = sudoku_board(string) sudoku.print_board('board') sudoku_solved = deepcopy(sudoku) sudoku_solved.board.update(a) print() sudoku_solved.print_board() ################################################################################ # SOLVE ALL SUDOKUS WITH AC3 # ################################################################################ solvable = {} count = 0
print success("Taking a screenshot") # take a screenshot of the board im = utility.take_screenshot(clicks) # get a board from the image # it could throw a ValueError exception try: print success("Parsing the screenshot") board = utility.get_board_from_image(im) break except ValueError: print error("Error parsing the board. I need a new screenshot") print success("Board taken") print sudoku.print_board(board) print success("Solving the board") solution = sudoku.complete_board(board) print success("Solution") print sudoku.print_board(board, solution) width = clicks[1][0] - clicks[0][0] height = clicks[1][1] - clicks[0][1] print success("Filling the board with the solution") utility.fill_board(board, solution, clicks[0][0], clicks[0][1], width, height) print success("Done!")
def generate(difficulty): try: board = open_file("./seeds/{0}.txt".format(difficulty)) except: print('Invalid difficulty selected.') return 0 x = random.randint(0, 1) if (x): reflect_x(board) x = random.randint(0, 1) if (x): reflect_y(board) x = random.randint(0, 3) for i in range(x): board = rotate(board) array = random_array() for i in range(9): for j in range(9): board[i][j] = array[board[i][j]] return board if __name__ == "__main__": board = generate("very_hard") assert board sudoku.print_board(board) print() assert sudoku.solve(board) sudoku.print_board(board)
print success("Taking a screenshot") # take a screenshot of the board im = utility.take_screenshot(clicks) # get a board from the image # it could throw a ValueError exception try: print success("Parsing the screenshot") board = utility.get_board_from_image(im) break except ValueError: print error("Error parsing the board. I need a new screenshot") print success("Board taken") print sudoku.print_board(board) print success("Solving the board") solution = sudoku.complete_board(board) print success("Solution") print sudoku.print_board(board, solution) width = clicks[1][0]-clicks[0][0] height = clicks[1][1]-clicks[0][1] print success("Filling the board with the solution") utility.fill_board(board, solution, clicks[0][0],clicks[0][1],width,height) print success("Done!")