-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.java
82 lines (73 loc) · 3.47 KB
/
Sudoku.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
import javax.swing.*;
import java.awt.*;
public class Sudoku extends JFrame {
public static final int GRID_SIZE = 9;
public static final int SUBGRID_SIZE = 3;
public static final int CELL_SIZE = 60;
public static final int CANVAS_WIDTH = CELL_SIZE * GRID_SIZE;
public static final int CANVAS_HEIGHT = CELL_SIZE * GRID_SIZE;
public static final Color OPEN_CELL_BGCOLOR = Color.YELLOW;
public static final Color OPEN_CELL_TEXT_YES = new Color(0, 255, 0);
public static final Color OPEN_CELL_TEXT_NO = Color.RED;
public static final Color CLOSED_CELL_BGCOLOR = new Color(240, 240, 240);
public static final Color CLOSED_CELL_TEXT = Color.BLACK;
public static final Font FONT_NUMBERS = new Font("AvantGarde", Font.BOLD, 20);
private final JTextField[][] tfCells = new JTextField[GRID_SIZE][GRID_SIZE];
private final int[][] puzzle = {
{5, 3, 4, 6, 7, 8, 9, 1, 2},
{6, 7, 2, 1, 9, 5, 3, 4, 8},
{1, 9, 8, 3, 4, 2, 5, 6, 7},
{8, 5, 9, 7, 6, 1, 4, 2, 3},
{4, 2, 6, 8, 5, 3, 7, 9, 1},
{7, 1, 3, 9, 2, 4, 8, 5, 6},
{9, 6, 1, 5, 3, 7, 2, 8, 4},
{2, 8, 7, 4, 1, 9, 6, 3, 5},
{3, 4, 5, 2, 8, 6, 1, 7, 9}
};
private final boolean[][] masks = {
{false, false, false, false, false, true, false, false, false},
{false, false, false, false, false, false, false, false, true},
{false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false}
};
private JPanel mainPanel;
public Sudoku() {
Container cp = getContentPane();
cp.setLayout(new GridLayout(GRID_SIZE, GRID_SIZE)); // 9x9 GridLayout
for (int row = 0; row < GRID_SIZE; ++row) {
for (int col = 0; col < GRID_SIZE; ++col) {
tfCells[row][col] = new JTextField();
cp.add(tfCells[row][col]);
if (masks[row][col]) {
tfCells[row][col].setText("");
tfCells[row][col].setEditable(true);
tfCells[row][col].setBackground(OPEN_CELL_BGCOLOR);
} else {
tfCells[row][col].setText(puzzle[row][col] + "");
tfCells[row][col].setEditable(false);
tfCells[row][col].setBackground(CLOSED_CELL_BGCOLOR);
tfCells[row][col].setForeground(CLOSED_CELL_TEXT);
}
tfCells[row][col].setHorizontalAlignment(JTextField.CENTER);
tfCells[row][col].setFont(FONT_NUMBERS);
}
}
cp.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Sudoku");
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Sudoku();
}
});
}
}