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

Commit676c674

Browse files
committed
add sudoku game
1 parent0e625fa commit676c674

File tree

9 files changed

+498
-0
lines changed

9 files changed

+498
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
321321
-[How to Make a Flappy Bird Game in Python](https://thepythoncode.com/article/make-a-flappy-bird-game-python). ([code](gui-programming/flappy-bird-game))
322322
-[How to Create a Pong Game in Python](https://thepythoncode.com/article/build-a-pong-game-in-python). ([code](gui-programming/pong-game))
323323
-[How to Create a Space Invaders Game in Python](https://thepythoncode.com/article/make-a-space-invader-game-in-python). ([code](gui-programming/space-invaders-game))
324+
-[How to Build a Sudoku Game with Python](https://thepythoncode.com/article/make-a-sudoku-game-in-python). ([code](gui-programming/sudoku-game))
324325

325326

326327
For any feedback, please consider pulling requests.

‎gui-programming/sudoku-game/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Build a Sudoku Game with Python](https://thepythoncode.com/article/make-a-sudoku-game-in-python)

‎gui-programming/sudoku-game/cell.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
importpygame
2+
fromsettingsimportconvert_list
3+
4+
pygame.font.init()
5+
6+
classCell:
7+
def__init__(self,row,col,cell_size,value,is_correct_guess=None):
8+
self.row=row
9+
self.col=col
10+
self.cell_size=cell_size
11+
self.width=self.cell_size[0]
12+
self.height=self.cell_size[1]
13+
self.abs_x=row*self.width
14+
self.abs_y=col*self.height
15+
16+
self.value=value
17+
self.is_correct_guess=is_correct_guess
18+
self.guesses=Noneifself.value!=0else [0forxinrange(9)]
19+
20+
self.color=pygame.Color("white")
21+
self.font=pygame.font.SysFont('monospace',self.cell_size[0])
22+
self.g_font=pygame.font.SysFont('monospace', (cell_size[0]//3))
23+
24+
self.rect=pygame.Rect(self.abs_x,self.abs_y,self.width,self.height)
25+
26+
27+
defupdate(self,screen,SRN=None):
28+
pygame.draw.rect(screen,self.color,self.rect)
29+
30+
ifself.value!=0:
31+
font_color=pygame.Color("black")ifself.is_correct_guesselsepygame.Color("red")
32+
num_val=self.font.render(str(self.value),True,font_color)
33+
screen.blit(num_val, (self.abs_x,self.abs_y))
34+
35+
elifself.value==0andself.guesses!=None:
36+
cv_list=convert_list(self.guesses, [SRN,SRN,SRN])
37+
foryinrange(SRN):
38+
forxinrange(SRN):
39+
num_txt=" "
40+
ifcv_list[y][x]!=0:
41+
num_txt=cv_list[y][x]
42+
num_txt=self.g_font.render(str(num_txt),True,pygame.Color("orange"))
43+
abs_x= (self.abs_x+ ((self.width//SRN)*x))
44+
abs_y= (self.abs_y+ ((self.height//SRN)*y))
45+
abs_pos= (abs_x,abs_y)
46+
screen.blit(num_txt,abs_pos)

‎gui-programming/sudoku-game/clock.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
importpygame,time
2+
fromsettingsimportCELL_SIZE
3+
4+
pygame.font.init()
5+
6+
classClock:
7+
def__init__(self):
8+
self.start_time=None
9+
self.elapsed_time=0
10+
self.font=pygame.font.SysFont("monospace",CELL_SIZE[0])
11+
self.message_color=pygame.Color("black")
12+
13+
# Start the timer
14+
defstart_timer(self):
15+
self.start_time=time.time()
16+
17+
# Update the timer
18+
defupdate_timer(self):
19+
ifself.start_timeisnotNone:
20+
self.elapsed_time=time.time()-self.start_time
21+
22+
# Display the timer
23+
defdisplay_timer(self):
24+
secs=int(self.elapsed_time%60)
25+
mins=int(self.elapsed_time/60)
26+
my_time=self.font.render(f"{mins:02}:{secs:02}",True,self.message_color)
27+
returnmy_time
28+
29+
# Stop the timer
30+
defstop_timer(self):
31+
self.start_time=None

‎gui-programming/sudoku-game/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
importpygame,sys
2+
fromsettingsimportWIDTH,HEIGHT,CELL_SIZE
3+
fromtableimportTable
4+
5+
pygame.init()
6+
7+
screen=pygame.display.set_mode((WIDTH,HEIGHT+ (CELL_SIZE[1]*3)))
8+
pygame.display.set_caption("Sudoku")
9+
10+
pygame.font.init()
11+
12+
classMain:
13+
def__init__(self,screen):
14+
self.screen=screen
15+
self.FPS=pygame.time.Clock()
16+
self.lives_font=pygame.font.SysFont("monospace",CELL_SIZE[0]//2)
17+
self.message_font=pygame.font.SysFont('Bauhaus 93', (CELL_SIZE[0]))
18+
self.color=pygame.Color("darkgreen")
19+
20+
defmain(self):
21+
table=Table(self.screen)
22+
23+
whileTrue:
24+
self.screen.fill("gray")
25+
foreventinpygame.event.get():
26+
ifevent.type==pygame.QUIT:
27+
pygame.quit()
28+
sys.exit()
29+
ifevent.type==pygame.MOUSEBUTTONDOWN:
30+
ifnottable.game_over:
31+
table.handle_mouse_click(event.pos)
32+
33+
# lower screen display
34+
ifnottable.game_over:
35+
my_lives=self.lives_font.render(f"Lives Left:{table.lives}",True,pygame.Color("black"))
36+
self.screen.blit(my_lives, ((WIDTH//table.SRN)- (CELL_SIZE[0]//2),HEIGHT+ (CELL_SIZE[1]*2.2)))
37+
38+
else:
39+
iftable.lives<=0:
40+
message=self.message_font.render("GAME OVER!!",True,pygame.Color("red"))
41+
self.screen.blit(message, (CELL_SIZE[0]+ (CELL_SIZE[0]//2),HEIGHT+ (CELL_SIZE[1]*2)))
42+
eliftable.lives>0:
43+
message=self.message_font.render("You Made It!!!",True,self.color)
44+
self.screen.blit(message, (CELL_SIZE[0] ,HEIGHT+ (CELL_SIZE[1]*2)))
45+
46+
table.update()
47+
pygame.display.flip()
48+
self.FPS.tick(30)
49+
50+
51+
if__name__=="__main__":
52+
play=Main(screen)
53+
play.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fromitertoolsimportislice
2+
3+
4+
WIDTH,HEIGHT=450,450
5+
6+
N_CELLS=9
7+
8+
CELL_SIZE= (WIDTH//N_CELLS,HEIGHT//N_CELLS)
9+
10+
# Convert 1D list to 2D list
11+
defconvert_list(lst,var_lst):
12+
it=iter(lst)
13+
return [list(islice(it,i))foriinvar_lst]

‎gui-programming/sudoku-game/sudoku.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
importrandom
2+
importmath
3+
importcopy
4+
5+
classSudoku:
6+
def__init__(self,N,E):
7+
self.N=N
8+
self.E=E
9+
10+
# compute square root of N
11+
self.SRN=int(math.sqrt(N))
12+
self.table= [[0forxinrange(N)]foryinrange(N)]
13+
self.answerable_table=None
14+
15+
self._generate_table()
16+
17+
def_generate_table(self):
18+
# fill the subgroups diagonally table/matrices
19+
self.fill_diagonal()
20+
21+
# fill remaining empty subgroups
22+
self.fill_remaining(0,self.SRN)
23+
24+
# Remove random Key digits to make game
25+
self.remove_digits()
26+
27+
deffill_diagonal(self):
28+
forxinrange(0,self.N,self.SRN):
29+
self.fill_cell(x,x)
30+
31+
defnot_in_subgroup(self,rowstart,colstart,num):
32+
forxinrange(self.SRN):
33+
foryinrange(self.SRN):
34+
ifself.table[rowstart+x][colstart+y]==num:
35+
returnFalse
36+
returnTrue
37+
38+
deffill_cell(self,row,col):
39+
num=0
40+
forxinrange(self.SRN):
41+
foryinrange(self.SRN):
42+
whileTrue:
43+
num=self.random_generator(self.N)
44+
ifself.not_in_subgroup(row,col,num):
45+
break
46+
self.table[row+x][col+y]=num
47+
48+
defrandom_generator(self,num):
49+
returnmath.floor(random.random()*num+1)
50+
51+
defsafe_position(self,row,col,num):
52+
return (self.not_in_row(row,num)andself.not_in_col(col,num)andself.not_in_subgroup(row-row%self.SRN,col-col%self.SRN,num))
53+
54+
defnot_in_row(self,row,num):
55+
forcolinrange(self.N):
56+
ifself.table[row][col]==num:
57+
returnFalse
58+
returnTrue
59+
60+
defnot_in_col(self,col,num):
61+
forrowinrange(self.N):
62+
ifself.table[row][col]==num:
63+
returnFalse
64+
returnTrue
65+
66+
67+
deffill_remaining(self,row,col):
68+
# check if we have reached the end of the matrix
69+
ifrow==self.N-1andcol==self.N:
70+
returnTrue
71+
72+
# move to the next row if we have reached the end of the current row
73+
ifcol==self.N:
74+
row+=1
75+
col=0
76+
77+
# skip cells that are already filled
78+
ifself.table[row][col]!=0:
79+
returnself.fill_remaining(row,col+1)
80+
81+
# try filling the current cell with a valid value
82+
fornuminrange(1,self.N+1):
83+
ifself.safe_position(row,col,num):
84+
self.table[row][col]=num
85+
ifself.fill_remaining(row,col+1):
86+
returnTrue
87+
self.table[row][col]=0
88+
89+
# no valid value was found, so backtrack
90+
returnFalse
91+
92+
defremove_digits(self):
93+
count=self.E
94+
95+
# replicates the table so we can have a filled and pre-filled copy
96+
self.answerable_table=copy.deepcopy(self.table)
97+
98+
# removing random numbers to create the puzzle sheet
99+
while (count!=0):
100+
row=self.random_generator(self.N)-1
101+
col=self.random_generator(self.N)-1
102+
if (self.answerable_table[row][col]!=0):
103+
count-=1
104+
self.answerable_table[row][col]=0
105+
106+
107+
defpuzzle_table(self):
108+
returnself.answerable_table
109+
110+
defpuzzle_answers(self):
111+
returnself.table
112+
113+
114+
defprintSudoku(self):
115+
forrowinrange(self.N):
116+
forcolinrange(self.N):
117+
print(self.table[row][col],end=" ")
118+
print()
119+
120+
print("")
121+
122+
forrowinrange(self.N):
123+
forcolinrange(self.N):
124+
print(self.answerable_table[row][col],end=" ")
125+
print()
126+
127+
128+
if__name__=="__main__":
129+
N=9
130+
E= (N*N)//2
131+
sudoku=Sudoku(N,E)
132+
sudoku.printSudoku()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp