2
2
import numpy as np
3
3
from hashlib import md5
4
4
from copy import deepcopy
5
+ from time import perf_counter
6
+ from PIL import Image , ImageDraw , ImageFont
5
7
6
8
class Node ():
7
9
def __init__ (self , state , parent , action ):
@@ -145,11 +147,9 @@ def validate(self, grid):
145
147
# Check rows
146
148
if action in board [row , :]:
147
149
possible = False
148
- errors += 1
149
150
# Check column
150
151
if action in board [:, col ]:
151
152
possible = False
152
- errors += 1
153
153
# If not in quadrant
154
154
startRow = row - (row % 3 )
155
155
startCol = col - (col % 3 )
@@ -159,10 +159,25 @@ def validate(self, grid):
159
159
possible = False
160
160
errors += 1
161
161
board [row ][col ] = action
162
- else :
162
+ if possible :
163
163
pass
164
+ else :
165
+ return possible
164
166
return possible
165
167
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' )
166
181
# Solving the puzzle
167
182
def solve (self ):
168
183
# Keep track of number of explored states
@@ -176,7 +191,6 @@ def solve(self):
176
191
frontier = QueueFrontier ()
177
192
else :
178
193
exit ()
179
-
180
194
frontier .add (Node (state = self .board , parent = None , action = None ))
181
195
182
196
# Keep track of explored Nodes
@@ -185,7 +199,7 @@ def solve(self):
185
199
# While loop until solution is found
186
200
while True :
187
201
# Progress report
188
- if (self .num_explored % 100 ) == 0 :
202
+ if (self .num_explored % 1000 ) == 0 :
189
203
print ("Number explored: " + str (self .num_explored ))
190
204
# Exception if no solutions
191
205
if frontier .empty ():
@@ -209,7 +223,7 @@ def solve(self):
209
223
if self .validate (grid ):
210
224
print ("Solved: " )
211
225
self .print (grid )
212
- return
226
+ return grid
213
227
else :
214
228
# Add to explored
215
229
self .num_explored += 1
@@ -220,24 +234,24 @@ def solve(self):
220
234
row , col = action [1 ][0 ], action [1 ][1 ]
221
235
num = action [0 ]
222
236
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 )))
229
240
else :
230
241
print ("Already explored" )
231
242
print (self .getMD5 (str (grid )))
232
243
print (self .explored )
233
-
234
244
# Main
235
245
#Check if arguments are met
236
246
if len (sys .argv ) != 2 :
237
247
sys .exit ("Usage: python3 main.py sudoku.txt" )
238
248
239
249
# Run game
250
+ tic = perf_counter ()
240
251
filename = sys .argv [1 ]
241
252
game = Sudoku (filename )
242
- game .solve ()
253
+ grid = game .solve ()
243
254
print ("Iterations used:" + str (game .num_explored ))
255
+ toc = perf_counter ()
256
+ print ("Time taken: " + str (toc - tic ))
257
+ game .generate_image (grid )
0 commit comments