Skip to content
This repository was archived by the owner on Jan 6, 2022. It is now read-only.

Commit 08934f9

Browse files
committedJun 4, 2020
Random Question Generator Added
1 parent ef2bf5a commit 08934f9

File tree

10 files changed

+303
-11
lines changed

10 files changed

+303
-11
lines changed
 

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ Sudoku that is written in Java Swing
55
To play the game, Open the SudokuApp.java and run it.
66

77
## To add Question
8-
Only add question to problems folder with the name "question #" where # is the number.
8+
Only add question to problems folder with the name "problem #" where # is the number.
99
The question has to be added with space between each column and new line between each row.

‎src/backend/Answer.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,40 @@ public class Answer {
1313
*/
1414
public static boolean solved(int[][] grid)
1515
{
16-
if (vertical(grid) && horizontal(grid) && gridWay(grid))
17-
return true;
18-
return false;
16+
return strictVertical(grid) && strictHorizontal(grid) && strictGridWay(grid);
1917
}
2018

21-
private static boolean vertical(int[][] grid)
19+
protected static boolean canPut(int[][] grid, int row, int col, int val)
20+
{
21+
return vertical(grid,col, val) && horizontal(grid, row, val) && gridWay(grid, row, col, val);
22+
}
23+
24+
protected static boolean vertical(int[][] grid, int col, int val)
25+
{
26+
for (int row = 0; row < 9; row++)
27+
if (grid[row][col] == val)
28+
return false;
29+
return true;
30+
}
31+
32+
protected static boolean horizontal(int[][] grid, int row, int val)
33+
{
34+
for (int col = 0; col < 9; col++)
35+
if (grid[row][col] == val)
36+
return false;
37+
return true;
38+
}
39+
40+
protected static boolean gridWay(int[][] grid, int i, int j, int val)
41+
{
42+
for (int row = 0; row < 3; row++)
43+
for (int col = 0; col < 3; col++)
44+
if (grid[row + i/3 * 3][col + j/3 * 3] == val)
45+
return false;
46+
return true;
47+
}
48+
49+
private static boolean strictVertical(int[][] grid)
2250
{
2351
// Vertical Check
2452
for (int col = 0; col < grid.length; col++)
@@ -36,7 +64,7 @@ private static boolean vertical(int[][] grid)
3664
return true;
3765
}
3866

39-
private static boolean horizontal(int[][] grid)
67+
private static boolean strictHorizontal(int[][] grid)
4068
{
4169
// Horizontal Check
4270
for (int row = 0; row < grid.length; row++)
@@ -54,7 +82,7 @@ private static boolean horizontal(int[][] grid)
5482
return true;
5583
}
5684

57-
private static boolean gridWay(int[][] grid)
85+
private static boolean strictGridWay(int[][] grid)
5886
{
5987
// Grid way checking
6088
for (int i = 0; i < 3; i++)

‎src/backend/SudokuGenerator.java

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
*
3+
*/
4+
package backend;
5+
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
12+
import model.Sudoku;
13+
14+
/**
15+
* @author Programmerryoki
16+
*
17+
*/
18+
public class SudokuGenerator {
19+
public static Sudoku generateSudoku()
20+
{
21+
int[][] generate = notEfficientGenerateAnswerGrid();
22+
while (generate == null)
23+
generate = notEfficientGenerateAnswerGrid();
24+
int[][] question = new int[9][9];
25+
String[] order = new String[81];
26+
for (int i = 0; i < 9; i++)
27+
{
28+
for (int j = 0; j < 9; j++)
29+
{
30+
order[i*9+j] = i+""+j;
31+
question[i][j] = generate[i][j];
32+
}
33+
}
34+
List<String> strList = Arrays.asList(order);
35+
Collections.shuffle(strList);
36+
strList.toArray(order);
37+
for (int i = 0; i < strList.size(); i++)
38+
{
39+
int row = strList.get(i).charAt(0) - '0';
40+
int col = strList.get(i).charAt(1) - '0';
41+
int val = question[row][col];
42+
question[row][col] = 0;
43+
int c = 0;
44+
for (int j = 1; j < 10; j++)
45+
if (Answer.canPut(question, row, col, j))
46+
c++;
47+
if (c != 1)
48+
question[row][col] = val;
49+
}
50+
Sudoku answer = new Sudoku();
51+
answer.setAnswer(generate);
52+
answer.setOriginal(question);
53+
return answer;
54+
}
55+
56+
private static int[] possibleList(int[][] grid, int i, int j)
57+
{
58+
int[] poss = new int[] {1,1,1,1,1,1,1,1,1};
59+
for (int k = 0; k < 9; k++)
60+
poss[grid[i][k] - 1] = 0;
61+
for (int k = 0; k < 9; k++)
62+
poss[grid[k][j] - 1] = 0;
63+
i = i/3;
64+
j = j/3;
65+
for (int a = 0; a < 3; a++)
66+
for (int b = 0; b < 3; b++)
67+
poss[grid[i+a][j+b] - 1] = 0;
68+
return poss;
69+
}
70+
71+
private static boolean isPossible(int[][] grid, int i, int j, int val)
72+
{
73+
for (int k = 0; k < 9; k++)
74+
if (grid[i][k] == val)
75+
return false;
76+
for (int k = 0; k < 9; k++)
77+
if (grid[k][j] == val)
78+
return false;
79+
for (int a = 0; a < 3; a++)
80+
for (int b = 0; b < 3; b++)
81+
if (grid[i+a][j+b] == val)
82+
return false;
83+
return true;
84+
}
85+
86+
private static int[][] notEfficientGenerateAnswerGrid()
87+
{
88+
int[][] ans = new int[9][9];
89+
// return genAns(ans);
90+
91+
for (int i = 0; i < 9; i++)
92+
{
93+
for (int j = 0; j < 9; j++)
94+
{
95+
int n = (int)(Math.random()*9 + 1);
96+
HashSet<Integer> count = new HashSet<>();
97+
while (!Answer.canPut(ans, i, j, n) && count.size() < 9)
98+
{
99+
n = (int)(Math.random()*9 + 1);
100+
count.add(n);
101+
}
102+
if (count.size() == 9)
103+
{
104+
return null;
105+
}
106+
ans[i][j] = n;
107+
}
108+
}
109+
return ans;
110+
}
111+
112+
// private static int[][] genAns(int[][] grid)
113+
// {
114+
// int[] randOrd = new int[] {1,2,3,4,5,6,7,8,9};
115+
// List<Integer> intList = Arrays.asList(randOrd);
116+
// Collections.shuffle(intList);
117+
// intList.toArray(randOrd);
118+
//
119+
// boolean change = true;
120+
// for (int i = 0; i < 9; i++)
121+
// {
122+
// for (int j = 0; j < 9; j++)
123+
// {
124+
// if (grid[i][j] == 0)
125+
// {
126+
// change = true;
127+
// int val = (int)(Math.random()*9 + 1);
128+
// if (isPossible(grid, i, j, val))
129+
// {
130+
// int[][] temp = new int[9][9];
131+
// for (int row = 0; row < 9; row++)
132+
// for (int col = 0; col < 9; col++)
133+
// temp[i][j] = grid[i][j];
134+
// temp[i][j] = val;
135+
// genAns(grid);
136+
// }
137+
// }
138+
// }
139+
// }
140+
// if (change)
141+
// return
142+
// }
143+
144+
// public static void main(String[] args)
145+
// {
146+
//
147+
// int[][] grid = notEfficientGenerateAnswerGrid();
148+
// int c = 0;
149+
// while (grid == null)
150+
// {
151+
// grid = notEfficientGenerateAnswerGrid();
152+
// c ++;
153+
// }
154+
// System.out.println(c);
155+
// for (int[] i : grid)
156+
// {
157+
// for (int j : i)
158+
// {
159+
// System.out.print(j + " ");
160+
// }
161+
// System.out.println();
162+
// }
163+
// System.out.println(Answer.solved(grid));
164+
// }
165+
}

‎src/problems/SudokuIO.java renamed to ‎src/backend/SudokuIO.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

2-
package problems;
2+
package backend;
33

44
import java.io.BufferedReader;
55
import java.io.File;
66
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.nio.file.DirectoryStream;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
712

813
/**
914
* The IO for the Sudoku
@@ -40,4 +45,22 @@ public static int[][] readFile(String fileName)
4045

4146
return input;
4247
}
48+
49+
public static int[][] getRandomGrid()
50+
{
51+
Path dir = Paths.get("src/problems/");
52+
int i = 0;
53+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir))
54+
{
55+
for (Path p : stream)
56+
i++;
57+
}
58+
catch (IOException ex)
59+
{
60+
ex.printStackTrace();
61+
}
62+
63+
String fileName = "problem " + (int)(Math.random() * i + 1);
64+
return readFile(fileName);
65+
}
4366
}

‎src/controller/MasterController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import javax.swing.JFrame;
66
import javax.swing.JPanel;
77

8+
import backend.SudokuGenerator;
9+
import backend.SudokuIO;
810
import model.Sudoku;
9-
import problems.SudokuIO;
1011
import view.MasterView;
1112

1213
/**
@@ -21,7 +22,9 @@ public class MasterController {
2122
public MasterController()
2223
{
2324
Sudoku example = new Sudoku();
24-
example.setOriginal(SudokuIO.readFile("question 1"));
25+
example = SudokuGenerator.generateSudoku();
26+
// example.setOriginal(SudokuIO.getRandomGrid());
27+
// example.setOriginal(SudokuIO.readFile("question 1"));
2528
sudoku = new SudokuController(example, this);
2629
// sudoku = new SudokuController(new Sudoku(), this);
2730
mainView = new MasterView(sudoku.getView());

‎src/controller/SudokuController.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import javax.swing.JOptionPane;
88

99
import backend.Answer;
10+
import backend.SudokuGenerator;
11+
import backend.SudokuIO;
1012
import model.Sudoku;
1113
import view.SudokuView;
1214

@@ -56,9 +58,19 @@ else if (action.equals("Check"))
5658
{
5759
int[][] check = view.getGridNumbers();
5860
if (Answer.solved(check))
61+
{
5962
JOptionPane.showMessageDialog(view, "It is Correct!");
63+
int option = JOptionPane.showConfirmDialog(view, "Would you like to Continue?", "Continue?", JOptionPane.YES_NO_OPTION);
64+
System.out.println(option);
65+
if (option == 0)
66+
{
67+
model = SudokuGenerator.generateSudoku();
68+
view.fixGridNumbers(model.getOriginal());
69+
view.resetColor();
70+
}
71+
}
6072
else
61-
JOptionPane.showMessageDialog(null, "It is Wrong");
73+
JOptionPane.showMessageDialog(view, "It is Wrong");
6274
}
6375
else if (action.substring(0, 5).equals("Radio"))
6476
{

‎src/model/Sudoku.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public Sudoku()
1010
{
1111
}
1212

13+
public Sudoku(int[][] original)
14+
{
15+
this.original = original;
16+
}
17+
1318
/**
1419
* Gets the grid
1520
* @return The grid
@@ -41,7 +46,45 @@ public int[][] getOriginal() {
4146
public void setOriginal(int[][] original) {
4247
this.original = original;
4348
}
49+
50+
/**
51+
* Gets the answer
52+
* @return The answer
53+
*/
54+
public int[][] getAnswer()
55+
{
56+
return answer;
57+
}
58+
59+
/**
60+
* Sets the answer
61+
* @param grid The answer to set
62+
*/
63+
public void setAnswer(int[][] grid)
64+
{
65+
this.answer = grid;
66+
}
67+
68+
/**
69+
* Checks if the answer given is correct or not
70+
* @param grid The grid to check
71+
* @return True if it is correct
72+
* @precondition answer cannot be null
73+
*/
74+
public boolean compareAnswer(int[][] grid)
75+
{
76+
for (int i = 0; i < 9; i++)
77+
{
78+
for (int j = 0; j < 9; j++)
79+
{
80+
if (grid[i][j] != answer[i][j])
81+
return false;
82+
}
83+
}
84+
return true;
85+
}
4486

4587
private int[][] grid;
4688
private int[][] original;
89+
private int[][] answer;
4790
}
File renamed without changes.

‎src/problems/problem 2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2 0 0 3 0 0 0 0 0
2+
8 0 4 0 6 2 0 0 3
3+
0 1 3 8 0 0 2 0 0
4+
0 0 0 0 2 0 3 9 0
5+
5 0 7 0 0 0 6 2 1
6+
0 3 2 0 0 6 0 0 0
7+
0 2 0 0 0 9 1 4 0
8+
6 0 1 2 5 0 8 0 9
9+
0 0 0 0 0 1 0 0 2

0 commit comments

Comments
 (0)