Sudoku generator OpenSudokuLib

What is OpenSudokuLib?

Sudoku is a logic puzzle in a 9×9 square, where all columns, all rows and all 3×3 inner squares of the sudoku have to be filled with the numbers from 1 to 9. In each of these columns, rows and inner squares the numbers from 1 to 9 have to appear exactly once. OpenSudokuLib is a small and simple sudoku generator written in Java to create 9×9 sudoku squares using a backtracking method to find valid cases.

You can use this sudoku generator for free and modify it under the terms of the GNU General Public License.

Note! This project has been renamed recently from JMLSudoku to OpenSudokuLib.

How to use the sudoku generator

To generate a valid sudoku case just create an instance of class Sudoku and call method createSudoku().

Sudoku MySudoku = new Sudoku();

The fields of the case are only editable by the getter and setter methods. Be aware about the fields are indexed from 0 to 8 by the sudoku generator. So when using the mentioned getter and setter methods you have to keep this in mind.


To copy the sudoku case, simply create another instance of the class passing a sudoku case as an image.

For example: To create instance MySudokuCopy from instace MySudoku you can execute the following:

Sudoku MySudokuCopy = new Sudoku(MySudoku);

You can also make usage of the method copySudoku(Sudoku) which returns an identical Sudoku image.

For example: To make instance AnotherCopy an identical copy of MySudoku you can execute the following:

Sudoku AnotherCopy = new Sudoku();

The method deleteFields(int deletedFields) will delete the number of fields passed as an argument. The method won’t return anything. The fields of the case will be rated to mark all fields which should be fixed to avoid multiple solutions due to mirrored fields. These are the so called hint fields which won’t be considered during the deletion of the fields, when using the deleteFields method.

For example: To delete 45 fields of the 81 fields of the whole sudoku execute the following:


To access the fields you can use the getter and setter methods of class Sudoku. The method getHintField(row,column) will return the information whether the field will be considered during the deletion of the fields or not.

int MySudoku.getField(int row, int column);
MySudoku.setField(int row, int column, int value);
int MySudoku.getHintField(int row, int column);

Examples: The first method will set the value of the field in the first row and last column to 4. The second method will return the value of the field in the first row and last column. The last method will return 0 whenever the field can be deleted as it is not involved in any mirroring issue, which would cause a multiple solution. In addition it will return 1 whenever the respective field is involved in a horizontal mirroring and 2 whenever it is involved in a vertical mirroring.

MySudoku.setField(0, 8, 4);

int field = MySudoku.getField(0, 8);

int fieldSolution = MySudoku.getHintField(0, 8);

You can check the validity of the whole sudoku case with the method checkSudoku(boolean exitOnFreeField). Setting exitOnFreeField = true the method will stop analysing if a free field is found.


boolean isValid = MySudoku.checkSudoku(true);

See also the example Applet which has been built with this sudoku generator.

Download source from the download area of this website