-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_input_gui.py
185 lines (147 loc) · 5.83 KB
/
my_input_gui.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#input board gui
import pygame
import time
class Grid:
#board = random_fn.random_inp()
def __init__(self, board, rows, cols, width, height, win):
self.rows = rows
self.cols = cols
self.board = board
self.cubes = [[Cube(self.board[i][j], i, j, width, height) for j in range(cols)] for i in range(rows)]
self.width = width
self.height = height
self.model = None
self.update_model()
self.selected = None
self.win = win
def update_model(self):
self.model = [[self.cubes[i][j].value for j in range(self.cols)] for i in range(self.rows)]
def update_board(self, b):
self.board = b
self.cubes = [[Cube(self.board[i][j], i, j, self.width, self.height) for j in range(self.cols)] for i in range(self.rows)]
self.update_model()
def is_finished(self):
for i in range(self.rows):
for j in range(self.cols):
if self.cubes[i][j].value == 0:
return False
return True
def select(self, row, col):
# Reset all other
for i in range(self.rows):
for j in range(self.cols):
self.cubes[i][j].selected = False
self.cubes[row][col].selected = True
self.selected = (row, col)
def click(self, pos):
if pos[0] < self.width and pos[1] < self.height:
gap = self.width / 9
x = pos[0] // gap
y = pos[1] // gap
return int(y), int(x)
else:
return None
def draw(self):
# Draw Grid Lines
gap = self.width / 9
for i in range(self.rows+1):
thick = 4 if i % 3 == 0 and i != 0 else 1
pygame.draw.line(self.win, (0,0,0), (0, i*gap), (self.width, i*gap), thick)
pygame.draw.line(self.win, (0, 0, 0), (i * gap, 0), (i * gap, self.height), thick)
# Draw Cubes
for i in range(self.rows):
for j in range(self.cols):
self.cubes[i][j].draw(self.win)
def sketch(self, val):
row, col = self.selected
self.cubes[row][col].set(val)
self.update_model()
def validate_board(self):
if isValidSudoku(self.model):
print("valid board")
#call sudoko main
return self.model
else:
print("invalid board")
b_restart = [[0 for _ in range(9)] for _ in range(9)]
self.update_board(b_restart)
return None
class Cube:
rows = 9
cols = 9
def __init__(self, value, row, col, width, height):
self.value = value
self.temp = 0
self.row = row
self.col = col
self.width = width
self.height = height
self.selected = False
def draw(self, win):
fnt = pygame.font.SysFont("comicsans", 40)
gap = self.width / 9
x = self.col * gap
y = self.row * gap
if self.temp != 0 and self.value == 0:
text = fnt.render(str(self.temp), 1, (128,128,128))
win.blit(text, (x+5, y+5))
elif not(self.value == 0):
text = fnt.render(str(self.value), 1, (0, 0, 0))
win.blit(text, (x + (gap/2 - text.get_width()/2), y + (gap/2 - text.get_height()/2)))
if self.selected:
pygame.draw.rect(win, (255,0,0), (x,y, gap ,gap), 3)
def set(self, val):
self.value = val
def set_temp(self, val):
self.temp = val
def is_valid(grid, row, col, val):
for i in range(9):
if((grid[row][i] == val) or
(grid[i][col] == val) or
(grid[(3*(row//3) +(i//3))][(3*(col//3))+(i%3)] == val)):
return False
return True
def isValidSudoku(board):
def is_valid_placement(board, num, row, col):
# Check if the number is not in the same row or column
for i in range(9):
if board[row][i] == num or board[i][col] == num:
return False
# Check if the number is not in the same 3x3 subgrid
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for i in range(3):
for j in range(3):
if board[start_row + i][start_col + j] == num:
return False
return True
def solve_sudoku(board):
for i in range(81):
row = i // 9
col = i % 9
cell_val = board[row][col]
board[row][col] = 0 #to avoid considering the cell value when validating
#early detection of invalid boards
if cell_val != 0:
if cell_val < 0 or cell_val >9:
return False
if not is_valid(board, row, col ,cell_val):
return False
board[row][col] = cell_val #return board to its initial form after validation
for i in range(9):
for j in range(9):
if board[i][j] == 0:
for num in range(1, 10):
if is_valid_placement(board, num, i, j):
board[i][j] = num
if solve_sudoku(board):
return True
board[i][j] = 0 # Backtrack if the current placement is not valid
return False # No valid number found for this cell
return True # All cells filled successfully
# Copy the board to avoid modifying the input
board_copy = [row[:] for row in board]
# Attempt to solve the Sudoku
if solve_sudoku(board_copy):
return True # The board is valid
else:
return False # No solution found, or the initial board is invalid