Skip to content

Commit 6f4be4f

Browse files
committed
Sudoku Game
Sudoku Game in python
1 parent 65e26c6 commit 6f4be4f

File tree

5 files changed

+295
-0
lines changed

5 files changed

+295
-0
lines changed

Sudoku Game/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SUDOKU IN PYTHON
2+
3+
It is one of the most popular puzzle games that we have been playing since our childhood.
4+
It’s a great brain game as it helps the player to improve their brain capacity.
5+
6+
7+
### GOAL
8+
9+
In this puzzle game, the user has to fill a 9×9 grid with digits such that each row,
10+
each column and each of the 3×3 subgrids that form the grid contains the digit from 1 to 9.
11+
12+
### DESCRIPTION
13+
14+
The main objective of the project is to develop a sudoku game.
15+
1.Install pygame in order to start the project.
16+
2.Import the pygame module for this project.
17+
18+
### Project File Structure
19+
20+
Project File Structure
21+
Let’s start developing python sudoku game:
22+
23+
1. Installation of Pygame module
24+
2. Initializing sudoku game window and variables
25+
3. Function for highlighting selected cell
26+
4. Function to draw lines for making sudoku grid
27+
5. Function to fill value in the cell
28+
6. Function for raising error when wrong value is entered
29+
7. Function to check if the entered value is valid
30+
8. Function to solve sudoku game
31+
9. Function to show result
32+
10. Rest code
33+
34+
35+
### LIBRARIES NEEDED
36+
37+
The follwing are required :
38+
1) Pygame(2.1.2) (python)
39+
40+
41+
42+
### NAME
43+
Aditi Kesarwani

Sudoku Game/images/Capture.PNG

40.7 KB
Loading

Sudoku Game/images/Filled Grid.PNG

37.3 KB
Loading

Sudoku Game/main.py

+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
# import pygame library
2+
import pygame
3+
4+
5+
# initialise the pygame font
6+
pygame.font.init()
7+
8+
# Total window
9+
Window = pygame.display.set_mode((500, 500))
10+
pygame.display.set_caption("SUDOKU GAME")
11+
x = 0
12+
z = 0
13+
diff = 500 / 9
14+
value= 0
15+
16+
# Default Sudoku Board.
17+
defaultgrid =[
18+
[0, 0, 4, 0, 6, 0, 0, 0, 5],
19+
[7, 8, 0, 4, 0, 0, 0, 2, 0],
20+
[0, 0, 2, 6, 0, 1, 0, 7, 8],
21+
[6, 1, 0, 0, 7, 5, 0, 0, 9],
22+
[0, 0, 7, 5, 4, 0, 0, 6, 1],
23+
[0, 0, 1, 7, 5, 0, 9, 3, 0],
24+
[0, 7, 0, 3, 0, 0, 0, 1, 0],
25+
[0, 4, 0, 2, 0, 6, 0, 0, 7],
26+
[0, 2, 0, 0, 0, 7, 4, 0, 0],
27+
]
28+
29+
# Load test fonts for future use
30+
31+
font = pygame.font.SysFont("comicsans", 30)
32+
font1 = pygame.font.SysFont("comicsans", 20)
33+
def cord(pos):
34+
global x
35+
x = pos[0]//diff
36+
global z
37+
z = pos[1]//diff
38+
39+
40+
# Highlight the cell selected
41+
def highlightbox():
42+
for k in range(2):
43+
pygame.draw.line(Window, (0, 0, 0), (x * diff-3, (z + k)*diff), (x * diff + diff + 3, (z + k)*diff), 7)
44+
pygame.draw.line(Window, (0, 0, 0), ( (x + k)* diff, z * diff ), ((x + k) * diff, z * diff + diff), 7)
45+
46+
47+
# Function to draw required lines for making Sudoku grid
48+
def drawlines():
49+
for i in range (9):
50+
for j in range (9):
51+
if defaultgrid[i][j]!= 0:
52+
pygame.draw.rect(Window, (255, 255, 0), (i * diff, j * diff, diff + 1, diff + 1))
53+
# Fill grid with default numbers specified
54+
text1 = font.render(str(defaultgrid[i][j]), 1, (0, 0, 0))
55+
Window.blit(text1, (i * diff + 15, j * diff + 15))
56+
for l in range(10):
57+
if l % 3 == 0 :
58+
thick = 7
59+
else:
60+
thick = 1
61+
pygame.draw.line(Window, (0, 0, 0), (0, l * diff), (500, l * diff), thick)
62+
pygame.draw.line(Window, (0, 0, 0), (l * diff, 0), (l * diff, 500), thick)
63+
64+
# Fill value entered in cell
65+
def fillvalue(value):
66+
text1 = font.render(str(value), 1, (0, 0, 0))
67+
Window.blit(text1, (x * diff + 15, z * diff + 15))
68+
69+
# Raise error when wrong value entered
70+
def raiseerror():
71+
text1 = font.render("wrong!", 1, (0, 0, 0))
72+
Window.blit(text1, (20, 570))
73+
def raiseerror1():
74+
text1 = font.render("wrong ! enter a valid key for the game", 1, (0, 0, 0))
75+
Window.blit(text1, (20, 570))
76+
77+
78+
# Check if the value entered in board is valid
79+
def validvalue(m, k, l, value):
80+
for it in range(9):
81+
if m[k][it]== value:
82+
return False
83+
if m[it][l]== value:
84+
return False
85+
it = k//3
86+
jt = l//3
87+
for k in range(it * 3, it * 3 + 3):
88+
for l in range (jt * 3, jt * 3 + 3):
89+
if m[k][l]== value:
90+
return False
91+
return True
92+
93+
# Solves the sudoku board using Backtracking Algorithm
94+
def solvegame(defaultgrid, i, j):
95+
96+
while defaultgrid[i][j]!= 0:
97+
if i<8:
98+
i+= 1
99+
elif i == 8 and j<8:
100+
i = 0
101+
j+= 1
102+
elif i == 8 and j == 8:
103+
return True
104+
pygame.event.pump()
105+
for it in range(1, 10):
106+
if validvalue(defaultgrid, i, j, it)== True:
107+
defaultgrid[i][j]= it
108+
global x, z
109+
x = i
110+
z = j
111+
Window.fill((255, 255, 255))
112+
drawlines()
113+
highlightbox()
114+
pygame.display.update()
115+
pygame.time.delay(20)
116+
if solvegame(defaultgrid, i, j)== 1:
117+
return True
118+
else:
119+
defaultgrid[i][j]= 0
120+
Window.fill((0,0,0))
121+
122+
drawlines()
123+
highlightbox()
124+
pygame.display.update()
125+
pygame.time.delay(50)
126+
return False
127+
# Display options when solved
128+
def gameresult():
129+
text1 = font.render("game finished", 1, (0, 0, 0))
130+
Window.blit(text1, (20, 570))
131+
flag=True
132+
flag1 = 0
133+
flag2 = 0
134+
rs = 0
135+
error = 0
136+
137+
# The loop thats keep the window running
138+
while flag:
139+
Window.fill((255,182,190))
140+
for event in pygame.event.get():
141+
# Quit the game window
142+
if event.type == pygame.QUIT:
143+
flag = False
144+
# Get the mouse position to insert number
145+
if event.type == pygame.MOUSEBUTTONDOWN:
146+
flag1 = 1
147+
pos = pygame.mouse.get_pos()
148+
cord(pos)
149+
# Get the number to be inserted if key pressed
150+
if event.type == pygame.KEYDOWN:
151+
if event.key == pygame.K_LEFT:
152+
x-= 1
153+
flag1 = 1
154+
if event.key == pygame.K_RIGHT:
155+
x+= 1
156+
flag1 = 1
157+
if event.key == pygame.K_UP:
158+
y-= 1
159+
flag1 = 1
160+
if event.key == pygame.K_DOWN:
161+
y+= 1
162+
flag1 = 1
163+
if event.key == pygame.K_1:
164+
value = 1
165+
if event.key == pygame.K_2:
166+
value = 2
167+
if event.key == pygame.K_3:
168+
value = 3
169+
if event.key == pygame.K_4:
170+
value = 4
171+
if event.key == pygame.K_5:
172+
value = 5
173+
if event.key == pygame.K_6:
174+
value = 6
175+
if event.key == pygame.K_7:
176+
value = 7
177+
if event.key == pygame.K_8:
178+
value = 8
179+
if event.key == pygame.K_9:
180+
value = 9
181+
if event.key == pygame.K_RETURN:
182+
flag2 = 1
183+
184+
# If R pressed clear the sudoku board
185+
if event.key == pygame.K_r:
186+
rs = 0
187+
error = 0
188+
flag2 = 0
189+
defaultgrid=[
190+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
191+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
192+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
193+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
194+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
195+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
196+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
197+
[0, 0, 0, 0, 0, 0, 0, 0, 0],
198+
[0, 0, 0, 0, 0, 0, 0, 0, 0]
199+
]
200+
201+
202+
# If D is pressed reset the board to default
203+
if event.key == pygame.K_d:
204+
rs = 0
205+
error = 0
206+
flag2 = 0
207+
defaultgrid =[
208+
[0, 0, 4, 0, 6, 0, 0, 0, 5],
209+
[7, 8, 0, 4, 0, 0, 0, 2, 0],
210+
[0, 0, 2, 6, 0, 1, 0, 7, 8],
211+
[6, 1, 0, 0, 7, 5, 0, 0, 9],
212+
[0, 0, 7, 5, 4, 0, 0, 6, 1],
213+
[0, 0, 1, 7, 5, 0, 9, 3, 0],
214+
[0, 7, 0, 3, 0, 0, 0, 1, 0],
215+
[0, 4, 0, 2, 0, 6, 0, 0, 7],
216+
[0, 2, 0, 0, 0, 7, 4, 0, 0],
217+
]
218+
if flag2 == 1:
219+
if solvegame(defaultgrid , 0, 0)== False:
220+
error = 1
221+
else:
222+
rs = 1
223+
flag2 = 0
224+
if value != 0:
225+
fillvalue(value)
226+
if validvalue(defaultgrid , int(x), int(z), value)== True:
227+
defaultgrid[int(x)][int(z)]= value
228+
flag1 = 0
229+
else:
230+
defaultgrid[int(x)][int(z)]= 0
231+
raiseerror1()
232+
value = 0
233+
234+
if error == 1:
235+
raiseerror()
236+
if rs == 1:
237+
gameresult()
238+
drawlines()
239+
if flag1 == 1:
240+
highlightbox()
241+
242+
# Update window
243+
pygame.display.update()
244+
245+
246+
# Quit pygame window
247+
pygame.quit()

Sudoku Game/requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
python 3.7>
2+
The required library is:
3+
4+
1) pygame = version 2.1.2
5+

0 commit comments

Comments
 (0)