CIT 590 Assignment 10: Sudoku
Spring 2009, David Matuszek

Purposes of this assignment:

General idea of the assignment:

In a Sudoku puzzle, you are given a 9x9 grid, divided into nine 3x3 "boxes," as shown on the right. Some of these boxes have digits in them.

The puzzle is to fill in the rest of the grid so that every row, every column, and every 3x3 box contains the digits 1 through 9.Your assignment is to write a program to let a user solve Sudoku puzzles.

The user should be able to:

  • Load in a puzzle from a text file. The format will be: Nine lines, with each line containing nine integers. "Blanks" will be indicated by the number zero. For example, the first two lines for the puzzle at the right would be
    0 6 0 1 0 4 0 5 0
    0 0 8 3 0 5 6 0 0
  • Enter a puzzle (say, one from a newspaper) by typing in numbers. Once it has been entered, the user can try to solve it.

  • Solve a puzzle by typing in numbers.

You do not need to make up your own Sudoku puzzles--you can find lots of them on the web.

Your program does not need to solve the puzzles, but it does have to be able to check whether the user has solved the puzzle.

 
Roll your mouse on and off the grid.
You may have to wait for the page to load fully

This image is from http://www.sudoku.com/, which is a very nice site with additional explanations and puzzles (and a Sudoku program for sale).

Details:

class Sudoku extends JFrame

The GUI will display a 9x9 grid of TextFields, along with all the controls necessary to allow the user to load, enter, or solve puzzles. It will also have an int[9][9] array, corresponding to the 81 TextFields, that it uses to communicate with the SudokuModel object.

You only need one GUI, but it should be clear from looking at the GUI whether the user should be entering a puzzle, or trying to solve one.

Each time the user types something into a TextField (whether the user is entering a puzzle, or trying to solve one), the field should turn red if the entry is illegal (either it isn't a blank or a digit 1..9, or it's a digit but there's a conflict with some other digit).

class SudokuModel

Write a class SudokuModel with at least the following methods, and a JUnit test method (at least one) for each one.

public SudokuModel(int[][] anArray)
The constructor for your SudokuModel class should be given access to the int[9][9] array in the Sudoku class.
public int[][] getBox(int boxRow, int boxColumn)
Returns a 3x3 array representing a "box" of the 9x9 array. The parameters boxRow and boxColumn are in the range 0 to 2, since there are three rows and three columns of "boxes."
public boolean occursInRow(int digit, int row)
Returns true if the given digit (which must be 1..9) occurs in the given row (rows are 0..8), and false otherwise.
public boolean occursInColumn(int digit, int column)
Returns true if the given digit (which must be 1..9) occurs in the given column (columns are 0..8), and false otherwise.
public boolean occursInBox(int digit, int row, int column)
Returns true if the given digit (which must be 1..9) occurs in the box containing the location at the given row and column of the 9x9 array, and false otherwise. Note that this method is given a row and column in the complete 9x9 array, but must search for the given digit in the box containing that (row, column) location.
public boolean occursInBox(int digit, int[][] box)
Returns true if the given digit (which must be 1..9) occurs in the given 3x3 box, and false otherwise.
public boolean isLegalDigit(int digit, int row, int column)
Returns true if the given digit (which must be 1..9) does not occur in the given row, or in the given column, or in the box containing this row and column, and false otherwise. That is, this digit is a possible candidate for putting in this location; there may be other candidates.
public boolean isSolved()
Returns true if the puzzle is complete and correctly solved.

There is some redundancy in the methods, and you might not use them all. That's OK; write them all and test them all, but use what you need.

Remember the DRY principle, and Don't Repeat Yourself. If one method can make use of another method in order to avoid duplicating code, it should do so. Feel free to add any additional methods you like; any additional methods should either be private, or should have a JUnit test..

Structure of the assignment:

The above are requirements. You may have additional classes and methods as needed, and they should be documented and unit tested as appropriate.

Due date:

Thursday, March 9, before midnight. Zip up all files and turn in one copy for your team via Blackboard.