-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
208 lines (148 loc) · 6.65 KB
/
main.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
import pygame
from sudokuGenerator import *
from sudokuSolver import *
#Constants
WINDOW_SIZE = 600
CELL_SIZE = WINDOW_SIZE // 9
GRID_SIZE = CELL_SIZE * 9
BACKGROUND_COLOR = (255, 255, 255)
LINE_COLOR = (0, 0, 0)
HIGHLIGHT_COLOR = (180, 180, 255)
TEXT_COLOR = (0, 0, 0)
LOCKED_TEXT_COLOR = (200, 0, 0)
BUTTON_COLOR = (0, 100, 0)
BUTTON_TEXT_COLOR = (255, 255, 255)
BLUR_COLOR = (200, 200, 200)
#Initialize pygame
pygame.init()
#Display
screen = pygame.display.set_mode((WINDOW_SIZE - 5, WINDOW_SIZE + 50))
pygame.display.set_caption("Sudoku")
#Font
font = pygame.font.Font(None, 36)
button_font = pygame.font.Font(None, 28)
message_font = pygame.font.Font(None, 48)
#Draw grid
def nacrtaj_grid():
for i in range(10):
width = 1 if i % 3 != 0 else 3
pygame.draw.line(screen, LINE_COLOR, (i * CELL_SIZE, 0), (i * CELL_SIZE, GRID_SIZE), width)
pygame.draw.line(screen, LINE_COLOR, (0, i * CELL_SIZE), (GRID_SIZE, i * CELL_SIZE), width)
#Printing numbers in GUI
def ispis_brojeva(board, zakljucane_celije):
for i in range(9):
for j in range(9):
if board[i][j] != 0:
text_color = LOCKED_TEXT_COLOR if zakljucane_celije[i][j] else TEXT_COLOR
text = font.render(str(board[i][j]), True, text_color)
screen.blit(text, (j * CELL_SIZE + 20, i * CELL_SIZE + 15))
#Marked cell
def oznacena_celija(cell):
if cell is not None:
vrsta, kolona = cell
pygame.draw.rect(screen, HIGHLIGHT_COLOR, (kolona * CELL_SIZE, vrsta * CELL_SIZE, CELL_SIZE, CELL_SIZE))
# Draw button
def draw_button(text, x, y, width, height, color, text_color):
pygame.draw.rect(screen, color, (x, y, width, height))
button_text = button_font.render(text, True, text_color)
screen.blit(button_text, (x + 10, y + 10))
#Check user solution
def check_solution(board, zakljucane_celije):
for vrsta in range(0, 9):
for kolona in range(0, 9):
if not zakljucane_celije[vrsta][kolona]:
broj = board[vrsta][kolona]
board[vrsta][kolona] = 0
if not validan_potez(board, vrsta, kolona, broj):
board[vrsta][kolona] = broj
return False
board[vrsta][kolona] = broj
return True
#Main function
def main():
def reset_board():
nonlocal board, zakljucane_celije, selektovana_celija, message, display_blur
board = generisi_sudoku()
zakljucane_celije = [[board[i][j] != 0 for j in range(9)] for i in range(9)]
selektovana_celija = (0,0)
message = ""
display_blur = False #unblur
board = generisi_sudoku()
zakljucane_celije = [[board[i][j] != 0 for j in range(9)] for i in range(9)]
selektovana_celija = (0,0)
igra = True
message = ""
display_blur = False
while igra:
for event in pygame.event.get():
if event.type == pygame.QUIT:
igra = False
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if y > GRID_SIZE:
if 20 <= x <= 140 and GRID_SIZE + 10 <= y <= GRID_SIZE + 50:
board = [[0] * 9 for _ in range(9)]
provera_resenja_sudoku(board)
zakljucane_celije = [[True] * 9 for _ in range(9)]
elif 160 <= x <= 280 and GRID_SIZE + 10 <= y <= GRID_SIZE + 50:
reset_board()
elif 300 <= x <= 420 and GRID_SIZE + 10 <= y <= GRID_SIZE + 50:
if check_solution(board, zakljucane_celije):
message = "Correct!"
else:
message = "Incorrect :("
display_blur = True
else:
selektovana_celija = (y // CELL_SIZE, x // CELL_SIZE)
elif event.type == pygame.KEYDOWN:
if selektovana_celija is not None:
vrsta, kolona = selektovana_celija
if event.key == pygame.K_UP:
selektovana_celija = ((vrsta - 1) % 9, kolona)
elif event.key == pygame.K_DOWN:
selektovana_celija = ((vrsta + 1) % 9, kolona)
elif event.key == pygame.K_LEFT:
selektovana_celija = (vrsta, (kolona - 1) % 9)
elif event.key == pygame.K_RIGHT:
selektovana_celija = (vrsta, (kolona + 1) % 9)
elif not zakljucane_celije[vrsta][kolona]:
if event.key == pygame.K_1:
board[vrsta][kolona] = 1
elif event.key == pygame.K_2:
board[vrsta][kolona] = 2
elif event.key == pygame.K_3:
board[vrsta][kolona] = 3
elif event.key == pygame.K_4:
board[vrsta][kolona] = 4
elif event.key == pygame.K_5:
board[vrsta][kolona] = 5
elif event.key == pygame.K_6:
board[vrsta][kolona] = 6
elif event.key == pygame.K_7:
board[vrsta][kolona] = 7
elif event.key == pygame.K_8:
board[vrsta][kolona] = 8
elif event.key == pygame.K_9:
board[vrsta][kolona] = 9
elif event.key == pygame.K_0 or event.key == pygame.K_BACKSPACE:
board[vrsta][kolona] = 0
screen.fill(BACKGROUND_COLOR)
oznacena_celija(selektovana_celija)
ispis_brojeva(board, zakljucane_celije)
nacrtaj_grid()
draw_button("Solution", 20, GRID_SIZE + 10, 120, 40, BUTTON_COLOR, BUTTON_TEXT_COLOR)
draw_button("Restart", 160, GRID_SIZE + 10, 120, 40, BUTTON_COLOR, BUTTON_TEXT_COLOR)
draw_button("WOOHOO", 300, GRID_SIZE + 10, 120, 40, BUTTON_COLOR, BUTTON_TEXT_COLOR)
if display_blur:
blur_surface = pygame.Surface((GRID_SIZE, GRID_SIZE))
blur_surface.fill(BLUR_COLOR)
blur_surface.set_alpha(228)
screen.blit(blur_surface, (0, 0))
if message:
message_text = message_font.render(message, True, TEXT_COLOR)
text_rect = message_text.get_rect(center=(WINDOW_SIZE // 2, WINDOW_SIZE // 2))
screen.blit(message_text, text_rect)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()