GithubHelp home page GithubHelp logo

quanhua-guan / sudokugeneratorandsolver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nguyennd97/sudokugeneratorandsolver

0.0 1.0 0.0 762 KB

sudoku generator and solver library for Java. Can custom form of puzzle. Supported: 4x4, 5x5, 6x6, 7x7, 8x8, 9x9, 12x12, 16x16, 25x25. Please give me a star if it could help you!

License: Apache License 2.0

Java 100.00%

sudokugeneratorandsolver's Introduction

Contents

Generator

To generate new puzzle use Generator.generate(size of puzzle, maximum time, minimum score, maximum score) In which size of puzzle is length of puzzle edge. Example 9x9 is 9, 25x25 is 25, ..etc.

Example:

// register new form to generate puzzle
// generator will select random form of registered forms 
// and generate a puzzle with that form
Puzzle7.registerForm(new Puzzle7.Form(new int[][]{
    {0, 0, 1, 1, 1, 1, 2},
    {0, 0, 0, 1, 1, 1, 2},
    {3, 0, 0, 4, 4, 2, 2},
    {3, 3, 4, 4, 4, 2, 2},
    {3, 3, 4, 4, 6, 6, 2},
    {3, 5, 5, 5, 6, 6, 6},
    {3, 5, 5, 5, 5, 6, 6}
}));
// generate 7x7 puzzle within 5 seconds with minimum score of 10 and maximum score of 1.000.000
Game game = Generator.generate(7,  5 * 1000, 10, 1000000);
// score of generated puzzle
System.out.println("Score: " + game.getScore());
// puzzle
System.out.println("Puzzle");
System.out.println(game.getQuestion());
// solved puzzle
System.out.println("Solved");
System.out.println(game.getAnswer());
// Form of puzzle (return null if classic form)
// use game.getQuestion().isClassicForm() to check classic form.
game.getQuestion().getForm();

Using specified form:

// generate 7x7 puzzle within 5 seconds with minimum score of 10 and maximum score of 1.000.000, specify which form will be use 
Game game = Generator.generate(7, 5 * 1000, 10, 1000000, new int[][]{
     {0, 0, 1, 1, 1, 1, 2},
     {0, 0, 0, 1, 1, 1, 2},
     {3, 0, 0, 4, 4, 2, 2},
     {3, 3, 4, 4, 4, 2, 2},
     {3, 3, 4, 4, 6, 6, 2},
     {3, 5, 5, 5, 6, 6, 6},
     {3, 5, 5, 5, 5, 6, 6}
     });
// or we can use one of default forms
Game game = Generator.generate(7, 5 * 1000, 10, 1000000, Puzzle7.FORM2.toArray());
        
// score of generated puzzle
System.out.println("Score: " + game.getScore());
// puzzle
System.out.println("Puzzle");
System.out.println(game.getQuestion());
// solved puzzle
System.out.println("Solved");
System.out.println(game.getAnswer());
// Form of puzzle (return null if classic form)
// use game.getQuestion().isClassicForm() to check classic form.
game.getQuestion().getForm();

Solver

To solve a puzzle (example 6x6)

  • create new empty puzzle by Puzzle6 puzzle = new Puzzle6() with classic form or Puzzle6 puzzle = new Puzzle6(Puzzle6.FORM1) if use custom form 1 of Puzzle 6. And
  • set board for puzzle by puzzle.setBoard, if square is empty, set it 0.
  • use PuzzleSolver.solve(puzzle, time) to solve puzzle and return the solved puzzle.

Example

Puzzle7 puzzle = new Puzzle7(Puzzle7.FORM2);
puzzle.setBoard(new int[][]{
        {0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 5},
        {0, 0, 0, 0, 0, 7, 0},
        {0, 2, 7, 0, 0, 4, 0},
        {6, 0, 0, 5, 0, 0, 0},
        {0, 0, 2, 3, 0, 0, 0},
});

Puzzle solved = PuzzleSolver.solve(puzzle, 5 * 1000);
System.out.println(solved);

Score

Score of sudoku puzzle is defined

  • < 100 : know the rule
  • > 100 and < 1000 : know some techniques
  • > 1000 : requires flat-out trial (score / 1000 is the number of tries)

The Puzzle with higher score is harder to solve.

To show the puzzle as string, use puzzle.toString()

To get score of a puzzle, use puzzle.difficultyScore(answer) where answer is the solved puzzle.

Puzzle Form

Puzzle form is form of all boxes inside the puzzle. The first box index is 0.

For example, the classic form of 9x9 sudoku is

[
    [0, 0, 0, 1, 1, 1, 2, 2, 2],
    [0, 0, 0, 1, 1, 1, 2, 2, 2],
    [0, 0, 0, 1, 1, 1, 2, 2, 2],
    [3, 3, 3, 4, 4, 4, 5, 5, 5],
    [3, 3, 3, 4, 4, 4, 5, 5, 5],
    [3, 3, 3, 4, 4, 4, 5, 5, 5],
    [6, 6, 6, 7, 7, 7, 8, 8, 8],
    [6, 6, 6, 7, 7, 7, 8, 8, 8],
    [6, 6, 6, 7, 7, 7, 8, 8, 8]
]
0 0 0 1 1 1 2 2 2
0 0 0 1 1 1 2 2 2
0 0 0 1 1 1 2 2 2
3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5
6 6 6 7 7 7 8 8 8
6 6 6 7 7 7 8 8 8
6 6 6 7 7 7 8 8 8

And one custom form is

[
    [0, 0, 0, 0, 1, 1, 1, 2, 2],
    [0, 0, 0, 1, 1, 2, 2, 2, 2],
    [3, 0, 3, 1, 1, 1, 1, 2, 2],
    [3, 0, 3, 4, 4, 4, 5, 5, 2],
    [3, 3, 3, 4, 4, 4, 5, 5, 5],
    [6, 3, 3, 4, 4, 4, 5, 8, 5],
    [6, 6, 7, 7, 7, 7, 5, 8, 5],
    [6, 6, 6, 6, 7, 7, 8, 8, 8],
    [6, 6, 7, 7, 7, 8, 8, 8, 8]
]
0 0 0 0 1 1 1 2 2
0 0 0 1 1 2 2 2 2
3 0 3 1 1 1 1 2 2
3 0 3 4 4 4 5 5 2
3 3 3 4 4 4 5 5 5
6 3 3 4 4 4 5 8 5
6 6 7 7 7 7 5 8 5
6 6 6 6 7 7 8 8 8
6 6 7 7 7 8 8 8 8

Supported Form

4x4

Support Form Register

  • Classic
0 0 1 1
0 0 1 1
2 2 3 3
2 2 3 3
  • Form 1
0 0 0 1
0 2 1 1
2 2 1 3
2 3 3 3
  • Form 2
0 0 0 1
0 1 1 1
2 2 2 3
2 3 3 3

5x5

Support Form Register

  • Form 1
0 0 0 1 1
0 0 2 1 1
3 2 2 2 1
3 3 2 4 4
3 3 4 4 4
  • Form 2
0 0 0 0 1
3 0 2 1 1
3 2 2 2 1
3 3 2 4 1
3 4 4 4 4
  • Form 3
0 0 0 0 1
3 2 2 0 1
3 3 2 1 1
3 4 2 2 1
3 4 4 4 4

6x6

Support Form Register

  • Classic
0 0 0 1 1 1
0 0 0 1 1 1
2 2 2 3 3 3
2 2 2 3 3 3
4 4 4 5 5 5
4 4 4 5 5 5
  • Form 1
0 0 0 0 1 1
0 0 2 1 1 1
2 2 2 1 3 3
2 2 4 3 3 3
4 4 4 3 5 5
4 4 5 5 5 5
  • Form 2
0 0 0 0 0 1
2 0 2 1 1 1
2 2 2 1 1 3
2 4 4 3 3 3
4 4 4 3 5 3
4 5 5 5 5 5

7x7

Support Form Register

  • Form 1
0 0 0 1 1 1 1
0 0 0 1 1 1 4
2 2 0 3 3 4 4
2 2 3 3 3 4 4
2 2 3 3 6 4 4
2 5 5 5 6 6 6
5 5 5 5 6 6 6
  • Form 2
0 0 1 1 1 1 2
0 0 0 1 1 1 2
3 0 0 4 4 2 2
3 3 4 4 4 2 2
3 3 4 4 6 6 2
3 5 5 5 6 6 6
3 5 5 5 5 6 6
  • Form 3
0 0 0 1 2 2 2
0 0 1 1 1 1 2
0 0 4 1 4 1 2
3 3 4 4 4 2 2
3 5 4 5 4 6 6
3 5 5 5 5 6 6
3 3 3 5 6 6 6
  • Form 4
0 0 0 1 1 2 2
0 0 1 1 1 2 2
0 0 3 1 1 2 2
4 4 3 3 3 2 5
4 4 3 3 3 5 5
4 4 6 6 6 5 5
4 6 6 6 6 5 5
  • Form 5
0 0 0 0 0 0 1
2 2 3 3 0 1 1
2 2 2 3 1 1 4
2 5 5 3 1 1 4
2 5 5 3 4 4 4
5 5 6 3 3 4 4
5 6 6 6 6 6 6

8x8

Support Form Register

  • Classic
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
2 2 2 2 3 3 3 3
2 2 2 2 3 3 3 3
4 4 4 4 5 5 5 5
4 4 4 4 5 5 5 5
6 6 6 6 7 7 7 7
6 6 6 6 7 7 7 7

9x9

Support Form Register

  • Classic
0 0 0 1 1 1 2 2 2
0 0 0 1 1 1 2 2 2
0 0 0 1 1 1 2 2 2
3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5
6 6 6 7 7 7 8 8 8
6 6 6 7 7 7 8 8 8
6 6 6 7 7 7 8 8 8

12x12

Support Classic Form Only

  • Classic

16x16

Support Classic Form Only

25x25

Support Classic Form Only

See Example: https://github.com/orismaster/SudokuGeneratorAndSolver/blob/master/src/vn/com/orismaster/Example.java

Supported:

  • 4x4
  • 5x5
  • 6x6
  • 7x7
  • 8x8
  • 9x9
  • 12x12
  • 16x16
  • 25x25

sudokugeneratorandsolver's People

Contributors

nguyennd97 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.