-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.java
More file actions
198 lines (167 loc) · 7.13 KB
/
Copy pathUI.java
File metadata and controls
198 lines (167 loc) · 7.13 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package suduko;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class UI extends JFrame {
int count = 0;
Logic lg;
ArrayList<Integer> result = new ArrayList<>();
//rigid ui objects.
JLabel nameLabel = new JLabel("My Suduko");
JButton ERASER = new JButton();
private final JButton BACK = new JButton("Check");
//no. keys
private JButton[] sudukoButton;
//default no. key
private String myString = "1";
//no of times eraser can be used.
public int ERASER_COUNT = 3;
//diffrentiates no keys and eraser by enabling multiple use of eraser and single use aof no. keys(acts as a tag)
public boolean IS_ERASER = false;
//link btw ui and grid(removes eraser button when ERASER_COUNT is less than eqal to one)
JPanel MENU_BAR_BOTTOM;
public int ROW_COLUMN_COUNT;
public void CreateSuduko(int WIDTH, int HEIGHT, int ROW_COLUMN_COUNT) {
this.ROW_COLUMN_COUNT = ROW_COLUMN_COUNT;
lg = new Logic(ROW_COLUMN_COUNT);
ERASER.setText("ERASER *" + ERASER_COUNT);
sudukoButton = new JButton[ROW_COLUMN_COUNT];
setName("My Suduko");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//layout for JPanel_CENTER : SUDUKO_GRID
Grid myGrid = new Grid(ROW_COLUMN_COUNT, ROW_COLUMN_COUNT, this, MENU_BAR_BOTTOM) {
@Override
public String GetText() {
return Get_myString();
}
};
//Logic lg = new Logic(ROW_COLUMN_COUNT);
Box bx = new Box(ROW_COLUMN_COUNT);
bx.GeneratePos1Index();
//layout for main frame
setLayout(new BorderLayout(25, 25));
//layout for JPanel_TOP : MENU_BAR_TOP
JPanel MENU_BAR_TOP = new JPanel(new BorderLayout());
MENU_BAR_TOP.add(nameLabel, BorderLayout.WEST);
MENU_BAR_TOP.add(BACK, BorderLayout.EAST);
ActionListener actionListenerTop = (event) -> {
if (event.getSource() == BACK && count == 0) {
for (int i = 0; i < ROW_COLUMN_COUNT; i++) {
for (int j = ROW_COLUMN_COUNT * i; j < ROW_COLUMN_COUNT * (i + 1); j++) {
int k = lg._numberOfRandomNumberIndex.get(i) + 1;
if (myGrid.GetName(bx.indexReff.get(j)).equals(Integer.toString(k))) {
result.add(1);
} else {
result.add(0);
}
}
}
if (result.contains(0)) {
nameLabel.setText("opps you missed click -->> to generate answer");
BACK.setText("generate Answer");
count++;
return;
} else if (result.contains(1)) {
nameLabel.setText("Yaay!! you won click -->> to close");
BACK.setText("close");
count = 2;
return;
}
}
if (event.getSource() == BACK && count == 1) {
BACK.setText("close");
if (result.contains(0)) {
lg._numberOfRandomNumberToGenerate(this.ROW_COLUMN_COUNT);
for (int i = 0; i < ROW_COLUMN_COUNT; i++) {
for (int j = ROW_COLUMN_COUNT * i; j < ROW_COLUMN_COUNT * (i + 1); j++) {
myGrid.SetName(bx.indexReff.get(j), Integer.toString(lg._numberOfRandomNumberIndex.get(i) + 1));
}
}
}
count++;
return;
}
if (event.getSource() == BACK && count == 2) {
this.dispose();
}
};
BACK.addActionListener(actionListenerTop);
add(MENU_BAR_TOP, BorderLayout.NORTH);
myGrid.SET_ONE_TIME_USE(ROW_COLUMN_COUNT, ROW_COLUMN_COUNT);
myGrid.SetGrid();
//populates the whole grid.
lg._numberOfRandomNumberToGenerate(this.ROW_COLUMN_COUNT);
for (int i = 0; i < ROW_COLUMN_COUNT; i++) {
for (int j = ROW_COLUMN_COUNT * i; j < ROW_COLUMN_COUNT * (i + 1); j++) {
myGrid.SetName(bx.indexReff.get(j), Integer.toString(lg._numberOfRandomNumberIndex.get(i) + 1));
}
}
// removes specified number of gridBoxes
lg.numberOfRandomNumberToGenerate(((ROW_COLUMN_COUNT * ROW_COLUMN_COUNT) + 5 - ROW_COLUMN_COUNT * 4));
for (int i = 0; i < lg.numberOfRandomNumberIndex.size(); i++) {
myGrid.SetName(lg.numberOfRandomNumberIndex.get(i), "");
myGrid.update();
}
add(myGrid, BorderLayout.CENTER);
//we are going to set up a grid..
//layout for JPanel_BOTTOM : MENU_BAR_BOTTOM
JPanel MENU_BAR_BOTTOM = new JPanel(new FlowLayout(FlowLayout.LEADING, 20, 20));
this.MENU_BAR_BOTTOM = MENU_BAR_BOTTOM;
//no. of buttons to create.
for (int i = 0; i <= ROW_COLUMN_COUNT - 1; i++) {
int reffInt = i + 1;
sudukoButton[i] = new JButton(String.valueOf(reffInt));
MENU_BAR_BOTTOM.add(sudukoButton[i]);
ActionListener actionListener = (event) -> {
if (event.getSource() == sudukoButton[reffInt - 1]) {
IS_ERASER = false;
Set_myString(sudukoButton[reffInt - 1].getText());
}
};
sudukoButton[i].addActionListener(actionListener);
}
MENU_BAR_BOTTOM.add(ERASER);
ActionListener actionListener = (event) -> {
if (event.getSource() == ERASER) {
if (ERASER_COUNT > 0) {
IS_ERASER = true;
Set_myString("");
} else {
IS_ERASER = false;
Set_myString("1");
RemoveEraser(MENU_BAR_BOTTOM);
}
}
};
ERASER.addActionListener(actionListener);
add(MENU_BAR_BOTTOM, BorderLayout.SOUTH);
setVisible(true);
}
public void RemoveEraser(JPanel MENU_BAR_BOTTOM) {
IS_ERASER = false;
Set_myString("1");
MENU_BAR_BOTTOM.remove(ERASER);
MENU_BAR_BOTTOM.revalidate();
MENU_BAR_BOTTOM.repaint();
}
public String Get_myString() {
return myString;
}
public void Set_myString(String SampleString) {
myString = SampleString;
}
@Override
public Insets getInsets() {
return new Insets(35, 15, 35, 15);
}
}