The post lab 9 assignment is to create a sudoku program. The program will need to read in text files that represent sudoku puzzles and verify if they are valid. Nobody wants to try to solve a puzzle with no solutions. The file SudokuPuzzle.java contains a class that represents a sudoku puzzle. It has a constructor that reads a puzzle from a file. The constructor calls the method numberOfSolutions
in order to verify that a puzzle is valid. You must complete this method.
Zeros in the puzzle file represent cells that the user can specify. The numberOfSolutions
method should begin by searching for the first element in the puzzle array that is a zero. It should then iterate through all possible inputs until it finds a valid value for the cell. Next, it should copy the puzzle array and place it on a stack along with the row and column for the cell that was just modified. It should then find the next zero and repeat. If it is able to put a valid value in the last cell of the array, then it has found a solution. If it there are no valid values for a particular cell it can backtrack to a previous state by popping the top of the stack. If the method finds more than one solution, it can stop searching for solutions and return 2. If it finds no solutions, it should return 0 and if it finds one solution, it should return 1.
In order to help write the numberOfSolutions
method, you should create a private method validMove
that when given a puzzle array, a cell row, a cell column, and a value to place into the specified cell, it returns true if the value is a valid move and false otherwise. This method would benefit from three private helper methods that test each of the three conditions that would cause a move to be invalid. One to test if the row already has the specified value, one to test if the column already has the specified value, and one to test if the block already has the specified value.
Use the JUnit testing file SudokuPuzzleTest.java and the puzzle file Puzzles.tar.gzto test your code and use Checkstyle to verify formatting.
Submission: Tar and submit your code on the course Inquire site.