-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuSolver.java
More file actions
253 lines (237 loc) · 7.22 KB
/
SudokuSolver.java
File metadata and controls
253 lines (237 loc) · 7.22 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.NoSuchElementException;
/**
* Solves a 9-by-9 Sudoku puzzle
*
* @author Patrick McCarty
*
*/
public class SudokuSolver {
private static HashMap<Integer, ArrayList<Integer>> rows; // contains all 9 rows of the board
private static HashMap<Integer, ArrayList<Integer>> cols; // contains all 9 cols of the board
private static HashMap<Integer, ArrayList<Integer>> blocks; // contains all 9 blocks of the board
private static int[] vals; // the array that is the actual puzzle
private static ArrayList<Integer> missingNums; // the blank squares that need to be filed in
/**
* Sets up the puzzle in a way so it can be solved
*
* @param arr the Sudoku puzzle in the form of a 1 dimensional array going left to right and
* starting from the top and making its way to the bottom. Row 1 column 1 is the first element
* in the array and row 9 column 9 is the last element in the array
*/
private static void boardSetup(int[] arr) {
rows = new HashMap<Integer, ArrayList<Integer>>();
cols = new HashMap<Integer, ArrayList<Integer>>();
blocks = new HashMap<Integer, ArrayList<Integer>>();
missingNums = new ArrayList<Integer>();
vals = Arrays.copyOf(arr, arr.length);
// populates rows, cols and blocks with keys 0-8 and values of empty Arraylists in each key
for(int i = 0; i < 9; i++) {
rows.put(i, new ArrayList<Integer>());
cols.put(i, new ArrayList<Integer>());
blocks.put(i, new ArrayList<Integer>());
}
// loops through the contents of input array and adds it to its speciffic row, col and block
for(int i = 0; i < arr.length; i++) {
if(arr[i] != 0) {
rows.get(i/9).add(arr[i]);
cols.get(i%9).add(arr[i]);
blocks.get(findBlock(i)).add(arr[i]);
}
if(arr[i] == 0) {
missingNums.add(i);
}
}
}
/**
* Checks if the number trying to be placed in the given index is already present in the row,
* column or block
*
* @param indx the index in the vals array
* @param val the number being tried
* @return true if the number can be placed at that index, false otherwise
*/
public static boolean checkPossible(int indx, int val) {
if(val == 0) {
return false;
}
int row = indx / 9;
int col = indx % 9;
int block = findBlock(indx);
// check if the number can be placed at that spot on the board
if(rows.get(row).contains(val) || cols.get(col).contains(val)
|| blocks.get(block).contains(val)) {
return false;
}
return true;
}
/**
* Finds which 3-by-3 block the given index belongs to. Blocks are read left to right
* then going to the next line
*
* @param i index of the number
* @return the block from 0-8 that it belongs too
*/
private static int findBlock(int i) {
int m = i % 9;
int d = i / 9;
if(m < 3 && d < 3) {
return 0;
}
else if(m < 6 && d < 3) {
return 1;
}
else if(m < 9 && d < 3) {
return 2;
}
else if(m < 3 && d < 6) {
return 3;
}
else if(m < 6 && d < 6) {
return 4;
}
else if(m < 9 && d < 6) {
return 5;
}
else if(m < 3 && d < 9) {
return 6;
}
else if(m < 6 && d < 9) {
return 7;
}
else if(m < 9 && d < 9) {
return 8;
}
else {
throw new NoSuchElementException(); // a problem occured
}
}
/**
* Updates the values of the Hashmaps that keep track of what numbers are in which rows, columns
* and blocks using the new values of the vals array
*/
private static void updateVals() {
rows.clear();
cols.clear();
blocks.clear();
for(int i = 0; i < 9; i++) {
rows.put(i, new ArrayList<Integer>());
cols.put(i, new ArrayList<Integer>());
blocks.put(i, new ArrayList<Integer>());
}
for(int i = 0; i < vals.length; i++) {
if(vals[i] != 0) {
rows.get(i / 9).add(vals[i]);
cols.get(i % 9).add(vals[i]);
blocks.get(findBlock(i)).add(vals[i]);
}
}
}
/**
* Prints the solved Sudoku puzzle in an easy to interpret way
*/
private static void printResult() {
System.out.print(" -----------------");
for(int i = 0; i < vals.length; i++) {
if(i % 9 == 0) {
System.out.println();
System.out.print("|");
}
if(i % 9 == 8) {
System.out.print(vals[i] + "|");
}
else {
System.out.print(vals[i] + " ");
}
}
System.out.println();
System.out.print(" -----------------");
}
/**
* Solves a 9-by-9 Sudoku puzzle
*
* @param arr the Sudoku puzzle in the form of a 1 dimensional array
* @param showWork whether or not the work done by the program is shown in a new window
*/
public static void solve(int[] arr, boolean showWork) {
boardSetup(arr); // set up the board so it can be solved
if(showWork) {
SudokuImage.setUpPuzzle(arr);
}
for(int i = 0; i < vals.length; i++) {
updateVals(); // update what numbers are in which rows/columns/blocks
if(i < 0) {
break;
}
if(!missingNums.contains(i)) { // a number given by the puzzle. Move on to next iteration
continue;
}
vals[i]++;
if(showWork) {
SudokuImage.updatePuzzle(i);
}
if(vals[i] > 9) { // no number can be placed at this spot
vals[i] = 0; // reset this number
if(showWork) {
SudokuImage.updatePuzzle(i);
}
if(i == missingNums.get(0)) {
System.out.println("unsolvable");
break; // the first missing number cannot be other than zero so no solution is possible
}
else {
i--;
// go back to the previous missing number
while(!missingNums.contains(i)) {
if(i < 0) {
break;
}
i--;
}
i--;
continue;
}
}
// find the first possible value for this position and update vals array
while(!checkPossible(i, vals[i])) {
vals[i]++;
if(showWork) {
SudokuImage.updatePuzzle(i);
}
if(vals[i] > 9) { // no possible number for this position
vals[i] = 0;
if(showWork) {
SudokuImage.updatePuzzle(i);
}
break;
}
}
// go back to the previous missing number
if(vals[i] == 0) {
i--;
while(!missingNums.contains(i)) {
if(i < 0) {
System.out.println("unsolvable");
break;
}
i--;
}
i--;
continue;
}
}
System.out.println("done!");
printResult();
}
/**
* Gets the number at the specified index of the Sudoku board
*
* @param indx index of the desired number
* @return the contents of the board at the desired location
*/
public static int getVal(int indx) {
return vals[indx];
}
}