-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
196 lines (167 loc) · 6.82 KB
/
game.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
import pygame
import sys
# Import functions
from functions import find_zero, check_value, generate_sudoku_from_scratch, remove_values, has_unique_solution
WIDTH, HEIGHT = 540, 600
GRID_SIZE = 9
CELL_SIZE = WIDTH // GRID_SIZE
FONT_SIZE = 36
# Initialize Pygame
pygame.init()
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sudoku")
FONT = pygame.font.SysFont("arial", FONT_SIZE)
SMALL_FONT = pygame.font.SysFont("arial", 20)
class SudokuGame:
def __init__(self):
complete = generate_sudoku_from_scratch()
self.solution = [row[:] for row in complete] # Store the correct solution
self.board = remove_values(complete, 50)
self.selected = None
self.user_grid = [row[:] for row in self.board]
self.solved = False
def draw(self):
WIN.fill((255, 255, 255))
# Draw grid lines
for i in range(10):
thickness = 3 if i % 3 == 0 else 1
pygame.draw.line(WIN, (0, 0, 0), (0, i * CELL_SIZE), (WIDTH, i * CELL_SIZE), thickness)
pygame.draw.line(WIN, (0, 0, 0), (i * CELL_SIZE, 0), (i * CELL_SIZE, WIDTH), thickness)
# Draw numbers on the grid
for i in range(9):
for j in range(9):
val = self.user_grid[i][j]
if val != 0:
# If the number was part of the initial board, draw in black
if self.board[i][j] != 0:
color = (0, 0, 0)
else:
# Color the number blue if valid, red if not
valid = check_value(self.user_grid, val, (i, j))
color = (0, 0, 255) if valid else (255, 0, 0)
text = FONT.render(str(val), True, color)
WIN.blit(text, (j * CELL_SIZE + 20, i * CELL_SIZE + 10))
# Highlight selected cell
if self.selected:
i, j = self.selected
pygame.draw.rect(WIN, (255, 0, 0), (j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE), 3)
# Draw buttons
pygame.draw.rect(WIN, (200, 200, 200), (20, 550, 100, 30))
WIN.blit(SMALL_FONT.render("Réinitialiser", True, (0, 0, 0)), (25, 555))
pygame.draw.rect(WIN, (200, 200, 200), (140, 550, 100, 30))
WIN.blit(SMALL_FONT.render("Résoudre", True, (0, 0, 0)), (165, 555))
pygame.draw.rect(WIN, (200, 200, 200), (260, 550, 100, 30))
WIN.blit(SMALL_FONT.render("Vérifier", True, (0, 0, 0)), (275, 555))
pygame.display.flip()
def select(self, pos):
x, y = pos
if x < WIDTH and y < WIDTH:
self.selected = y // CELL_SIZE, x // CELL_SIZE
def place_number(self, key):
if self.selected and not self.solved:
i, j = self.selected
# Allow placing only in cells that were originally empty in the puzzle
if self.board[i][j] == 0:
self.user_grid[i][j] = key
def remove_number(self):
"""
Remove the number from the currently selected cell if it's not an initial clue.
"""
if self.selected and not self.solved:
i, j = self.selected
if self.board[i][j] == 0:
self.user_grid[i][j] = 0
def reset(self):
self.__init__()
def solve(self):
# Backtracking visualization button; do not check solution here.
self.solved = True
self._solve_with_animation()
def _solve_with_animation(self):
def backtrack():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pos = find_zero(self.user_grid)
if not pos:
return True
x, y = pos
for e in range(1, 10):
if check_value(self.user_grid, e, (x, y)):
self.user_grid[x][y] = e
self.draw()
pygame.display.update()
pygame.time.delay(2)
if backtrack():
return True
self.user_grid[x][y] = 0
self.draw()
pygame.display.update()
pygame.time.delay(2)
return False
backtrack()
def verify(self):
"""
Check if the user's grid is completely filled and matches the solution.
If so, show a pop-up message indicating success.
If the grid is complete but incorrect, a pop-up indicates an error.
"""
# Check for any empty cell (0)
for i in range(9):
for j in range(9):
if self.user_grid[i][j] == 0:
self.show_popup("Incomplete board!")
return
if self.user_grid == self.solution:
self.solved = True
self.show_popup("Sudoku Solved!")
else:
self.show_popup("Incorrect solution!")
def show_popup(self, message):
"""
Display a pop-up message overlaying the current game board.
The pop-up stays visible for 3 seconds.
"""
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 180)) # Semi-transparent overlay
WIN.blit(overlay, (0, 0))
popup_font = pygame.font.SysFont("arial", 48)
text = popup_font.render(message, True, (255, 255, 255))
rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
WIN.blit(text, rect)
pygame.display.update()
pygame.time.delay(3000)
# Main loop
def main():
game = SudokuGame()
clock = pygame.time.Clock()
while True:
clock.tick(30)
game.draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# Reset button
if 20 <= event.pos[0] <= 120 and 550 <= event.pos[1] <= 580:
game.reset()
# Solve (backtracking visualization) button
elif 140 <= event.pos[0] <= 240 and 550 <= event.pos[1] <= 580:
game.solve()
# Verify button
elif 260 <= event.pos[0] <= 360 and 550 <= event.pos[1] <= 580:
game.verify()
else:
game.select(event.pos)
elif event.type == pygame.KEYDOWN:
# Allow removal of a placed number using Backspace or Delete key.
if event.key == pygame.K_BACKSPACE or event.key == pygame.K_DELETE:
game.remove_number()
elif event.unicode.isdigit():
val = int(event.unicode)
if 1 <= val <= 9:
game.place_number(val)
if __name__ == "__main__":
main()