def in_game_key_down(key, window): global in_game_time # s to solve the sudoku. if key == pg.K_s: start_time = time() print("solving") solvboard_level1(window) print("It took me: ", round(time() - start_time, 2), "seconds to solve this.") return "In_game" elif key == pg.K_l: print("solving level 6") solvboard_level6(window) return "In_game" elif key == pg.K_BACKSPACE: try: cox, coy = prev_nums.pop() for numy, yrow in enumerate(board): for numx, x in enumerate(yrow): if x == 11: board[numy][numx] = 10 board[coy][cox] = 11 used_nums(cox, coy) return "Fill_in" except IndexError: print("The board is empty, You can't go further back.") return "In_game" elif key == pg.K_ESCAPE: in_game_time = time() - in_game_start_time return "Paused" else: return "In_game"
def in_game_mousekey_down(cox, coy): # If coordinates on an empty cell: gamestate = Fill in. # Fill_in state and footnote will use this mousekey_down too. xrow = int(cox // (WIDTH / ROWS)) yrow = int(coy // (HEIGHT / COLS)) if board[yrow][xrow] == 11: board[yrow][xrow] = 12 return "Footnote" elif board[yrow][xrow] == 12: board[yrow][xrow] = 10 return "In_game" if static_board[yrow][xrow] == 0: for numy, y in enumerate(board): for numx, x in enumerate(y): if x == 11 or x == 12: board[numy][numx] = 10 if board[yrow][xrow] < 10: prev_nums.append((xrow, yrow, board[yrow][xrow])) print(prev_nums) board[yrow][xrow] = 11 used_nums(xrow, yrow) print(av_nums) return "Fill_in" else: return "In_game"
def guess_one(): shortest_av_nums = 10 shortest_x, shortest_y = 0, 0 for ynum, yrow in enumerate(board): for xnum, x in enumerate(yrow): used_nums(xnum, ynum) if len(av_nums) < shortest_av_nums and x == 10: shortest_av_nums = len(av_nums) shortest_x = xnum shortest_y = ynum used_nums(shortest_x, shortest_y) if len(av_nums) != 0: print("Guessing: ", av_nums[0], "for: ", shortest_x, shortest_y) board[shortest_y][shortest_x] = av_nums[0] return shortest_x, shortest_y else: return 444, 444
def solvboard_level6(window): global guessing guessing += 1 old_board = deepcopy(board) guess_x, guess_y = guess_one() print("Guessloop: ", guessing) solvboard_level1(window) for i in av_board: i.clear() for y in range(9): for x in range(9): if board[y][x] == 10: used_nums(x, y) av_board[y].append(tuple(av_nums)) else: av_board[y].append(10) finished = False zeros = 0 for yrow in av_board: for i in yrow: try: if len(i) == 0 and not finished: finished = True for row in range(9): board[row] = old_board[row] used_nums(guess_x, guess_y) if check_win(): pass elif guessing == 1: print("Guess was not right, So it has to be: ", av_nums[1], "for: ", guess_x, guess_y) guessing -= 1 board[guess_y][guess_x] = av_nums[1] solvboard_level1(window) else: print("Prev guess was not right, so will now try: ", av_nums[1], "for: ", guess_x, guess_y) board[guess_y][guess_x] = av_nums[1] solvboard_level1(window) except TypeError: pass
def solvboard_level1(window): stuck = True for y in range(9): for x in range(9): if board[y][x] == 10: used_nums(x, y) if len(av_nums) == 1: stuck = False print("Making ", x, y, "this: ", av_nums) board[y][x] = av_nums.pop() window.fill(WHITE) drawgrid(window) drawboard(window) pg.display.update() if check_win(): print("solved") elif stuck: print("Couldn't solve more than this. going on to level 2.") solvboard_level2(window) else: solvboard_level1(window)
def fill_in_key_down(key): global in_game_time if 48 <= key <= 57: for numy, yrow in enumerate(board): for numx, x in enumerate(yrow): if x == 11 and key - 48 in av_nums: board[numy][numx] = key - 48 if numx < 8 and board[numy][numx + 1] == 10: used_nums(numx + 1, numy) print(av_nums) board[numy][numx + 1] = 11 elif numx == 8 and board[numy + 1][0] == 10: board[numy + 1][0] = 11 used_nums(0, numy) print(av_nums) prev_nums.append((numx, numy)) return "Fill_in" else: return "Fill_in" elif key == pg.K_BACKSPACE: try: prev = prev_nums.pop() for numy, yrow in enumerate(board): for numx, x in enumerate(yrow): if x == 11: board[numy][numx] = 10 if len(prev) == 2: cox, coy = prev board[coy][cox] = 11 used_nums(cox, coy) elif len(prev) == 3: cox, coy, num = prev board[coy][cox] = num return "Fill_in" except IndexError: print("The board is empty, You can't go further back.") return "Fill_in" elif key == pg.K_f: for numy, yrow in enumerate(board): for numx, x in enumerate(yrow): if x == 11: board[numy][numx] = 12 return "Footnote" elif key == pg.K_ESCAPE: in_game_time = time() - in_game_start_time return "Paused" elif key == pg.K_RIGHT or key == pg.K_TAB or key == pg.K_d: move_selected(1, 0, 11) return "Fill_in" elif key == pg.K_LEFT or key == pg.K_a: move_selected(-1, 0, 11) return "Fill_in" elif key == pg.K_UP or key == pg.K_w: move_selected(0, -1, 11) return "Fill_in" elif key == pg.K_DOWN or key == pg.K_RETURN or key == pg.K_s: move_selected(0, 1, 11) return "Fill_in" else: return "Fill_in"
def solvboard_level2(window): stuck = True for y in range(9): for x in range(9): if board[y][x] == 10: xbox = x // 3 ybox = y // 3 used_nums(x, y) av_numbers = av_nums.copy() for num in av_numbers: alone_x, alone_y, alone_cell = True, True, True for x_y_in_row in range(9): used_nums(x_y_in_row, y) if num in av_nums and x_y_in_row != x and board[y][ x_y_in_row] == 10: alone_x = False used_nums(x, x_y_in_row) if num in av_nums and x_y_in_row != y and board[ x_y_in_row][x] == 10: alone_y = False for y_cell in range(3): for x_cell in range(3): used_nums((xbox * 3 + x_cell), (ybox * 3 + y_cell)) if num in av_nums and (xbox * 3 + x_cell != x or ybox * 3 + y_cell != y) \ and board[ybox * 3 + y_cell][xbox * 3 + x_cell] == 10: alone_cell = False if alone_x or alone_y or alone_cell: stuck = False print("Making ", x, y, "this: ", num) board[y][x] = num if not stuck: print("Level 2 solved! going back to level 1") window.fill(WHITE) drawgrid(window) drawboard(window) pg.display.update() solvboard_level1(window) else: print("Couldn't solve more than this. going on to level 3.") solvboard_level3(window)
def solvboard_raw_solver(window, normal_board, co): if co == (10, 10): new_cox, new_coy = guess_one() else: x, y = co used_nums(x, y) print(av_nums[0]) print(av_nums[1]) window.fill(WHITE) drawgrid(window) drawboard(window) pg.display.update() stuck = False while not stuck: stuck = True for ynum, yrow in enumerate(board): for xnum, x in enumerate(yrow): used_nums(xnum, ynum) if len(av_nums) == 1 and x == 10: print("Making: ", xnum, ynum, ": ", av_nums[0]) board[ynum][xnum] = av_nums[0] stuck = False window.fill(WHITE) drawgrid(window) drawboard(window) pg.display.update() for y in range(9): for x in range(9): if board[y][x] == 10: used_nums(x, y) av_board[y].append(tuple(av_nums)) else: av_board[y].append(10) impossible = False for yrow in av_board: for x in yrow: try: if len(x) == 0: impossible = True except TypeError: pass if not impossible: solvboard_raw_solver(window, normal_board, (new_cox, new_coy)) else: print("Got stuck here.") print("Normal_board:") for i in normal_board: print(i) print("In_game")
def solvboard_level3(window): for i in av_board: i.clear() for y in range(9): for x in range(9): if board[y][x] == 10: used_nums(x, y) av_board[y].append(tuple(av_nums)) else: av_board[y].append(10) for y in range(9): for x in range(9): if av_board[y][x] != 10: for x_in_yrow in range(9): if av_board[y][x] == av_board[y][ x_in_yrow] and x_in_yrow != x and len( av_board[y][x]) == 2: num1, num2 = av_board[y][x] for xrow in range(9): if av_board[y][xrow] != 10 and av_board[y][ xrow] != av_board[y][x]: lst = list(av_board[y][xrow]) try: try: lst.remove(num1) lst.remove(num2) except ValueError: lst.remove(num2) except ValueError: pass av_board[y][xrow] = tuple(lst) if av_board[y][x] == av_board[x_in_yrow][ x] and x_in_yrow != y and len(av_board[y][x]) == 2: num1, num2 = av_board[y][x] for yrow in range(9): if av_board[yrow][x] != 10 and av_board[yrow][ x] != av_board[y][x]: lst = list(av_board[yrow][x]) try: try: lst.remove(num1) lst.remove(num2) except ValueError: lst.remove(num2) except ValueError: pass av_board[yrow][x] = tuple(lst) ybox = y // 3 xbox = x // 3 for y_in_grid in range(3): for x_in_grid in range(3): if av_board[y][x] == av_board[ybox * 3 + y_in_grid][xbox * 3 + x_in_grid]\ and (y != ybox * 3 + y_in_grid or x != xbox * 3 + x_in_grid)\ and len(av_board[y][x]) == 2: num1, num2 = av_board[y][x] for yrow_in_grid in range(3): for xrow_in_grid in range(3): if av_board[ybox * 3 + yrow_in_grid][xbox * 3 + xrow_in_grid] != 10 \ and av_board[ybox * 3 + yrow_in_grid][xbox * 3 + xrow_in_grid] != av_board[y][x]: lst = list( av_board[ybox * 3 + yrow_in_grid][ xbox * 3 + xrow_in_grid]) try: try: lst.remove(num1) lst.remove(num2) except ValueError: lst.remove(num2) except ValueError: pass av_board[ybox * 3 + yrow_in_grid][ xbox * 3 + xrow_in_grid] = tuple(lst) stuck = True for y in range(9): for x in range(9): try: if len(av_board[y][x]) == 1: stuck = False print("Making ", x, y, "this: ", av_board[y][x][0]) board[y][x] = av_board[y][x][0] except TypeError: pass if not stuck: print("Level 3 solved! going back to level 1") for i in av_board: i.clear() window.fill(WHITE) drawgrid(window) drawboard(window) pg.display.update() solvboard_level1(window) else: print("Couldn't solve more than this. going on to level 4.") for i in av_board: i.clear() solvboard_level4(window)
def solvboard_level5(window): for i in av_board: i.clear() for y in range(9): for x in range(9): if board[y][x] == 10: used_nums(x, y) av_board[y].append(tuple(av_nums)) else: av_board[y].append(10) for y_cell in range(3): for x_cell in range(3): for num in range(1, 10): co_nums_x = [] co_nums_y = [] for y_in_cell in range(3): for x_in_cell in range(3): try: if num in av_board[y_cell * 3 + y_in_cell][x_cell * 3 + x_in_cell] \ or board[y_cell * 3 + y_in_cell][x_cell * 3 + x_in_cell] == num: co_nums_x.append(x_cell * 3 + x_in_cell) co_nums_y.append(y_cell * 3 + y_in_cell) except TypeError: pass if len(co_nums_x) > 0 and len(co_nums_x) == co_nums_x.count( co_nums_x[0]): for y_down in range(9): try: if num in av_board[y_down][ co_nums_x[0]] and y_down not in co_nums_y: lst = list(av_board[y_down][co_nums_x[0]]) lst.remove(num) av_board[y_down][co_nums_x[0]] = tuple(lst) except TypeError: pass if len(co_nums_y) > 0 and len(co_nums_y) == co_nums_y.count( co_nums_y[0]): for x_right in range(9): try: if num in av_board[co_nums_y[0]][ x_right] and x_right not in co_nums_x: lst = list(av_board[co_nums_y[0]][x_right]) lst.remove(num) av_board[co_nums_y[0]][x_right] = tuple(lst) except TypeError: pass stuck = True for y in range(9): for x in range(9): try: if len(av_board[y][x]) == 1: stuck = False print("Making ", x, y, "this: ", av_board[y][x][0]) board[y][x] = av_board[y][x][0] except TypeError: pass if not stuck: print("Level 5 solved! going back to level 1") for i in av_board: i.clear() window.fill(WHITE) drawgrid(window) drawboard(window) pg.display.update() solvboard_level1(window) elif guessing == 0: print("Couldn't solve more than this, going on to level 6") for i in av_board: i.clear() solvboard_level6(window) if guessing > 0: for i in av_board: i.clear for y in range(9): for x in range(9): if board[y][x] == 10: used_nums(x, y) av_board[y].append(tuple(av_nums)) else: av_board[y].append(10) stuck = False for yrow in av_board: for i in yrow: try: if len(i) < 1: stuck = True except TypeError: pass if not stuck and not check_win(): print( "Couldn't solve more for this guess, so guessing another one.") for i in av_board: i.clear() solvboard_level6(window)