-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratePuzzle.py
286 lines (224 loc) · 9.78 KB
/
generatePuzzle.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
import random
from svgwrite import Drawing
from svgwrite.container import Group
import os
class SudokuGenerator:
def __init__(self, n_hints=20):
self.n_hints = n_hints
self.size = 9
self.grid = [[0 for _ in range(self.size)] for _ in range(self.size)]
def is_valid(self, num, row, col):
"""Check if placing a number is valid."""
for i in range(self.size):
if self.grid[row][i] == num or self.grid[i][col] == num:
return False
box_start_row = row - row % 3
box_start_col = col - col % 3
for i in range(3):
for j in range(3):
if self.grid[box_start_row + i][box_start_col + j] == num:
return False
return True
def fill_grid(self):
"""Fill the entire grid recursively."""
for row in range(self.size):
for col in range(self.size):
if self.grid[row][col] == 0:
random_numbers = list(range(1, self.size + 1))
random.shuffle(random_numbers)
for num in random_numbers:
if self.is_valid(num, row, col):
self.grid[row][col] = num
if self.fill_grid():
return True
self.grid[row][col] = 0
return False
return True
def remove_numbers(self):
"""Remove numbers to create a puzzle with n hints."""
cells_to_remove = self.size ** 2 - self.n_hints
while cells_to_remove > 0:
row = random.randint(0, self.size - 1)
col = random.randint(0, self.size - 1)
if self.grid[row][col] != 0:
self.grid[row][col] = 0
cells_to_remove -= 1
def generate_puzzle(self):
"""Generate a Sudoku puzzle."""
self.fill_grid()
self.remove_numbers()
return self.grid
class SolverSudoku:
def solve(self, grid, row, col, num):
for x in range(9):
if grid[row][x] == num:
return False
for x in range(9):
if grid[x][col] == num:
return False
startRow = row - row % 3
startCol = col - col % 3
for i in range(3):
for j in range(3):
if grid[i + startRow][j + startCol] == num:
return False
return True
def Suduko(self, grid, row, col):
if (row == 9 - 1 and col == 9):
return True
if col == 9:
row += 1
col = 0
if grid[row][col] > 0:
return self.Suduko(grid, row, col + 1)
for num in range(1, 9 + 1):
if self.solve(grid, row, col, num):
grid[row][col] = num
if self.Suduko(grid, row, col + 1):
return True
grid[row][col] = 0
return False
class EnhancedSudokuGenerator(SudokuGenerator):
def __init__(self, difficulty, puzzle_number, n_hints, n_placeholders=0, global_number=1):
super().__init__(n_hints)
self.difficulty = difficulty
self.puzzle_number = puzzle_number
self.n_placeholders = n_placeholders
self.puzzle_folder = "puzzles"
self.global_number = global_number
self.coordinates_file = f"{self.puzzle_folder}/{self.global_number}. {
self.difficulty}{self.puzzle_number}_coordinates.txt"
def update_coordinates_file(self, solution_grid):
"""Create coordinates file for the current puzzle."""
os.makedirs(self.puzzle_folder, exist_ok=True)
coordinates_dict = {}
for num in range(1, 10):
coordinates = []
for i in range(9):
for j in range(9):
if solution_grid[i][j] == num:
coordinates.append(f"R{i+1}C{j+1}")
coordinates_dict[num] = coordinates
with open(self.coordinates_file, 'w') as f:
for num, coords in coordinates_dict.items():
random.shuffle(coords)
f.write(f"{num}: {', '.join(coords)}\n")
def generate_linked_puzzle(self):
"""Generate a puzzle with placeholders if needed."""
self.fill_grid()
solution_grid = [row[:] for row in self.grid]
solution_filename = f"{
self.puzzle_folder}/{self.global_number}. {self.difficulty}{self.puzzle_number}S"
createPuzzleSvg(solution_filename, solution_grid)
self.update_coordinates_file(solution_grid)
self.remove_numbers()
puzzle_grid = [row[:] for row in self.grid]
if self.n_placeholders > 0 and self.puzzle_number > 1:
prev_coord_file = f"{self.puzzle_folder}/{self.global_number -
1}. {self.difficulty}{self.puzzle_number-1}_coordinates.txt"
if os.path.exists(prev_coord_file):
placeholders = {}
available_numbers = []
used_values = set()
for i in range(9):
for j in range(9):
if puzzle_grid[i][j] != 0:
available_numbers.append((puzzle_grid[i][j], i, j))
random.shuffle(available_numbers)
coord_mapping = {}
with open(prev_coord_file, 'r') as f:
for line in f:
if ': ' in line:
num, coords = line.strip().split(': ')
coord_mapping[int(num)] = coords.split(', ')
placeholder_idx = 0
for num, i, j in available_numbers:
if placeholder_idx >= self.n_placeholders:
break
if num not in used_values and num in coord_mapping and coord_mapping[num]:
placeholder = chr(97 + placeholder_idx)
coord = random.choice(coord_mapping[num])
placeholders[placeholder] = f"{coord} [={num}]"
used_values.add(num)
for x in range(9):
for y in range(9):
if puzzle_grid[x][y] == num:
puzzle_grid[x][y] = placeholder
placeholder_idx += 1
with open(f"{self.puzzle_folder}/{self.global_number}. {self.difficulty}{self.puzzle_number}_placeholders.txt", 'w') as f:
for placeholder, coord in placeholders.items():
f.write(f"{placeholder} = {coord}\n")
puzzle_filename = f"{
self.puzzle_folder}/{self.global_number}. {self.difficulty}{self.puzzle_number}"
createPuzzleSvg(puzzle_filename, puzzle_grid)
return puzzle_grid, solution_grid
def createPuzzleSet(difficulty_level, num_puzzles, num_hints, num_placeholders=0, start_number=1, global_start=1):
"""Create a set of puzzles for a specific difficulty level."""
os.makedirs("puzzles", exist_ok=True)
for i in range(num_puzzles):
puzzle_number = start_number + i
global_number = global_start + i
generator = EnhancedSudokuGenerator(
difficulty=difficulty_level,
puzzle_number=puzzle_number,
n_hints=num_hints,
n_placeholders=num_placeholders,
global_number=global_number
)
puzzle_grid, solution_grid = generator.generate_linked_puzzle()
def createPuzzleSvg(filename="Puzzle", grid=[]):
filename = filename if filename.endswith(".svg") else filename + ".svg"
grid_size = len(grid)
cell_size = 40
grid_font_size = 20
grid_outline_width = 5
grid_width = grid_size * cell_size
grid_height = grid_size * cell_size
dwg = Drawing(filename, size=(grid_width, grid_height))
grid_group = Group()
for row in range(grid_size):
for col in range(grid_size):
cell_x = col * cell_size
cell_y = row * cell_size
char_x = cell_x + cell_size // 2
char_y = cell_y + cell_size // 2
value = str(grid[row][col]) if grid[row][col] != 0 else ""
grid_group.add(dwg.text(value, insert=(char_x, char_y), text_anchor="middle",
alignment_baseline="central", font_size=grid_font_size, fill='black'))
grid_group.add(dwg.rect(insert=(cell_x, cell_y), size=(
cell_size, cell_size), fill='none', stroke='black', stroke_width=1))
for i in range(1, grid_size):
if i % 3 == 0:
# Horizontal line
y = i * cell_size
dwg.add(dwg.line(start=(0, y), end=(grid_width, y),
stroke='black', stroke_width=3))
# Vertical line
x = i * cell_size
dwg.add(dwg.line(start=(x, 0), end=(x, grid_height),
stroke='black', stroke_width=3))
dwg.add(grid_group)
# Add outer grid border
dwg.add(dwg.rect(insert=(0, 0), size=(
grid_width, grid_height), fill='none', stroke='red', stroke_width=grid_outline_width))
dwg.save()
def displayGrid(grid):
for i in range(9):
for j in range(9):
print(grid[i][j], end=" ")
print()
if __name__ == "__main__":
# Minimum number of hints required to have a unique solution to a Sudoku puzzle is 17, so n_hints should always be>= 17
n_hints = 20
sudoku = SudokuGenerator(n_hints)
solver = SolverSudoku()
grid = sudoku.generate_puzzle()
print('\n', '*'*5, 'PUZZLE GRID', '*'*5)
displayGrid(grid)
createPuzzleSvg("Puzzle", grid)
if (solver.Suduko(grid, 0, 0)):
print('\n', '*'*5, 'SOLUTION', '*'*5)
displayGrid(grid)
createPuzzleSvg("Solution", grid)
else:
print("No Solution exist:(")