-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuApp.java
155 lines (139 loc) · 5.2 KB
/
SudokuApp.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
import javax.swing.*;
// import javax.swing.text.DefaultFormatterFactory;
// import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// import java.text.NumberFormat;
import java.util.Random;
public class SudokuApp {
public static final int SIZE = 9;
private JFrame frame;
private JTextField[][] sudokuCells = new JTextField[9][9];
private JButton solveButton;
private JButton clearButton;
private JButton generateButton;
public SudokuApp() {
frame = new JFrame("Sudoku Solver");
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(11, 9));
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
JTextField textField = new JTextField(2);
textField.setHorizontalAlignment(JTextField.CENTER); // Center the text
textField.setFont(new Font("Arial", Font.BOLD, 20)); // Increase font size
if ((i / 3 + j / 3) % 2 == 0) {
textField.setBackground(Color.LIGHT_GRAY);
}
frame.add(textField);
sudokuCells[i][j] = textField;
}
}
solveButton = new JButton("Solve");
solveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int[][] board = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (!sudokuCells[i][j].getText().equals("")) {
board[i][j] = Integer.parseInt(sudokuCells[i][j].getText());
}
}
}
if (solveSudoku(board)) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sudokuCells[i][j].setText(String.valueOf(board[i][j]));
}
}
} else {
JOptionPane.showMessageDialog(frame, "Cannot solve this Sudoku puzzle.");
}
}
});
frame.add(solveButton);
clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sudokuCells[i][j].setText("");
}
}
}
});
frame.add(clearButton);
generateButton = new JButton("Generate");
generateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int[][] generatedBoard = generatePuzzle();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (generatedBoard[i][j] != 0) {
sudokuCells[i][j].setText(String.valueOf(generatedBoard[i][j]));
} else {
sudokuCells[i][j].setText("");
}
}
}
}
});
frame.add(generateButton);
// Add placeholders for the remaining slots in the last row to maintain the 9x9
// grid structure
for (int i = 0; i < 6; i++) {
frame.add(new JLabel());
}
frame.setVisible(true);
}
public static void main(String[] args) {
new SudokuApp();
}
// Sudoku Solver methods
public boolean solveSudoku(int[][] board) {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (board[row][col] == 0) {
for (int num = 1; num <= SIZE; num++) {
if (isValid(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board)) {
return true;
} else {
board[row][col] = 0;
}
}
}
return false;
}
}
}
return true;
}
private boolean isValid(int[][] board, int row, int col, int num) {
for (int x = 0; x < SIZE; x++) {
if (board[row][x] == num || board[x][col] == num
|| board[row - row % 3 + x / 3][col - col % 3 + x % 3] == num) {
return false;
}
}
return true;
}
// Sudoku Generator methods
public int[][] generatePuzzle() {
int[][] board = new int[SIZE][SIZE];
solveSudoku(board);
int numberOfCellsToRemove = 40;
Random random = new Random();
for (int i = 0; i < numberOfCellsToRemove; i++) {
int x = random.nextInt(SIZE);
int y = random.nextInt(SIZE);
board[x][y] = 0;
}
return board;
}
}