-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcel-Sudoku-Master-Solver_V2_Module(2)-Code.vb
630 lines (391 loc) · 23.8 KB
/
Excel-Sudoku-Master-Solver_V2_Module(2)-Code.vb
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal ms as Long)
#End If
'ensures board is valid
Public Function ensureValid()
Dim SKIP As Boolean
Dim row As Integer 'current row position
Dim col As Integer 'current column position
Dim current As Integer 'try value (1-9)
Dim xCheck As Integer 'check row
Dim yCheck As Integer 'check column
Dim sqCheck As Integer 'check 3x3 block
Dim D As Integer 'used to check row/column
Dim x As Integer 'used to check 3x3 square
Dim y As Integer 'used to check 3x3 square
Dim k As Integer 'used to check 3x3 square
Dim j As Integer 'used to check 3x3 square
SKIP = False
FAIL = False
row = 2 'starts row 2
Do While (row <= 10) 'move down one column
col = 1 'reset column
Do While (col <= 9) 'move over one column
current = CInt(Cells(row, col)) 'look at current value
If (current = 0) Then 'blank cell - SKIP
col = col + 1
Else 'check if valid on board
''''''''''''''''''''''''''''''''''''''
''''''''''check valid in row''''''''''
''''''''''''''''''''''''''''''''''''''
If (SKIP <> True) Then
D = 1
Do While (D <= 9)
If (D = col) Then 'current cell - SKIP
D = D + 1
Else
xCheck = CInt(Cells(row, D))
If (xCheck = current) Then 'found in row - INVALID
FAIL = True
SKIP = True
'turn font red for failed numbers
With Cells(row, D)
.Font.Color = vbRed
End With
With Cells(row, col)
.Font.Color = vbRed
End With
D = 10
Else
D = D + 1
End If
End If
Loop
End If
''''''''''''''''''''''''''''''''''''''
''''''''check valid in column'''''''''
''''''''''''''''''''''''''''''''''''''
If (SKIP <> True) Then
D = 2 'board starts on second row down
Do While (D <= 10)
If (D = row) Then 'current cell - SKIP
D = D + 1
Else
yCheck = CInt(Cells(D, col))
If (yCheck = current) Then 'found in column - INVALID
FAIL = True
SKIP = True
'turn font red for failed numbers
With Cells(D, col)
.Font.Color = vbRed
End With
With Cells(row, col)
.Font.Color = vbRed
End With
D = 11
Else
D = D + 1
End If
End If
Loop
End If
''''''''''''''''''''''''''''''''''''''
'''''''''check valid in square''''''''
''''''''''''''''''''''''''''''''''''''
If (SKIP <> True) Then
'feed in coordinates (row, col) and get top left
'cell coordinates of the current 3x3 block (x, y)
y = 3 * Int((row - 2) / 3) + 2
x = 3 * Int((col - 1) / 3) + 1
k = 0
Do While (k <= 2) 'move down one row in 3x3 block
j = 0
Do While (j <= 2) 'move right one column in 3x3 block
If (x = col) And (y = row) Then 'current cell - SKIP
j = j + 1
x = x + 1
Else
sqCheck = CInt(Cells(y, x))
If (sqCheck = current) Then 'found in 3x3 block - INVALID
FAIL = True
SKIP = True
'turn font red for failed numbers
With Cells(y, x)
.Font.Color = vbRed
End With
With Cells(row, col)
.Font.Color = vbRed
End With
j = 4
k = 4
Else
j = j + 1
x = x + 1
End If
End If
If (j = 3) Then 'reset x
x = 3 * Int((col - 1) / 3) + 1
End If
Loop
k = k + 1
y = y + 1
Loop
'''''''''''''''past cell checks''''''''''''''''''''''
End If
col = col + 1
End If
Loop
row = row + 1
Loop
''''''''''''''''''''''''''''''''''''''
'''''''''valid if FAIL <> True''''''''
''''''''''''''''''''''''''''''''''''''
If (FAIL = True) Then
'display failed message
genMsg = MsgBox("Board is invalid! Check board and try again!", vbOKOnly, "INVALID BOARD")
Exit Function
Else
'only displays if this function is called from the "VALIDATE" button
If (valMsg = True) Then 'display valid message
'display success message
genMsg = MsgBox("VALID BOARD!!", vbOKOnly, "VALID")
End If
End If
End Function
'solves sudoku puzzle
Public Function solvePuzzle()
If (FAIL = True) Then 'puzzle not possible dont attempt
Exit Function
End If
Dim SKIP As Boolean 'used to skip future checks on a number
Dim BACKTRACK As Boolean 'flag that code is running backwards through puzzle
Dim backTracking As Boolean 'flad that code needs to continue backtracking
Dim GOLD As Boolean 'used to determine previously placed number
Dim sloMo As Boolean 'slow motion activation
Dim row As Integer 'current row position
Dim col As Integer 'current column position
Dim try As Integer 'value to try in cell (1-9)
Dim current As Integer 'current position cell contents
Dim xCheck As Integer 'check row
Dim yCheck As Integer 'check column
Dim sqCheck As Integer 'check 3x3 block
Dim D As Integer 'used to check rows/columns
Dim x As Integer 'used to check 3x3 square
Dim y As Integer 'used to check 3x3 square
Dim k As Integer 'used for loop check 3x3 square
Dim j As Integer 'used for loop check 3x3 square
Dim v As Integer 'used for finding valid cell for backtracking
Dim fontSize As Integer 'used to tell if code is backtracking
Dim slow As Integer
Dim attempt As Long
SKIP = False
BACKTRACK = False
backTracking = False
row = 2
col = 1
attempt = 0
slow = CInt(Range("O11").Value)
If (slow = 0) Then 'revert to default
slow = 10
End If
If (Range(Cells(6, 10), Cells(6, 14)).Interior.Color = vbGreen) Then 'SLO-MO TIME
sloMo = True
Else
sloMo = False
End If
Do While (row <= 10) 'move down 1 row
col = 1
Do While (col <= 9) 'move over 1 column
current = CInt(Cells(row, col)) 'get value of current cell
fontSize = CInt(Cells(row, col).Font.Size) 'gets the font size
If (fontSize = 39) Then 'valid cell for BACKTRACK code
GOLD = True
Else
GOLD = False
End If
If (current <> 0) And (BACKTRACK <> True) Then 'not blank space or backtracking - SKIP
col = col + 1
Else
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''ENSURE BACKTRACKED CELL IS VALID SPACE''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If (BACKTRACK = True) Then
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''MOVE BACK ONE CELL IF NOT A NUMBER WE PLACED'''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If (GOLD <> True) Then 'keep moving back through puzzle until spot is found
v = 1
Do While (v <= 81) 'how many placements we can go back
''''STEP BACK THROUGH CODE UNTIL CELL IS FOUND THAT IS GOLD = True''''
If (col = 1) Then
'up one row far right cell
col = 9
row = row - 1
If (row = 1) Then 'no solution
genMsg = MsgBox("Well this means the code backtracked past the beginning of the puzzle. No solution to this possible was found. Sorry :(", vbOKOnly, "WELL THIS IS EMBARRASING")
Exit Function
Else
'check if valid space
fontSize = CInt(Cells(row, col).Font.Size)
If (fontSize = 39) Then 'gold space - VALID
GOLD = True
Else
GOLD = False
End If
End If
Else
'move left one column
col = col - 1
'check if valid
fontSize = CInt(Cells(row, col).Font.Size)
If (fontSize = 39) Then 'gold space - VALID
GOLD = True
Else
GOLD = False
End If
End If
If (GOLD = True) Then 'found valid space
try = CInt(Cells(row, col)) + 1
BACKTRACK = False
backTracking = False
Exit Do
Else 'try next space back
v = v + 1
End If
Loop
Else 'CURRENT CELL VALID FOR BACKTRACKING - TRY NEXT NUMBER
try = CInt(Cells(row, col)) + 1
BACKTRACK = False
backTracking = False
'reset backTracking
End If
If (try = 10) Then 'cant try next number, need to backtrack again
backTracking = True
BACKTRACK = True
With Cells(row, col)
.Value = 0
.Font.Size = 36
.Font.Color = RGB(175, 95, 95)
End With
Else
backTracking = False
BACKTRACK = False
End If
Else 'not backtracking - start at 1
try = 1
End If
If (backTracking = False) Then 'check try value
SKIP = False
Do While (try <= 9) 'try numbers 1-9
''''''''''''''''''''''''''''''''''''''
''''check if 'try' is valid in row''''
''''''''''''''''''''''''''''''''''''''
'SKIP = False 'reset skip (only before row!)
D = 1 'reset D
Do While (D <= 9) 'move over one column
xCheck = CInt(Cells(row, D))
If (xCheck = try) Then 'try found in row - INVALID
SKIP = True
Exit Do
Else
D = D + 1
End If
Loop
'''''''''''''''''''''''''''''''''''''''
'''check if 'try' is valid in column'''
'''''''''''''''''''''''''''''''''''''''
If (SKIP <> True) Then 'try not found in row and not backtracking
D = 2 'reset D
Do While (D <= 10) 'move down one row
yCheck = CInt(Cells(D, col))
If (yCheck = try) Then 'found in column - INVALID
SKIP = True
Exit Do
Else
D = D + 1
End If
Loop
End If
''''''''''''''''''''''''''''''''''''''''''
'''check if 'try' is valid in 3x3 block'''
''''''''''''''''''''''''''''''''''''''''''
If (SKIP <> True) Then 'try not found in row or column not backtracking
'feed in coordinates (row, col) and get top left
'cell coordinates of the current 3x3 block (x, y)
x = 3 * Int((col - 1) / 3) + 1
y = 3 * Int((row - 2) / 3) + 2
k = 1
Do While (k <= 3) 'move down one row in 3x3
j = 1 'reset column
Do While (j <= 3) 'move over one column
sqCheck = CInt(Cells(y, x))
If (sqCheck = try) Then 'found in square - INVALID
SKIP = True
j = 4
k = 4
Else
j = j + 1
x = x + 1
If (j = 4) Then 'reset x
x = 3 * Int((col - 1) / 3) + 1
End If
End If
Loop
k = k + 1
y = y + 1
Loop
End If
''''''''''''''''''''''''''''''''''''''''''
''past checks - see if 'try' was skipped''
''''''''''''''''''''''''''''''''''''''''''
If (SKIP <> True) Then 'try made it through checks is VALID
'fill in cell with try
With Cells(row, col)
.Value = try
.Font.Color = RGB(208, 154, 0)
.Font.Size = 39
End With
'counts how many numbers were tried
attempt = attempt + 1
Exit Do 'dont try any more numbers
Else 'try was skipped, move to next number
try = try + 1
SKIP = False 'reset skip
If (try = 10) Then 'HERE IS WHERE I WILL NEED TO IMPLEMENT BACKTRACKING
'IF try = 10 THEN ALL NUMBERS HAVE BEEN TRIED AND WE NEED TO GO BACK TO
'OUR LAST CHOICE TO TRY A DIFFERENT NUMBER. WHEN A CHOICE IS MADE THE FONT
'SIZE IS CHANGED. I'VE USED THIS AS A FLAG TO TELL ME IF I CAN CHANGE A
'NUMBER IN THE CELL BASED ON FONT SIZE - TRACKED WITH THE 'GOLD' BOOLEAN
BACKTRACK = True
'reset current cell to 0
With Cells(row, col)
.Value = 0
.Font.Size = 36
.Font.Color = RGB(175, 95, 95)
End With
Else
BACKTRACK = False
End If
End If
Loop
End If
If (BACKTRACK = False) Then 'going forward in puzzle
col = col + 1
Else 'going backwards in puzzle
If (col = 1) Then
'move up one row far right cell
col = 9
row = row - 1
If (row = 1) Then 'no solution
genMsg = MsgBox("Well this means the code backtracked past the beginning of the puzzle. No solution to this possible was found. Sorry :(", vbOKOnly, "WELL THIS IS EMBARRASING")
Exit Function
End If
Else
'move back one column
col = col - 1
End If
End If
End If
If (sloMo = True) Then 'PAUSE
Sleep slow
End If
Loop
If (BACKTRACK = False) Then 'going forward in puzzle
row = row + 1
End If
Loop
genMsg = MsgBox("SUCCESS!!" + vbNewLine + "The algorithm placed " + CStr(Format(attempt, "#,###")) + " different numbers to come to this solution.", vbOKOnly, "SUCCESS")
End Function