-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudoku.java
More file actions
247 lines (239 loc) · 5.21 KB
/
Sudoku.java
File metadata and controls
247 lines (239 loc) · 5.21 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
/*Matthew Digman
Matthew Digman
Manages the sudoku board and contains the rules*/
import java.util.*;
public class Sudoku{
private int[][] matrix;
private int backtracks;
public Sudoku(String puzzle){
matrix=new int[9][9];
for(int x=0; x<9; x++)
for(int y=0; y<9; y++)
matrix[x][y]=Integer.parseInt(""+puzzle.charAt(x*9+y));
}
public String toString(){
String str="\n";
for(int x=0; x<9; x++){
for(int y=0; y<9; y++){
str += matrix[x][y]+" ";
}
}
return str;
}
// fills in all blanks that admit a single possibility
public void rule1(){
boolean done=false;
while(!done){
done=true;
for(int x=0; x<9; x++){
for(int y=0; y<9; y++){
if(matrix[x][y]==0){
int value=findUniqueValueFor(x,y);
if(value!=0){
matrix[x][y]=value;
done=false;
}
}
}
}
}
}
// This method is project 12-9. It considers all numbers missing from a row and determines is there is only one place within said row that the number can go.
public void rule2(){
boolean done=false;
while(!done){
done=true;
for(int y=0; y<9; y++){
List<Integer> missingNum=new LinkedList<Integer>();
for(int itr=1; itr<=9; itr++){
boolean test=true;
for(int x=0; x<9; x++){
if(matrix[y][x]==itr){
test=false;
break;
}
}
if(test)
missingNum.add(itr);
else
test=true;
}
for(int testNum:missingNum){
int loc=-1;
for(int x=0; x<9; x++){
if(probeIsOkay(testNum, y,x)){
if(loc!=-1){
loc=-1;
break;
}else{
loc=x;
}
}
}
if(loc!=-1){
matrix[y][loc]=testNum;
done=false;
loc=-1;
}
}
}
}
}
//This method is project 12-10. it considers all numbers missing from a row and determines if there is only one place within said row that number can go.
public void rule3(){
boolean done=false;
while(!done){
done=true;
for(int y=0; y<9; y++){
List<Integer> missingNum=new LinkedList<Integer>();
for(int itr=1; itr<=9; itr++){
boolean test=true;
for(int x=0; x<9; x++){
if(matrix[x][y]==itr){
test=false;
break;
}
}
if(test)
missingNum.add(itr);
else
test=true;
}
for(int testNum:missingNum){
int loc=-1;
for(int x=0; x<9; x++){
if(probeIsOkay(testNum, x,y)){
if(loc!=-1){
loc=-1;
break;
}else{
loc=x;
}
}
}
if(loc!=-1){
matrix[loc][y]=testNum;
done=false;
loc=-1;
}
}
}
}
}
//though not required for any project, this recursive method uses a modified brute force strategy to solve a puzzle, no matter how long it takes.
public int breakGlass(){
int x=-1;
int y=0;
backtracks=0;
rule4(x,y);
return backtracks;
}
public boolean rule4(int x, int y){
if(x>=8){
x=0;
y++;
}else x++;
if(matrix[x][y]!=0){
if(!(x==8&&y==8)){
if(rule4(x,y))
return true;
else return false;
}else return true;
}else{
for(int itr=1; itr<=9; itr++){
if(probeIsOkay(itr,x,y)){
matrix[x][y]=itr;
if(!(x==8&&y==8)){
if(rule4(x,y)){
return true;
}else{
matrix[x][y]=0;
}
}else return true;
}
}
backtracks++;
return false;
}
}
//method that trys rules 1-3
public void tryRules(){
int numZeros=countNumberOfZeros();
int oldNumZeros=numZeros+1;
while(numZeros<oldNumZeros){
oldNumZeros=numZeros;
rule1();
rule2();
rule3();
if(countNumberOfZeros()==0)
return;
numZeros=countNumberOfZeros();
}
}
//for use in brute force strategy, determines if the program has done something impossible
private boolean isContradiction(){
for(int x=0; x<9; x++){
for(int y=0; y<9; y++){
if(matrix[x][y]==0){
boolean hasPossibility=false;
for(int itr=1; itr<=9; itr++){
if(probeIsOkay(itr, x,y)){
hasPossibility=true;
break;
}
}
if(!hasPossibility){
System.out.print("Contradiction detected");
return false;
}else{
hasPossibility=false;
}
}
}
}
return true;
}
private int findUniqueValueFor(int x, int y){
int value=0;
for(int v=1; v<=9; v++){
if(probeIsOkay(v,x,y)){
if(value==0)
value=v;
else
return 0;
}
}
return value;
}
private boolean probeIsOkay(int probe, int x, int y){
if(matrix[x][y]!=0)
return false;
for(int itr=0; itr<9; itr++)
if(itr!=y){
if(probe==matrix[x][itr])
return false;
}
for(int itr=0; itr<9; itr++)
if(itr!=x){
if(probe==matrix[itr][y])
return false;
}
int topLeftRow=x-x%3;
int topLeftColumn=y-y%3;
for(int ii=topLeftRow; ii<topLeftRow+3; ii++)
for(int jj=topLeftColumn; jj<topLeftColumn+3; jj++)
if(ii!=x||jj!=y){
if (probe==matrix[ii][jj])
return false;
}
return true;
}
public int countNumberOfZeros(){
int count=0;
for(int x=0; x<9; x++)
for(int y=0; y<9; y++)
if(matrix[x][y]==0)
count++;
return count;
}
}