Author: abaine Date: Tue Aug 21 08:32:48 2007 New Revision: 212
Modified: trunk/funds/examples/sudoku.lisp Log: Fixed solver (!) and made pretty printer.
Modified: trunk/funds/examples/sudoku.lisp ============================================================================== --- trunk/funds/examples/sudoku.lisp (original) +++ trunk/funds/examples/sudoku.lisp Tue Aug 21 08:32:48 2007 @@ -126,7 +126,7 @@ "")))))))))
(defun puzzle-solve (puzzle) - (if (puzzle-complete-p puzzle) + (if (puzzle-solved-p puzzle) puzzle (iter (for f in x-y-z-functions) (for (values x y n) = (best-group puzzle f)) @@ -209,3 +209,49 @@ #'i-k-j-coordinates #'j-k-i-coordinates #'b-k-x-coordinates)) + +(defun print-sudoku (list) + (let* ((size (length list)) + (order (order size))) + (labels ((f (result list i) + (if (null list) + result + (f (concatenate 'string result + (if (zerop (mod i order)) + (filler-string size) + "") + (row-as-string (first list))) + (rest list) + (1+ i))))) + (concatenate 'string (f (format nil "~%") list 0) + (filler-string size))))) + +(defun row-as-string (row) + (let* ((size (length row)) + (order (order size))) + (labels ((f (result list i) + (if (null list) + result + (f (concatenate 'string result + (if (zerop (mod i order)) + "| " + "") + (format nil "~2A" (if (zerop (first list)) + "" + (first list)))) + (rest list) + (1+ i))))) + (concatenate 'string (format nil "~%") (f "" row 0) "|")))) + +(defun filler-string (size) + (let ((order (order size))) + (labels ((f (result i) + (if (= i size) + result + (f (concatenate 'string result + (if (zerop (mod i order)) + "+-" + "") + "--") + (1+ i))))) + (concatenate 'string (format nil "~%") (f "" 0) "+"))))