Skip to content

Commit 7044ded

Browse files
committed
Finalize and add pillow images
1 parent 383b0ce commit 7044ded

6 files changed

+48
-15
lines changed

OpenSans-Regular.ttf

212 KB
Binary file not shown.

evil_sudoku.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
7 9
2+
5 8
3+
178 3
4+
1 46 9
5+
3 2
6+
7
7+
4 81 6
8+
5
9+
6 9

expert_sudoku.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
5 96
2+
6 3 2
3+
9
4+
1 8 7
5+
2 3
6+
8 5
7+
269
8+
4 5
9+
57 3

main.py

+28-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import numpy as np
33
from hashlib import md5
44
from copy import deepcopy
5+
from time import perf_counter
6+
from PIL import Image, ImageDraw, ImageFont
57

68
class Node():
79
def __init__(self, state, parent, action):
@@ -145,11 +147,9 @@ def validate(self, grid):
145147
# Check rows
146148
if action in board[row, :]:
147149
possible = False
148-
errors += 1
149150
# Check column
150151
if action in board[:, col]:
151152
possible = False
152-
errors += 1
153153
# If not in quadrant
154154
startRow = row - (row%3)
155155
startCol = col - (col%3)
@@ -159,10 +159,25 @@ def validate(self, grid):
159159
possible = False
160160
errors += 1
161161
board[row][col] = action
162-
else:
162+
if possible:
163163
pass
164+
else:
165+
return possible
164166
return possible
165167

168+
# Creating an image
169+
def generate_image(self, grid):
170+
grid = np.array(grid)
171+
text = ""
172+
for row in range(self.height):
173+
for col in range(self.width):
174+
text = text + grid[row][col] + " "
175+
text = text + "\n"
176+
img = Image.new('RGB', (500,500), (250,250,250))
177+
draw = ImageDraw.Draw(img)
178+
font = ImageFont.truetype("OpenSans-Regular.ttf", 30)
179+
draw.text((0, 0),str(text),(0,0,0),font=font)
180+
img.save('output.jpg')
166181
# Solving the puzzle
167182
def solve(self):
168183
# Keep track of number of explored states
@@ -176,7 +191,6 @@ def solve(self):
176191
frontier = QueueFrontier()
177192
else:
178193
exit()
179-
180194
frontier.add(Node(state = self.board, parent = None, action = None))
181195

182196
# Keep track of explored Nodes
@@ -185,7 +199,7 @@ def solve(self):
185199
# While loop until solution is found
186200
while True:
187201
# Progress report
188-
if (self.num_explored%100) == 0:
202+
if (self.num_explored%1000) == 0:
189203
print("Number explored: " + str(self.num_explored))
190204
# Exception if no solutions
191205
if frontier.empty():
@@ -209,7 +223,7 @@ def solve(self):
209223
if self.validate(grid):
210224
print("Solved: ")
211225
self.print(grid)
212-
return
226+
return grid
213227
else:
214228
# Add to explored
215229
self.num_explored += 1
@@ -220,24 +234,24 @@ def solve(self):
220234
row, col = action[1][0], action[1][1]
221235
num = action[0]
222236
new_state[row][col] = num
223-
if self.validate(new_state):
224-
new_node = Node(state = deepcopy(new_state), parent = node.state, action = None)
225-
frontier.add(new_node)
226-
self.explored.append(self.getMD5(str(new_state)))
227-
else:
228-
print("Invalid")
237+
new_node = Node(state = deepcopy(new_state), parent = node.state, action = None)
238+
frontier.add(new_node)
239+
self.explored.append(self.getMD5(str(new_state)))
229240
else:
230241
print("Already explored")
231242
print(self.getMD5(str(grid)))
232243
print(self.explored)
233-
234244
# Main
235245
#Check if arguments are met
236246
if len(sys.argv) != 2:
237247
sys.exit("Usage: python3 main.py sudoku.txt")
238248

239249
# Run game
250+
tic = perf_counter()
240251
filename = sys.argv[1]
241252
game = Sudoku(filename)
242-
game.solve()
253+
grid = game.solve()
243254
print("Iterations used:" + str(game.num_explored))
255+
toc = perf_counter()
256+
print("Time taken: " + str(toc - tic))
257+
game.generate_image(grid)

question_mark_img.jpg

22.4 KB
Loading

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
numpy==1.22.1
1+
pillow
2+
numpy

0 commit comments

Comments
 (0)