-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
191 lines (168 loc) · 3.89 KB
/
Copy pathBoard.java
File metadata and controls
191 lines (168 loc) · 3.89 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
// Represents the Sudoku board
import java.util.ArrayList;
public class Board {
Square[][] squares;
public Board(Square[][] squares){
this.squares = squares;
}
public int get(int row, int col){
return squares[row][col].get();
}
public boolean set(int row, int col, int value){
if(this.squares[row][col].set(value)) return true;
return false;
}
public Square nextEmpty(){
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(this.get(i,j) == 0) return this.squares[i][j];
}
}
return new Square(-1,-1,-1,false);
}
// Get currently allowable assignments for variable
public ArrayList<Integer> varDomain(int row, int col){
ArrayList<Integer> d = new ArrayList<Integer>();
for(int i=1; i<10; i++){
if(meetsConstraints(row, col, i) && !this.squares[row][col].isGiven())
d.add(i);
}
return d;
}
// Return list of other variables in row, column, and box
public ArrayList<Square> getNeighbors(int row, int col){ // May need to check constraints
// Square current = new Square(row, col, 0, false);
ArrayList<Square> vars = new ArrayList<Square>();
// Get row neighbors
for(int i=0; i<9; i++){
// Skip if same
if(i == col) continue;
if(!this.squares[row][i].isGiven() && !vars.contains(squares[row][i]))
vars.add(this.squares[row][i]);
}
// Get column neighbors
for(int j=0; j<9; j++){
// Skip if same
if(j == row) continue;
if(!this.squares[j][col].isGiven() && !vars.contains(squares[j][col]))
vars.add(squares[j][col]);
}
// Get box neighbors
int r, c, limR, limC;
if(row<3){
r=0;
limR=r+3;
}else if(row>2 && row<6){
r=3;
limR=r+3;
}else{
r=6;
limR=r+3;
}
if(col<3){
c=0;
limC=c+3;
}else if(col>2 && col<6){
c=3;
limC=c+3;
}else{
c=6;
limC=c+3;
}
for(int k=r; k<limR; k++){
for(int l=c; l<limC; l++){
// Skip if same
if(k==row && l==col) continue;
if(!this.squares[k][l].isGiven() && !vars.contains(this.squares[k][l])){
vars.add(this.squares[k][l]);
}
}
}
return vars;
}
// Get all currently blank squares
public ArrayList<Square> getVars(){
ArrayList<Square> vars = new ArrayList<Square>();
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(!squares[i][j].isGiven() && squares[i][j].value == 0) // THIS CHANGED
vars.add(squares[i][j]);
}
}
return vars;
}
public boolean complete(){
if(this.nextEmpty().value == -1) return true;
return false;
}
// Reset board
public void clear(){
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(!squares[i][j].isGiven()) this.set(i,j,0);
}
}
}
public boolean noSolution(){
if(this.complete() && !meetsConstraints()){
return true;
}
return false;
}
public boolean meetsConstraints(){
// check all as is
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
if(!checkRow(i,j,this.get(i,j))) return false;
if(!checkCol(i,j,this.get(i,j))) return false;
if(!checkBox(i,j,this.get(i,j))) return false;
}
}
return true;
}
public boolean meetsConstraints(int row, int col, int v){
if(checkRow(row, col, v) && checkCol(row, col, v) && checkBox(row, col, v)) return true;
return false;
}
public boolean checkRow(int row, int col, int v){
for(int i=0; i<9; i++){
if(this.squares[row][i].get()==v) return false;
}
return true;
}
public boolean checkCol(int row, int col, int v){
for(int i=0; i<9; i++){
if(this.squares[i][col].get()==v) return false;
}
return true;
}
public boolean checkBox(int row, int col, int v){
int r, c, limR, limC;
if(row<3){
r=0;
limR=r+3;
}else if(row>2 && row<6){
r=3;
limR=r+3;
}else{
r=6;
limR=r+3;
}
if(col<3){
c=0;
limC=c+3;
}else if(col>2 && col<6){
c=3;
limC=c+3;
}else{
c=6;
limC=c+3;
}
for(int i=r; i<limR; i++){
for(int j=c; j<limC; j++){
if(this.squares[i][j].get()==v) return false;
}
}
return true;
}
}