forked from sickunicorn24/Sudoku-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.py
372 lines (327 loc) · 14.5 KB
/
sudoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import pygame
import sys
from board import Board
from constants import *
from sudoku_generator import SudokuGenerator
def game_start(screen):
#Initialize Font
start_title_font = pygame.font.Font(None, 85)
subtitle_font = pygame.font.Font(None, 65)
button_font = pygame.font.Font(None, 63)
title_board = SudokuGenerator(9,35)
title_board.fill_values()
title_board.remove_cells()
disp = title_board.get_board()
display_board = Board(70, 70, screen, 0)
for i in range(9):
for j in range(9):
display_board.cells[i][j].value = disp[i][j]
if disp[i][j] != 0:
display_board.cells[i][j].editable = False
#Main title text
screen.fill(PINK)
display_board.draw()
main_title_surface = start_title_font.render("Welcome to Sudoku", True, PURPLE)
main_title_rectangle = main_title_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 100))
screen.blit(main_title_surface, main_title_rectangle)
#Subtitle text
subtitle_surface = subtitle_font.render("Select Game Mode", True, PURPLE)
subtitle_rectangle = subtitle_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 200))
screen.blit(subtitle_surface, subtitle_rectangle)
#Outline setup
main_title_outline = start_title_font.render("Welcome to Sudoku", 0, WHITE)
for i in range(-4, 5):
for j in range(-4, 5):
screen.blit(main_title_outline, main_title_surface.get_rect(center=(WIDTH // 2 + i, HEIGHT // 2 - 100 + j)))
screen.blit(main_title_surface, main_title_rectangle)
subtitle_outline = subtitle_font.render("Select Game Mode", 0, WHITE)
for i in range(-4, 5):
for j in range(-4, 5):
screen.blit(subtitle_outline, subtitle_surface.get_rect(center=(WIDTH // 2 + i, HEIGHT // 2 + 200 + j)))
screen.blit(subtitle_surface, subtitle_rectangle)
#Main menu buttons
button_data = [("Easy", WIDTH // 2+200), ("Medium", WIDTH // 2), ("Hard", WIDTH // 2 - 200)]
buttons = []
for text, x_pos in button_data:
button_text = button_font.render(text, True, WHITE)
button_surface = pygame.Surface((button_text.get_width() + 14, button_text.get_height() + 14))
button_surface.fill(PURPLE)
button_surface.blit(button_text, (10, 10))
button_rectangle = button_surface.get_rect(center=(x_pos, HEIGHT // 2 + 300))
buttons.append((button_surface, button_rectangle, text.lower()))
button_ot_surface = pygame.Surface((button_text.get_width() + 21, button_text.get_height() + 21))
button_ot_surface.fill(BLACK)
screen.blit(button_ot_surface, button_rectangle)
screen.blit(button_surface, button_rectangle)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for _, button_rect, difficulty in buttons:
if button_rect.collidepoint(event.pos):
return difficulty
pygame.display.update()
def during_game(screen, display_board, difficulty):
#Font
bottom_button_font = pygame.font.Font(None, 50)
small_msg_font = pygame.font.Font(None, 40)
#Difficulty text
difficulty = difficulty.upper()
small_msg_surface = small_msg_font.render(difficulty, 0, BLACK)
small_msg_rectangle = small_msg_surface.get_rect(center=(WIDTH // 2 - 370, HEIGHT // 2 + 420))
#Init text
reset_text = bottom_button_font.render("RESET", 0, BLACK)
restart_text = bottom_button_font.render("RESTART", 0, BLACK)
exit_text = bottom_button_font.render("EXIT", 0, BLACK)
#Init reset button
reset_surface = pygame.Surface((reset_text.get_size()[0] + 20, reset_text.get_size()[1] + 20))
reset_surface.fill(PINK)
reset_surface.blit(reset_text, (10, 10))
#Init restart button
restart_surface = pygame.Surface((restart_text.get_size()[0] + 20, restart_text.get_size()[1] + 20))
restart_surface.fill(PINK)
restart_surface.blit(restart_text, (10, 10))
#Init exit button
exit_surface = pygame.Surface((exit_text.get_size()[0] + 20, exit_text.get_size()[1] + 20))
exit_surface.fill(PINK)
exit_surface.blit(exit_text, (10, 10))
#Set button locations
reset_rectangle = reset_surface.get_rect(center=(WIDTH // 2 + 200, HEIGHT - 50))
restart_rectangle = restart_surface.get_rect(center=(WIDTH // 2, HEIGHT - 50))
exit_rectangle = exit_surface.get_rect(center=(WIDTH // 2 - 200, HEIGHT - 50))
#Outline setup
reset_ot_surface = pygame.Surface((reset_text.get_size()[0] + 30, reset_text.get_size()[1] + 30))
reset_ot_surface.fill(BLACK)
restart_ot_surface = pygame.Surface((restart_text.get_size()[0] + 30, restart_text.get_size()[1] + 30))
restart_ot_surface.fill(BLACK)
exit_ot_surface = pygame.Surface((exit_text.get_size()[0] + 30, exit_text.get_size()[1] + 30))
exit_ot_surface.fill(BLACK)
clicked_row, clicked_col = -1, -1
while True:
screen.fill(PURPLE)
display_board.draw()
if clicked_row != -1 and clicked_col != -1:
display_board.select(clicked_row, clicked_col)
screen.blit(reset_ot_surface, reset_rectangle)
screen.blit(restart_ot_surface, restart_rectangle)
screen.blit(exit_ot_surface, exit_rectangle)
screen.blit(reset_surface, reset_rectangle)
screen.blit(restart_surface, restart_rectangle)
screen.blit(exit_surface, exit_rectangle)
screen.blit(small_msg_surface, small_msg_rectangle)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if reset_rectangle.collidepoint(event.pos):
display_board.reset_to_original()
display_board.draw()
elif restart_rectangle.collidepoint(event.pos):
main()
elif exit_rectangle.collidepoint(event.pos):
sys.exit()
if 0 <= event.pos[0] < WIDTH and 0 <= event.pos[1] < WIDTH:
row, col = display_board.click(event.pos[0], event.pos[1])
if display_board.cells[row][col].editable:
clicked_row, clicked_col = row, col
else:
clicked_row, clicked_col = -1, -1
if event.type == pygame.KEYDOWN:
if clicked_row != -1 and clicked_col != -1:
if event.key in (pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5,
pygame.K_6, pygame.K_7, pygame.K_8, pygame.K_9):
value = int(event.unicode)
if display_board.cells[clicked_row][clicked_col].editable:
display_board.cells[clicked_row][clicked_col].value = value
elif event.key == pygame.K_UP:
clicked_row = (clicked_row - 1) % 9
elif event.key == pygame.K_DOWN:
clicked_row = (clicked_row + 1) % 9
elif event.key == pygame.K_LEFT:
clicked_col = (clicked_col - 1) % 9
elif event.key == pygame.K_RIGHT:
clicked_col = (clicked_col + 1) % 9
while not display_board.cells[clicked_row][clicked_col].editable:
if event.key in (pygame.K_UP, pygame.K_DOWN):
clicked_row = (clicked_row - 1) % 9 if event.key == pygame.K_UP else (clicked_row + 1) % 9
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
clicked_col = (clicked_col - 1) % 9 if event.key == pygame.K_LEFT else (clicked_col + 1) % 9
if display_board.is_full():
if display_board.check_board():
draw_game_win(screen)
else:
draw_game_lose(screen)
pygame.display.update()
def draw_game_win(screen):
win_font = pygame.font.Font(None, 200)
try_font = pygame.font.Font(None, 70)
button_font = pygame.font.Font(None, 80)
screen.fill(BLACK)
color = WHITE
b_color = WHITE
b_color2 = b_color
x_pos1 = 200
x_pos2 = 70
b_size = 50
for i in range(5):
if i == 0:
color = GREEN
b_color = WHITE
b_color2 = b_color
x_pos1 += 5
x_pos2 += 5
elif i == 1:
color = PURPLE
b_color = WHITE
b_color2 = b_color
x_pos1 += 5
x_pos2 += 5
b_size -= 10
elif i == 2:
color = PINK
b_color = BLACK
b_color2 = b_color
x_pos1 += 5
x_pos2 += 5
elif i == 3:
color = RED
b_color = BLACK
b_color2 = b_color
x_pos1 += 5
x_pos2 += 5
b_size -= 10
elif i == 4:
b_color = RED
b_color2 = GREEN
color = WHITE
x_pos1 += 5
x_pos2 += 5
b_size -= 10
win_surface = win_font.render("YOU", True, color)
win_rectangle = win_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 - x_pos1))
screen.blit(win_surface, win_rectangle)
yw_surface = win_font.render("WIN!", 0, color)
yw_rectangle = yw_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 - x_pos2))
screen.blit(yw_surface, yw_rectangle)
try_surface = try_font.render("PLAY AGAIN?", True, GREEN)
try_rectangle = try_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 100))
screen.blit(try_surface, try_rectangle)
restart_text = button_font.render("YES", True, BLACK)
restart_surface = pygame.Surface((restart_text.get_width() + b_size, restart_text.get_height() + b_size))
restart_surface.fill(b_color2)
restart_surface.blit(restart_text, (10, 10))
restart_rectangle = restart_surface.get_rect(center=(WIDTH // 2 - 100, HEIGHT // 2 + 200))
exit_text = button_font.render("NO", True, BLACK)
exit_surface = pygame.Surface((exit_text.get_width() + b_size, exit_text.get_height() + b_size))
exit_surface.fill(b_color)
exit_surface.blit(exit_text, (10, 10))
exit_rectangle = exit_surface.get_rect(center=(WIDTH // 2 + 100, HEIGHT // 2 + 200))
screen.blit(restart_surface, restart_rectangle)
screen.blit(exit_surface, exit_rectangle)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if restart_rectangle.collidepoint(event.pos):
main()
elif exit_rectangle.collidepoint(event.pos):
sys.exit()
pygame.display.update()
def draw_game_lose(screen):
lose_font = pygame.font.Font(None, 200)
try_font = pygame.font.Font(None, 70)
button_font = pygame.font.Font(None, 80)
screen.fill(BLACK)
color = WHITE
b_color = WHITE
b_color2 = b_color
x_pos1 = 200
x_pos2 = 70
b_size = 50
for i in range(5):
if i == 0:
color = GREEN
b_color = WHITE
b_color2 = b_color
x_pos1 += 5
x_pos2 += 5
elif i == 1:
color = PURPLE
b_color = WHITE
b_color2 = b_color
x_pos1 += 5
x_pos2 += 5
b_size -= 10
elif i == 2:
color = PINK
b_color = BLACK
b_color2 = b_color
x_pos1 += 5
x_pos2 += 5
elif i == 3:
color = RED
b_color = BLACK
b_color2 = b_color
x_pos1 += 5
x_pos2 += 5
b_size -= 10
elif i == 4:
b_color = RED
b_color2 = GREEN
color = WHITE
x_pos1 += 5
x_pos2 += 5
b_size -= 10
lose_surface = lose_font.render("YOU", True, color)
lose_rectangle = lose_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 - x_pos1))
screen.blit(lose_surface, lose_rectangle)
yl_surface = lose_font.render("LOSE!", 0, color)
yl_rectangle = yl_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 - x_pos2))
screen.blit(yl_surface, yl_rectangle)
try_surface = try_font.render("TRY AGAIN?", True, RED)
try_rectangle = try_surface.get_rect(center = (WIDTH // 2, HEIGHT // 2 + 100))
screen.blit(try_surface, try_rectangle)
restart_text = button_font.render("YES", True, BLACK)
restart_surface = pygame.Surface((restart_text.get_width() + b_size, restart_text.get_height() + b_size))
restart_surface.fill(b_color2)
restart_surface.blit(restart_text, (10, 10))
restart_rectangle = restart_surface.get_rect(center=(WIDTH // 2 - 100, HEIGHT // 2 + 200))
exit_text = button_font.render("NO", True, BLACK)
exit_surface = pygame.Surface((exit_text.get_width() + b_size, exit_text.get_height() + b_size))
exit_surface.fill(b_color)
exit_surface.blit(exit_text, (10, 10))
exit_rectangle = exit_surface.get_rect(center=(WIDTH // 2 + 100, HEIGHT // 2 + 200))
screen.blit(restart_surface, restart_rectangle)
screen.blit(exit_surface, exit_rectangle)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if restart_rectangle.collidepoint(event.pos):
main()
elif exit_rectangle.collidepoint(event.pos):
sys.exit()
pygame.display.update()
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sudoku")
difficulty = game_start(screen)
removed_cells = {"easy": 30, "medium": 40, "hard": 50}
sudoku = SudokuGenerator(9, removed_cells[difficulty])
sudoku.fill_values()
sudoku.remove_cells()
board_state = sudoku.get_board()
display_board = Board(WIDTH, WIDTH, screen, removed_cells[difficulty])
for i in range(9):
for j in range(9):
display_board.cells[i][j].value = board_state[i][j]
if board_state[i][j] != 0:
display_board.cells[i][j].editable = False
display_board.draw()
during_game(screen, display_board, difficulty)
if __name__ == "__main__":
main()