-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuSolverGUI.java
More file actions
122 lines (107 loc) · 4.19 KB
/
Copy pathSudokuSolverGUI.java
File metadata and controls
122 lines (107 loc) · 4.19 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SudokuSolverGUI extends JFrame {
private JTextField[][] cells;
private JButton solveButton;
public SudokuSolverGUI() {
setTitle("Sudoku Solver");
setSize(450, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Create a panel for the Sudoku grid
JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(9, 9));
cells = new JTextField[9][9];
// Initialize the grid with text fields
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
cells[row][col] = new JTextField();
cells[row][col].setHorizontalAlignment(JTextField.CENTER);
cells[row][col].setFont(new Font("Arial", Font.PLAIN, 20));
cells[row][col].setPreferredSize(new Dimension(40, 40));
gridPanel.add(cells[row][col]);
}
}
add(gridPanel, BorderLayout.CENTER);
// Create a panel for the Solve button
JPanel buttonPanel = new JPanel();
solveButton = new JButton("Solve");
buttonPanel.add(solveButton);
add(buttonPanel, BorderLayout.SOUTH);
// Add an action listener for the Solve button
solveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[][] board = new int[9][9];
// Read user input into a 2D array
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
String text = cells[row][col].getText();
if (!text.isEmpty()) {
board[row][col] = Integer.parseInt(text);
} else {
board[row][col] = 0;
}
}
}
// Solve the Sudoku puzzle
if (solveSudoku(board)) {
// Display the solved Sudoku board
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
cells[row][col].setText(String.valueOf(board[row][col]));
}
}
} else {
JOptionPane.showMessageDialog(null, "No solution found!");
}
}
});
}
// Sudoku solver using backtracking
private boolean solveSudoku(int[][] board) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
if (board[row][col] == 0) {
for (int num = 1; num <= 9; num++) {
if (isSafe(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board)) {
return true;
}
board[row][col] = 0;
}
}
return false;
}
}
}
return true;
}
// Check if a number can be placed in the given cell
private boolean isSafe(int[][] board, int row, int col, int num) {
for (int i = 0; i < 9; i++) {
if (board[row][i] == num || board[i][col] == num) {
return false;
}
}
int boxStartRow = row - row % 3;
int boxStartCol = col - col % 3;
for (int i = boxStartRow; i < boxStartRow + 3; i++) {
for (int j = boxStartCol; j < boxStartCol + 3; j++) {
if (board[i][j] == num) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SudokuSolverGUI().setVisible(true);
}
});
}
}