Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

Commit27a0948

Browse files
authored
Merge pull request#1 from python019/sudoku-solver
sudoku-solver
2 parentsd18c4fc +43e42b5 commit27a0948

12 files changed

+426
-186
lines changed

‎.DS_Store

6 KB
Binary file not shown.

‎README.md

Lines 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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
importpygame
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+
defdrawButton(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+
defchooseLevel():
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+
whiledone:
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+
foreventinpygame.event.get():
50+
ifevent.type==pygame.QUIT:
51+
# deactivates the pygame library
52+
pygame.quit()
53+
# quit the program.
54+
quit()
55+
ifevent.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+
iflevel!=0:
64+
# print(level)
65+
pygame.quit()
66+
returnlevel
67+
68+
# Draws the surface object to the screen.
69+
pygame.display.update()
70+
71+
72+
# chooseLevel()

‎generator.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
importrandom
2+
importcopy
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+
defprintBoard(board):
18+
foriinrange(len(board)):
19+
ifi%3==0andi!=0:
20+
print("- - - - - - - - - - - -")
21+
forjinrange(len(board[0])):
22+
ifj%3==0andj!=0:
23+
print(" | ",end="")
24+
ifj==8:# end of the row
25+
print(board[i][j])
26+
else:
27+
print(str(board[i][j])+" ",end="")
28+
29+
30+
deffindEmpty(board):
31+
foryinrange(len(board)):
32+
forxinrange(len(board[0])):
33+
ifboard[y][x]==0:
34+
returny,x# y = row , x = column
35+
# if we got here it mean that we finish the sudoku, so return none
36+
returnNone
37+
38+
39+
defvalidCheck(board,number,coordinates):
40+
# checking row
41+
forxinrange(len(board[0])):
42+
ifnumber==board[coordinates[0]][x]andcoordinates[1]!=x:# coordinates[0]= row
43+
returnFalse
44+
45+
# checking column
46+
foryinrange(len(board)):
47+
ifnumber==board[y][coordinates[1]]andcoordinates[0]!=y:
48+
returnFalse
49+
50+
# checking the box
51+
box_x=coordinates[1]//3
52+
box_y=coordinates[0]//3
53+
54+
foryinrange(box_y*3,box_y*3+3):
55+
forxinrange(box_x*3,box_x*3+3):
56+
ifnumber==board[y][x]and (y,x)!=coordinates:
57+
returnFalse
58+
59+
returnTrue
60+
61+
62+
defgenerateRandomBoard(board):
63+
# end condition:- getting to the end of the board - the function findEmpty return NONE
64+
find=findEmpty(board)
65+
iffindisNone:# if find != False
66+
returnTrue
67+
else:
68+
row,col=find
69+
fornumberinrange(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+
ifvalidCheck(board,randomNumber, (row,col)):
73+
board[row][col]=randomNumber
74+
ifgenerateRandomBoard(board):
75+
returnTrue
76+
77+
board[row][col]=0
78+
returnFalse
79+
80+
81+
defdeleteCells(firstBoard,number):
82+
whilenumber:
83+
row=random.randint(0,8)
84+
col=random.randint(0,8)
85+
iffirstBoard[row][col]!=0:
86+
firstBoard[row][col]=0
87+
number=number-1
88+
89+
90+
defsudokuGenerate(firstBoard,level):
91+
92+
# printBoard(firstBoard)
93+
generateRandomBoard(firstBoard)
94+
# printBoard(firstBoard)
95+
iflevel==1:
96+
deleteCells(firstBoard,30)
97+
iflevel==2:
98+
deleteCells(firstBoard,40)
99+
iflevel==3:
100+
deleteCells(firstBoard,50)

‎gifs/playing the game.gif

388 KB
Loading

‎gifs/start the game.gif

615 KB
Loading

‎gui.py

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
importpygame
2+
fromsudokuSolverimport*
3+
fromchooseLevelimport*
4+
importtime
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+
defcheatingAllTheWay():
38+
forrowinrange(len(Board)):
39+
forcolumninrange(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+
defaddNumToBoard(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+
deffinish():
59+
ifsolvedBoard==Board:
60+
print("good")
61+
else:
62+
print("not good")
63+
64+
65+
defaddNewRect(row,col,color,width):
66+
rectSize=pygame.Rect((MARGIN+WIDTH)*col+MARGIN, (MARGIN+HEIGHT)*row+MARGIN,WIDTH,
67+
HEIGHT)
68+
ifwidthisnotNone:
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+
defflickering(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+
defdrawTheBorder():
89+
dif=500//9
90+
foriinrange(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+
foriinrange(10):
95+
ifi%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+
defdrawInitBoard():
102+
# printBoard(solvedBoard)
103+
forrowinrange(len(Board)):
104+
forcolumninrange(len(Board[row])):
105+
color=L_GRAY
106+
ifBoard[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+
ifBoard[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+
whileflag1:
130+
level=chooseLevel()
131+
iflevel==1orlevel==2orlevel==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+
whilenotdone:
149+
# --- Main event loop
150+
151+
foreventinpygame.event.get():
152+
ifevent.type==pygame.QUIT:
153+
done=True
154+
ifevent.type==pygame.KEYDOWN:
155+
ifevent.keyinnumbers_1to9:
156+
key=chr(event.key)
157+
ifevent.key==pygame.K_RETURN:
158+
finish()
159+
ifevent.key==pygame.K_c:
160+
cheatingAllTheWay()
161+
ifevent.type==pygame.MOUSEBUTTONDOWN:
162+
# ------ if clicked on a cell get his row and column ------
163+
ifreadyForInputisTrue:
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+
ifBoard[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+
ifreadyForInputandkeyisnotNone:
180+
# ------ checking if the key is good at it's place ------
181+
ifint(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

14.9 KB
Loading

‎images/gaming.PNG

34.9 KB
Loading

‎images/playing the game.PNG

38 KB
Loading

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp