Tutorial

Tutorial

1. Simple puzzle generation

 


import org.mariuszgromada.math.janetsudoku.*;

...

SudokuGenerator sg = new SudokuGenerator();
int[][] puzzle = sg.generate();
SudokuStore.consolePrintBoard(puzzle);

Result


# Sudoku puzzle

+-------+-------+-------+
| . . 8 | . . 5 | . . . |
| . . . | . . . | 1 . 4 |
| . . 4 | . . 3 | . . . |
+-------+-------+-------+
| . 2 . | 9 . . | . 5 . |
| . . . | . . . | . 4 2 |
| . 5 . | 6 . 4 | . . . |
+-------+-------+-------+
| 9 . . | . 7 . | 8 . . |
| 6 . . | . 9 . | . . . |
| . . . | . 5 . | . 3 1 |
+-------+-------+-------+

# One line definition:
# ..8..5.........1.4..4..3....2.9...5........42.5.6.4...9...7.8..6...9........5..31
# 008005000000000104004003000020900050000000042050604000900070800600090000000050031

 

2. Random puzzle generation (using parameters)

 


import org.mariuszgromada.math.janetsudoku.*;

...

SudokuGenerator sg = new SudokuGenerator(SudokuGenerator.PARAM_GEN_RND_BOARD);
int[][] puzzle = sg.generate();
SudokuStore.consolePrintBoard(puzzle);

Result


# Sudoku puzzle

+-------+-------+-------+
| 8 7 . | . . . | . . 9 |
| . . . | 5 6 . | . . 7 |
| 5 . . | . 8 1 | . . . |
+-------+-------+-------+
| . . . | . . . | 4 9 1 |
| . . . | . 2 3 | . . . |
| 7 4 . | . . . | . . . |
+-------+-------+-------+
| . . 5 | 3 . . | . 6 . |
| 2 . 3 | . . . | . 8 . |
| . . . | 1 . 8 | . . . |
+-------+-------+-------+

# One line definition:
# 87......9...56...75...81.........491....23...74.........53...6.2.3....8....1.8...
# 870000009000560007500081000000000491000023000740000000005300060203000080000108000

 

3. Puzzle generation + showing difficulty rating

 


import org.mariuszgromada.math.janetsudoku.*;

...

SudokuGenerator sg = new SudokuGenerator();
int[][] puzzle = sg.generate();
int rating = SudokuStore.calculatePuzzleRating(puzzle);
SudokuStore.consolePrintBoard(puzzle);
SudokuStore.consolePrintln("Puzzle rating: " + rating);

Result


# Sudoku puzzle

+-------+-------+-------+
| . . . | . . . | 4 1 . |
| . . . | 6 . 2 | 5 . . |
| 8 6 9 | . . . | . . . |
+-------+-------+-------+
| . 3 . | . . 8 | . . . |
| . . . | . . 5 | 2 4 . |
| 9 . . | 1 . . | . 3 . |
+-------+-------+-------+
| . . . | 4 6 . | . . 7 |
| 6 . . | . . . | . . 3 |
| 1 7 5 | . 2 . | . . . |
+-------+-------+-------+

# One line definition:
# ......41....6.25..869.......3...8........524.9..1...3....46...76.......3175.2....
# 000000410000602500869000000030008000000005240900100030000460007600000003175020000

Puzzle rating: 119

 

4. Solving puzzle from provided list of puzzle examples

 


import org.mariuszgromada.math.janetsudoku.*;

...

SudokuSolver ss = new SudokuSolver( SudokuPuzzles.PUZZLE_EXAMPLE_001 );
SudokuStore.consolePrintBoard( ss.getBoard() );
ss.solve();
SudokuStore.consolePrintBoard( ss.getSolvedBoard() );

Result

# Sudoku puzzle

+-------+-------+-------+
| 5 3 . | . 7 . | . . . |
| 6 . . | 1 9 5 | . . . |
| . 9 8 | . . . | . 6 . |
+-------+-------+-------+
| 8 . . | . 6 . | . . 3 |
| 4 . . | 8 . 3 | . . 1 |
| 7 . . | . 2 . | . . 6 |
+-------+-------+-------+
| . 6 . | . . . | 2 8 . |
| . . . | 4 1 9 | . . 5 |
| . . . | . 8 . | . 7 9 |
+-------+-------+-------+

# One line definition:
# 53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79
# 530070000600195000098000060800060003400803001700020006060000280000419005000080079

# Janet-Sudoku-v.1.1.0, 2016-04-17 20:20:28


# Sudoku puzzle

+-------+-------+-------+
| 5 3 4 | 6 7 8 | 9 1 2 |
| 6 7 2 | 1 9 5 | 3 4 8 |
| 1 9 8 | 3 4 2 | 5 6 7 |
+-------+-------+-------+
| 8 5 9 | 7 6 1 | 4 2 3 |
| 4 2 6 | 8 5 3 | 7 9 1 |
| 7 1 3 | 9 2 4 | 8 5 6 |
+-------+-------+-------+
| 9 6 1 | 5 3 7 | 2 8 4 |
| 2 8 7 | 4 1 9 | 6 3 5 |
| 3 4 5 | 2 8 6 | 1 7 9 |
+-------+-------+-------+

# One line definition:
# 534678912672195348198342567859761423426853791713924856961537284287419635345286179
# 534678912672195348198342567859761423426853791713924856961537284287419635345286179

 

5. Saving puzzle


import org.mariuszgromada.math.janetsudoku.*;

...

String tmpDir = FileX.getTmpDir();
SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-1.txt");
SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-2.txt", "This is a head comment");
SudokuStore.saveBoard(SudokuPuzzles.PUZZLE_EXAMPLE_001, tmpDir + "sudoku-board-ex-3.txt", "This is a head comment", "And a tail comment");
SudokuSolver ss = new SudokuSolver(1);
ss.solve();
ss.saveSolvedBoard(tmpDir + "sudoku-board-ex-sol.txt", "Solution for the PUZZLE_EXAMPLE_001");

6. Puzzle modification - i.e. rotation


import org.mariuszgromada.math.janetsudoku.*;

...

int[][] puzzle = SudokuStore.getPuzzleExample(0);
int[][] puzzleRotated = SudokuStore.rotateClockWise(puzzle);
SudokuStore.consolePrintBoard(puzzle);
SudokuStore.consolePrintBoard(puzzleRotated);

Result


# Sudoku puzzle

+-------+-------+-------+
| . 9 . | . 7 . | . . . |
| . . 1 | . . 2 | . . 8 |
| . 2 . | 9 . . | 3 . . |
+-------+-------+-------+
| . . 8 | . . . | . 4 . |
| . 1 . | . . 9 | . . 5 |
| . . 5 | 6 . . | . . . |
+-------+-------+-------+
| . . . | 7 . . | 4 . . |
| 1 . . | . . 8 | . . 9 |
| . . . | . 3 . | . 6 . |
+-------+-------+-------+

# One line definition:
# .9..7......1..2..8.2.9..3....8....4..1...9..5..56........7..4..1....8..9....3..6.
# 090070000001002008020900300008000040010009005005600000000700400100008009000030060

# Janet-Sudoku-v.1.1.0, 2016-04-17 20:36:23


# Sudoku puzzle

+-------+-------+-------+
| . 1 . | . . . | . . . |
| . . . | . 1 . | 2 . 9 |
| . . . | 5 . 8 | . 1 . |
+-------+-------+-------+
| . . 7 | 6 . . | 9 . . |
| 3 . . | . . . | . . 7 |
| . 8 . | . 9 . | . 2 . |
+-------+-------+-------+
| . . 4 | . . . | 3 . . |
| 6 . . | . . 4 | . . . |
| . 9 . | . 5 . | . 8 . |
+-------+-------+-------+

# One line definition:
# .1...........1.2.9...5.8.1...76..9..3.......7.8..9..2...4...3..6....4....9..5..8.
# 010000000000010209000508010007600900300000007080090020004000300600004000090050080

# Janet-Sudoku-v.1.1.0, 2016-04-17 20:36:23

 

7. Starting regression / unit tests

JAVA

 

java -cp janetsudoku.jar org.mariuszgromada.math.janetsudoku.regtests.RegTests
java -cp janetsudoku.jar org.mariuszgromada.math.janetsudoku.regtests.RegTestsApi
java -cp janetsudoku.jar org.mariuszgromada.math.janetsudoku.regtests.RegTestsGenerator
java -cp janetsudoku.jar org.mariuszgromada.math.janetsudoku.regtests.RegTestsSolver
java -cp janetsudoku.jar org.mariuszgromada.math.janetsudoku.regtests.RegTestsStore
java -cp janetsudoku.jar org.mariuszgromada.math.janetsudoku.tutorial.Tutorial

 

.NET from PowerShell

[Reflection.Assembly]::LoadFile("full\exact\path\to\janetsudoku.dll")
[org.mariuszgromada.math.janetsudoku.regtests.RegTests]::Start()
[org.mariuszgromada.math.janetsudoku.regtests.RegTestsApi]::Start()
[org.mariuszgromada.math.janetsudoku.regtests.RegTestsGenerator]::Start()
[org.mariuszgromada.math.janetsudoku.regtests.RegTestsSolver]::Start()
[org.mariuszgromada.math.janetsudoku.regtests.RegTestsStore]::Start()
[org.mariuszgromada.math.janetsudoku.tutorial.Tutorial]::Start()

 

8. Starting Sudoku Demo App

JAVA

 

java -jar janetsudoku.jar
java -cp janetsudoku.jar org.mariuszgromada.math.janetsudoku.demoapp.JanetSudoku

 

.NET from PowerShell

[Reflection.Assembly]::LoadFile("full\exact\path\to\janetsudoku.dll")
[org.mariuszgromada.math.janetsudoku.demoapp.JanetSudoku]::Start()