-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuGrid.java
254 lines (226 loc) · 9.16 KB
/
SudokuGrid.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.scene.shape.*;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.scene.control.*;
import javafx.geometry.*;
import java.util.Random;
public class SudokuGrid extends Application
{
// grid[Subgrid][Row inside subGrid][Column inside subGrid]
Cell[][][] grid = new Cell[9][3][3]; // Holds all cells in the sudoku grid, used for calculations.
Label[][][] labels = new Label[9][3][3]; // Holds what's to be shown on the visual sudoku board.
GridPane board = new GridPane(); // This will represent the visual Sudoku board
Button solveBtn = new Button("Solve!");
Random gen = new Random();
public void start(Stage primary) throws BoundaryViolationException
{
// Fill the grid matrix with Cells as if we were reading the sudoku puzzle left to right, row by row.
// Initialize the grid matrix and labels matrix.
for (int subGrid = 0; subGrid < 9; subGrid++)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
grid[subGrid][row][col] = new Cell(0, subGrid, row, col);
labels[subGrid][row][col] = new Label("0");
}
}
}
solve(0,0,0);
setHints(40);
removeNonHints();
// Create 9 sub-grid GridPanes and place them in correct order into the board GridPane.
for (int subGrid = 0; subGrid < 9; subGrid++)
{
GridPane subGridPane = new GridPane();
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
Cell currentCell = grid[subGrid][row][col]; // Retrieve the current cell from the grid matrix
Label currentLabel = labels[subGrid][row][col];
String number = Integer.toString(currentCell.getData());
// Create and stylize each number on the sudoku board.
currentLabel.setText(number);
if (number.equals("0")) // If the value of the cell is 0, then make the cell look empty.
{
currentLabel.setTextFill(Color.WHITE);
}
currentLabel.setStyle("-fx-border-color: rgb(170, 170, 170); -fx-padding: 10 24 10 24;");
currentLabel.setFont(new Font(20));
// Place the number into the correct cell on the board.
GridPane.setConstraints(currentLabel, col, row);
subGridPane.getChildren().add(currentLabel); // Add the label to the Regional Box
}
}
// Sub-grid GridPane styling.
subGridPane.setHgap(0);
subGridPane.setVgap(0);
subGridPane.setStyle("-fx-background-color: white;");
// Place the sub-grid into the correct spot.
GridPane.setConstraints(subGridPane, subGrid % 3, subGrid / 3);
board.getChildren().add(subGridPane);
}
// Sudoku board styling
board.setHgap(4);
board.setVgap(3);
board.setTranslateX(95);
board.setTranslateY(50);
board.setStyle("-fx-background-color: black; -fx-padding: 1;");
// Sudoku solve button
solveBtn.setTranslateX(324);
solveBtn.setTranslateY(540);
solveBtn.setPrefWidth(100);
solveBtn.setOnAction(this::solveAction);
// Add the sudoku board to the scene and show it.
Group root = new Group(board, solveBtn);
Scene scene = new Scene(root, 800, 600, Color.rgb(240,240,240));
primary.setTitle("Sudoku");
primary.setScene(scene);
primary.show();
}
// We are recursively solving each cell on the sudoku board.
public boolean solve(int subGrid, int row, int col) throws BoundaryViolationException
{
//System.out.println("CALL: solve(subGrid = " + subGrid + ", row = " + row + ", col = " + col + ")");
int s = subGrid;
int r = row;
int c = col + 1;
// Base case
if (c > 2) // Move to next column
{
if (r < 2) // Move to next row
{
r = row + 1;
c = 0;
}
else // Move to next subGrid
{
if (s < 8)
{
s = s + 1;
r = 0;
c = 0;
}
else if (col > 2)
{
return true;
}
}
}
Cell current = grid[subGrid][row][col]; // Current cell
if (current.isHint())
{
if (solve(s, r, c)) // If the recursive solve is successful, then return true.
{
return true;
}
else
{
return false;
}
}
int rank; // Rank to use within the ranked sequence of valid numbers.
int num; // Number to add to the empty cell.
RankSeq validNumbers = current.getValidNumbers(); // Current cell's valid number choices.
//boolean mustBackTrack = true; // If we run out of number choices, this will remain true.
int maxAttempts = validNumbers.size();
for (int attempts = 0; attempts < maxAttempts; attempts++)
{
rank = gen.nextInt(validNumbers.size())+1; // Generate a random rank.
num = (validNumbers.nodeAtRank(rank)).getData(); // Use that rank to get a valid number from the validNumbers ranked sequence.
validNumbers.deleteItem(rank); // In case we must backtrack, remove this number from valid numbers.
current.setValidNumbers(validNumbers); // Replace the cell's old valid numbers with an updated list of valid numbers.
validNumbers = current.getValidNumbers(); // Store the cell's new list of valid number choices.
if (Check.valid(grid, num, current)) // Check if placing the random number there is valid or not.
{
current.setData(num); // Set the cell to the random number.
if (solve(s, r, c)) // If the recursive solve is successful, then return true.
{
return true;
}
}
else // Set the cell back to 0 and try another number.
{
current.setData(0);
}
}
// If the for loop completes without returning, then we must backtrack.
current.setData(0); // Reset this current cell's data to 0.
current.initializeValidNumbers(); // Refill the ranked sequence of valid numbers.
return false;
}
public void setHints(int numberOfHints)
{
for (int i = 0; i < numberOfHints; i++)
{
int subgrid = gen.nextInt(9);
int row = gen.nextInt(3);
int col = gen.nextInt(3);
if (!grid[subgrid][row][col].makeHint())
{
i--;
}
}
}
public void removeNonHints() throws BoundaryViolationException {
for (int subGrid = 0; subGrid < 9; subGrid++)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
// If the cell is not a hint, remove integer from cell.
if (!grid[subGrid][row][col].isHint())
{
grid[subGrid][row][col].setData(0);
grid[subGrid][row][col].initializeValidNumbers();
}
}
}
}
}
public void solveAction(ActionEvent event) //throws BoundaryViolationException
{
try
{
solve(0,0,0);
}
catch (BoundaryViolationException x)
{
System.out.println("Invalid rank");
}
// Create 9 sub-grid GridPanes and place them in correct order into the board GridPane.
for (int subGrid = 0; subGrid < 9; subGrid++)
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
Cell currentCell = grid[subGrid][row][col]; // Retrieve the current cell from the grid matrix
String number = Integer.toString(currentCell.getData());
// Fill in each blank spot on the sudoku board.
labels[subGrid][row][col].setText(number);
if (number.equals("0")) // If the value of the cell is 0, then make the cell look empty.
{
labels[subGrid][row][col].setTextFill(Color.WHITE);
}
else
{
labels[subGrid][row][col].setTextFill(Color.BLACK);
}
}
}
}
solveBtn.setDisable(true);
}
}