This preview shows pages 1–3. Sign up to view the full content.
CS108, Stanford
Handout #20
Winter 2012
Young
HW3 Sudoku/DB
Sudoku Part of Assignment by Nick Parlante
Homework 3 makes extensive use of the Java Collection classes while exercising OOP themes 
assembling a large solution out of modular classes, giving each class a good API for its clients, testing, and
some GUI coding. You’ll also get an opportunity to work with accessing a MySQL database.
The whole
thing is due at midnight of the evening of Wednesday February 8th.
Part A  Sudoku
For this part of the project, you will build code to solve Sudoku puzzles. Our approach will concentrate on
OOP and API design, and give us a chance to start doing some GUI coding. You do not need to be good at
Sudoku to build this code. I'm quite slow at them. In fact, this whole project is perhaps cheap revenge
against the Sudoku puzzles I've struggled with.
Sudoku is a puzzle where you fill numbers into a grid. The history is that it originated in the Dell puzzle
magazine in the 1970's, and later became very popular in Japan, possibly filling the niche that crossword
puzzles play in English newspapers, as the Japanese language is not suited to crossword puzzles. Sometime
around 2005 it because a worldwide sensation. (See the Wikipedia page for the full story.)
The Sudoku rules are: fill the empty squares in the 9x9 grid so that each square contains a number in the
range 1.
.9. In each row and each column across the grid, the numbers 1.
.9 much appear just once ("Sudoku"
translating roughly as "single"). Likewise, each of the nine 3x3 squares that make up the grid must contain
the just numbers 1.
.9.
Here is an easy Sudoku puzzle. I can solve this one in about 5 minutes. Look at the topmost row. It is only
missing 1 and 7. Looking down the columns, you can figure out where the 1 and 7 go in that row. Proceed
in that way, looking at rows, columns, and 3x3 squares that are mostly filled in, gradually figuring out the
empty squares. A common technique is to write the numbers that might go in a square in small letters at the
top of the square, and write in a big number when it's really figured out. Solve this puzzle to get a feel for
how the game works (the solution is shown on the next page).
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Sudoku Strategy
There are
many
ways to solve Sudoku. We will use the following approach which is a sort of OOP
interpretation of classic recursive backtracking search. Call each square in the puzzle a "spot". We want to
do a recursive backtracking search for a solution, assigning numbers to spots to find a combination that
works. (If you are rusty with recursion, see the practice recursion problems at javabat.com).
•
When assigning a number to a spot, never assign a number that, at that moment, conflicts
with the spot's row, column, or square. We are upfront careful about assigning legal
numbers to a spot, rather than assigning any number 1.
.9 and finding the problem later in
the recursion. Assume that the initial grid is all legal, and make only legal spot
assignments thereafter.
•
There are 81 spots in the game. You could try making assignments to the blank spots in
This is the end of the preview. Sign up
to
access the rest of the document.
 Winter '08
 JIMENEZ

Click to edit the document details