Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A Sudoku library, which targets .NET Standard 1.0, supports generating and solving game boards

NotificationsYou must be signed in to change notification settings

hoaftq/SudokuLib

Repository files navigation

A Sudoku library, which targets .NET Standard, supports generating and solving game boards

Project SudokuLib is a library containing 2 namespaces whose classes can be used directly depending on your purpose

1. Sudoku.Generator namespace

This is the low level library. It has a generator which can be used to generate new game boards and a solver which can be used to solve game boards with pre-filled values.Both are derived from a generator base GeneratorBase. This class is where the backtracking algorithm implemented. You can use this part of the library to develop your own game or a game solver.

Usages

  • Generator usages

    • Generate a game board
    intx=3,y=2;vargame=newSudokuGenerator(x,y,Processor);game.Generate();boolProcessor(int[][]contents){// Here we have a game board stored in argument contents, we can store or process it (whatever we want)// return false if we want to stop generating a new game board, otherwise the generator continues to generate a different game boardreturnfalse;}
    • Create a permutation
    intx=2,y=2;vargame=newSudokuGenerator(x,y,null);int[]values=game.GetPermutation();

    For example, we have the following goard board

    1234
    3412
    2143
    4321

    When using SudokuGenerator.GetPermuation we will get a random permuation of the set [1, 2, 3, 4], let say [2 4 1 3]
    That means we will transform our game board with this map

    1 ⟶ 2  2 ⟶ 4  3 ⟶ 1  4 ⟶ 3

    Now we have another gameboard

    2413
    1324
    4231
    3142

    In fact, SudokuGenerator.Generate always generates game boards with the first row is a sequence from 1 -> max number.So we have to use a permuation to create a random game board.

    • Create a random mark
    intx=2,y=2,numberOfOpenBoxes=5;vargame=newSudokuGenerator(x,y,null);varmask=game.CreateRandomMask(numberOfOpenBoxes);// If mask[i][j] is true then the value at coordinate (i, j) is revealed to user

    If we have a game board like this

    2413
    1324
    4231
    3142

    and we get the following mask after calling CreateRandomMask

    truetruetruefalse
    falsefalsefalsefalse
    falsefalsetruetrue
    falsefalsefalsefalse

    true means the corresponding value is displayed to user, false means user has to fill it

    then we will display the game board to user like this

    241?
    ????
    ??31
    ????
  • Solver usage

    Provide a gameboard with missing values denoted with 0, SodokuSolver will find complete game boards for us

    intX=3,Y=3;int[][]presetBoard={newint[]{4,0,0,2,0,6,1,7,0},newint[]{0,9,0,5,0,3,0,0,0},newint[]{0,5,0,0,7,0,6,9,0},newint[]{0,0,0,7,3,0,0,1,0},newint[]{0,0,0,0,5,0,2,0,9},newint[]{0,0,0,0,4,1,0,0,6},newint[]{0,4,5,0,6,0,0,0,8},newint[]{0,7,8,4,0,0,5,0,0},newint[]{0,0,6,3,0,5,0,0,0}};varsolver=newSudokuSolver(X,Y,Processor);solver.InitializeBoard(presetBoard);solver.Solve();boolProcessor(int[][]contents){// We have the complete goard board stored in agument contents here// return true if we want to find all solutions, otherwise it will stop finding after getting the first onereturntrue;}

2. SudokuLib.Game namespace

This is a higher level of the library and it uses Sudoku.Generator behind. It is like back-end of the game, almost forms a game without a view.If you use these classes there, you just need to implement your own game view.

Currently it has 4 classes

  • EnumGameLevel represents game levels
  • GameBox a box which is coressponding to a number in the game board
  • GameBoard provide basic operations to the game board
  • SudokuGame inherits functionality from GameBoard and uses a SudokuGenerator to generate game board

SudokuUWP

There is also a complete Sudoku game based on this library SudokuUWP written using UWP. It is using SudokuGame class for backing process. Here is a screenshot of it

Sudoku game screenshot

To-do list

  • Develope a game solver UI

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp