Exploring Strategies to Generate and Solve Sudoku Grids SUNY Oswego CSC 466 Spring '09 Theodore Trotz
Terminology A Sudoku grid contains 81 cells Each cell is a member of a particular region, row, and column There are 9 regions, rows and columns each contains 9 cells A B C D E F G H I ------------------------- 1 2 NW N NE 3 -------+-------+------- 4 5 W C E 6 -------+-------+------- 7 8 SW S SE 9 -------------------------
Objective The goal of Sudoku is to fill each cell such that the numbers 1-9 appear only once in each region, column, and row A B C D E F G H I ------------------------- 1 1 6 5 7 4 3 8 2 9 2 2 3 7 9 5 8 6 1 4 3 8 9 4 1 2 6 7 5 3 -------+-------+------- 4 9 1 6 2 8 7 4 3 5 5 3 7 8 4 1 5 9 6 2 6 5 4 2 3 6 9 1 8 7 -------+-------+------- 7 7 5 3 8 9 1 2 4 6 8 4 8 9 6 3 2 5 7 1 9 6 2 1 5 7 4 3 9 8 ------------------------- Board Consistency: T
Motivation Sudoku is new to the family of logic games Sudoku Player Forums Other peoples programs Few Sudoku Programs in Lisp 5,472,730,538 Sudoku boards after equivalence classes have been removed
Representation Each region contains an association list for the enclosed cells Rows of the board are fetched by conjoining three regions, and then passing an offset to get the desired row Columns work in a similar manner Required to board consistency
Backtracking Analogous to Guess and Check For each cell loop: Populate a list of 9 random numbers 1-9 - no duplicates Pop the first random number off the list and set that value to the current cell If the board is valid If the next cell returns true, return true from recursion Else set cell to empty Return NIL http://moxie.cs.oswego.edu/~trotz/csc466/sudoku/backtracking_demo.txt
User Interface Current Stage Checks Board Consistency Generates Complete Grids Load in / print out a string of length 81 representing a board configuration
Features To Be Added Removal of cells from completed grid to allow the user to play interactively Beat the Clock Game Play General Sudoku Solver Almost There Store / Load Boards
Code Segments ; IN: A zero-indexed offset to select a column AND the ; region list to which the column belongs ; OUT: A list of arity 3 which is the a-list of the column (defmethod get-region-col ((offset integer (region list &optional col (cond ((eq (nth offset region nil col ((and (> offset 2 (null col ; Do not allow initial offsets greater than 2 as ;the output is not valid nil (t (get-region-col (+ offset 3 region (append col (list (nth offset region ; IN: An association list to check for consistency. ; OUT: Returns T if the numbers 1-9 appear only once. Returns NIL if they appear more than once. (defmethod check-cells ((alist list &aux values (setf values (extract-alist-values alist (loop for i from 1 to 9 do (setf values (remove i values :count 1 (setf values (remove 0 values (cond ((null values t (t nil ; DES: This method utilizes a backtracking algorithm to populate the list of cells given. ; IN: A board and the list of cells to be filled ; OUT: Returns T when completed. (defmethod populate-cell-backtrack ((b board (cells list &aux cell random-nums (cond ((null cells t (t (setf random-nums (nine-random-nums ( (setf cell (car (car cells (dolist (value random-nums (rplacd (assoc cell cells value (display-board b (format t "Board Consistency: ~A~%" (check-board b (if (check-board b (if (populate-cell-backtrack b (cdr cells (return-from populate-cell-backtrack t (rplacd (assoc cell cells 0 nil (defclass board ( ( (nw :accessor board-nw :initform (pairlis '(c3 b3 a3 c2 b2 a2 c1 b1 a1 '(0 0 0 0 0 0 0 0 0 (n :accessor board-n :initform (pairlis '(f3 e3 d3 f2 e2 d2 f1 e1 d1 '(0 0 0 0 0 0 0 0 0 (ne :accessor board-ne :initform (pairlis '(i3 h3 g3 i2 h2 g2 i1 h1 g1 '(0 0 0 0 0 0 0 0 0 (w :accessor board-w :initform (pairlis '(c6 b6 a6 c5 b5 a5 c4 b4 a4 '(0 0 0 0 0 0 0 0 0 (c :accessor board-c :initform (pairlis '(f6 e6 d6 f5 e5 d5 f4 e4 d4 '(0 0 0 0 0 0 0 0 0 (e :accessor board-e :initform (pairlis '(i6 h6 g6 i5 h5 g5 i4 h4 g4 '(0 0 0 0 0 0 0 0 0 (sw :accessor board-sw :initform (pairlis '(c9 b9 a9 c8 b8 a8 c7 b7 a7 '(0 0 0 0 0 0 0 0 0 (s :accessor board-s :initform (pairlis '(f9 e9 d9 f8 e8 d8 f7 e7 d7 '(0 0 0 0 0 0 0 0 0 (se :accessor board-se :initform (pairlis '(i9 h9 g9 i8 h8 g8 i7 h7 g7 '(0 0 0 0 0 0 0 0 0
#!/usr/bin/perl -w use 5.004; use CGI qw(:standard; Perl Script my $clisp = "/opt/sfw/bin/clisp -q "; #-----[ Main Routine ]--------------------------------------- print header(-type => 'text/html'; print start_html(-head => style({-type => 'text/css'}, join('',<data>,, ; displayboard(; print end_html; print "\n"; exit 0; #-----[ Subroutines ]--------------------------------------- sub displayboard { print '<pre>'; $path = "/home/trotz/public-html/csc466/cgi/sudoku-cgi.fas"; system $clisp. $path; print '</pre>'; } DATA pre { text-align: center; }
Questions?
References [1] Delahaye, Jean-Paul. The Science Behind SUDOKU. Scientific American 294.6 (June 2006: 80 87. Business Source Premier. EBSCO. 23 Feb. 2009 [2] Chang,Christopher. hsolve: A Difficulty Metric and Puzzle Generator for Sudoku. 2008. [3] Chandwick, Seth. Ease and Toil: Analyzing Sudoku. UMAP 29(2008: 363-379. [4] Dahl, Geir. Permutation matracies related to Sudoku. Linear Algebra and its Applications (2008: 1-7 [5] Santos-Garcia, Gustavo. Solving Sudoku Puzzles with Rewriting Rules. Electronic Notes in Theoretical Computer Science 176(2007 79-93. [6] Schreiner, Axel. Sudoku A little lesson in OOP. ACM SIGCSE Bullitin 40(2008: 44-47. [7] Hui-Dong, MO. Sudoku Square a New Design in Field Experiments. ACTA Argonomica Sinica 34(2008: 1489-1493. [8] Boyer, Christian. Sudoku's French Ancestors Solutions to the Problems. Math Intel 29(2007: 41-44. [9] Glaister, Elizabeth. Su Doku. Mathematics in School 34(2005: 2-3. [10] Martin, David. Cracking the Sudoku: A Deterministic Approach. UMAP 29(2008: 381-394.