-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnswer.java
More file actions
573 lines (503 loc) · 15 KB
/
Copy pathAnswer.java
File metadata and controls
573 lines (503 loc) · 15 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package com.testyourself.knowyoursport;
import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.media.MediaPlayer;
import android.widget.RelativeLayout;
public class Answer extends Activity {
// string to get each letter from database
String[] first = new String[12];
private final String PREFS_NAME = "football";
String[] getAnswered = new String[200];
Boolean answered;
private int p = 0;
private int q;
private int k = 0;
private int y = 0;
public int category = 1;
private boolean hasLife = false;
MediaPlayer mp;
MediaPlayer mp2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_answer);
// create music
mp = MediaPlayer.create(this, R.raw.answer_sound);
mp2 = MediaPlayer.create(this, R.raw.button);
// get the game layout
final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.example1);
// get the button layout to create dynamic button
RelativeLayout buttonlayout = (RelativeLayout) findViewById(R.id.example3);
// getting the question number from the question class.
Bundle bundle = getIntent().getExtras();
q = bundle.getInt("from");
// getting the random letters from the answer
first = bundle.getStringArray("first");
// size of the answer
k = bundle.getInt("k");
// creating a view object for each letter in the database
final MyView1 mv1 = new MyView1(this, first[0]);
final MyView1 mv2 = new MyView1(this, first[1]);
final MyView1 mv3 = new MyView1(this, first[2]);
final MyView1 mv4 = new MyView1(this, first[3]);
final MyView1 mv5 = new MyView1(this, first[4]);
final MyView1 mv6 = new MyView1(this, first[5]);
final MyView1 mv7 = new MyView1(this, first[6]);
final MyView1 mv8 = new MyView1(this, first[7]);
final MyView1 mv9 = new MyView1(this, first[8]);
final MyView1 mv10 = new MyView1(this, first[9]);
// adding first view to the screen
relativelayout.addView(mv1);
// starting position for the dynamically added buttons.
int currentX = 0;
int currentY = 0;
// adding required buttons dynamically to the screen.
for (int i = 0; i < (k - 2); i++) {
// getting the buttons and setting some parameters
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("" + (id_ + 1));
btn.setBackgroundColor(Color.BLACK);
btn.setTextColor(Color.parseColor("#999999"));
btn.setTextSize(25);
params.setMargins(currentX, currentY, 0, 0);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screen = (metrics.widthPixels) / (k - 2);
params.width = screen;
params.height = (metrics.heightPixels) / 9;
btn.setLayoutParams(params);
buttonlayout.addView(btn, params);
// finding buttons separately
final Button button = (Button) findViewById(id_);
// setting what happens when clicking the buttons
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mp.start();
System.gc();
y++;
String[] second = new String[8];
Bundle bundle = getIntent().getExtras();
second = bundle.getStringArray("second");
// checking if the letter falling on the screen and the
// letter that should be on the button are same.
if (first[p].equals(second[id_])) {
// changing the text on the button to the letter
// falling.
// setting the text on the button as falling letter
button.setText("" + second[id_].toUpperCase());
button.setTextColor(Color.parseColor("#FFFFFF"));
// removing the old view and creating a view for the new
// falling letter on the screen
if (y == (k - 2)) {
relativelayout.removeAllViews();
// the answer is correct, we go to winner function
winner();
}
// switch to find exact view for the button in database
switch (p) {
case 0:
relativelayout.removeView(mv1);
p++;
relativelayout.addView(mv2);
break;
case 1:
relativelayout.removeView(mv2);
p++;
relativelayout.addView(mv3);
break;
case 2:
relativelayout.removeView(mv3);
p++;
relativelayout.addView(mv4);
break;
case 3:
relativelayout.removeView(mv4);
p++;
relativelayout.addView(mv5);
break;
case 4:
relativelayout.removeView(mv5);
p++;
relativelayout.addView(mv6);
break;
case 5:
relativelayout.removeView(mv6);
p++;
relativelayout.addView(mv7);
break;
case 6:
relativelayout.removeView(mv7);
p++;
relativelayout.addView(mv8);
break;
case 7:
relativelayout.removeView(mv8);
p++;
relativelayout.addView(mv9);
break;
case 8:
relativelayout.removeView(mv9);
p++;
relativelayout.addView(mv10);
break;
}
if (y == (k - 2)) {
relativelayout.removeAllViews();
winner();
}
} else {
// button pressed doesn't match the position of the
// letter
relativelayout.removeAllViews();
losser();
}
}
// correct answer goes to correct class
private void winner() {
// for onStop, to check if onStop is due to activity change
// or something else
hasLife = true;
mp2.stop();
final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.example1);
relativelayout.removeAllViewsInLayout();
// get our SharedPreferences object and create an editor for
// it
SharedPreferences prefs = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Bundle bundle = getIntent().getExtras();
q = bundle.getInt("from");
final int maxUnlockedLevel = prefs.getInt(
"maxUnlockedLevel", 1);
final Boolean answer = prefs.getBoolean("getAnswered_" + q,
false);
if (!answer) {
editor.putInt("maxUnlockedLevel", maxUnlockedLevel + 2);
editor.commit();
}
answered = true;
editor.putBoolean("getAnswered_" + q, answered);
editor.commit();
if (q == 200) {
Intent intent = new Intent(Answer.this,
Congratulation.class).putExtra("category",
category);
startActivity(intent);
} else {
Intent intent = new Intent(Answer.this, Correct.class);
Bundle extras = new Bundle();
extras.putInt("category", 1);
extras.putInt("from", q);
intent.putExtras(extras);
startActivity(intent);
overridePendingTransition(R.anim.rightin,
R.anim.rightout);
}
}
}
);
currentX = currentX + (screen - 5);
}
final ImageButton imgbtn1 = (ImageButton) findViewById(R.id.imgbtn);
imgbtn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mp.start();
RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.example1);
String[] extra = new String[3];
Bundle bundle = getIntent().getExtras();
extra = bundle.getStringArray("extra");
if (first[p].equals(extra[0]) || first[p].equals(extra[1])
|| first[p].equals("b")) {
switch (p) {
case 0:
relativelayout.removeView(mv1);
p++;
relativelayout.addView(mv2);
break;
case 1:
relativelayout.removeView(mv2);
p++;
relativelayout.addView(mv3);
break;
case 2:
relativelayout.removeView(mv3);
p++;
relativelayout.addView(mv4);
break;
case 3:
relativelayout.removeView(mv4);
p++;
relativelayout.addView(mv5);
break;
case 4:
relativelayout.removeView(mv5);
p++;
relativelayout.addView(mv6);
break;
case 5:
relativelayout.removeView(mv6);
p++;
relativelayout.addView(mv7);
break;
case 6:
relativelayout.removeView(mv7);
p++;
relativelayout.addView(mv8);
break;
case 7:
relativelayout.removeView(mv8);
p++;
relativelayout.addView(mv9);
break;
case 8:
relativelayout.removeView(mv9);
p++;
relativelayout.addView(mv10);
break;
}
} else {
relativelayout.removeAllViews();
losser();
}
}
}
);
}
private void losser() {
hasLife = true;
final RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.example1);
relativelayout.removeAllViewsInLayout();
mp2.stop();
Bundle bundle = getIntent().getExtras();
q = bundle.getInt("from");
Intent intent = new Intent(Answer.this, GameOver.class);
Bundle extras = new Bundle();
extras.putInt("category", 1);
extras.putInt("from", q);
extras.putString("database", "football");
intent.putExtras(extras);
startActivity(intent);
overridePendingTransition(R.anim.rightin, R.anim.rightout);
}
@Override
protected void onPause() {
mp2.stop();
if (!hasLife) {
this.finish();
}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
mp = MediaPlayer.create(this, R.raw.button);
mp2 = MediaPlayer.create(this, R.raw.answer_sound);
mp2.setLooping(true);
mp2.start();
}
@Override
protected void onStop() {
super.onStop();
// Deallocate all memory
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
if (mp2 != null) {
if (mp2.isPlaying()) {
mp2.stop();
}
mp2.release();
mp2 = null;
}
}
@Override
public void onBackPressed() {
mp.start();
mp2.stop();
Bundle bundle = getIntent().getExtras();
category = bundle.getInt("category");
Intent i = new Intent(Answer.this, Buttons.class);
startActivity(i);
overridePendingTransition(R.anim.leftin, R.anim.leftout);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
updateSizeInfo1();
}
private int updateSizeInfo1() {
RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.example1);
int h = relativelayout.getHeight();
return (h);
}
class MyView1 extends View {
Bitmap cloud;
private int x1 = 0, y1 = 0, m, n, q1;
Paint paint;
Canvas canvas;
char ch;
// creating views for the letters
public MyView1(Context context, String first1) {
super(context);
if (!mp2.isLooping())
mp2.start();
if (cloud != null) {
cloud.recycle();
cloud = null;
System.gc();
}
if (first1.equals("a")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.a);
}
if (first1.equals("b")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.b);
}
if (first1.equals("c")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.c);
}
if (first1.equals("d")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.d);
}
if (first1.equals("e")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.e);
}
if (first1.equals("f")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.f);
}
if (first1.equals("g")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.g);
}
if (first1.equals("h")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.h);
}
if (first1.equals("i")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.i);
}
if (first1.equals("j")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.j);
}
if (first1.equals("k")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.k);
}
if (first1.equals("l")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.l);
}
if (first1.equals("m")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.m);
}
if (first1.equals("n")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.n);
}
if (first1.equals("o")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.o);
}
if (first1.equals("p")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.p);
}
if (first1.equals("q")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.q);
}
if (first1.equals("r")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.r);
}
if (first1.equals("s")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.s);
}
if (first1.equals("t")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.t);
}
if (first1.equals("u")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.u);
}
if (first1.equals("v")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.v);
}
if (first1.equals("w")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.w);
}
if (first1.equals("x")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.x);
}
if (first1.equals("y")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.y);
}
if (first1.equals("z")) {
cloud = BitmapFactory.decodeResource(getResources(),
R.drawable.z);
}
}
// making letters fall
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
x1 = canvas.getWidth();
canvas.drawBitmap(cloud, ((2 * x1) / 5), y1, paint);
m = updateSizeInfo1();
n = cloud.getHeight();
q1 = m - n;
// if the falling letter is on the main screen
if (y1 < q1) {
y1 = y1 + 4;
}
// if the falling letter touches the bottom of the main screen.
else if (y1 > q1) {
y1 = q1;
losser();
}
invalidate();
}
}
}