Repository navigation
Expand file tree
/
Copy pathGame1.java
More file actions
327 lines (214 loc) · 7.23 KB
/
Copy pathGame1.java
File metadata and controls
327 lines (214 loc) · 7.23 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class Game1 extends JFrame
{
int a,b,r=0, t=0,count=-1,total=-1,tl=0,time=0;
LogWriter log=new LogWriter();
char c;
boolean stop=true;
Random generator=new Random();
private JRadioButton jrbAdd = new JRadioButton("Add");
private JRadioButton jrbSubtract = new JRadioButton("Subtract");
private JRadioButton jrbMultiply = new JRadioButton("Multiply");
private JRadioButton jrbDivide = new JRadioButton("Divide");
private JRadioButton jrbLevel1 = new JRadioButton("Numbers from 0 to 9");
private JRadioButton jrbLevel2 = new JRadioButton("Numbers from 0 to 20");
private JRadioButton jrbLevel3 = new JRadioButton("Numbers from 0 to 50");
private JRadioButton jrbLevel4 = new JRadioButton("Any single/double digit number");
ButtonGroup GroupA = new ButtonGroup();
ButtonGroup GroupB = new ButtonGroup();
private JLabel jlblQuestion = new JLabel("Question will be shown ", 4);
private TextField jtfAnswer = new TextField("");
private JButton jbtStart = new JButton("Start");
private JButton jbtNext=new JButton("Next");
private JLabel jlblCorrectCount = new JLabel("0/0");
private JLabel jlblTimeSpent = new JLabel("0 seconds");
public Game1() {
GroupA.add(jrbAdd);
GroupA.add(jrbSubtract);
GroupA.add(jrbMultiply);
GroupA.add(jrbDivide);
GroupB.add(jrbLevel1);
GroupB.add(jrbLevel2);
GroupB.add(jrbLevel3);
GroupB.add(jrbLevel4);
JPanel jpTypeSetting = new JPanel(new GridLayout(4, 1));
jpTypeSetting.setBorder(BorderFactory.createTitledBorder("Choose a type"));
jpTypeSetting.add(this.jrbAdd);
jpTypeSetting.add(this.jrbSubtract);
jpTypeSetting.add(this.jrbMultiply);
jpTypeSetting.add(this.jrbDivide);
JPanel jpLevelSetting = new JPanel(new GridLayout(4, 1));
jpLevelSetting.setBorder(BorderFactory.createTitledBorder("Choose a level"));
jpLevelSetting.add(this.jrbLevel1);
jpLevelSetting.add(this.jrbLevel2);
jpLevelSetting.add(this.jrbLevel3);
jpLevelSetting.add(this.jrbLevel4);
JPanel jpSetting = new JPanel(new GridLayout(1, 2));
jpSetting.add(jpTypeSetting);
jpSetting.add(jpLevelSetting);
JPanel jpQuestion = new JPanel(new GridLayout(2, 2));
jpQuestion.add(new JLabel("Question:"));
jpQuestion.add(new JLabel("Answer:"));
jpQuestion.add(this.jlblQuestion);
jpQuestion.add(this.jtfAnswer);
JPanel jpButton = new JPanel(new FlowLayout(2));
jpButton.add(this.jbtStart);
//jpButton.add(this.jbtStop);
jpButton.add(this.jbtNext);
JPanel jpLabel = new JPanel(new GridLayout(1, 2));
jpLabel.add(this.jlblCorrectCount);
jpLabel.add(this.jlblTimeSpent);
this.jlblCorrectCount.setBorder(BorderFactory.createTitledBorder("Correct Count"));
this.jlblTimeSpent.setBorder(BorderFactory.createTitledBorder("Time Spent"));
JPanel jpResult = new JPanel(new BorderLayout());
jpResult.add(jpButton, "North");
jpResult.add(jpLabel, "Center");
add(jpSetting, "North");
add(jpQuestion, "Center");
add(jpResult, "South");
jtfAnswer.setEnabled(false);
jbtNext.setEnabled(false);
jrbAdd.setSelected(true);
jrbLevel1.setSelected(true);
this.jbtStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
log.writeLog("Started");
jlblCorrectCount.setText("0/0");
stop=true;
jbtNext.setEnabled(true);
jtfAnswer.setEnabled(true);
tDisplay();
logic();
jbtStart.setEnabled(false);
}
});
this.jbtNext.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
jtfAnswer.addActionListener(this);
try{
r=Integer.parseInt(jtfAnswer.getText());
}catch(Exception ex){
}
log.writeLog("Input is "+r);
jtfAnswer.setText("");
logic();
}
});
};
void tDisplay(){
Thread th=new Thread(){
int i=0;
@Override
public void run(){
while(true){
if(i<=119){
time=i;
i=i+1;
jlblTimeSpent.setText(i +" seconds");
}
else{
Exit(time);
break;
}
try {
sleep(1000);
} catch (InterruptedException ex) {
}
}
}
};
th.start();
}
void Exit(int n){
if(n==119)
{
stop=false;
jlblQuestion.setText("Questions will be shown");
jlblCorrectCount.setText(count+"");
Score sC=new Score();
sC.writeScore(Collection.pName(),"Game1",count);
total=-1;
count=0;
r = 0;
jtfAnswer.setText("");
jtfAnswer.setEnabled(false);
jbtStart.setEnabled(true);
jbtNext.setEnabled(false);
jlblTimeSpent.setText("");
}
}
void logic(){
if(stop && total-count<10){
if (jrbAdd.isSelected()){
c='+';
log.writeLog("Addition Selected");
}
else if(jrbSubtract.isSelected())
c='-';
else if(jrbMultiply.isSelected())
c='*';
else if(jrbDivide.isSelected())
c='/';
log.writeLog("Operation "+c);
if(jrbLevel1.isSelected())
{
a=generator.nextInt(10);
b=generator.nextInt(10);
}
else if(jrbLevel2.isSelected())
{
a=generator.nextInt(20);
b=generator.nextInt(20);
}
else if(jrbLevel3.isSelected())
{
a=generator.nextInt(50);
b=generator.nextInt(50);
}
else if(jrbLevel4.isSelected())
{
a=generator.nextInt(100);
b=generator.nextInt(100);
}
if(c=='+' || (c=='-' /*&& a>b*/) || (c=='*' && (a<=10 || b<=10)) || (c=='/' /*&& a%b==0*/))
{
jlblQuestion.setText(a +" "+ c +" "+ b + "=");
total=total+1;
if(r==t){
count++;
log.writeLog("Correct answer");
jlblCorrectCount.setText(count +"/"+ total);
}
else{
log.writeLog("Wrong Answer");
jlblCorrectCount.setText("Ans "+t+" "+" "+count +"/"+ total);
}
log.writeLog("Correct Count is "+count);
if( c=='+')
t=a+b;
else if(c=='-')
t=a-b;
else if(c=='*' )
t=a*b;
else if(c=='/')
t=a/b;
}
}
}
public static void main(String[] args)
{
JFrame frame = new Game1();
frame.setTitle("Game1");
frame.setSize(500, 350);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}