Big Project II: Sudoku

In this project you will write a program to solve a generalized version of Sudoku puzzles and analyze the complexity of your solver algorithm.

Generalized Sudoku

Normal sudoku has a 3-by-3 board made up of 3-by-3 panels, which all together it is a 9-by-9 board, and in the solution, each row, column, and panel must contain each number 1 to 9. This can be generalize this way: n-sudoku is a game like normal sudoku except that it has an n-by-n board made up of n-by-n panels (all together an n2-by-n2 board) and in the solution, each row, column, and panel must contain each number 1 to n2.

Your task is to come up with an algorithm for solving n-sudoku puzzles, given n and certain entries on the board, and to give a careful and rigorous complexity analysis of your algorithm in terms of n (or n2, the actual size of the data; you may assume a change of variables m = n2).

Code base

Your program will have a graphical user interface which will allow the user to enter element from a sudoku puzzle and click a "solve" button. The window will look like this:

I have provided the GUI code for you in a tar file. Copy it from the class directory:

cp /homeemp/tvandrun/pub/445/sudoku.tar .
tar xvf sudoku.tar

Formally, you must write a class that implements the ActionListener interface, meaning it needs to have a public method actionPerformed(ActionEvent e). This method indicates the user has clicked the solve button. You may ignore the parameter e. You can interact with the board through an object of type SudokuWI ("WI" is for "window interface") that has methods

    public int getValueAt(int i, int j);
    public void setValueAt(int i, int j, int x);
    public JTextField getFieldAt(int i, int j) ;
    public void registerListener(ActionListener al);
    public int getNSize();

getValueAt() gets the value at position i, j. setValueAt() sets the value at a position, and will therefore make the number x appear on the board. If you are familiar with Java's GUI libraries (Swing and AWT), you may want to manipulate the JTextFields directly, for which you may use getFieldAt(). getNSize() gets the size (for normal Sudoku, the "size" is 3, not 9).

Your solver (ActionListener) class should have a constructor with a SudokuWI parameter-- that is how your class will know the window. Immediately in the constructor your class should register itself as an ActionListener for that window. To make things simple, just make sure you have an instance variable and constructor like this:

     private SudokuWI window;

     public YourSudokuSolverClass(SudokuWI window) {
           this.window = window;
           window.registerListener(this);
     }

I've tried to make this as simple as possible, so do not need to know anything about GUI or event-driven programming. But if any of this code is confusing, do not hesitate to ask for help or an explanation.

The main class is Sudoku.java. It currently contains a line

        ActionListener solver = new SolverVD(window);

SolverVD is a (brute force) solution I've written for this. I'm providing the class file for you to compare with when you test your solution, but I'm not giving you the source code. (The classes BoxPanel and BoxAgent are part of my solution.) You should replace SolverVD with your class. That is the only change you should make in the code I am giving you.

To turn in

Turn in well-documented code (see here for some documentation guidelines; you do not have to follow these to the letter, but your code should be documented somewhat along these lines). Also, give a short (half page or so?) explanation of your algorithm, including the data structures you use. Finally, give a detailed, careful, and rigorous analysis of your code, noting the costs of the operations on your data structures. You may need to use amortized analysis at some points.

DUE: Monday, Dec 4


Thomas VanDrunen