-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuExtension4.java
128 lines (92 loc) · 3.72 KB
/
SudokuExtension4.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
/*
File name: SudokuExtension4.java
Author: Nafis Saadiq Bhuiyan
Colby ID: 778267
Course: CS231B
Lab Section: CS231LC
Project 4
*/
//We have changed this class and used an ArrayStack instead of a node stack
public class SudokuExtension4 {
private ArrayStack mystack;
private Board board;
private LandscapeDisplay ld;
public SudokuExtension4(int locked) {
board = new Board(locked);
ld = new LandscapeDisplay(board);
}
public boolean solve(int delay) throws InterruptedException {
mystack = new ArrayStack();
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 <= 9; 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 < 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();
}
/*
* The main method here is almost the same as the main method of our Sudoku.java. We created his to compare the time required in a nodestack and an arraystack.
*/
public static void main(String[] args) throws InterruptedException {
long startTime = System.nanoTime();
SudokuExtension4 ourSudoku = new SudokuExtension4(10);
System.out.println("The sudoku board is ");
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");
}
}