-2

I have a problem with my sudoku-solver function, it's not doing the backtracking process and i can't see why, just when the backtracking comes, the function stops, here is the code:

(define (solve-sudoku grid)
 (define blank-space (check-for-empty-space 0 '()))
 (cond [(empty? blank-space) (begin (display "FIN\n") true)])
 (define x (first blank-space))
 (define y (first (rest blank-space)))
 (display x) (display y) (display "\n")
 (cond [(eqv? #f (try-value grid 1 x y )) false])
)

(define (try-value grid num  x y )
  (cond [(> num 9) false]
       [(is-safe grid num x y) 
        (begin 
          (assign-to-pos grid num x y) 
           (cond [(solve-sudoku grid) true]
                 [else (begin (display "reset\n")  
                       (assign-to-pos grid 0 x y))]
               ))]
    [else (try-value grid (+ 1 num) x y)])
  )

I have a matrix for tests:

;---------------+-----+-----+-----+--
(define row0 (vector 3 0 6 5 0 8 4 0 0 ))
(define row1 (vector 5 2 0 0 0 0 0 0 0 ))
(define row2 (vector 0 8 7 0 0 0 0 3 1 ))
;---------------+-----+-----+-----+--
(define row3 (vector 0 0 3 0 1 0 0 8 0 ))
(define row4 (vector 9 0 0 8 6 3 0 0 5 ))
(define row5 (vector 0 5 0 0 9 0 6 0 0 ))
;---------------+-----+-----+-----+--
(define row6 (vector 1 3 0 0 0 0 2 5 0 ))
(define row7 (vector 0 0 0 0 0 0 0 7 4 ))
(define row8 (vector 0 0 5 2 0 6 3 0 0 ))
;---------------+-----+-----+-----+--
(define grid (vector row0 row1 row2 row3 row4 row5 row6 row7 row8))

The output is:

empty-position: 0,1
empty-position: 0,4
empty-position: 0,7
empty-position: 0,8
empty-position: 1,2
empty-position: 1,3
empty-position: 1,4
empty-position: 1,5
empty-position: 1,6
empty-position: 1,7
empty-position: 1,8
reset

result
'#(#(3 1 6 5 2 8 4 9 7)
#(5 2 4 1 3 7 8 0 0)
#(0 8 7 0 0 0 0 3 1)
#(0 0 3 0 1 0 0 8 0)
#(9 0 0 8 6 3 0 0 5)
#(0 5 0 0 9 0 6 0 0)
#(1 3 0 0 0 0 2 5 0)
#(0 0 0 0 0 0 0 7 4)
#(0 0 5 2 0 6 3 0 0))

ronsuez
  • 1
  • 1
  • 6

1 Answers1

1

Your test case is enormous, and I can see why it would be really hard to figure out what's going wrong. Rather than trying to debug the whole huge thing, test small pieces first. I can't see all of your program, but it sounds like you need to write many small tests for all of the functions in your program.

John Clements
  • 16,895
  • 3
  • 37
  • 52