Sudoku Puzzle Generator
If you can solve sudoku so that will be question is arise in your mind how to making sudoku puzzle or game. This post are is provide basic logic to create sudoku puzzle with specific size of elements.
If you make a game using sudoku puzzle you are need the following points.
Layout : This is user interface, Which are based on graphic design.
Difficulty Levels : We can create [NxN] size of sudoku puzzle. The most standard size of this puzzle is [9x9]. So we can decide its difficulty by applying initial number of fill element. Because less number of given sudoku puzzle are need more time to solve. But it is a basic logic is also depends arrangement of initial fill element.
Test Solution : This is useful to test sudoku solution.
What are our goal?
Our goal is to we create a new random puzzle which initial exist n solve number and the solution of this puzzle create a valid result. In other words, generate a sudoku puzzle and also find there solution.

In above puzzle initial given 27 elements. And zero indicates unsolved element. Sudoku puzzles allow non-repeating numbers of particular row and particular column. So solution of this puzzle are.
3 6 1 5 4 9 7 2 8
9 5 2 3 8 7 1 4 6
4 7 8 1 2 6 5 9 3
8 9 4 7 1 2 6 3 5
2 3 7 9 6 5 8 1 4
6 1 5 4 3 8 2 7 9
5 4 3 8 7 1 9 6 2
7 8 6 2 9 4 3 5 1
1 2 9 6 5 3 4 8 7
For example (size from 10 to 38 and check).
In given below program, is generating new sudoku puzzles with initial user defined element which solution is possible.
// C program
// Sudoku Generator
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 9
//Set default value of sudoku problem and output
void set_default(int grid[N][N], int output[N][N])
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < N; ++i)
{
for (j = 0; j < N; ++j)
{
//Set initial value
grid[i][j] = 0;
output[i][j] = 0;
}
}
}
//Display request elements
void display_data(int request[N][N])
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < N; ++i)
{
for (j = 0; j < N; ++j)
{
printf(" %d", request[i][j]);
}
printf("\n");
}
}
//Check that given number are suitable to particular row and column or not
int is_vaild(int grid[N][N], int row, int col, int num)
{
//Define loop controlling variable
int i = 0;
int j = 0;
// Test case 1
// Check that whether given number are exist in given row and column or not
for (i = 0; i < N; ++i)
{
if (grid[row][i] == num || grid[i][col] == num)
{
//When given number already exists in given row and column
return 0;
}
}
//Test case 2
//Check that given number are exist in 3x3 sub grid or not
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
if (grid[(row - row % 3) + i][(col - col % 3) + j] == num)
{
//When subgrid contains element
//Then it is not valid
return 0;
}
}
}
//When number is valid
return 1;
}
//Find the location of empty element in given grid
int new_empty_location(int grid[N][N])
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < N; ++i)
{
for (j = 0; j < N; ++j)
{
if (grid[i][j] == 0)
{
//When empty location exist
return (N *i) + j;
}
}
}
return -1;
}
//Recursive find solution of given grid
int test_sudoku(int grid[N][N])
{
int location = new_empty_location(grid);
if (location == -1)
{
//When sudoku is complete
return 1;
}
// Define some auxiliary variables
int row = 0;
int col = 0;
int num = -1;
if (location != 0)
{
//Get current change element location
//Get element row
row = location / N;
//Get element column
col = location % N;
}
for (num = 1; num <= N; ++num)
{
if (is_vaild(grid, row, col, num) == 1)
{
//Set this number of location [row][col]
grid[row][col] = num;
if (test_sudoku(grid) == 1)
{
// When sudoku is solved
return 1;
}
//reset value
grid[row][col] = 0;
}
}
return 0;
}
//This is create sudoku problem in given length of element
int create_sudoku(int grid[N][N], int output[N][N], int length)
{
if (length <= 0)
{
return 1;
}
//Get a random row and column
int row = rand() % N;
int col = rand() % N;
if (grid[row][col] == 0)
{
//Get a number from (1 to 9)
int num = (rand() % (N)) + 1;
int counter = 0;
for (counter = 0; counter < N; ++counter)
{
if (is_vaild(grid, row, col, num) == 1)
{
//Set this number of location [row][col]
grid[row][col] = num;
output[row][col] = num;
if (create_sudoku(grid, output, length - 1) == 1)
{
// When sudoku is created
return 1;
}
//reset values
grid[row][col] = 0;
output[row][col] = 0;
}
num++;
if (num > N)
{
num = 1;
}
}
}
else
{
if (create_sudoku(grid, output, length) == 1)
{
// When sudoku is created
return 1;
}
}
return 0;
}
//Generate a sudoku of given length of initial elements
//And also providing its solution
void generate_sudoku(int grid[N][N], int output[N][N], int length)
{
if (length <= 0 || length >= N *N)
{
// When get invalid input
// Valid length are between (1 to 81)
return;
}
// Set initial value of grid and resultant output
set_default(grid, output);
if (create_sudoku(grid, output, length) == 1)
{
//Test solution of sudoku
if (test_sudoku(output) == 1)
{
//When get a valid solution
printf("\n Created sudoku of initial %d elements\n\n", length);
display_data(grid);
printf("\n Solution \n\n");
display_data(output);
}
else
{
//Created sudoku are no solution, Try again ..
generate_sudoku(grid, output, length);
}
}
else
{
printf("\n Sudoku are not create \n");
}
}
int main()
{
srand(time(NULL));
// Capable to store information of sudoku problem
int grid[N][N];
// This is used to get the result of sudoku
int output[N][N];
//Number of initial element in sudoku
int length = 27;
generate_sudoku(grid, output, length);
length = 34;
generate_sudoku(grid, output, length);
return 0;
}
Output
Created sudoku of initial 27 elements
0 1 0 9 0 0 8 6 0
0 0 4 3 0 5 0 0 0
8 0 0 0 0 6 0 9 0
0 0 6 0 2 0 0 7 0
0 0 0 0 0 0 9 2 6
0 0 0 0 0 1 0 3 0
2 9 0 0 0 7 1 4 3
0 0 0 0 0 0 0 5 0
0 0 0 2 1 0 0 0 0
Solution
3 1 7 9 4 2 8 6 5
9 6 4 3 8 5 2 1 7
8 5 2 1 7 6 3 9 4
4 3 6 8 2 9 5 7 1
5 8 1 7 3 4 9 2 6
7 2 9 5 6 1 4 3 8
2 9 8 6 5 7 1 4 3
1 7 3 4 9 8 6 5 2
6 4 5 2 1 3 7 8 9
Created sudoku of initial 34 elements
0 0 0 0 8 0 9 0 6
3 0 0 2 0 0 0 8 0
0 0 0 0 5 0 0 2 0
0 5 0 1 9 0 4 0 8
9 0 6 0 2 8 7 3 1
0 0 0 4 0 3 5 9 2
0 9 8 0 7 0 0 0 0
0 3 4 0 1 0 8 0 0
0 7 2 0 0 0 0 0 0
Solution
4 2 5 3 8 1 9 7 6
3 6 7 2 4 9 1 8 5
8 1 9 7 5 6 3 2 4
2 5 3 1 9 7 4 6 8
9 4 6 5 2 8 7 3 1
7 8 1 4 6 3 5 9 2
1 9 8 6 7 5 2 4 3
6 3 4 9 1 2 8 5 7
5 7 2 8 3 4 6 1 9
/*
Java Program
Sudoku Generator
*/
import java.util.Random;
class SudokuGenerator
{
//Sudoku {N} grid size
public int size;
public Random rand = new Random();
public SudokuGenerator(int size)
{
//Assuming that given size are suitable to sudoku
this.size = size;
this.rand = new Random();
}
public int random_num(int low, int high)
{
return rand.nextInt(high - low) + low;
}
//Set default value of sudoku problem and output
public void set_default(int[][] grid, int[][] output)
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
//Set initial value
grid[i][j] = 0;
output[i][j] = 0;
}
}
}
//Check that given number are suitable to particular row and column or not
public boolean is_vaild(int[][] request, int row, int col, int num)
{
//Define loop controlling variable
int i = 0;
int j = 0;
// Test case 1
// Check that whether given number are exist in given row and column or not
for (i = 0; i < this.size; ++i)
{
if (request[row][i] == num || request[i][col] == num)
{
//When given number already exists in given row and column
return false;
}
}
//Test case 2
//Check that given number are exist in 3x3 sub grid or not
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
if (request[(row - row % 3) + i][(col - col % 3) + j] == num)
{
//When subgrid contains element
//Then it is not valid
return false;
}
}
}
//When number is valid
return true;
}
//Find the location of empty element in given sudoku grid
public int new_empty_location(int[][] request)
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
if (request[i][j] == 0)
{
//When empty element exist
//return it's location
return (i * this.size) + j;
}
}
}
return -1;
}
//Find result
public boolean test_sudoku(int[][] output)
{
int location = new_empty_location(output);
if (location == -1)
{
//When sudoku is complete
return true;
}
// Define some auxiliary variables
int row = 0;
int col = 0;
int num = -1;
if (location != 0)
{
//Get current change element location
//Get element row
row = location / this.size;
//Get element column
col = location % this.size;
}
for (num = 1; num <= this.size; num++)
{
if (is_vaild(output, row, col, num) == true)
{
//Set this number of location [row][col]
output[row][col] = num;
if (test_sudoku(output) == true)
{
// When sudoku is solved
return true;
}
//reset value
output[row][col] = 0;
}
}
return false;
}
//Display grid elements
public void display_data(int[][] request)
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
System.out.print(" " + request[i][j]);
}
System.out.print("\n");
}
}
//This is create sudoku problem in given length of element
public boolean create_sudoku(int[][] grid, int[][] output, int length)
{
if (length <= 0)
{
return true;
}
//Get a random row and column
int row = random_num(0, this.size);
int col = random_num(0, this.size);
if (grid[row][col] == 0)
{
//Get a number from (1 to 9)
int num = random_num(1, this.size);
int counter = 0;
for (counter = 0; counter < this.size; ++counter)
{
if (is_vaild(grid, row, col, num) == true)
{
//Set this number of location [row][col]
grid[row][col] = num;
output[row][col] = num;
if (create_sudoku(grid, output, length - 1) == true)
{
// When sudoku is created
return true;
}
//reset values
grid[row][col] = 0;
output[row][col] = 0;
}
num++;
if (num > this.size)
{
num = 1;
}
}
}
else
{
if (create_sudoku(grid, output, length) == true)
{
// When sudoku is created
return true;
}
}
return false;
}
//Generate a sudoku of given length of initial elements
//And also providing its solution
public void generate_sudoku(int[][] grid, int[][] output, int length)
{
if (length <= 0 || length >= (this.size * this.size))
{
// When get invalid input
// Valid length are between (1 to 81) when size is 9
return;
}
// Set initial value of grid and resultant output
set_default(grid, output);
if (create_sudoku(grid, output, length) == true)
{
//Test solution of sudoku
if (test_sudoku(output) == true)
{
//When get a valid solution
System.out.print("\n Created sudoku of initial " + length + " elements\n\n");
display_data(grid);
System.out.print("\n Solution \n\n");
display_data(output);
}
else
{
//Created sudoku are no solution, Try again ..
generate_sudoku(grid, output, length);
}
}
else
{
//When provide invalid length
System.out.print("\n Sudoku are not create \n");
}
}
public static void main(String[] args)
{
int n = 9;
SudokuGenerator obj = new SudokuGenerator(n);
// Capable to store information of sudoku problem
int[][] grid = new int[n][n];
// This is used to get the result of sudoku
int[][] output = new int[n][n];
//Number of initial element in sudoku
int length = 27;
obj.generate_sudoku(grid, output, length);
length = 34;
obj.generate_sudoku(grid, output, length);
}
}
Output
Created sudoku of initial 27 elements
0 0 0 0 0 0 0 9 0
0 4 0 0 0 0 0 0 0
0 2 3 0 0 0 8 0 0
0 0 0 6 0 0 5 0 2
6 0 0 0 4 0 0 3 0
0 0 0 2 0 3 0 0 1
0 1 0 0 3 6 2 7 0
2 0 0 4 0 7 3 0 5
0 0 0 5 0 0 0 8 4
Solution
1 6 5 3 2 8 4 9 7
8 4 7 9 6 5 1 2 3
9 2 3 1 7 4 8 5 6
3 7 1 6 8 9 5 4 2
6 5 2 7 4 1 9 3 8
4 9 8 2 5 3 7 6 1
5 1 4 8 3 6 2 7 9
2 8 6 4 9 7 3 1 5
7 3 9 5 1 2 6 8 4
Created sudoku of initial 34 elements
0 0 2 1 6 8 0 0 0
0 0 5 3 0 0 0 0 0
0 4 0 9 5 2 1 0 0
7 0 0 0 0 0 0 8 6
0 0 0 0 0 0 4 5 0
8 5 0 0 0 0 0 1 3
3 0 8 0 0 4 0 0 0
0 9 6 7 8 5 3 0 1
0 2 0 0 0 3 8 0 4
Solution
9 3 2 1 6 8 5 4 7
1 8 5 3 4 7 6 9 2
6 4 7 9 5 2 1 3 8
7 1 4 5 3 9 2 8 6
2 6 3 8 7 1 4 5 9
8 5 9 4 2 6 7 1 3
3 7 8 2 1 4 9 6 5
4 9 6 7 8 5 3 2 1
5 2 1 6 9 3 8 7 4
/*
C++ Program
Sudoku Generator
*/
//Include header file
#include <iostream>
#include <time.h>
#include <stdlib.h>
#define N 9
using namespace std;
class SudokuGenerator
{
public:
//Sudoku {N} grid size
int size;
SudokuGenerator(int size)
{
//Assuming that given size are suitable to sudoku
this->size = size;
}
//Set default value of sudoku problem and output
void set_default(int grid[N][N], int output[N][N])
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this->size; ++i)
{
for (j = 0; j < this->size; ++j)
{
//Set initial value
grid[i][j] = 0;
output[i][j] = 0;
}
}
}
//Check that given number are suitable to particular row and column or not
bool is_vaild(int request[N][N], int row, int col, int num)
{
//Define loop controlling variable
int i = 0;
int j = 0;
// Test case 1
// Check that whether given number are exist in given row and column or not
for (i = 0; i < this->size; ++i)
{
if (request[row][i] == num || request[i][col] == num)
{
//When given number already exists in given row and column
return false;
}
}
//Test case 2
//Check that given number are exist in 3x3 sub grid or not
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
if (request[(row - row % 3) + i][(col - col % 3) + j] == num)
{
//When subgrid contains element
//Then it is not valid
return false;
}
}
}
//When number is valid
return true;
}
//Find the location of empty element in given sudoku grid
int new_empty_location(int request[N][N])
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this->size; ++i)
{
for (j = 0; j < this->size; ++j)
{
if (request[i][j] == 0)
{
//When empty element exist
//return it's location
return (i *this->size) + j;
}
}
}
return -1;
}
//Find result
bool test_sudoku(int output[N][N])
{
int location = this->new_empty_location(output);
if (location == -1)
{
//When sudoku is complete
return true;
}
// Define some auxiliary variables
int row = 0;
int col = 0;
int num = -1;
if (location != 0)
{
//Get current change element location
//Get element row
row = location / this->size;
//Get element column
col = location % this->size;
}
for (num = 1; num <= 9; num++)
{
if (this->is_vaild(output, row, col, num) == true)
{
//Set this number of location [row][col]
output[row][col] = num;
if (this->test_sudoku(output) == true)
{
// When sudoku is solved
return true;
}
//reset value
output[row][col] = 0;
}
}
return false;
}
//Display grid elements
void display_data(int request[N][N])
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this->size; ++i)
{
for (j = 0; j < this->size; ++j)
{
cout << " " << request[i][j];
}
cout << "\n";
}
}
//This is create sudoku problem in given length of element
bool create_sudoku(int grid[N][N], int output[N][N], int length)
{
if (length <= 0)
{
return true;
}
//Get a random row and column
int row = rand() % N;
int col = rand() % N;
if (grid[row][col] == 0)
{
//Get a number from (1 to 9)
int num = (rand() % (N)) + 1;
int counter = 0;
for (counter = 0; counter < this->size; ++counter)
{
if (this->is_vaild(grid, row, col, num) == true)
{
//Set this number of location [row][col]
grid[row][col] = num;
output[row][col] = num;
if (this->create_sudoku(grid, output, length - 1) == true)
{
// When sudoku is created
return true;
}
//reset values
grid[row][col] = 0;
output[row][col] = 0;
}
num++;
if (num > 9)
{
num = 1;
}
}
}
else
{
if (this->create_sudoku(grid, output, length) == true)
{
// When sudoku is created
return true;
}
}
return false;
}
//Generate a sudoku of given length of initial elements
//And also providing its solution
void generate_sudoku(int grid[N][N], int output[N][N], int length)
{
if (length <= 0 || length >= (this->size *this->size))
{
// When get invalid input
// Valid length are between (1 to 81) when size is 9
return;
}
// Set initial value of grid and resultant output
this->set_default(grid, output);
if (this->create_sudoku(grid, output, length) == true)
{
//Test solution of sudoku
if (this->test_sudoku(output) == true)
{
//When get a valid solution
cout << "\n Created sudoku of initial " << length << " elements\n\n";
this->display_data(grid);
cout << "\n Solution \n\n";
this->display_data(output);
}
else
{
//Created sudoku are no solution, Try again ..
this->generate_sudoku(grid, output, length);
}
}
else
{
//When provide invalid length
cout << "\n Sudoku are not create \n";
}
}
};
int main()
{
srand(time(NULL));
SudokuGenerator obj = SudokuGenerator(N);
// Capable to store information of sudoku problem
int grid[N][N];
// This is used to get the result of sudoku
int output[N][N];
//Number of initial element in sudoku
int length = 27;
obj.generate_sudoku(grid, output, length);
length = 34;
obj.generate_sudoku(grid, output, length);
return 0;
}
Output
Created sudoku of initial 27 elements
0 6 2 0 3 0 5 0 0
8 3 0 0 0 0 0 0 0
5 0 7 0 0 0 0 0 0
0 0 0 2 0 1 8 0 3
0 0 0 0 0 0 0 0 7
9 7 0 8 0 0 0 0 6
6 0 5 0 0 3 0 0 0
0 0 0 0 9 0 0 6 0
0 0 0 6 0 7 1 4 5
Solution
1 6 2 7 3 4 5 8 9
8 3 4 9 5 6 7 2 1
5 9 7 1 2 8 6 3 4
4 5 6 2 7 1 8 9 3
2 1 8 3 6 9 4 5 7
9 7 3 8 4 5 2 1 6
6 8 5 4 1 3 9 7 2
7 4 1 5 9 2 3 6 8
3 2 9 6 8 7 1 4 5
Created sudoku of initial 34 elements
1 0 0 0 0 2 4 7 9
8 0 2 0 0 4 6 0 3
0 0 0 3 0 1 5 0 0
2 0 0 6 0 0 9 5 4
0 0 1 0 0 0 7 0 8
0 0 0 0 0 9 0 0 6
0 4 9 1 0 3 8 0 0
0 0 0 0 9 0 0 0 7
6 0 0 7 0 8 0 0 1
Solution
1 3 6 8 5 2 4 7 9
8 5 2 9 7 4 6 1 3
4 9 7 3 6 1 5 8 2
2 8 3 6 1 7 9 5 4
9 6 1 4 3 5 7 2 8
5 7 4 2 8 9 1 3 6
7 4 9 1 2 3 8 6 5
3 1 8 5 9 6 2 4 7
6 2 5 7 4 8 3 9 1
//Include namespace system
using System;
class SudokuGenerator
{
//Sudoku {N} grid size
public int size;
public Random rand = new Random();
public SudokuGenerator(int size)
{
//Assuming that given size are suitable to sudoku
this.size = size;
this.rand = new Random();
}
public int random_num(int low, int high)
{
return rand.Next(low, high);
}
//Set default value of sudoku problem and output
public void set_default(int[, ] grid, int[, ] output)
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
//Set initial value
grid[i, j] = 0;
output[i, j] = 0;
}
}
}
//Check that given number are suitable to particular row and column or not
public Boolean is_vaild(int[, ] request, int row, int col, int num)
{
//Define loop controlling variable
int i = 0;
int j = 0;
// Test case 1
// Check that whether given number are exist in given row and column or not
for (i = 0; i < this.size; ++i)
{
if (request[row, i] == num || request[i, col] == num)
{
//When given number already exists in given row and column
return false;
}
}
//Test case 2
//Check that given number are exist in 3x3 sub grid or not
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
if (request[(row - row % 3) + i, (col - col % 3) + j] == num)
{
//When subgrid contains element
//Then it is not valid
return false;
}
}
}
//When number is valid
return true;
}
//Find the location of empty element in given sudoku grid
public int new_empty_location(int[, ] request)
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
if (request[i, j] == 0)
{
//When empty element exist
//return it's location
return (i * this.size) + j;
}
}
}
return -1;
}
//Find result
public Boolean test_sudoku(int[, ] output)
{
int location = new_empty_location(output);
if (location == -1)
{
//When sudoku is complete
return true;
}
// Define some auxiliary variables
int row = 0;
int col = 0;
int num = -1;
if (location != 0)
{
//Get current change element location
//Get element row
row = location / this.size;
//Get element column
col = location % this.size;
}
for (num = 1; num <= 9; num++)
{
if (is_vaild(output, row, col, num) == true)
{
//Set this number of location [row,col]
output[row, col] = num;
if (test_sudoku(output) == true)
{
// When sudoku is solved
return true;
}
//reset value
output[row, col] = 0;
}
}
return false;
}
//Display grid elements
public void display_data(int[, ] request)
{
//Loop controlling variable
int i = 0;
int j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
Console.Write(" " + request[i, j]);
}
Console.Write("\n");
}
}
//This is create sudoku problem in given length of element
public Boolean create_sudoku(int[, ] grid, int[, ] output, int length)
{
if (length <= 0)
{
return true;
}
//Get a random row and column
int row = random_num(0, this.size);
int col = random_num(0, this.size);
if (grid[row, col] == 0)
{
//Get a number from (1 to 9)
int num = random_num(1, this.size) ;
int counter = 0;
for (counter = 0; counter < this.size; ++counter)
{
if (is_vaild(grid, row, col, num) == true)
{
//Set this number of location [row,col]
grid[row, col] = num;
output[row, col] = num;
if (create_sudoku(grid, output, length - 1) == true)
{
// When sudoku is created
return true;
}
//reset values
grid[row, col] = 0;
output[row, col] = 0;
}
num++;
if (num > this.size)
{
num = 1;
}
}
}
else
{
if (create_sudoku(grid, output, length) == true)
{
// When sudoku is created
return true;
}
}
return false;
}
//Generate a sudoku of given length of initial elements
//And also providing its solution
public void generate_sudoku(int[, ] grid, int[, ] output, int length)
{
if (length <= 0 || length >= (this.size * this.size))
{
// When get invalid input
// Valid length are between (1 to 81) when size is 9
return;
}
// Set initial value of grid and resultant output
set_default(grid, output);
if (create_sudoku(grid, output, length) == true)
{
//Test solution of sudoku
if (test_sudoku(output) == true)
{
//When get a valid solution
Console.Write("\n Created sudoku of initial " + length + " elements\n\n");
display_data(grid);
Console.Write("\n Solution \n\n");
display_data(output);
}
else
{
//Created sudoku are no solution, Try again ..
generate_sudoku(grid, output, length);
}
}
else
{
//When provide invalid length
Console.Write("\n Sudoku are not create \n");
}
}
public static void Main(String[] args)
{
int n = 9;
SudokuGenerator obj = new SudokuGenerator(n);
// Capable to store information of sudoku problem
int[, ] grid = new int[n, n];
// This is used to get the result of sudoku
int[, ] output = new int[n, n];
//Number of initial element in sudoku
int length = 27;
obj.generate_sudoku(grid, output, length);
length = 34;
obj.generate_sudoku(grid, output, length);
}
}
Output
Created sudoku of initial 27 elements
0 0 0 0 0 0 0 0 0
0 0 8 0 0 9 0 0 0
0 0 4 2 7 8 0 0 5
5 6 0 0 0 0 7 0 1
8 0 0 0 0 5 0 0 0
0 3 0 7 6 0 5 8 0
7 0 0 0 0 4 0 0 0
4 0 0 5 0 0 3 0 0
0 2 0 1 0 0 4 0 8
Solution
2 5 3 6 4 1 8 7 9
1 7 8 3 5 9 2 4 6
6 9 4 2 7 8 1 3 5
5 6 2 4 8 3 7 9 1
8 4 7 9 1 5 6 2 3
9 3 1 7 6 2 5 8 4
7 1 5 8 3 4 9 6 2
4 8 9 5 2 6 3 1 7
3 2 6 1 9 7 4 5 8
Created sudoku of initial 34 elements
0 6 2 0 0 0 0 0 8
4 1 7 0 5 0 9 0 3
0 0 0 3 2 0 0 0 0
0 9 6 0 7 3 8 0 0
1 7 5 6 8 0 0 3 0
0 0 4 0 0 0 6 0 2
0 0 0 0 0 0 0 8 0
0 8 9 0 0 0 0 0 1
7 0 0 0 1 8 2 9 0
Solution
3 6 2 7 4 9 1 5 8
4 1 7 8 5 6 9 2 3
9 5 8 3 2 1 7 6 4
2 9 6 4 7 3 8 1 5
1 7 5 6 8 2 4 3 9
8 3 4 1 9 5 6 7 2
5 2 1 9 6 4 3 8 7
6 8 9 2 3 7 5 4 1
7 4 3 5 1 8 2 9 6
<?php
class SudokuGenerator
{
//Sudoku {N} grid size
public $size;
function __construct($size)
{
//Assuming that given size are suitable to sudoku
$this->size = $size;
}
public function random_num($low, $high)
{
return rand($low, $high);
}
//Set default value of sudoku problem and output
public function set_default( &$grid, &$output)
{
//Loop controlling variable
$i = 0;
$j = 0;
for ($i = 0; $i < $this->size; ++$i)
{
for ($j = 0; $j < $this->size; ++$j)
{
//Set initial value
$grid[$i][$j] = 0;
$output[$i][$j] = 0;
}
}
}
//Check that given number are suitable to particular row and column or not
public function is_vaild( $request, $row, $col, $num)
{
//Define loop controlling variable
$i = 0;
$j = 0;
// Test case 1
// Check that whether given number are exist in given row and column or not
for ($i = 0; $i < $this->size; ++$i)
{
if ($request[$row][$i] == $num || $request[$i][$col] == $num)
{
//When given number already exists in given row and column
return false;
}
}
//Test case 2
//Check that given number are exist in 3x3 sub grid or not
for ($i = 0; $i < 3; ++$i)
{
for ($j = 0; $j < 3; ++$j)
{
if ($request[($row - $row % 3) + $i][($col - $col % 3) + $j] == $num)
{
//When subgrid contains element
//Then it is not valid
return false;
}
}
}
//When number is valid
return true;
}
//Find the location of empty element in given sudoku grid
public function new_empty_location( $request)
{
//Loop controlling variable
$i = 0;
$j = 0;
for ($i = 0; $i < $this->size; ++$i)
{
for ($j = 0; $j < $this->size; ++$j)
{
if ($request[$i][$j] == 0)
{
//When empty element exist
//return it's location
return ($i * $this->size) + $j;
}
}
}
return -1;
}
//Find result
public function test_sudoku( & $output)
{
$location = $this->new_empty_location($output);
if ($location == -1)
{
//When sudoku is complete
return true;
}
// Define some auxiliary variables
$row = 0;
$col = 0;
$num = -1;
if ($location != 0)
{
//Get current change element location
//Get element row
$row = intval($location / $this->size);
//Get element column
$col = $location % $this->size;
}
for ($num = 1; $num <= $this->size; $num++)
{
if ($this->is_vaild($output, $row, $col, $num) == true)
{
//Set this number of location [row][col]
$output[$row][$col] = $num;
if ($this->test_sudoku($output) == true)
{
// When sudoku is solved
return true;
}
//reset value
$output[$row][$col] = 0;
}
}
return false;
}
//Display grid elements
public function display_data( & $request)
{
//Loop controlling variable
$i = 0;
$j = 0;
for ($i = 0; $i < $this->size; ++$i)
{
for ($j = 0; $j < $this->size; ++$j)
{
echo " ". $request[$i][$j];
}
echo "\n";
}
}
//This is create sudoku problem in given length of element
public function create_sudoku( & $grid, & $output, $length)
{
if ($length <= 0)
{
return true;
}
//Get a random row and column
$row = $this->random_num(0, $this->size-1);
$col = $this->random_num(0, $this->size-1);
if ($grid[$row][$col] == 0)
{
//Get a number from (1 to 9)
$num = $this->random_num(1, $this->size) ;
$counter = 0;
for ($counter = 0; $counter < $this->size; ++$counter)
{
if ($this->is_vaild($grid, $row, $col, $num) == true)
{
//Set this number of location [row][col]
$grid[$row][$col] = $num;
$output[$row][$col] = $num;
if ($this->create_sudoku($grid, $output, $length - 1) == true)
{
// When sudoku is created
return true;
}
//reset values
$grid[$row][$col] = 0;
$output[$row][$col] = 0;
}
$num++;
if ($num > $this->size)
{
$num = 1;
}
}
}
else
{
if ($this->create_sudoku($grid, $output, $length) == true)
{
// When sudoku is created
return true;
}
}
return false;
}
//Generate a sudoku of given length of initial elements
//And also providing its solution
public function generate_sudoku( & $grid, & $output, $length)
{
if ($length <= 0 || $length >= ($this->size * $this->size))
{
// When get invalid input
// Valid length are between (1 to 81) when size is 9
return;
}
// Set initial value of grid and resultant output
$this->set_default($grid, $output);
if ($this->create_sudoku($grid, $output, $length) == true)
{
//Test solution of sudoku
if ($this->test_sudoku($output) == true)
{
//When get a valid solution
echo "\n Created sudoku of initial ". $length ." elements\n\n";
$this->display_data($grid);
echo "\n Solution \n\n";
$this->display_data($output);
}
else
{
//Created sudoku are no solution, Try again ..
$this->generate_sudoku($grid, $output, $length);
}
}
else
{
//When provide invalid length
echo "\n Sudoku are not create \n";
}
}
}
function main()
{
$n = 9;
$obj = new SudokuGenerator($n);
// Capable to store information of sudoku problem
$grid = array_fill(0, $n, array_fill(0, $n,0));
// This is used to get the result of sudoku
$output = array_fill(0, $n, array_fill(0, $n,0));
//Number of initial element in sudoku
$length = 27;
$obj->generate_sudoku($grid, $output, $length);
$length = 34;
$obj->generate_sudoku($grid, $output, $length);
}
main();
Output
Created sudoku of initial 27 elements
0 0 0 5 0 0 4 1 8
0 0 8 0 1 0 0 9 0
0 0 4 0 0 0 5 0 3
5 8 0 4 0 0 2 7 9
0 0 3 0 0 0 0 0 0
0 0 2 0 0 6 0 0 0
0 0 0 6 2 0 7 0 0
0 2 0 3 0 0 1 0 0
4 0 0 0 0 0 0 0 2
Solution
2 6 9 5 7 3 4 1 8
3 5 8 2 1 4 6 9 7
1 7 4 9 6 8 5 2 3
5 8 6 4 3 1 2 7 9
9 1 3 7 5 2 8 4 6
7 4 2 8 9 6 3 5 1
8 9 1 6 2 5 7 3 4
6 2 7 3 4 9 1 8 5
4 3 5 1 8 7 9 6 2
Created sudoku of initial 34 elements
2 0 5 0 4 0 3 0 0
0 0 0 0 0 0 0 1 0
3 0 0 7 0 5 0 2 0
8 0 9 0 6 0 0 3 7
1 2 4 0 0 3 6 5 0
6 7 0 5 0 4 1 0 2
5 1 0 4 0 7 0 0 0
4 0 0 0 0 8 0 0 1
0 0 0 9 0 0 0 0 0
Solution
2 6 5 1 4 9 3 7 8
9 8 7 6 3 2 5 1 4
3 4 1 7 8 5 9 2 6
8 5 9 2 6 1 4 3 7
1 2 4 8 7 3 6 5 9
6 7 3 5 9 4 1 8 2
5 1 6 4 2 7 8 9 3
4 9 2 3 5 8 7 6 1
7 3 8 9 1 6 2 4 5
/*
Node Js Program
Sudoku Generator
*/
class SudokuGenerator
{
//Sudoku {N} grid size
constructor(size)
{
//Assuming that given size are suitable to sudoku
this.size = size;
}
random_num(low, high)
{
return Math.floor(Math.random() * (high - low) + low);
}
//Set default value of sudoku problem and output
set_default(grid, output)
{
//Loop controlling variable
var i = 0;
var j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
//Set initial value
grid[i][j] = 0;
output[i][j] = 0;
}
}
}
//Check that given number are suitable to particular row and column or not
is_vaild(request, row, col, num)
{
//Define loop controlling variable
var i = 0;
var j = 0;
// Test case 1
// Check that whether given number are exist in given row and column or not
for (i = 0; i < this.size; ++i)
{
if (request[row][i] == num || request[i][col] == num)
{
//When given number already exists in given row and column
return false;
}
}
//Test case 2
//Check that given number are exist in 3x3 sub grid or not
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
if (request[(row - row % 3) + i][(col - col % 3) + j] == num)
{
//When subgrid contains element
//Then it is not valid
return false;
}
}
}
//When number is valid
return true;
}
//Find the location of empty element in given sudoku grid
new_empty_location(request)
{
//Loop controlling variable
var i = 0;
var j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
if (request[i][j] == 0)
{
//When empty element exist
//return it's location
return (i * this.size) + j;
}
}
}
return -1;
}
//Find result
test_sudoku(output)
{
var location = this.new_empty_location(output);
if (location == -1)
{
//When sudoku is complete
return true;
}
// Define some auxiliary variables
var row = 0;
var col = 0;
var num = -1;
if (location != 0)
{
//Get current change element location
//Get element row
row = parseInt(location / this.size);
//Get element column
col = location % this.size;
}
for (num = 1; num <= 9; num++)
{
if (this.is_vaild(output, row, col, num) == true)
{
//Set this number of location [row][col]
output[row][col] = num;
if (this.test_sudoku(output) == true)
{
// When sudoku is solved
return true;
}
//reset value
output[row][col] = 0;
}
}
return false;
}
//Display grid elements
display_data(request)
{
//Loop controlling variable
var i = 0;
var j = 0;
for (i = 0; i < this.size; ++i)
{
for (j = 0; j < this.size; ++j)
{
process.stdout.write(" " + request[i][j]);
}
process.stdout.write("\n");
}
}
//This is create sudoku problem in given length of element
create_sudoku(grid, output, length)
{
if (length <= 0)
{
return true;
}
//Get a random row and column
var row = this.random_num(0, this.size);
var col = this.random_num(0, this.size);
if (grid[row][col] == 0)
{
//Get a number from (1 to 9)
var num = this.random_num(0,this.size);
var counter = 0;
for (counter = 0; counter < this.size; ++counter)
{
if (this.is_vaild(grid, row, col, num) == true)
{
//Set this number of location [row][col]
grid[row][col] = num;
output[row][col] = num;
if (this.create_sudoku(grid, output, length - 1) == true)
{
// When sudoku is created
return true;
}
//reset values
grid[row][col] = 0;
output[row][col] = 0;
}
num++;
if (num > this.size)
{
num = 1;
}
}
}
else
{
if (this.create_sudoku(grid, output, length) == true)
{
// When sudoku is created
return true;
}
}
return false;
}
//Generate a sudoku of given length of initial elements
//And also providing its solution
generate_sudoku(grid, output, length)
{
if (length <= 0 || length >= (this.size * this.size))
{
// When get invalid input
// Valid length are between (1 to 81) when size is 9
return;
}
// Set initial value of grid and resultant output
this.set_default(grid, output);
if (this.create_sudoku(grid, output, length) == true)
{
//Test solution of sudoku
if (this.test_sudoku(output) == true)
{
//When get a valid solution
process.stdout.write("\n Created sudoku of initial " + length + " elements\n\n");
this.display_data(grid);
process.stdout.write("\n Solution \n\n");
this.display_data(output);
}
else
{
//Created sudoku are no solution, Try again ..
this.generate_sudoku(grid, output, length);
}
}
else
{
//When provide invalid length
process.stdout.write("\n Sudoku are not create \n");
}
}
}
function main()
{
var n = 9;
var obj = new SudokuGenerator(n);
// Capable to store information of sudoku problem
var grid = Array(n).fill(0).map(() => new Array(n).fill(0));
// This is used to get the result of sudoku
var output = Array(n).fill(0).map(() => new Array(n).fill(0));
//Number of initial element in sudoku
var length = 27;
obj.generate_sudoku(grid, output, length);
length = 34;
obj.generate_sudoku(grid, output, length);
}
main();
Output
Created sudoku of initial 27 elements
0 9 0 0 0 0 0 0 0
0 2 0 0 1 0 0 0 0
0 0 0 3 0 7 0 0 6
6 0 0 0 0 0 0 5 0
5 7 0 0 0 0 0 0 3
0 0 8 0 0 5 6 2 4
0 0 7 8 0 0 0 0 0
9 8 4 0 0 0 0 6 2
3 0 5 0 2 0 1 0 0
Solution
4 9 3 2 5 6 7 8 1
7 2 6 9 1 8 4 3 5
8 5 1 3 4 7 2 9 6
6 4 9 1 3 2 8 5 7
5 7 2 6 8 4 9 1 3
1 3 8 7 9 5 6 2 4
2 1 7 8 6 3 5 4 9
9 8 4 5 7 1 3 6 2
3 6 5 4 2 9 1 7 8
Created sudoku of initial 34 elements
5 8 1 4 0 3 0 0 2
0 0 0 6 0 0 0 8 5
0 0 0 0 0 0 0 3 7
9 5 0 0 0 0 0 0 0
0 1 0 5 0 0 3 0 0
0 7 3 1 0 0 0 0 0
8 6 9 7 0 4 0 5 3
0 0 0 8 3 0 0 0 9
0 0 2 9 5 6 0 1 0
Solution
5 8 1 4 7 3 9 6 2
3 2 7 6 9 1 4 8 5
4 9 6 2 8 5 1 3 7
9 5 4 3 6 8 7 2 1
2 1 8 5 4 7 3 9 6
6 7 3 1 2 9 5 4 8
8 6 9 7 1 4 2 5 3
1 4 5 8 3 2 6 7 9
7 3 2 9 5 6 8 1 4
# Python 3 Program
# Sudoku Generator
import random
import sys
class SudokuGenerator :
# Sudoku :N grid size
def __init__(self, size) :
# Assuming that given size are suitable to sudoku
self.size = size
def random_num(self, low, high) :
return random.randint(low,high)
# Set default value of sudoku problem and output
def set_default(self, grid, output) :
# Loop controlling variable
i = 0
j = 0
while (i < self.size) :
j = 0
while (j < self.size) :
# Set initial value
grid[i][j] = 0
output[i][j] = 0
j += 1
i += 1
# Check that given number are suitable to particular row and column or not
def is_vaild(self, request, row, col, num) :
# Define loop controlling variable
i = 0
j = 0
# Test case 1
# Check that whether given number are exist in given row and column or not
while (i < self.size) :
if (request[row][i] == num or request[i][col] == num) :
# When given number already exists in given row and column
return False
i += 1
# Test case 2
# Check that given number are exist in 3x3 sub grid or not
i = 0
while (i < 3) :
j = 0
while (j < 3) :
if (request[(row - row % 3) + i][(col - col % 3) + j] == num) :
# When subgrid contains element
# Then it is not valid
return False
j += 1
i += 1
# When number is valid
return True
# Find the location of empty element in given sudoku grid
def new_empty_location(self, request) :
# Loop controlling variable
i = 0
j = 0
while (i < self.size) :
j = 0
while (j < self.size) :
if (request[i][j] == 0) :
# When empty element exist
# return it's location
return (i * self.size) + j
j += 1
i += 1
return -1
# Find result
def test_sudoku(self, output) :
location = self.new_empty_location(output)
if (location == -1) :
# When sudoku is complete
return True
# Define some auxiliary variables
row = 0
col = 0
num = -1
if (location != 0) :
# Get current change element location
# Get element row
row = int(location / self.size)
# Get element column
col = location % self.size
num = 1
while (num <= self.size) :
if (self.is_vaild(output, row, col, num) == True) :
# Set this number of location [row][col]
output[row][col] = num
if (self.test_sudoku(output) == True) :
# When sudoku is solved
return True
# reset value
output[row][col] = 0
num += 1
return False
# Display grid elements
def display_data(self, request) :
# Loop controlling variable
i = 0
j = 0
while (i < self.size) :
j = 0
while (j < self.size) :
print(" ", request[i][j], end = "")
j += 1
print("\n", end = "")
i += 1
# This is create sudoku problem in given length of element
def create_sudoku(self, grid, output, length) :
if (length <= 0) :
return True
# Get a random row and column
row = self.random_num(0, self.size-1)
col = self.random_num(0, self.size-1)
if (grid[row][col] == 0) :
# Get a number from (1 to 9)
num = self.random_num(1, self.size)
counter = 0
while (counter < self.size) :
if (self.is_vaild(grid, row, col, num) == True) :
# Set this number of location [row][col]
grid[row][col] = num
output[row][col] = num
if (self.create_sudoku(grid, output, length - 1) == True) :
# When sudoku is created
return True
# reset values
grid[row][col] = 0
output[row][col] = 0
num += 1
if (num > self.size) :
num = 1
counter += 1
else :
if (self.create_sudoku(grid, output, length) == True) :
# When sudoku is created
return True
return False
# Generate a sudoku of given length of initial elements
# And also providing its solution
def generate_sudoku(self, grid, output, length) :
if (length <= 0 or length >= (self.size * self.size)) :
# When get invalid input
# Valid length are between (1 to 81) when size is 9
return
# Set initial value of grid and resultant output
self.set_default(grid, output)
if (self.create_sudoku(grid, output, length) == True) :
# Test solution of sudoku
if (self.test_sudoku(output) == True) :
# When get a valid solution
print("\n Created sudoku of initial ", length ," elements\n\n", end = "")
self.display_data(grid)
print("\n Solution \n\n", end = "")
self.display_data(output)
else :
# Created sudoku are no solution, Try again ..
self.generate_sudoku(grid, output, length)
else :
# When provide invalid length
print("\n Sudoku are not create \n", end = "")
def main() :
n = 9
obj = SudokuGenerator(n)
# Capable to store information of sudoku problem
grid = [[0 for i in range(n) ] for j in range(n)]
# This is used to get the result of sudoku
output = [[0 for i in range(n) ] for j in range(n)]
# Number of initial element in sudoku
length = 27
obj.generate_sudoku(grid, output, length)
length = 34
obj.generate_sudoku(grid, output, length)
if __name__ == "__main__": main()
Output
Created sudoku of initial 27 elements
1 0 0 9 6 4 0 0 3
0 0 3 7 1 5 2 0 0
0 0 0 0 0 0 0 6 0
3 0 0 0 0 0 0 2 0
2 0 0 6 0 0 0 5 0
0 9 7 0 2 0 0 1 8
0 6 0 0 7 0 5 0 0
0 0 0 0 8 2 0 0 0
0 0 0 0 0 0 0 3 0
Solution
1 2 5 9 6 4 7 8 3
6 8 3 7 1 5 2 9 4
7 4 9 2 3 8 1 6 5
3 5 6 8 9 1 4 2 7
2 1 8 6 4 7 3 5 9
4 9 7 5 2 3 6 1 8
8 6 1 3 7 9 5 4 2
5 3 4 1 8 2 9 7 6
9 7 2 4 5 6 8 3 1
Created sudoku of initial 34 elements
8 0 6 7 4 0 0 0 0
2 7 0 1 9 0 0 3 0
0 0 0 0 0 0 6 0 0
3 0 0 0 0 0 8 0 5
0 6 0 0 5 0 3 0 0
0 5 4 0 0 0 0 6 0
1 0 0 8 7 0 9 5 0
0 0 0 0 2 1 7 0 0
6 8 0 9 3 5 1 4 0
Solution
8 1 6 7 4 3 5 2 9
2 7 5 1 9 6 4 3 8
4 9 3 5 8 2 6 7 1
3 2 1 4 6 7 8 9 5
7 6 8 2 5 9 3 1 4
9 5 4 3 1 8 2 6 7
1 3 2 8 7 4 9 5 6
5 4 9 6 2 1 7 8 3
6 8 7 9 3 5 1 4 2
# Ruby Program
# Sudoku Generator
class SudokuGenerator
# Define the accessor and reader of class SudokuGenerator
attr_reader :size
attr_accessor :size
# Sudoku Nend grid size
def initialize(size)
# Assuming that given size are suitable to sudoku
self.size = size
end
def random_num(low, high)
return Random.new.rand(low...high);
end
# Set default value of sudoku problem and output
def set_default(grid, output)
# Loop controlling variable
i = 0
j = 0
while (i < self.size)
j = 0
while (j < self.size)
# Set initial value
grid[i][j] = 0
output[i][j] = 0
j += 1
end
i += 1
end
end
# Check that given number are suitable to particular row and column or not
def is_vaild(request, row, col, num)
# Define loop controlling variable
i = 0
j = 0
# Test case 1
# Check that whether given number are exist in given row and column or not
while (i < self.size)
if (request[row][i] == num || request[i][col] == num)
# When given number already exists in given row and column
return false
end
i += 1
end
# Test case 2
# Check that given number are exist in 3x3 sub grid or not
i = 0
while (i < 3)
j = 0
while (j < 3)
if (request[(row - row % 3) + i][(col - col % 3) + j] == num)
# When subgrid contains element
# Then it is not valid
return false
end
j += 1
end
i += 1
end
# When number is valid
return true
end
# Find the location of empty element in given sudoku grid
def new_empty_location(request)
# Loop controlling variable
i = 0
j = 0
while (i < self.size)
j = 0
while (j < self.size)
if (request[i][j] == 0)
# When empty element exist
# return it's location
return (i * self.size) + j
end
j += 1
end
i += 1
end
return -1
end
# Find result
def test_sudoku(output)
location = self.new_empty_location(output)
if (location == -1)
# When sudoku is complete
return true
end
# Define some auxiliary variables
row = 0
col = 0
num = -1
if (location != 0)
# Get current change element location
# Get element row
row = location / self.size
# Get element column
col = location % self.size
end
num = 1
while (num <= self.size)
if (self.is_vaild(output, row, col, num) == true)
# Set this number of location [row][col]
output[row][col] = num
if (self.test_sudoku(output) == true)
# When sudoku is solved
return true
end
# reset value
output[row][col] = 0
end
num += 1
end
return false
end
# Display grid elements
def display_data(request)
# Loop controlling variable
i = 0
j = 0
while (i < self.size)
j = 0
while (j < self.size)
print(" ", request[i][j])
j += 1
end
print("\n")
i += 1
end
end
# This is create sudoku problem in given length of element
def create_sudoku(grid, output, length)
if (length <= 0)
return true
end
# Get a random row and column
row = self.random_num(0, self.size)
col = self.random_num(0, self.size)
if (grid[row][col] == 0)
# Get a number from (1 to 9)
num = self.random_num(1, self.size)
counter = 0
counter = 0
while (counter < self.size)
if (self.is_vaild(grid, row, col, num) == true)
# Set this number of location [row][col]
grid[row][col] = num
output[row][col] = num
if (self.create_sudoku(grid, output, length - 1) == true)
# When sudoku is created
return true
end
# reset values
grid[row][col] = 0
output[row][col] = 0
end
num += 1
if (num > self.size)
num = 1
end
counter += 1
end
else
if (self.create_sudoku(grid, output, length) == true)
# When sudoku is created
return true
end
end
return false
end
# Generate a sudoku of given length of initial elements
# And also providing its solution
def generate_sudoku(grid, output, length)
if (length <= 0 || length >= (self.size * self.size))
# When get invalid input
# Valid length are between (1 to 81) when size is 9
return
end
# Set initial value of grid and resultant output
self.set_default(grid, output)
if (self.create_sudoku(grid, output, length) == true)
# Test solution of sudoku
if (self.test_sudoku(output) == true)
# When get a valid solution
print("\n Created sudoku of initial ", length ," elements\n\n")
self.display_data(grid)
print("\n Solution \n\n")
self.display_data(output)
else
# Created sudoku are no solution, Try again ..
self.generate_sudoku(grid, output, length)
end
else
# When provide invalid length
print("\n Sudoku are not create \n")
end
end
end
def main()
n = 9
obj = SudokuGenerator.new(n)
# Capable to store information of sudoku problem
grid = Array.new(n) {Array.new(n) {0}}
# This is used to get the result of sudoku
output = Array.new(n) {Array.new(n) {0}}
# Number of initial element in sudoku
length = 27
obj.generate_sudoku(grid, output, length)
length = 34
obj.generate_sudoku(grid, output, length)
end
main()
Output
Created sudoku of initial 27 elements
0 1 0 0 0 9 0 0 0
0 8 0 0 5 6 0 0 0
2 0 0 0 8 0 0 0 0
0 0 7 0 9 0 0 0 3
0 4 0 7 0 0 0 0 5
0 2 0 0 6 0 0 0 0
0 5 0 0 4 0 3 0 7
6 7 0 3 0 5 2 0 0
8 0 0 0 7 0 1 0 0
Solution
4 1 5 2 3 9 6 7 8
7 8 3 1 5 6 9 4 2
2 9 6 4 8 7 5 3 1
5 6 7 8 9 1 4 2 3
9 4 1 7 2 3 8 6 5
3 2 8 5 6 4 7 1 9
1 5 2 6 4 8 3 9 7
6 7 9 3 1 5 2 8 4
8 3 4 9 7 2 1 5 6
Created sudoku of initial 34 elements
0 0 4 5 3 8 0 0 0
0 8 0 0 0 0 0 9 2
3 7 0 0 0 0 4 8 0
7 5 9 0 4 0 0 6 8
0 0 0 0 0 0 5 0 0
0 0 8 0 5 9 1 4 0
0 4 0 8 0 5 7 2 6
8 0 0 0 0 6 0 0 0
0 0 2 0 0 0 0 3 1
Solution
2 9 4 5 3 8 6 1 7
1 8 5 4 6 7 3 9 2
3 7 6 2 9 1 4 8 5
7 5 9 1 4 3 2 6 8
4 3 1 6 8 2 5 7 9
6 2 8 7 5 9 1 4 3
9 4 3 8 1 5 7 2 6
8 1 7 3 2 6 9 5 4
5 6 2 9 7 4 8 3 1
/*
Scala Program
Sudoku Generator
*/
class SudokuGenerator(var size: Int)
{
def random_num(low: Int, high: Int): Int = {
val r = new scala.util.Random;
return low + r.nextInt((high - low));
}
//Set default value of sudoku problem and output
def set_default(grid: Array[Array[Int]], output: Array[Array[Int]]): Unit = {
//Loop controlling variable
var i: Int = 0;
var j: Int = 0;
while (i < this.size)
{
j = 0;
while (j < this.size)
{
//Set initial value
grid(i)(j) = 0;
output(i)(j) = 0;
j += 1;
}
i += 1;
}
}
//Check that given number are suitable to particular row and column or not
def is_vaild(request: Array[Array[Int]], row: Int, col: Int, num: Int): Boolean = {
//Define loop controlling variable
var i: Int = 0;
var j: Int = 0;
// Test case 1
// Check that whether given number are exist in given row and column or not
while (i < this.size)
{
if (request(row)(i) == num || request(i)(col) == num)
{
//When given number already exists in given row and column
return false;
}
i += 1;
}
//Test case 2
//Check that given number are exist in 3x3 sub grid or not
i = 0;
while (i < 3)
{
j = 0;
while (j < 3)
{
if (request((row - row % 3) + i)((col - col % 3) + j) == num)
{
//When subgrid contains element
//Then it is not valid
return false;
}
j += 1;
}
i += 1;
}
//When number is valid
return true;
}
//Find the location of empty element in given sudoku grid
def new_empty_location(request: Array[Array[Int]]): Int = {
//Loop controlling variable
var i: Int = 0;
var j: Int = 0;
while (i < this.size)
{
j = 0;
while (j < this.size)
{
if (request(i)(j) == 0)
{
//When empty element exist
//return it's location
return (i * this.size) + j;
}
j += 1;
}
i += 1;
}
return -1;
}
//Find result
def test_sudoku(output: Array[Array[Int]]): Boolean = {
var location: Int = new_empty_location(output);
if (location == -1)
{
//When sudoku is complete
return true;
}
// Define some auxiliary variables
var row: Int = 0;
var col: Int = 0;
var num: Int = -1;
if (location != 0)
{
//Get current change element location
//Get element row
row = (location / this.size).toInt;
//Get element column
col = location % this.size;
}
num = 1;
while (num <= this.size)
{
if (is_vaild(output, row, col, num) == true)
{
//Set this number of location [row][col]
output(row)(col) = num;
if (test_sudoku(output) == true)
{
// When sudoku is solved
return true;
}
//reset value
output(row)(col) = 0;
}
num += 1;
}
return false;
}
//Display grid elements
def display_data(request: Array[Array[Int]]): Unit = {
//Loop controlling variable
var i: Int = 0;
var j: Int = 0;
while (i < this.size)
{
j = 0;
while (j < this.size)
{
print(" " + request(i)(j));
j += 1;
}
print("\n");
i += 1;
}
}
//This is create sudoku problem in given length of element
def create_sudoku(grid: Array[Array[Int]], output: Array[Array[Int]], length: Int): Boolean = {
if (length <= 0)
{
return true;
}
//Get a random row and column
var row: Int = random_num(0, this.size);
var col: Int = random_num(0, this.size);
if (grid(row)(col) == 0)
{
//Get a number from (1 to 9)
var num: Int = random_num(1, this.size);
var counter: Int = 0;
counter = 0;
while (counter < this.size)
{
if (is_vaild(grid, row, col, num) == true)
{
//Set this number of location [row][col]
grid(row)(col) = num;
output(row)(col) = num;
if (create_sudoku(grid, output, length - 1) == true)
{
// When sudoku is created
return true;
}
//reset values
grid(row)(col) = 0;
output(row)(col) = 0;
}
num += 1;
if (num > this.size)
{
num = 1;
}
counter += 1;
}
}
else
{
if (create_sudoku(grid, output, length) == true)
{
// When sudoku is created
return true;
}
}
return false;
}
//Generate a sudoku of given length of initial elements
//And also providing its solution
def generate_sudoku(grid: Array[Array[Int]], output: Array[Array[Int]], length: Int): Unit = {
if (length <= 0 || length >= (this.size * this.size))
{
// When get invalid input
// Valid length are between (1 to 81) when size is 9
return;
}
// Set initial value of grid and resultant output
set_default(grid, output);
if (create_sudoku(grid, output, length) == true)
{
//Test solution of sudoku
if (test_sudoku(output) == true)
{
//When get a valid solution
print("\n Created sudoku of initial " + length + " elements\n\n");
display_data(grid);
print("\n Solution \n\n");
display_data(output);
}
else
{
//Created sudoku are no solution, Try again ..
generate_sudoku(grid, output, length);
}
}
else
{
//When provide invalid length
print("\n Sudoku are not create \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var n: Int = 9;
var obj: SudokuGenerator = new SudokuGenerator(n);
// Capable to store information of sudoku problem
var grid: Array[Array[Int]] = Array.fill[Int](n, n)(0);
// This is used to get the result of sudoku
var output: Array[Array[Int]] = Array.fill[Int](n, n)(0);
//Number of initial element in sudoku
var length: Int = 27;
obj.generate_sudoku(grid, output, length);
length = 34;
obj.generate_sudoku(grid, output, length);
}
}
Output
Created sudoku of initial 27 elements
0 3 0 0 0 0 0 0 0
0 0 0 0 2 4 3 0 0
6 0 0 0 7 0 0 5 0
0 0 8 0 0 7 6 0 0
0 0 2 0 6 0 7 0 0
7 0 0 4 9 2 1 8 0
0 0 0 0 0 0 5 0 0
0 7 3 8 5 0 0 4 0
0 0 0 0 0 0 8 6 0
Solution
1 3 4 6 8 5 2 7 9
5 8 7 9 2 4 3 1 6
6 2 9 1 7 3 4 5 8
3 9 8 5 1 7 6 2 4
4 1 2 3 6 8 7 9 5
7 5 6 4 9 2 1 8 3
8 6 1 2 4 9 5 3 7
2 7 3 8 5 6 9 4 1
9 4 5 7 3 1 8 6 2
Created sudoku of initial 34 elements
7 3 8 0 0 0 0 0 2
0 0 1 0 0 8 0 0 7
4 0 5 3 0 1 8 6 9
0 0 0 0 0 0 0 3 0
1 8 7 6 0 0 0 0 5
6 9 0 0 0 0 0 7 8
0 0 0 2 0 0 0 0 0
3 4 0 7 0 0 5 0 1
0 1 2 0 4 0 0 0 3
Solution
7 3 8 9 6 5 4 1 2
9 6 1 4 2 8 3 5 7
4 2 5 3 7 1 8 6 9
2 5 4 8 9 7 1 3 6
1 8 7 6 3 2 9 4 5
6 9 3 1 5 4 2 7 8
5 7 9 2 1 3 6 8 4
3 4 6 7 8 9 5 2 1
8 1 2 5 4 6 7 9 3
/*
Swift Program
Sudoku Generator
*/
import Foundation
#if os(Linux)
srandom(UInt32(time(nil)))
#endif
class SudokuGenerator
{
//Sudoku {N} grid size
var size: Int;
init(_ size: Int)
{
//Assuming that given size are suitable to sudoku
self.size = size;
}
func random_num(_ low: Int, _ high: Int) -> Int
{
var number = 0;
//Calculate random number
#if os(Linux)
number = low + Int(random()%(high-low))
#else
number = Int(low + arc4random_uniform(high - low))
#endif
return number;
}
//Set default value of sudoku problem and output
func set_default(_ grid: inout[[Int]], _ output: inout[[Int]])
{
//Loop controlling variable
var i: Int = 0;
var j: Int = 0;
while (i < self.size)
{
j = 0;
while (j < self.size)
{
//Set initial value
grid[i][j] = 0;
output[i][j] = 0;
j += 1;
}
i += 1;
}
}
//Check that given number are suitable to particular row and column or not
func is_vaild(_ request: [
[Int]
], _ row: Int, _ col: Int, _ num: Int) -> Bool
{
//Define loop controlling variable
var i: Int = 0;
var j: Int = 0;
// Test case 1
// Check that whether given number are exist in given row and column or not
while (i < self.size)
{
if (request[row][i] == num || request[i][col] == num)
{
//When given number already exists in given row and column
return false;
}
i += 1;
}
//Test case 2
//Check that given number are exist in 3x3 sub grid or not
i = 0;
while (i < 3)
{
j = 0;
while (j < 3)
{
if (request[(row - row % 3) + i][(col - col % 3) + j] == num)
{
//When subgrid contains element
//Then it is not valid
return false;
}
j += 1;
}
i += 1;
}
//When number is valid
return true;
}
//Find the location of empty element in given sudoku grid
func new_empty_location(_ request: [
[Int]
]) -> Int
{
//Loop controlling variable
var i: Int = 0;
var j: Int = 0;
while (i < self.size)
{
j = 0;
while (j < self.size)
{
if (request[i][j] == 0)
{
//When empty element exist
//return it"s location
return (i * self.size) + j;
}
j += 1;
}
i += 1;
}
return -1;
}
//Find result
func test_sudoku(_ output: inout[[Int]]) -> Bool
{
let location: Int = self.new_empty_location(output);
if (location == -1)
{
//When sudoku is complete
return true;
}
// Define some auxiliary variables
var row: Int = 0;
var col: Int = 0;
var num: Int = -1;
if (location != 0)
{
//Get current change element location
//Get element row
row = location / self.size;
//Get element column
col = location % self.size;
}
num = 1;
while (num <= self.size)
{
if (self.is_vaild(output, row, col, num) == true)
{
//Set this number of location [row][col]
output[row][col] = num;
if (self.test_sudoku(&output) == true)
{
// When sudoku is solved
return true;
}
//reset value
output[row][col] = 0;
}
num += 1;
}
return false;
}
//Display grid elements
func display_data(_ request: [
[Int]
])
{
//Loop controlling variable
var i: Int = 0;
var j: Int = 0;
while (i < self.size)
{
j = 0;
while (j < self.size)
{
print(" ", request[i][j], terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
//This is create sudoku problem in given length of element
func create_sudoku(_ grid: inout[[Int]], _ output: inout[[Int]], _ length: Int) -> Bool
{
if (length <= 0)
{
return true;
}
//Get a random row and column
let row: Int = self.random_num(1, self.size+1)-1;
let col: Int = self.random_num(1, self.size+1)-1;
if (grid[row][col] == 0)
{
//Get a number from (1 to 9)
var num: Int = self.random_num(1, self.size);
var counter: Int = 0;
counter = 0;
while (counter < self.size)
{
if (self.is_vaild(grid, row, col, num) == true)
{
//Set this number of location [row][col]
grid[row][col] = num;
output[row][col] = num;
if (self.create_sudoku(&grid, &output, length - 1) == true)
{
// When sudoku is created
return true;
}
//reset values
grid[row][col] = 0;
output[row][col] = 0;
}
num += 1;
if (num > self.size)
{
num = 1;
}
counter += 1;
}
}
else
{
if (self.create_sudoku(&grid, &output, length) == true)
{
// When sudoku is created
return true;
}
}
return false;
}
//Generate a sudoku of given length of initial elements
//And also providing its solution
func generate_sudoku(_ grid: inout[
[Int]
], _ output: inout[
[Int]
], _ length: Int)
{
if (length <= 0 || length >= (self.size * self.size))
{
// When get invalid input
// Valid length are between (1 to 81) when size is 9
return;
}
// Set initial value of grid and resultant output
self.set_default(&grid, &output);
if (self.create_sudoku(&grid, &output, length) == true)
{
//Test solution of sudoku
if (self.test_sudoku(&output) == true)
{
//When get a valid solution
print("\n Created sudoku of initial ", length ," elements\n\n", terminator: "");
self.display_data(grid);
print("\n Solution \n\n", terminator: "");
self.display_data(output);
}
else
{
//Created sudoku are no solution, Try again ..
self.generate_sudoku(&grid, &output, length);
}
}
else
{
//When provide invalid length
print("\n Sudoku are not create \n", terminator: "");
}
}
}
func main()
{
let n: Int = 9;
let obj: SudokuGenerator = SudokuGenerator(n);
// Capable to store information of sudoku problem
var grid: [
[Int]
] = Array(repeating: Array(repeating: 0, count: n), count: n);
// This is used to get the result of sudoku
var output: [
[Int]
] = Array(repeating: Array(repeating: 0, count: n), count: n);
//Number of initial element in sudoku
var length: Int = 27;
obj.generate_sudoku(&grid, &output, length);
length = 34;
obj.generate_sudoku(&grid, &output, length);
}
main();
Output
Created sudoku of initial 27 elements
0 7 8 0 0 0 0 4 0
0 1 2 0 6 0 0 9 7
6 0 0 7 0 0 0 8 3
4 8 0 0 1 0 0 0 5
5 2 0 0 0 0 0 0 0
0 0 0 0 0 4 0 0 0
1 0 0 6 0 0 0 5 0
0 0 5 0 0 0 0 0 4
0 0 0 3 0 0 0 1 2
Solution
9 7 8 1 3 5 2 4 6
3 1 2 4 6 8 5 9 7
6 5 4 7 2 9 1 8 3
4 8 3 2 1 6 9 7 5
5 2 1 9 7 3 4 6 8
7 9 6 5 8 4 3 2 1
1 3 7 6 4 2 8 5 9
2 6 5 8 9 1 7 3 4
8 4 9 3 5 7 6 1 2
Created sudoku of initial 34 elements
7 0 3 0 0 0 0 0 0
0 6 0 0 0 7 1 0 0
8 0 0 3 2 0 6 7 0
0 0 0 0 1 8 4 9 5
5 0 0 7 0 0 0 3 0
0 0 1 5 0 0 0 6 0
9 0 0 0 3 4 0 2 0
0 8 0 0 0 0 0 0 4
1 4 6 9 8 0 0 5 7
Solution
7 1 3 4 6 9 5 8 2
2 6 9 8 5 7 1 4 3
8 5 4 3 2 1 6 7 9
6 3 7 2 1 8 4 9 5
5 9 8 7 4 6 2 3 1
4 2 1 5 9 3 7 6 8
9 7 5 1 3 4 8 2 6
3 8 2 6 7 5 9 1 4
1 4 6 9 8 2 3 5 7
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment