-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
118 lines (112 loc) · 4.29 KB
/
Copy pathController.java
File metadata and controls
118 lines (112 loc) · 4.29 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
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class Controller {
private SudokuInterface window;
public static void main(String[] args) {
new Controller(new SudokuInterface());
}
private Controller(SudokuInterface window){
this.window = window;
this.window.getRootPane().setDefaultButton(window.getSolveButton());
setupClearButton();
setupSolveButton();
}
private void setupSolveButton(){
JButton button = window.getSolveButton();
JPanel[] blocks = getBlocks();
button.addActionListener(e -> {
Solver solver;
try {
Cell[] clues = getInputClues(blocks);
int pretty = 0;
for(Cell clue:clues){
if(clue.getValue().equals(3)){
pretty++;
}
}
if(pretty==81){
JOptionPane.showMessageDialog(window, "Beautiful!", "333333", JOptionPane.INFORMATION_MESSAGE);
}else {
solver = new Solver(clues);
solver.solveGrid();
Cell[][] grid = solver.getGrid();
philGridd(grid, blocks);
}
}catch (Exception uDoneGoofedMate){
JOptionPane.showMessageDialog(window, "That arrangement of clues isn't possible to solve!", "Invalid Sudoku", JOptionPane.ERROR_MESSAGE);
revertFormat();
}
});
}
private Cell[] getInputClues(JPanel[] blocks){
ArrayList<Cell> clues = new ArrayList<>();
for(int i=0;i<9;i++){
for(int j=0;j<9;j++) {
int col, row;
col = (j % 3) + (3 * (i % 3));
row = Math.floorDiv(j, 3) + (3 * (Math.floorDiv(i, 3)));
JTextField cell = (JTextField) blocks[i].getComponents()[j];
if (!cell.getText().equals("")) {
int value = Integer.parseInt(cell.getText());
clues.add(new Cell(col, row, value));
Font boldFont = new Font(cell.getFont().getName(), Font.BOLD, cell.getFont().getSize());
cell.setFont(boldFont);
}else{
cell.setForeground(Color.BLUE);
}
}
}
Cell[] clueCluxClan = new Cell[clues.size()];
for(int i=0;i<clues.size();i++){
clueCluxClan[i] = clues.get(i);
}
return clueCluxClan;
}
private void philGridd(Cell[][] gridd, JPanel[] blocks){
for(int i=0;i<9;i++){
for(int j=0;j<9;j=j+1){
int col, row;
col = (j % 3) + (3 * (i % 3));
row = Math.floorDiv(j, 3) + (3 * (Math.floorDiv(i, 3)));
JTextField cell = (JTextField) blocks[i].getComponents()[j];
cell.setText(gridd[col][row].getValue().toString());
cell.setEditable(false);
}
}
}
private void setupClearButton(){
JButton button = window.getClearButton();
JPanel[] blocks = getBlocks();
button.addActionListener(e -> clearGrid(blocks));
}
private JPanel[] getBlocks(){
Component[] cBlocks = window.getSudokuGrid().getComponents();
JPanel[] blocks = new JPanel[9];
for(int i=0;i<9;i++){
blocks[i] = (JPanel) cBlocks[i];
}
return blocks;
}
private void clearGrid(JPanel[] blocks){
for(JPanel block:blocks){
for(Component cell:block.getComponents()){
JTextField tfCell = (JTextField)cell;
tfCell.setText("");
tfCell.setEditable(true);
Font normalFont = new Font(tfCell.getFont().getName(), Font.PLAIN, tfCell.getFont().getSize());
tfCell.setFont(normalFont);
tfCell.setForeground(Color.BLACK);
}
}
}
private void revertFormat(){
for(JPanel block:getBlocks()){
for(Component _cell:block.getComponents()){
JTextField cell = (JTextField)_cell;
cell.setFont(new Font(cell.getFont().getName(), Font.PLAIN, cell.getFont().getSize()));
cell.setForeground(Color.BLACK);
}
}
}
}