-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_logic.py
79 lines (55 loc) · 2.11 KB
/
main_logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from math import ceil
def find_empty_pos(board):
for r in range(9):
for c in range(9):
if board[r][c] == 0:
return r, c
return None, None
def locate_num(row, col, board):
"""
# To locate and print a 3x3 subgrid, the row and the column of a given position.
"""
sub_board_of_interest = []
row_min, row_max = get_nearest_values(row)
col_min, col_max = get_nearest_values(col)
for line in range(9):
if row_min <= line < row_max:
sub_board_of_interest.append(board[line][col_min:col_max])
sub_board_as_list = [val for sublist in sub_board_of_interest for val in sublist]
row_as_list = board[row]
col_as_list = [(row[col]) for row in board]
return row_as_list, col_as_list, sub_board_as_list
def get_nearest_values(index):
roundup_max_float = ceil((index + 1) / 3) * 3
roundup_max_int = int(roundup_max_float)
if roundup_max_int <= 3:
roundup_min_int = 0
elif roundup_max_int <= 6:
roundup_min_int = 3
else:
roundup_min_int = 6
return roundup_min_int, roundup_max_int
def find_possible_nums(row, col, board):
"""
Find uniqs in 3x3, row and column
"""
target_row, target_col, sub_board = locate_num(row, col, board)
all_uniques = set(target_row + target_col + sub_board)
potential_values = [int(num) for num in range(10) if num not in all_uniques]
return potential_values
def solve(board):
# check for empty spaces, if none left, stop solving by returning True
row, col = find_empty_pos(board)
if row is None:
return True
potential_values = find_possible_nums(row, col, board)
# try to finish the game with guessing from possible variants
for num in potential_values:
# wild guess and check whether it is valid
board[row][col] = num
# continue solving with the wild guess.
if solve(board):
return True
# if wrong, backtrack and try next potential value
board[row][col] = 0
return False