;;;;;;;;;;;;;;;;;;;;Sudoku Solver;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;Abstract definition of basic functions;;;;;;;;;;;;
(define solutionnum 0)
(define getSolution
(lambda (arrayList)
(begin
; currentboardSolution.clear();
(let ((solutionnum 0))
(if (!checkValidity)
(void)
(tryRow arrayList 0))))))
(define tryRow
(lambda (arrayList row)
(if (< row BOARD_SIZE)
(tryCell arrayList row 0)
(begin
(let* ((solutionnum (+ solutionnum 1)))
;; printBoard;;;;
;; currentBoardSolution.add(tem);;
)))))
(define tryCell
(lambda (arrayList row col)
(if (< col BOARD_SIZE)
(if (isEmpty arrayList row col)
(tryWithValue arrayList row col 1)
(tryCell arrayList row (+ col 1)))
(tryRow arrayList (+ 1 row)))))
(define tryWithValue
(lambda (arrayList row col n)
(if (> n BOARD_SIZE)
(void)
(if (and (isEmpty arrayList row col) (noConflict arrayList n row col))
(begin
(setBoard arrayList row col n)
(tryCell arrayList row (+ col 1))
;;arrayList[row][col] = 0;;
(tryWithValue arrayList row col (+ n 1))
)
(tryWithValue arrayList row col (+ n 1))))))
;;;;;;;;;;;;;;;;;;;;;;;;CPSing;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define getSolution-top
(lambda (arrayList)
(getSolution-cps arrayList (lambda () (void)))))
(define getSolution-cps
(lambda (arrayList k)
(begin
;; currentboardSolution.clear();
(let ((solutionnum 0))
(if (!checkValidity)
(k)
(tryRow-cps arrayList 0 k))))))
(define tryRow-cps
(lambda (arrayList row k)
(if (< row BOARD_SIZE)
(tryCell-cps arrayList row 0 k)
(begin
(let (solutionnum (+ solutionnum 1))
;; printBoard;;;
;; currentBoardSolution.add(tem);;
)))))
(define tryCell-cps
(lambda (arrayList row col k)
(if (< col BOARD_SIZE
(if (isEmpty arrayList row col)
(tryWithValue-cps arrayList row col 1 k)
(tryCell-cps arrayList row (+ col 1) k))
(tryRow-cps arrayList (+ 1 row) k)))))
(define tryWithValue-cps
(lambda (arrayList row col n k)
(if (> n BOARD_SIZE)
(k)
(if (and (isEmpty arrayList row col) (noConflict arrayList n row col))
(begin
(setBoard arrayList row col n)
(tryCell-cps arrayList row (+ col 1) (lambda ()
;; arrayList[row][col] = 0;
(tryWithValue-cps arrayList row col (+ n 1) k))))
(tryWithValue-cps arrayList row col (+ n 1) k)))))
;;;;;;;;;;;;;;;;;;;;;;;;Representation independent;;;;;;;;;;;;;;;;;;;;;;;
(define make-init-k
(lambda ()
(lambda () (void))))
(define getSolution-top-ri
(lambda (arrayList)
(getSolution-cps-ri arrayList (make-init-k))))
(define getSolution-cps-ri
(lambda (arrayList k)
(begin
;; currentboardSolution.clear();
(let ((solutionnum 0))
(if (!checkValidity)
(apply-k k)
(tryRow-cps-ri arrayList 0 k))))))
(define tryRow-cps-ri
(lambda (arrayList row k)
(if (< row BOARD_SIZE)
(tryCell-cps-ri arrayList row 0 k)
(begin
(let (solutionnum (+ solutionnum 1))
;; printBoard;;;
;; currentBoardSolution.add(tem);;
)))))
(define tryCell-cps-ri
(lambda (arrayList row col k)
(if (< col BOARD_SIZE
(if (isEmpty arrayList row col)
(tryWithValue-cps-ri arrayList row col 1 k)
(tryCell-cps-ri arrayList row (+ col 1) k))
(tryRow-cps-ri arrayList (+ 1 row) k)))))
(define tryWithValue-cps-ri
(lambda (arrayList row col n k)
(if (> n BOARD_SIZE)
(apply-k k)
(if (and (isEmpty arrayList row col) (noConflict arrayList n row col))
(begin
(setBoard arrayList row col n)
(tryCell-cps-ri arrayList row (+ col 1) (make-tryWithvalue-k1 arrayList row col n k)))
(tryWithValue-cps-ri arrayList row col (+ n 1) k)))))
(define make-tryWithValue-k1
(lambda (arrayList row col n k)
(lambda ()
;; arrayList[row][col] = 0;
(tryWithValue-cps-ri arrayList row col (+ n 1) k))))
(define apply-k
(lambda (k)
(k)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Change representation;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define make-init-k
(lambda ()
'()))
(define make-tryWithValue-k1
(lambda (arrayList row col n k)
;; ;; arrayList[row][col] = 0;
(list 'k1 arrayList row col n k)))
(define apply-k
(lambda (k)
(cond
[(null? k) (void)]
[(eq? (car k) 'k1)
(begin
;; arrayList[row][col] = 0;
(tryWithValue-cps-ri (cadr k) (caddr k) (cadddr k) (+ (caddddr k) 1) (cadddddr k)))])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Registerized code;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define arrayListr '*)
(define kr '*)
(define solutionnumr '*)
(define rowr '*)
(define colr '*)
(define nr '*)
(define getSolution-top-ri
(lambda (arrayList)
(set! arrayListr arrayList)
(set! kr '())
(getSolution-cps-ri)))
(define getSolution-cps-ri
(lambda () ;;arrayListr kr
(begin
;; currentboardSolution.clear();
(let ((solutionnum 0))
(set! solutionnumr solutionnum)
(if (!checkValidity)
(apply-k)
(set! rowr 0)
(tryRow-cps-ri))))))
(define tryRow-cps-ri
(lambda () ;;arrayListr rowr kr
(if (< rowr BOARD_SIZE)
(set! colr 0)
(tryCell-cps-ri)
(begin
(set! solutionnumr (+ 1 solutionnumr))
;; printBoard;;;
;; currentBoardSolution.add(tem);;
))))
(define tryCell-cps-ri
(lambda () ;;arrayListr rowr colr kr
(if (< colr BOARD_SIZE)
(if (isEmpty arrayListr rowr colr)
(begin
(set! nr 1)
(tryWithValue-cps-ri)
)
(begin
(set! colr (+ 1 colr))
(tryCell-cps-ri)
))
(begin
(set! rowr (+ 1 rowr))
(tryRow-cps-ri)))))
(define tryWithValue-cps-ri
(lambda () ;;arrayListr rowr colr nr kr
(if (> nr BOARD_SIZE)
(apply-k)
(if (and (isEmpty arrayListr rowr colr) (noConflict arrayListr nr rowr colr))
(begin
(setBoard arrayListr rowr colr nr)
(set! colr (+ 1 colr))
(set! kr (list 'k1 arrayList row col n k))
(tryCell-cps-ri))
(begin
(set! nr (+ 1 nr))
(tryWithValue-cps-ri))))))
(lambda () ;;kr
(cond
[(null? kr) (kr)]
[(eq? (car kr) 'k1)
(begin
;; arrayList[row][col] = 0;
(set! arrayListr (cadr k))
(set! rowr (caddr k))
(set! colr (cadddr k))
(set! nr (+ (caddddr k) 1))
(set! kr (cadddddr k))
(tryWithValue-cps-ri))])))