-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuExtension3.java
200 lines (150 loc) · 6.83 KB
/
SudokuExtension3.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
File name: SudokuExtension3.java
Author: Nafis Saadiq Bhuiyan
Colby ID: 778267
Course: CS231B
Lab Section: CS231LC
Project 4
*/
import java.util.HashMap;
import java.util.Random;
public class SudokuExtension3 {
private CellStack mystack;
private Board board;
private LandscapeDisplay ld;
public SudokuExtension3(int locked, int size) {
board = new Board(locked, size);
ld = new LandscapeDisplay(board);
}
public boolean solve(int delay) throws InterruptedException {
mystack = new CellStack();
while (mystack.size() < board.getRows() * board.getCols() - board.numLocked()) {
if (delay > 0)
Thread.sleep(delay);
if (ld != null)
ld.repaint();
Cell nextCell = this.findNextCell();
if (nextCell != null) {
mystack.push(nextCell);
board.set(nextCell.getRow(), nextCell.getCol(), nextCell.getValue());
}
else {
while (!mystack.empty()) {
Cell popped = mystack.pop();
boolean stuck = true;
for (int i = popped.getValue() + 1; i <= board.getRows(); i++) {
if (board.validValue(popped.getRow(), popped.getCol(), i)) {
popped.setValue(i);
mystack.push(popped);
board.set(popped.getRow(), popped.getCol(), popped.getValue());
stuck = false;
break;
}
}
if (stuck){
popped.setValue(0);
}
else {
break;
}
}
if (mystack.empty()) {
System.out.println("There's no solution");
return false;
}
}
}
System.out.println("The board contains the solution");
return true;
}
// public Cell findNextCell() {
// for (int i = 0; i < board.getRows(); i++) {
// for (int j = 0; j < board.getCols(); j++) {
// if (board.get(i, j).getValue() == 0){
// for (int m = 1; m < board.getCols() + 1; m++) {
// if (board.validValue(i, j, m) == true) {
// board.set(i, j, m);
// return board.get(i, j);
// }
// }
// return null;
// }
// }
// }
// return null;
// }
public Cell findNextCell() {
//HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
Random r = new Random();
for (int i = 0; i < board.getRows(); i++) {
for (int j = 0; j < board.getCols(); j++) {
if (board.get(i, j).getValue() == 0){
/**
* Try a Hashmap, check if you ahve gone through all values from 1 to 10 of the hashmap. The reason is that trying a
* random value can be a better approach rather going doing brute force from 1 to 10.
*/ HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
//whike
int m = r.nextInt(board.getRows()+1);
while (!map.containsKey(m) && map.size() <board.getCols()) {
if (board.validValue(i, j, m) == true) {
board.set(i, j, m);
map.put(m, true);
return board.get(i, j);
}
else {
map.put(m,false);
/*
* Try another value from 1 to 9 that previously has not been visited
*/
if (map.size () < board.getCols() ) {
m = r.nextInt(board.getRows());
while (map.containsKey(m)){
m = r.nextInt(board.getRows());
}
}
}
}
// for (int m = 1; m < 10; m++) {
// if (board.validValue(i, j, m) == true) {
// board.set(i, j, m);
// return board.get(i, j);
// }
// }
return null;
}
}
}
return null;
}
public String toString() {
return board.toString();
}
/*We have made use of the command line arguments for taking in the grid size and also the number of locked cells. We changed the Sudoku constructor so that it takes
the grid size and the number of locked cells as the parameter.
*/
public static void main(String[] args) throws InterruptedException {
long startTime = System.nanoTime();
if (args.length != 2) {
System.out.println("Please enter the size of your sudoku (e.g 1, 4, 9,...), and make sure it's a perfect square, also enter the number of locked cells, and this number should be less than the total number of cells in your sudoku grid");
System.out.println("Enter the size and number of locked cells with just a space between them");
return;
}
int size = Integer.parseInt(args[0]);
int lockedCells = Integer.parseInt(args[1]);
if (Math.sqrt(size) % 1 != 0) {
System.out.println("Please enter a perfect square number as the grid size");
return;
}
if (lockedCells >= size*size) {
System.out.println("Number of locked cells must be less than the total number of cells");
return;
}
SudokuExtension3 ourSudoku = new SudokuExtension3(lockedCells, size);
System.out.println(ourSudoku);
ourSudoku.solve(0);
System.out.println(ourSudoku);
long endTime = System.nanoTime();
long elapsedTime = (endTime - startTime);
System.out.println("Time required to solve the sudoku is " + elapsedTime / 1000000 + " milisecond");
}
}