/
ControllerInputPanel.java
149 lines (132 loc) · 4.43 KB
/
ControllerInputPanel.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
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class ControllerInputPanel {
private ViewerInputPanel inputPanel;
<<<<<<< HEAD
private ViewerBoardPanel board;
=======
>>>>>>> ba8a3e98ba6fb98b68d338765d7e89dc8861aaeb
private SudokuModel model;
public ControllerInputPanel(SudokuModel model, ViewerInputPanel inputPanel) {
this.model = model;
this.inputPanel = inputPanel;
this.board = inputPanel.getBoard();
this.inputPanel.addActionListener(new ClearPress(), inputPanel.getClearButton());
for(ViewerKeyButton k : inputPanel.getKeyButtons()){
this.inputPanel.addActionListener(new KeyClick(inputPanel.getBoard(), k.getLabel()), k);
}
}
class ClearPress implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
clearSelectedCell();
showInvalid();
showIncorrect();
}
private void clearSelectedCell(){
Position p = board.getSelectedCell();
if (p != null) {
if(!inputPanel.getCandidateButton().isSelected()){
model.clearCellNumber(p.getRow(), p.getCol());
int curr = model.getCellNumber(p.getRow(), p.getCol());
if (curr == 0) {
board.getButton(p).setNumberLabel(null);
} else {
board.getButton(p).setNumberLabel(Integer.toString(curr));
}
}else{
model.clearCellCandidates(p.getRow(), p.getCol());
board.getButton(p).clearCandidateLabel();
}
}
}
}
class KeyClick implements ActionListener {
private String label;
public KeyClick(ViewerBoardPanel board, String label){
this.label = label;
}
@Override
public void actionPerformed(ActionEvent arg0) {
Position p = board.getSelectedCell();
setSelectedCell(label, p);
showInvalid();
showIncorrect();
}
// private void checkFinished() {
// if (model.isSudokuFinished()) {
// JOptionPane.showMessageDialog(null, "Trophy 4 u!");
// }
// }
}
public void setSelectedCell(String label, Position p) {
if (!model.isCellGiven(p.getRow(), p.getCol())) {
if(!inputPanel.getCandidateButton().isSelected()){
model.setCellNumber(p.getRow(), p.getCol(), Integer.parseInt(label));
board.getSelectedButton().setNumberLabel(label);
}else{
if(model.hasCellCandidate(p.getRow(), p.getCol(), Integer.parseInt(label))){
// System.out.println(label);
model.removeCellCandidate(p.getRow(), p.getCol(), Integer.parseInt(label));
board.getSelectedButton().setCandidateLabel(model.getCandidates(p.getRow(), p.getCol()));
}else{
// System.out.println(label);
model.addCellCandidate(p.getRow(), p.getCol(), Integer.parseInt(label));
board.getSelectedButton().setCandidateLabel(model.getCandidates(p.getRow(), p.getCol()));
}
}
}
}
public void showInvalid(){
for(int i=0; i < 9; i++){
for(int j=0; j < 9; j++){
board.getButtons().get(i).get(j).setValid(model.isCellValid(i, j));
}
}
}
/* if(!model.isBoxValid(curBox)){
for(int i=(curBox/3)*3; i<(curBox/3)*3+3; i++) {
for(int j=(curBox%3)*3; j<(curBox%3)*3+3; j++) {
board.getButtons().get(i).get(j).setValid(false);
}
}
}else{
for(int i=(curBox/3)*3; i<(curBox/3)*3+3; i++) {
for(int j=(curBox%3)*3; j<(curBox%3)*3+3; j++) {
if(model.isCellValid(board.getButtons().get(i).get(j).getPosition().getRow(), board.getButtons().get(i).get(j).getPosition().getCol())){
board.getButtons().get(i).get(j).setValid(true);
}
}
}
if(!model.isRowValid(curRow)){
for(int i = 0; i < 9; i ++){
board.getButtons().get(curRow).get(i).setValid(false);
}
}else{
for(int i = 0; i < 9; i ++){
if(model.isCellValid(board.getButtons().get(curRow).get(i).getPosition().getRow(), board.getButtons().get(curRow).get(i).getPosition().getCol())){
board.getButtons().get(curRow).get(i).setValid(true);
}
}
}
if(!model.isColumnValid(curCol)){
for(int j = 0; j < 9; j++){
board.getButtons().get(j).get(curCol).setValid(false);
}
}else{
for(int j = 0; j < 9; j++){
if(model.isCellValid(board.getButtons().get(j).get(curCol).getPosition().getRow(), board.getButtons().get(j).get(curCol).getPosition().getCol())){
board.getButtons().get(j).get(curCol).setValid(true);
}
}
}
}
}*/
public void showIncorrect(){
int curRow = board.getSelectedCell().getRow();
int curCol = board.getSelectedCell().getCol();
board.getSelectedButton().setCorrect(model.isCellCorrect(curRow, curCol));
}
}