-
Notifications
You must be signed in to change notification settings - Fork 2
/
solver.asm
782 lines (642 loc) · 13.8 KB
/
solver.asm
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
; Super Sudoku - A Sudoku game for the SNES
; Copyright 2019-2022 Raphaël Assenat <[email protected]>
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
; OTHER DEALINGS IN THE SOFTWARE.
.include "header.inc"
.include "snesregs.inc"
.include "misc_macros.inc"
.include "zeropage.inc"
.include "grid.inc"
.bank 0
.ramsection "solver_variables" SLOT RAM_SLOT
slv_num_valid_moves: dw
slv_valid_list: dsb 9
slv_digit: dw
slv_x: dw
slv_y: dw
slv_tmp_x: dw
slv_tmp_y: dw
; An array whose indexes matches gridata. Each element
; is a count of how many valid moves there are.
slv_num_moves_per_cell: dsw 81
;slv_tmpval: dw
.ends
.16BIT
.section "solver code" FREE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Count how many legal moves a cell has.
;
; Input args: gridarg_x, gridarg_y
; Output: slv_num_valid_moves, slv_valid_list
;
solver_countValidMovesInCell:
pushall
A8
XY8
ldx #0 ; legal move counter
lda #1 ; digit to try
@next_digit
sta gridarg_value
jsr grid_canInsertValueAt ; sets carry when illegal
bcs @illegal
@legal:
sta slv_valid_list, X
inx
@illegal:
inc A ; advance to next digit value
cmp #10
bne @next_digit
XY16
stx slv_num_valid_moves
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if a given digit is already present in a column
;
; Args:
; - X: Index into griddata
; - slv_digit: The value to look for
;
; Returns with carry set if present.
_solver_isDigitInColumn:
pushall
AXY16
ldy #9 ; Check 9 cells
@next_cell:
lda griddata, X
and #$ff
cmp slv_digit
beq @present
; Skip to next row (+18)
txa
clc
adc #18
tax
dey ; count where we're at
bne @next_cell
@absent:
popall
clc
rts
@present:
popall
sec
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if a given digit is already present in a row.
;
; Args:
; - X: Index into griddata
; - slv_digit: The value to look for
;
; Returns with carry set if present.
_solver_isDigitInRow:
pushall
AXY16
ldy #9 ; Check 9 cells
@next_cell:
lda griddata, X
and #$ff
cmp slv_digit
beq @present
inx ; advance to
inx ; next cell
dey ; count where we're at
bne @next_cell
@absent:
popall
clc
rts
@present:
popall
sec
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if a number that is not already in a given column can be inserted
; legally in only ONE empty cell.
;
; Input: gridarg_x, slv_digit
; Output: slv_tmp_y, carry
;
; Modifies: gridarg_y
;
; returns with carry clear if it only fits in one place.
; sets slv_tmp_y to Y coordinate where the digit fits
;
_solver_canNumberFitInOnlyOneColumnCell:
pushall
AXY16
; First, check if the number is already in the column
stz gridarg_y
jsr grid_getDataOffset
jsr _solver_isDigitInColumn
bcs @return_no ; already there
; Now check each cell
ldx #0 ; Holds current Y
ldy #0 ; Counter for places it can fit
lda slv_digit ; prepare argument for
sta gridarg_value ; grid_canInsertValueAt calls
@next_cell:
stx gridarg_y
; gridarg_y stays constant
jsr grid_isEmptyAt
bcs @cannot
jsr grid_canInsertValueAt
bcs @cannot
; we can. count it!
stx slv_tmp_y ; save where it fitted
iny
cpy #2 ; Check if it was found to fit in more than 1 place
beq @return_no ; Yes? Then return false
@cannot:
inx
cpx #9
bne @next_cell
; Done looping. Did we find a place where it fits?
cpy #1
bne @return_no ; if this branch is taken, the puzzle is not solvable
@return_yes:
popall
clc
rts
@return_no:
popall
sec
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if a number that is not already in a given row can be inserted
; legally in only ONE empty cell.
;
; Input: gridarg_y, slv_digit
; Output: slv_tmp_x, carry
;
; Modifies: gridarg_x
;
; returns with carry clear if it only fits in one place.
; sets slv_tmp_x to X coordinate where the digit fits
;
_solver_canNumberFitInOnlyOneRowCell:
pushall
AXY16
; First, check if the number is already in the row
stz gridarg_x
jsr grid_getDataOffset
jsr _solver_isDigitInRow
bcs @return_no ; already there
; Now check each cell
ldx #0 ; Holds current X
ldy #0 ; Counter for places it can fit
lda slv_digit ; prepare argument for
sta gridarg_value ; grid_canInsertValueAt calls
@next_cell:
stx gridarg_x
; gridarg_y stays constant
jsr grid_isEmptyAt
bcs @cannot
jsr grid_canInsertValueAt
bcs @cannot
; we can. count it!
stx slv_tmp_x ; save where it fitted
iny
cpy #2 ; Check if it was found to fit in more than 1 place
beq @return_no ; Yes? Then return false
@cannot:
inx
cpx #9
bne @next_cell
; Done looping. Did we find a place where it fits?
cpy #1
bne @return_no ; if this branch is taken, the puzzle is not solvable
@return_yes:
popall
clc
rts
@return_no:
popall
sec
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Iterate over non-occupied cells, looking for a cell that would
; legally accept only once value.
;
; Uses: gridarg_x, gridarg_y, gridarg_value (for grid_* functions)
;
; Output: slv_digit (if found), slv_x, slv_y
;
; Returns with carry clear if a candidate was found.
;
solver_findUniqueColumnCandidate:
pushall
AXY16
lda #1 ; First digit to try
@next_digit:
ldy #0 ; X coord.
@next_row:
sta slv_digit
sty gridarg_x
jsr _solver_canNumberFitInOnlyOneColumnCell
bcc @found
iny
cpy #9
bne @next_row
inc A
cmp #10
bne @next_digit
bra @not_found
@found:
sty slv_x
ldx slv_tmp_y
stx slv_y
; note: slv_digit already set
popall
clc
rts
@not_found:
popall
sec
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Iterate over non-occupied cells, looking for a cell that would
; legally accept only once value.
;
; Uses: gridarg_x, gridarg_y, gridarg_value (for grid_* functions)
;
; Output: slv_digit (if found), slv_x, slv_y
;
; Returns with carry clear if a candidate was found.
;
solver_findUniqueRowCandidate:
pushall
AXY16
lda #1 ; First digit to try
@next_digit:
ldy #0 ; Y coord.
@next_row:
sta slv_digit
sty gridarg_y
jsr _solver_canNumberFitInOnlyOneRowCell
bcc @found
iny
cpy #9
bne @next_row
inc A
cmp #10
bne @next_digit
bra @not_found
@found:
sty slv_y
ldx slv_tmp_x
stx slv_x
; note: slv_digit already set
popall
clc
rts
@not_found:
popall
sec
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Iterate over non-occupied cells, looking for a cell that would
; legally accept only once value.
;
; Uses: gridarg_x, gridarg_y, gridarg_value (for grid_* functions)
;
; Output: slv_digit (if found), slv_x, slv_y
;
; Returns with carry clear if a candidate was found.
;
solver_findSoleCandidate:
pushall
AXY16
ldy #0
@next_y:
ldx #0
@next_x:
; Try all digits, from 9 to 1, counting how many are valid
; moves in this cell
lda #9
sta gridarg_value
; Counter for valid digits
lda #0
stx gridarg_x
sty gridarg_y
@next_digit:
jsr grid_isEmptyAt
bcs @occupied ; If occupied, noting to do here
jsr grid_canInsertValueAt ; sets carry when illegal
bcs @not_legal
pha
lda gridarg_value
sta slv_digit
pla
ina ; Count the possibility
cmp #2 ; When more than 1, stop trying digits in this cell
beq @more_than_1
@not_legal:
dec gridarg_value
bne @next_digit
; Check if we found 1 digit
cmp #1
beq @found_single
@occupied:
@more_than_1:
inx
cpx #9
bne @next_x
iny
cpy #9
bne @next_y
; No cell found
popall
sec
rts
@found_single:
; slv_digit is already set. Store X and Y.
stx slv_x
sty slv_y
popall
clc
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
/*
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Setup a list of cell indices (for griddata) in dp_slv_emptyCells in
; increasing number of possible moves. Celles with 0 moves omitted.
;
; The last entry is set to 0xFFFF
;
_solver_prepareEmptyCellList:
; First count how many valid moves in each cell, storing the result
; in slv_num_moves_per_cell in the same order as griddata
jsr _solver_buildNumMovesArray
pushall
AXY16
; Now create a sorted (1 to 9) array of griddata indices.
ldy #0 ; Offset into dp_slv_emptyCells
lda #9
sta dp_slv_sorttmp
@next_count:
ldx #0 ; Offset into griddata
@nextcell:
; Load the number of moves for the current cell
lda slv_num_moves_per_cell, X
beq @skip
; cmp dp_slv_sorttmp
; bne @skip
stx dp_slv_emptyCells, Y
iny
iny
@skip:
inx
inx
cpx #81*2
bne @nextcell
; dec dp_slv_sorttmp
; bpl @next_count
; End of list marker
ldx #$ffff
stx dp_slv_emptyCells, Y
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Populate an array whose indexes matches gridata. Each element
; is a count of how many valid moves there are.
;
; Uses: dp_slv_tmpval
;
; Output: slv_num_moves_per_cell
;
_solver_buildNumMovesArray:
pushall
AXY16
lda #slv_num_moves_per_cell
sta dp_slv_tmpval
ldy #0
@next_row:
ldx #0
@next_cell:
stx gridarg_x
sty gridarg_y
; First check if the cell is occupied. An occupied cell has
; no valid moves. It is assumed that it holds the correct value already.
lda #0 ; prepare a move count of 0
jsr grid_isEmptyAt
bcs @occupied
; Count valid moves
jsr solver_countValidMovesInCell
; Retrive the number of valid moves
lda slv_num_valid_moves
@occupied:
; write it to the array
sta (dp_slv_tmpval)
; advance pointer to next cell
inc dp_slv_tmpval
inc dp_slv_tmpval
inx
cpx #9
bne @next_cell
iny
cpy #9
bne @next_row
popall
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Find an empty cell, starting at offset X.
;
; Return with X the offset into griddata
;
; If none is found, returns with carry set
;
AXY16
_solver_findEmptyCellNew:
ldx #0
A8
@nextCell:
ldy dp_slv_emptyCells, X
bmi @nomore ; Stop at $ffff
lda griddata, Y
beq @empty_found
inx
inx
bra @nextCell
@nomore:
A16
; None found. Return with carry set.
sec
rts
@empty_found:
A16
tyx
clc
rts
*/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Find an empty cell, starting at offset X.
;
; Return with X the offset into griddata
;
; If none is found, returns with carry set
;
AXY16
_solver_findEmptyCell:
; ldx #0
@nextCell:
lda griddata, X
and #$ff
beq @empty_found
inx
inx
cpx #81*2
bne @nextCell
; None found. Re
sec
rts
@empty_found:
clc
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Check if a given values exists in the neighbors of a cell.
;
; Arguments:
; - X : Offset in gridata/neighbor_list
; - dp_slv_tmpval : Value to look for
;
; Return with carry set if found, clear otherwise.
;
AXY16
_solverCheckNeighborsForValue:
phx
phy
; Get the pointer to the list of neighbors for the designated cell
lda neighbor_list, X
; Store it in the direct page for upcoming indirect acceses
sta dp_indirect_tmp1
ldy #20*2
@checknext:
dey
dey
bmi @not_found
lda (dp_indirect_tmp1),Y ; Load offset for neighbor
tax ; Move the offset to X to use it
lda griddata, X ; Load the value at this position
and #$FF
cmp dp_slv_tmpval ; Check if it is the value we are looking for
bne @checknext
@foundit:
sec
ply
plx
rts
@not_found:
clc
ply
plx
rts
;;;;;;;;
;
; Recursive routine for solving a sudoku.
;
; Returns with carry set when all cells are populated.
;
; X: Current offset in griddata for faster empty cell finding
;
AXY16
_solver_bruteforcer:
phy
phx
lda cancel_solver
bne @triedall
; 1. Find an empty cell.
;jsr _solver_findEmptyCell2
;bcs @solved ; Carry set means none was found. Puzzle solved.
; inlined version of the above
@nextCell:
lda griddata, X
and #$ff
beq @empty_found
inx
inx
cpx #81*2
bne @nextCell
bra @solved
@empty_found:
; X now holds the offset for the cell. It is used by the neighbor
; check in the loop below.
; 2. Try digits at current position
ldy #10 ; First digit to try
@next_value:
dey
beq @triedall
; Check if this is a valid move here
sty dp_slv_tmpval ; Value to check (arg for subroutine below)
jsr _solverCheckNeighborsForValue
bcs @next_value ; Carry is set if value is unallowed
; Value allowed! Insert it.
tya
; jsr grid_insertHintedValueOffset
ora #(BRUTEFORCED_DIGIT_PAL<<8)
sta griddata, X
; Try recursion.
jsr _solver_bruteforcer
bcs @solved
; Carry clear? Then this was a dead end. Try another digit...
bra @next_value
@triedall:
; None of the tried digits worked. Clear the cell.
;lda #0
;jsr grid_insertHintedValueOffset
stz griddata, X
lda #1
sta grid_changed
clc
plx
ply
rts
@solved:
lda #1
sta grid_changed
plx
ply
rts
solver_bruteForce:
pushall
AXY16
; this was for use with _solver_findEmptyCellNew but
; it seems slower.
; jsr _solver_prepareEmptyCellList
brk1:
ldx #0
; stx dp_slv_nextEmpty
jsr _solver_bruteforcer
popall
rts
.ends