Skip to content

creitz/SudokuSolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SudokuSolver

Simple command line utility and java components for solving 4-, 9-, 16-, or 25-Sudoku puzzles. The solver is theoretically already capable of handling boards larger than 25, though it is untested.

Downloading

To download, you can clone the source from GitHub:

$ git clone [email protected]:creitz/SudokuSolver.git
$ cd SudokuSolver

Compiling

Once in the cloned directory, run the following commands to create a bin/ directory and compile.

$ mkdir bin
$ javac -d bin/ src/com/creitz/sudokusolver/*

Running from Command Line

$ cd bin/
$ java com/creitz/sudokusolver/SudokuSolver /path/to/file

The /path/to/file should be replaced by the path to the file containing a sudoku board you wish to solve. The solver expects a board formatted as such:

9
* 3 * * * 7 * * *
2 * * * 5 6 * 1 *
* * * 1 9 * * * *
* 5 9 * * * 8 * *
3 * * * * * * * 5
* * 6 * * * 7 9 *
* * * * 4 1 * * *
* 2 * 8 3 * * * 7
* * * 2 * * * 6 *

The *'s indicate an empty square in the board, and the numbers are the values for filled squares. The 9 on the first line indicates that the board will be a 9x9. If you wish to solve a 4x4, the file could containing the following:

4
3 * 1 *
* 1 2 *
* * * *
1 3 * 2

Or for a 16x16:

16
*  *  *  *  13 *  12 *  3  *  5  *  *  8  1  10
1  *  15 4  9  *  16 14 13 12 *  *  6  *  *  3
*  13 *  5  *  *  *  10 *  9  *  2  7  12 *  *
8  *  *  *  *  15 2  *  *  1  6  *  *  *  4  *
*  4  *  *  *  *  *  *  *  *  *  5  2  *  *  14
*  *  1  6  *  *  *  *  15 13 *  14 *  *  16 12
*  12 *  *  *  *  *  8  *  3  *  4  1  *  7  *
*  *  *  15 *  3  1  *  16 *  *  10 *  4  8  11
*  *  *  *  *  *  8  16 *  4  *  1  *  14 *  *
* 15  *  *  *  6  9  *  10 5  11 *  *  16 12 1
* 11  9  *  *  *  *  *  8  *  *  *  *  2  3  7
13 *  *  *  *  12 11 *  6  2  16 7  *  *  *  * 
*  10 12 *  *  13 3  *  4  *  15 *  16 *  *  5
2  *  4  *  *  *  7  1  *  *  *  6  *  *  14 13
*  *  *  3  8  9  *  *  1  16 2  11 12 10 *  * 
*  *  16 1  12 11 *  *  5  14 *  *  3  7  9  * 

Using the Components

To create a SudokuSolver object and solve a sudoku, you can do the following:

	SudokuSolver solver = new SudokuSolver("filename");
	if (solver.solve()) {
		Grid solutionGrid = solver.getGrid();
		ArrayList<Square> solvedSquares = solutionGrid.getSquares();
		
		/*
		* Do whatever you'd like with the squares
		*/
		
	} else {
		System.out.println("Board does not have a unique solution.");
	}

Alternatively, you can create a grid programmatically instead of with a file, like so:

Grid grid = new Grid(gridSize);
for (int row = 0; row < gridSize; row++) {
	for (int col = 0; col < gridSize; col++) {
		if (squareShouldHaveAValue) {
			grid.setValueAtCoords(row, col, someValue);
		}
	}
}
SudokuSolver solver = new SudokuSolver(grid);
solver.solve();
solver.getGrid();

A square object's value can be retrieved, for example, by

Square firstSquare = solvedSquares.get(0);
int firstValue = firstSquare.getValue();

Alternatively, you can query for the value at a given coordinate

solutionGrid.getValueAtLocation(0, 0);

The top row is row 0, beneath that is row 1, then row 2, etc. The left-most column is column 0, then column 1, etc. The ArrayList of squares that is returned by getSquares() is a wrapped ArrayList, so the first 9 elements are the squares in the top row, then the next 9 elements are, from left to right, the squares in the next row from the top.

So, for example, to retrieve the value of the middle square in the top right quadrant, you could do:

solutionGrid.getValueAtLocation(1, 7);

or:

solvedSquares.get(16);

About

Java utility for solving Sudoku puzzles of various sizes.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages