Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
nrossol
Sudoku Backtracking
Commits
e8adde56
Commit
e8adde56
authored
Sep 05, 2020
by
nrossol
Browse files
Implemented solve button.
parent
77dcb603
Changes
2
Show whitespace changes
Inline
Side-by-side
gui.py
View file @
e8adde56
...
...
@@ -25,20 +25,23 @@ solvedGrid = [ [ 3, 2, 1, 0, 5, 0, 9, 4, 7 ],
solve
(
solvedGrid
)
#Basic Colors:
BLACK
=
(
0
,
0
,
0
)
WHITE
=
(
255
,
255
,
255
)
BLUE
=
(
0
,
0
,
255
)
#Initialize Pygame and variables::
pygame
.
init
()
pygame
.
display
.
init
()
screen_width
=
490
screen_height
=
490
buttonArea
=
100
#height of area where buttons will be below the grid
box_size
=
50
margin
=
5
font
=
pygame
.
font
.
SysFont
(
"Comic Sans MS"
,
30
)
font2
=
pygame
.
font
.
SysFont
(
"Arial"
,
28
)
screen
=
pygame
.
display
.
set_mode
([
screen_width
,
screen_height
+
buttonArea
])
pygame
.
display
.
set_caption
(
"Sudoku"
)
clock
=
pygame
.
time
.
Clock
()
...
...
@@ -62,14 +65,20 @@ while not done:
for
j
in
range
(
0
,
screen_height
,
box_size
+
margin
):
pygame
.
draw
.
rect
(
screen
,
WHITE
,
(
i
,
j
,
box_size
,
box_size
),
0
)
pygame
.
draw
.
rect
(
screen
,
WHITE
,
(
0
,
495
,
490
,
100
),
0
)
pygame
.
draw
.
rect
(
screen
,
BLUE
,
(
170
,
510
,
150
,
60
),
0
)
word_surface
=
font2
.
render
(
'Solve Puzzle'
,
False
,
(
0
,
0
,
0
))
screen
.
blit
(
word_surface
,
(
180
,
520
))
#mouse click:
if
clicked
:
print
(
"CLICKED"
)
if
pos
[
1
]
<
screen_height
:
if
pos
[
1
]
<
screen_height
:
#grid clicked
row
=
int
(
pos
[
0
]
/
(
box_size
+
margin
))
col
=
int
(
pos
[
1
]
/
(
box_size
+
margin
))
tempGrid
[
row
][
col
]
=
solvedGrid
[
row
][
col
]
print
(
"Grid Clicked"
)
elif
(
pos
[
1
]
>=
510
and
pos
[
1
]
<=
570
)
and
(
pos
[
0
]
>=
170
and
pos
[
0
]
<=
320
):
tempGrid
=
solvedGrid
print
(
"Solve Button Clicked"
)
#fill in grid:
for
i
in
range
(
0
,
9
):
...
...
sudokuSolverPython.py
View file @
e8adde56
#Recursive/backtracking algorithm to solve sudoku puzzle if possible.
#By Nathan Rossol, Computer Science sophomore at the University of Michigan
grid
=
[
[
3
,
2
,
1
,
0
,
5
,
0
,
9
,
4
,
7
],
[
7
,
8
,
0
,
0
,
1
,
0
,
0
,
6
,
5
],
[
0
,
0
,
6
,
7
,
0
,
4
,
1
,
0
,
0
],
[
5
,
4
,
9
,
0
,
0
,
0
,
7
,
8
,
6
],
[
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
],
[
0
,
0
,
0
,
9
,
0
,
6
,
0
,
0
,
0
],
[
1
,
0
,
5
,
0
,
6
,
0
,
4
,
0
,
2
],
[
0
,
3
,
0
,
2
,
0
,
7
,
0
,
5
,
0
],
[
2
,
0
,
7
,
0
,
4
,
0
,
8
,
0
,
3
]
]
#TO RUN THIS PROGRAM: Instal pygame and use terminal to run gui.py. Make sure this file and gui.py
#are in the same directory.
#Game rules: The grid has 81 squares: 9 boxes of 9 squares. Each box must contain
#all numbers 1-9 in its squares, and each number can appear only once in any row,
...
...
@@ -89,7 +83,7 @@ def printBoard(grid):
def
solve
(
grid
):
if
solveSudoku
(
grid
):
print
(
"S
OLVED
!"
)
print
(
"S
olveable :)
!"
)
else
:
print
(
"Unsolvable :("
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment