-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuGUI.java
159 lines (121 loc) · 4.41 KB
/
SudokuGUI.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
156
157
158
159
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.*;
public class SudokuGUI extends JFrame implements ActionListener{
private final int sqSize = 80;
private final int topPlace = 80;
private JTextField[][] board;
private int dimension;
private int verticalInBox;
private int horizontalInBox;
private JButton nextButton;
private Board realBoard;
private boolean first = true;
LinkedList<int[][]> list;
Iterator<int[][]> it;
private int cntNextCall = 0;
public SudokuGUI(int dimension, int verticalInBox, int horizontalInBox, boolean showGUI, Board realBoard) {
this.dimension = dimension;
this.verticalInBox = verticalInBox;
this.horizontalInBox = horizontalInBox;
this.realBoard = realBoard;
board = new JTextField[dimension][dimension];
setPreferredSize(new Dimension(dimension * sqSize, dimension * sqSize + topPlace));
setTitle("Sudoku (" + dimension +" x "+ dimension + ")");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel buttonPanel = createButtons();
JPanel boardPanel = createBoard();
getContentPane().add(buttonPanel,BorderLayout.NORTH);
getContentPane().add(boardPanel,BorderLayout.CENTER);
showInitialCondition();
pack();
setVisible(showGUI);
}
public void showInitialCondition() {
for(int i = 0; i < dimension; i++){
for(int j = 0; j < dimension; j++){
if(realBoard.getTable()[i][j].getValue() != 0){
board[i][j].setText(Integer.toString(realBoard.getTable()[i][j].getValue()));
}
}
}
}
public JPanel createBoard() {
int top, left;
JPanel boardPanel = new JPanel();
boardPanel.setLayout(new GridLayout(dimension,dimension));
boardPanel.setAlignmentX(CENTER_ALIGNMENT);
boardPanel.setAlignmentY(CENTER_ALIGNMENT);
setPreferredSize(new Dimension(new Dimension(dimension * sqSize,
dimension * sqSize)));
for(int i = 0; i < dimension; i++) {
/* Check if given row needs a thicker topline*/
top = (i % verticalInBox == 0 && i != 0) ? 4 : 1;
for(int j = 0; j < dimension; j++) {
/* Check if "theSquare" is a part of a column
that needs a thicker leftline*/
left = (j % horizontalInBox == 0 && j != 0) ? 4 : 1;
JTextField theSquare = new JTextField();
theSquare.setBorder(BorderFactory.createMatteBorder(top,left,1,1, Color.black));
theSquare.setHorizontalAlignment(SwingConstants.CENTER);
theSquare.setPreferredSize(new Dimension(sqSize, sqSize));
theSquare.setText("");
board[i][j] = theSquare;
boardPanel.add(theSquare);
}
}
return boardPanel;
}
public JPanel createButtons() {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
nextButton = new JButton("Next");
nextButton.addActionListener(this);
buttonPanel.add(nextButton);
return buttonPanel;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == nextButton) {
if (realBoard.getContainer().getSolutions().isEmpty()) {
nextButton.setEnabled(false);
} else {
nextSolution();
cntNextCall++;
if(cntNextCall == realBoard.getContainer().getSolutions().size()) {
nextButton.setEnabled(false);
System.out.println("Number of solutions: "+ realBoard.getContainer().getSolutionsNumber());
}
}
}
}
private void nextSolution() {
if(first){
this.list = realBoard.getContainer().getSolutions();
it = list.iterator();
}
first = false;
if(it.hasNext()){
int[][] tmp = it.next();
for(int i = 0; i < tmp.length; i++){
for(int j = 0; j < tmp.length; j++){
board[i][j].setText(Integer.toHexString(Integer.parseInt(Integer.toString(tmp[i][j]))));
}
}
}
}
public Board getRealBoard(){
return this.realBoard;
}
public void setRealBoard(Board board){
realBoard = board;
}
public JTextField[][] getBoard() {
return this.board;
}
}