-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuGUI.java
188 lines (169 loc) · 6.57 KB
/
SudokuGUI.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
package sudoku;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class SudokuGUI extends JFrame {
private static final int GRID_SIZE = 9;
private static final Random random = new Random();
private final JTextField[][] cells = new JTextField[GRID_SIZE][GRID_SIZE];
private final JButton solveButton = new JButton("Solve");
private final JButton resetButton = new JButton("Reset");
private final int[][] board = new int[GRID_SIZE][GRID_SIZE];
private final JPanel buttonPanel = new JPanel();
public SudokuGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JPanel gridPanel = new JPanel(new GridLayout(GRID_SIZE, GRID_SIZE));
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
cells[i][j] = new JTextField();
cells[i][j].setHorizontalAlignment(JTextField.CENTER);
cells[i][j].setFont(new Font("Arial", Font.BOLD, 20));
cells[i][j].setOpaque(true);
gridPanel.add(cells[i][j]);
}
}
add(gridPanel, BorderLayout.CENTER);
solveButton.addActionListener(e -> new Thread(() -> {
if (solveBoard(board, true)) {
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(this, "Sudoku Solved!"));
}
}).start());
resetButton.addActionListener(e -> resetBoard());
buttonPanel.add(solveButton);
buttonPanel.add(resetButton);
add(buttonPanel, BorderLayout.SOUTH);
resetBoard();
}
private void resetBoard() {
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
board[i][j] = 0;
cells[i][j].setBackground(Color.WHITE);
}
}
fillBoard(board);
makePuzzle(board);
updateBoard();
}
private boolean fillBoard(int[][] board) {
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
if (board[row][col] == 0) {
for (int number = 1; number <= GRID_SIZE; number++) {
int numToTry = (number + random.nextInt(GRID_SIZE)) % GRID_SIZE + 1;
if (isValidPlacement(board, numToTry, row, col)) {
board[row][col] = numToTry;
if (fillBoard(board)) {
return true;
} else {
board[row][col] = 0;
}
}
}
return false;
}
}
}
return true;
}
private void makePuzzle(int[][] board) {
int steps = GRID_SIZE * 3;
while (steps > 0) {
int row = random.nextInt(GRID_SIZE);
int col = random.nextInt(GRID_SIZE);
if (board[row][col] != 0) {
board[row][col] = 0;
steps--;
}
}
}
private boolean solveBoard(int[][] board, boolean firstCall) {
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
if (board[row][col] == 0) {
for (int numToTry = 1; numToTry <= GRID_SIZE; numToTry++) {
if (isValidPlacement(board, numToTry, row, col)) {
board[row][col] = numToTry;
updateCell(row, col, numToTry);
if (solveBoard(board, false)) {
return true;
} else {
board[row][col] = 0;
updateCell(row, col, 0);
}
}
}
return false;
}
}
}
if (firstCall) {
SwingUtilities.invokeLater(this::updateBoard);
}
return true;
}
private void updateCell(int row, int col, int numToTry) {
SwingUtilities.invokeLater(() -> {
cells[row][col].setText(numToTry > 0 ? Integer.toString(numToTry) : "");
if (numToTry > 0) {
cells[row][col].setBackground(Color.CYAN);
} else {
cells[row][col].setBackground(Color.WHITE);
}
cells[row][col].repaint();
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private void updateBoard() {
SwingUtilities.invokeLater(() -> {
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
cells[row][col].setText(board[row][col] > 0 ? Integer.toString(board[row][col]) : "");
}
}
});
}
private boolean isValidPlacement(int[][] board, int number, int row, int column) {
return !isNumberInRow(board, number, row) &&
!isNumberInColumn(board, number, column) &&
!isNumberInBox(board, number, row, column);
}
private boolean isNumberInRow(int[][] board, int number, int row) {
for (int i = 0; i < GRID_SIZE; i++) {
if (board[row][i] == number) {
return true;
}
}
return false;
}
private boolean isNumberInColumn(int[][] board, int number, int column) {
for (int i = 0; i < GRID_SIZE; i++) {
if (board[i][column] == number) {
return true;
}
}
return false;
}
private boolean isNumberInBox(int[][] board, int number, int row, int column) {
int localBoxRow = row - row % 3;
int localBoxColumn = column - column % 3;
for (int i = localBoxRow; i < localBoxRow + 3; i++) {
for (int j = localBoxColumn; j < localBoxColumn + 3; j++) {
if (board[i][j] == number) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SudokuGUI().setVisible(true));
}
}