Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 43e42b5

Browse filesBrowse files
committed
sudoku-solver
1 parent d18c4fc commit 43e42b5
Copy full SHA for 43e42b5

12 files changed

+426
-186
lines changed

‎.DS_Store

Copy file name to clipboard
6 KB
Binary file not shown.

‎README.md

Copy file name to clipboard
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
## # Simple Snake Game in Python 3 for RANDOM, TIME, Turtle themes
1+
# Sudoku Game In Python
2+
3+
4+
### run:
5+
6+
* python gui.py
7+
8+
### Mac
9+
10+
* python3 gui.py

‎chooseLevel.py

Copy file name to clipboard
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import pygame
2+
3+
BLACK = (0, 0, 0)
4+
WHITE = (255, 255, 255)
5+
GREEN = (0, 255, 0)
6+
L_GREEN = (150, 255, 150)
7+
RED = (255, 0, 0)
8+
L_RED = (255, 204, 203)
9+
GRAY = (80, 80, 80)
10+
YELLOW = (255, 255, 0)
11+
#
12+
pygame.init()
13+
X = 300
14+
Y = 200
15+
size = (X, Y)
16+
window = pygame.display.set_mode(size)
17+
font = pygame.font.Font('freesansbold.ttf', 25)
18+
19+
20+
21+
def drawButton(left, top, color, textInButton):
22+
rectSize = pygame.Rect(left, top, 60, 30)
23+
pygame.draw.rect(window, color, rectSize) # left, top, width, height
24+
pygame.draw.rect(window, BLACK, rectSize, 3)
25+
fontButton = pygame.font.Font('freesansbold.ttf', 20)
26+
textButton = fontButton.render(textInButton, True, BLACK, )
27+
textRectButton = textButton.get_rect()
28+
textRectButton.center = (left + 30, top + 15)
29+
window.blit(textButton, textRectButton)
30+
31+
32+
def chooseLevel():
33+
level = 0
34+
text = font.render('choose difficulty level', True, BLACK, WHITE)
35+
textRect = text.get_rect()
36+
textRect.center = (X // 2, Y // 2 - 40)
37+
38+
pygame.display.set_caption("Sudoku King")
39+
40+
done = True
41+
while done:
42+
window.fill(WHITE)
43+
window.blit(text, textRect)
44+
drawButton(40, 100, GRAY, "1")
45+
drawButton(120, 100, GRAY, "2")
46+
drawButton(200, 100, GRAY, "3")
47+
pos = pygame.mouse.get_pos()
48+
49+
for event in pygame.event.get():
50+
if event.type == pygame.QUIT:
51+
# deactivates the pygame library
52+
pygame.quit()
53+
# quit the program.
54+
quit()
55+
if event.type == pygame.MOUSEBUTTONDOWN:
56+
# print("Click ", pos)
57+
if (40 <= pos[0] <= 100) and (100 <= pos[1] <= 130):
58+
level = 1
59+
if (120 <= pos[0] <= 180) and (100 <= pos[1] <= 130):
60+
level = 2
61+
if (200 <= pos[0] <= 260) and (100 <= pos[1] <= 130):
62+
level = 3
63+
if level != 0:
64+
# print(level)
65+
pygame.quit()
66+
return level
67+
68+
# Draws the surface object to the screen.
69+
pygame.display.update()
70+
71+
72+
# chooseLevel()

‎generator.py

Copy file name to clipboard
+100Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import random
2+
import copy
3+
4+
# firstBoard = [
5+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
6+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
7+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
8+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
9+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
10+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
11+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
12+
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
13+
# [0, 0, 0, 0, 0, 0, 0, 0, 0]
14+
# ]
15+
16+
17+
def printBoard(board):
18+
for i in range(len(board)):
19+
if i % 3 == 0 and i != 0:
20+
print("- - - - - - - - - - - -")
21+
for j in range(len(board[0])):
22+
if j % 3 == 0 and j != 0:
23+
print(" | ", end="")
24+
if j == 8: # end of the row
25+
print(board[i][j])
26+
else:
27+
print(str(board[i][j]) + " ", end="")
28+
29+
30+
def findEmpty(board):
31+
for y in range(len(board)):
32+
for x in range(len(board[0])):
33+
if board[y][x] == 0:
34+
return y, x # y = row , x = column
35+
# if we got here it mean that we finish the sudoku, so return none
36+
return None
37+
38+
39+
def validCheck(board, number, coordinates):
40+
# checking row
41+
for x in range(len(board[0])):
42+
if number == board[coordinates[0]][x] and coordinates[1] != x: # coordinates[0]= row
43+
return False
44+
45+
# checking column
46+
for y in range(len(board)):
47+
if number == board[y][coordinates[1]] and coordinates[0] != y:
48+
return False
49+
50+
# checking the box
51+
box_x = coordinates[1] // 3
52+
box_y = coordinates[0] // 3
53+
54+
for y in range(box_y * 3, box_y * 3 + 3):
55+
for x in range(box_x * 3, box_x * 3 + 3):
56+
if number == board[y][x] and (y, x) != coordinates:
57+
return False
58+
59+
return True
60+
61+
62+
def generateRandomBoard(board):
63+
# end condition:- getting to the end of the board - the function findEmpty return NONE
64+
find = findEmpty(board)
65+
if find is None: # if find != False
66+
return True
67+
else:
68+
row, col = find
69+
for number in range(1, 10):
70+
randomNumber = random.randint(1, 9) # TODO: need to work on the algorithm a bit more -
71+
# TODO: to not rand the same number over and over again
72+
if validCheck(board, randomNumber, (row, col)):
73+
board[row][col] = randomNumber
74+
if generateRandomBoard(board):
75+
return True
76+
77+
board[row][col] = 0
78+
return False
79+
80+
81+
def deleteCells(firstBoard,number):
82+
while number:
83+
row = random.randint(0, 8)
84+
col = random.randint(0, 8)
85+
if firstBoard[row][col] != 0:
86+
firstBoard[row][col] = 0
87+
number = number - 1
88+
89+
90+
def sudokuGenerate(firstBoard, level):
91+
92+
# printBoard(firstBoard)
93+
generateRandomBoard(firstBoard)
94+
# printBoard(firstBoard)
95+
if level == 1:
96+
deleteCells(firstBoard,30)
97+
if level == 2:
98+
deleteCells(firstBoard,40)
99+
if level == 3:
100+
deleteCells(firstBoard,50)

‎gifs/playing the game.gif

Copy file name to clipboard
388 KB
Loading

‎gifs/start the game.gif

Copy file name to clipboard
615 KB
Loading

‎gui.py

Copy file name to clipboard
+199Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import pygame
2+
from sudokuSolver import *
3+
from chooseLevel import *
4+
import time
5+
6+
# Define some colors
7+
BLACK = (0, 0, 0)
8+
WHITE = (255, 255, 255)
9+
GREEN = (0, 255, 0)
10+
L_GREEN = (150, 255, 150)
11+
RED = (255, 0, 0)
12+
L_RED = (255, 204, 203)
13+
GRAY = (60, 60, 60)
14+
L_GRAY = (220, 220, 220)
15+
YELLOW = (255, 255, 0)
16+
17+
# This sets the WIDTH and HEIGHT of each grid location
18+
WIDTH = HEIGHT = 50
19+
20+
# This sets the margin between each cell
21+
MARGIN = 5
22+
numbers_1to9 = [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5, pygame.K_6, pygame.K_7, pygame.K_8,
23+
pygame.K_9]
24+
25+
# Set the width and height of the screen [width, height]
26+
size = (500, 500)
27+
# screen = pygame.display.set_mode(size)
28+
pygame.init()
29+
font = pygame.font.Font('freesansbold.ttf', 32)
30+
31+
# pygame.display.set_caption("Sudoku King")
32+
33+
# Loop until the user clicks the close button.
34+
done = False
35+
36+
37+
def cheatingAllTheWay():
38+
for row in range(len(Board)):
39+
for column in range(len(Board[row])):
40+
Board[row][column] = solvedBoard[row][column]
41+
addNumToBoard(Board[row][column], row, column, L_GREEN)
42+
time.sleep(0.05)
43+
pygame.display.flip()
44+
finish()
45+
46+
47+
def addNumToBoard(number, row, column, color):
48+
addNewRect(row, column, WHITE, 5)
49+
addNewRect(row, column, color, None)
50+
font = pygame.font.Font('freesansbold.ttf', 32)
51+
text = font.render(str(number), True, BLACK, )
52+
textRect = text.get_rect() # get_rect() -> Returns a new rectangle covering the entire surface.
53+
textRect.center = ((MARGIN + WIDTH) * column + MARGIN + WIDTH / 2, (MARGIN + HEIGHT) * row + MARGIN + WIDTH / 2)
54+
screen.blit(text, textRect)
55+
drawTheBorder()
56+
57+
58+
def finish():
59+
if solvedBoard == Board:
60+
print("good")
61+
else:
62+
print("not good")
63+
64+
65+
def addNewRect(row, col, color, width):
66+
rectSize = pygame.Rect((MARGIN + WIDTH) * col + MARGIN, (MARGIN + HEIGHT) * row + MARGIN, WIDTH,
67+
HEIGHT)
68+
if width is not None:
69+
pygame.draw.rect(screen, color, rectSize, width) # coloring only the border
70+
else:
71+
pygame.draw.rect(screen, color, rectSize) # coloring the whole rectangle
72+
73+
74+
def flickering(timeFlickering, color): # flickering with color on-off
75+
addNewRect(row, column, color, 5)
76+
pygame.display.flip()
77+
time.sleep(timeFlickering)
78+
addNewRect(row, column, WHITE, 5)
79+
pygame.display.flip()
80+
time.sleep(timeFlickering)
81+
addNewRect(row, column, color, 5)
82+
pygame.display.flip()
83+
time.sleep(timeFlickering)
84+
addNewRect(row, column, WHITE, 5)
85+
pygame.display.flip()
86+
87+
88+
def drawTheBorder():
89+
dif = 500 // 9
90+
for i in range(10):
91+
thick = 5
92+
pygame.draw.line(screen, GRAY, (0, i * dif + 2), (500, i * dif + 2), thick)
93+
pygame.draw.line(screen, GRAY, (i * dif + 2, 0), (i * dif + 2, 500), thick)
94+
for i in range(10):
95+
if i % 3 == 0:
96+
thick = 8
97+
pygame.draw.line(screen, BLACK, (0, i * dif), (500, i * dif), thick)
98+
pygame.draw.line(screen, BLACK, (i * dif, 0), (i * dif, 500), thick)
99+
100+
101+
def drawInitBoard():
102+
# printBoard(solvedBoard)
103+
for row in range(len(Board)):
104+
for column in range(len(Board[row])):
105+
color = L_GRAY
106+
if Board[row][column] == 0: # if we want to change to background of the empty cells
107+
color = WHITE
108+
# ----- drawing the rect ------
109+
pygame.draw.rect(screen, color,
110+
[(MARGIN + WIDTH) * column + MARGIN, (MARGIN + HEIGHT) * row + MARGIN, WIDTH, HEIGHT])
111+
# show nothing if the number is 0
112+
font = pygame.font.Font('freesansbold.ttf', 32)
113+
if Board[row][column] == 0:
114+
text = font.render(" ", True, BLACK, ) # render(text, anti-alias[True], color, background=None)
115+
else:
116+
text = font.render(str(Board[row][column]), True, BLACK, )
117+
118+
textRect = text.get_rect() # get_rect() -> Returns a new rectangle covering the entire surface.
119+
textRect.center = (
120+
(MARGIN + WIDTH) * column + MARGIN + WIDTH / 2, (MARGIN + HEIGHT) * row + MARGIN + WIDTH / 2)
121+
screen.blit(text, textRect)
122+
drawTheBorder()
123+
124+
125+
# -------- Main Program Loop -----------
126+
if __name__ == "__main__":
127+
flag1 = True
128+
129+
while flag1:
130+
level = chooseLevel()
131+
if level == 1 or level == 2 or level == 3:
132+
print(level)
133+
flag1 = False
134+
pygame.display.set_caption("Sudoku King1")
135+
screen = pygame.display.set_mode(size)
136+
137+
sol = mainSolver(level) # first at all the script solve the sudoku by itself
138+
139+
print("solveBoard")
140+
printBoard(sol)
141+
142+
# ------ draw the board ------
143+
pygame.init()
144+
screen.fill(BLACK)
145+
drawInitBoard()
146+
readyForInput = False
147+
key = None
148+
while not done:
149+
# --- Main event loop
150+
151+
for event in pygame.event.get():
152+
if event.type == pygame.QUIT:
153+
done = True
154+
if event.type == pygame.KEYDOWN:
155+
if event.key in numbers_1to9:
156+
key = chr(event.key)
157+
if event.key == pygame.K_RETURN:
158+
finish()
159+
if event.key == pygame.K_c:
160+
cheatingAllTheWay()
161+
if event.type == pygame.MOUSEBUTTONDOWN:
162+
# ------ if clicked on a cell get his row and column ------
163+
if readyForInput is True:
164+
addNewRect(row, column, WHITE, None)
165+
drawTheBorder()
166+
readyForInput = False
167+
168+
pos = pygame.mouse.get_pos()
169+
column = pos[0] // (WIDTH + MARGIN)
170+
row = pos[1] // (WIDTH + MARGIN)
171+
# ------ checking if it is a empty (0 inside) ------
172+
if Board[row][column] == 0:
173+
# ------ coloring the border of the clicked cell ----- #TODO YELLOW
174+
175+
addNewRect(row, column, YELLOW, 5)
176+
readyForInput = True
177+
# ------ now only wait for input from the user -----
178+
179+
if readyForInput and key is not None:
180+
# ------ checking if the key is good at it's place ------
181+
if int(key) == sol[row][column]:
182+
Board[row][column] = key
183+
flickering(0.1, GREEN) # flickering at a 0.2 seconds with the color green
184+
addNumToBoard(key, row, column, L_GREEN)
185+
else:
186+
flickering(0.1, RED) # flickering at a 0.2 seconds with the color red
187+
addNumToBoard(key, row, column, L_RED)
188+
189+
# -----------------------------------------------
190+
drawTheBorder()
191+
readyForInput = False
192+
193+
key = None
194+
pygame.display.flip()
195+
pygame.display.update()
196+
197+
198+
# Close the window and quit.
199+
pygame.quit()

‎images/choosing difficulty level.PNG

Copy file name to clipboard
14.9 KB
Loading

‎images/gaming.PNG

Copy file name to clipboard
34.9 KB
Loading

‎images/playing the game.PNG

Copy file name to clipboard
38 KB
Loading

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.