-
Notifications
You must be signed in to change notification settings - Fork 0
/
SudokuGen.java
executable file
·88 lines (79 loc) · 1.75 KB
/
SudokuGen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.*;
public class SudokuGen {
static int sudoku[][] = new int[4][4];
public static void main(String[] args) {
int n =1,k;
Random r = new Random();
for(int i=0;i<4;i++){//Generate basic sudoku matrix
k=n;
for(int j=0;j<4;j++){
if(k<=4){
sudoku[i][j]=k;
k++;
}else{
k=1;
sudoku[i][j]=k;
k++;
}
}
n=k+2;
if(k==5)
n=3;
if(n>4)
n=(n%4)+1;
}
//Swapping rows randomly within their blocks
int k1 = 0, k2=1;//First block
for(int i=0;i<2;i++){
int check = r.nextInt(2);
if(check==1){
for(int j=0;j<4;j++)
{
int temp=sudoku[k1][j];
sudoku[k1][j]=sudoku[k2][j];
sudoku[k2][j]=temp;
}
}
k1=2;//Second block
k2=3;
}
//Swapping columns randomly within their blocks
k1 = 0;//First column block
k2 = 1;
for(int i=0;i<2;i++){
int check = r.nextInt(2);
if(check==1){
for(int j=0;j<4;j++)
{
int temp=sudoku[j][k1];
sudoku[j][k1]=sudoku[j][k2];
sudoku[j][k2]=temp;
}
}
k1=2;//Second column block
k2=3;
}
//int limit = r.nextInt(17); to remove arbitrary number of elements
for(int a=0;a<8;a++){//Randomly remove 8 elements
int i = r.nextInt(4);
int j = r.nextInt(4);
if(sudoku[i][j] == 0)
a--;
else
sudoku[i][j] = 0;
}
for(int i=0;i<4;i++){//Printing the sudoku to console
for(int j=0;j<4;j++){
if(sudoku[i][j] == 0)
System.out.print("( )\t");
else
System.out.print(" "+sudoku[i][j]+ "\t");
if(i==1&&j==3)
System.out.println("\n\n------------------------------------");
else if(j==1)
System.out.print("|\t");
}
System.out.println("");
}
}
}