-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
234 lines (203 loc) · 6.61 KB
/
Board.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
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
import java.util.Scanner;
public class Board {
private Tile[][] grid;
private Tile[][] squares;
private static final int size = 9;
private static final int sizeRoot = (int) Math.sqrt(size);
private int steps;
// private VisualGrid vg;
public Board(int[] input) {
grid = new Tile[size][size];
squares = new Tile[size][size];
steps = 0;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
grid[x][y] = new Tile(input[x * size + y], size);
squares[(x / sizeRoot) * sizeRoot + y / sizeRoot][(x % sizeRoot) * sizeRoot
+ y % sizeRoot] = grid[x][y];
}
}
}
public String toString() {
String ret = "";
boolean solved = true;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (y % sizeRoot == 0)
ret += "|";
ret += (grid[x][y].getAnswer() == 0 ? " "
: "\u001b[" + (39 + grid[x][y].getAnswer()) + "m" + grid[x][y].getAnswer() + "\u001b[0m") + "|";
solved = solved && grid[x][y].solved();
}
ret += "\n";
}
if (solved)
ret += "Done!";
return ret;
}
public String opsLenToString() {
String ret = "";
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (y % sizeRoot == 0)
ret += "|";
ret += grid[x][y].getOps().size() + "|";
}
ret += "\n";
}
return ret;
}
public boolean isDone() {
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (!grid[x][y].solved())
return false;
}
}
return true;
}
private int[][] getRelations(int x, int y) {
int save = grid[x][y].getAnswer();
grid[x][y].setAnswer(0);
int[] row = new int[size];
int[] col = new int[size];
int[] sq = new int[size];
for (int i = 0; i < size; i++) {
row[i] = grid[x][i].getAnswer();
col[i] = grid[i][y].getAnswer();
sq[i] = squares[(x / sizeRoot) * sizeRoot + y / sizeRoot][i].getAnswer();
}
grid[x][y].setAnswer(save);
return new int[][] { row, col, sq };
}
public void setOptions() {
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (!grid[x][y].solved()) {
grid[x][y].setOps(getRelations(x, y));
//vg.updateAlt(convert());
}
}
}
}
/**
* @return false if the board has a problem, true if the board does not have a
* problem
*/
private boolean noIssues() {
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (!grid[x][y].isGood((getRelations(x, y))))
return false;
}
}
return true;
}
private int[] progress(int[] coords) {
int x = coords[0];
int y = coords[1];
x++;
if (x == size) {
x = 0;
y++;
}
return new int[] { x, y };
}
public boolean randomCheck(int[] coords) {
int x = coords[1];
int y = coords[0];
if (grid[x][y].getOps().size() == 1) {
int[] progressed = progress(new int[] { x, y });
if (progressed[1] == size)
return true;
return randomCheck(progressed);
} else {
int index = 0;
grid[x][y].setAnswer(grid[x][y].getOps().get(index));
boolean nextOk = true;
while (index < grid[x][y].getOps().size()) {
steps++;
grid[x][y].setAnswer(grid[x][y].getOps().get(index));
if (noIssues()) {
int[] progressed = progress(new int[] { x, y });
if (progressed[1] == size)
return true;
nextOk = randomCheck(progressed);
if (nextOk)
break;
}
if (!noIssues() || !nextOk) {
index++;
if (index >= grid[x][y].getOps().size()) {
index = 0;
grid[x][y].setAnswer(0);
return false;
}
}
}
}
return grid[x][y].isGood(getRelations(x, y));
}
public int[] convert() {
int[] ret = new int[size * size];
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
ret[x * size + y] = grid[x][y].getAnswer();
}
}
return ret;
}
public int getAt(int x, int y) {
return grid[x][y].getAnswer();
}
public Tile[][] getBoard() {
return grid;
}
public boolean equals(Object o) {
Board p = (Board) o;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (p.getAt(x, y) != this.getAt(x, y))
return false;
}
}
return true;
}
public int getSteps() {
return steps;
}
public static void main(String[] args) throws Exception {
// https://www.nytimes.com/puzzles/sudoku/hard
String[] allBoards = ReadFile.getInput();
// Change this to change the board
int gameIndex = 0;
String thisGame = allBoards[gameIndex];
int[] grid = new int[81];
for (int i = 0; i < 81; i++) {
grid[i] = Integer.parseInt(thisGame.substring(i, i + 1));
}
long startTime = System.currentTimeMillis();
Board b = new Board(grid);
//b.makeGraphic(grid);
System.out.println("Original\n" + b);
Board save = new Board(b.convert());
b.setOptions();
while (!save.equals(b)) {
b.setOptions();
save = new Board(b.convert());
}
b.setOptions();
System.out.println("Initial Runthrough\n" + b);
//int p = 0;
while (!b.isDone()) {
b.randomCheck(new int[] { 0, 0 });
b.setOptions();
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
System.out.println(b);
System.out.println(b.getSteps());
System.out.println(b.noIssues());
// System.out.println(b.getAt(1, 0));
}
}