CPSC 170 Post Lab 9
Sudoku
Due Before Class on Tuesday, March 27th

Sudoku is a puzzle that has been found in most newspapers since it gained popularity in 2005. Typically, the puzzle is played with a 9x9 grid that is subdivided into nine 3x3 boxes. The object of the puzzle is to fill the grid such that the digits 1 to 9 are contained (without repeats) in each row, each column, and each box. Seed digits are initially placed in the grid to define the puzzle and to limit where additional digits can be added by the person solving the puzzle. When trying to solve a Sudoku, people rely on logical reasoning to determine where certain digits can and cannot be placed in the grid.

Details

Create a program that reads a sudoku puzzle from a text file that is specified as a command line argument. The program should print whether there are no solutions, one solution, or multiple solutions to the specified sudoku puzzle. If the user does not specify a file to read, if the file does not exist, or if the file is not properly formatted, the program should inform the user of the specific problem.

You should add a constructor to the sudoku class that takes a string that is the name of a file that contains a puzzle. The constructor should read the file and initialize the puzzle instance data. If there is an error reading the file or if the file is not well formatted the constructor should throw an exception. The main program should catch the exception and inform the user of the problem.

You should also add a numberOfSolutions method to the sudoku class. This method should use backtracking, implemented using either recursion or a stack, and return an int representing the number of solutions to a puzzle. Note, this method does not need to determine the actual number of solutions, only if there are 0, 1, or more. The method should return 0 if there are no solutions, 1 if there is one solution, and 2 if there is more than one solution. The zip file Puzzles.zip contains three puzzle text files that you can use to test your program. One of the puzzles has no solution, one has one solution, and one has multiple solutions.


Submission: Submit your code as a zip file with your name as the zip file name on the course Inquire site.

Extra

Puzzle Generation

Write a program that can generate a random puzzle with only one solution. The program should begin by creating a puzzle that is completely empty. Then program should choose a random cell and generate a random seed value. If the puzzle has no solutions, the cell should be set back to its previous value and a different random cell should be selected. If the puzzle has multiple solutions, then another random cell should be selected. This should proceed until the puzzle has only one solution. Note, this algorithm has a flaw, sometimes it will generate an infinite loop. Can you write a version that will never fall into an infinite loop?

Command Line Game

Write a command-line sudoku game. The game should display the puzzle for the player and ask for a move (a row, column, and value). If the move is legal (use the validMove method) and it is correct (use the numberOfSolutions method) it should add modify the puzzle and prompt the user for another move. If the move is not legal or correct the program should inform the user and prompt the user for a different move. The program should also determine if the user completed the puzzle and stop the loop with a congratulatory message. A harder version of the game would end the loop and tell the player they lost as soon as they make a move that is not correct. A more frustrating version of the game would let the user make incorrect moves and inform the player they lost when it is no longer possible to make a valid move.

Graphical Game

Write a graphical sudoku game. Displaying a sudoku board in a graphical program is easy, but getting user input can be difficult. You could can it with mouse clicks, key board input, text fields, or drop down menus. Devise a way that is user friendly and implement it. The program should verify user input and determine if the player has won or lost as in the command line game.