-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
187 lines (125 loc) · 4.85 KB
/
MainActivity.java
File metadata and controls
187 lines (125 loc) · 4.85 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
package com.example.firuz.braintrainer;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends Activity {
Button startButton;
TextView resultTextView;
TextView pointsTextView;
Button button0;
Button button1;
Button button2;
Button button3;
TextView sumTextView;
TextView timerTextView;
Button playAgainButton;
RelativeLayout gameRelativeLayout;
ArrayList<Integer> answers = new ArrayList<Integer>();
int locationOfCorrectAnswer;
int score = 0;
int numberOfQuestions = 0;
public void playAgain(View view) {
score = 0;
numberOfQuestions = 0;
timerTextView.setText("30s");
pointsTextView.setText("0/0");
resultTextView.setText("");
playAgainButton.setVisibility(View.INVISIBLE);
generateQuestion();
new CountDownTimer(30100, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timerTextView.setText(String.valueOf(millisUntilFinished / 1000) + "s");
}
@Override
public void onFinish() {
playAgainButton.setVisibility(View.VISIBLE);
timerTextView.setText("0s");
resultTextView.setText("Your score: " + Integer.toString(score) + "/" + Integer.toString(numberOfQuestions));
}
}.start();
}
public void generateQuestion() {
Random rand = new Random();
int a = rand.nextInt(21);
int b = rand.nextInt(21);
sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b));
locationOfCorrectAnswer = rand.nextInt(4);
answers.clear();
int incorrectAnswer;
for (int i=0; i<4; i++) {
if (i == locationOfCorrectAnswer) {
answers.add(a + b);
} else {
incorrectAnswer = rand.nextInt(41);
while (incorrectAnswer == a + b) {
incorrectAnswer = rand.nextInt(41);
}
answers.add(incorrectAnswer);
}
}
button0.setText(Integer.toString(answers.get(0)));
button1.setText(Integer.toString(answers.get(1)));
button2.setText(Integer.toString(answers.get(2)));
button3.setText(Integer.toString(answers.get(3)));
}
public void chooseAnswer(View view) {
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))) {
score++;
resultTextView.setText("Correct!");
} else {
resultTextView.setText("Wrong!");
}
numberOfQuestions++;
pointsTextView.setText(Integer.toString(score) + "/" + Integer.toString(numberOfQuestions));
generateQuestion();
}
public void start(View view) {
startButton.setVisibility(View.INVISIBLE);
gameRelativeLayout.setVisibility(RelativeLayout.VISIBLE);
playAgain(findViewById(R.id.playAgainButton));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button)findViewById(R.id.startButton);
sumTextView = (TextView)findViewById(R.id.sumTextView);
button0 = (Button)findViewById(R.id.button0);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
resultTextView = (TextView)findViewById(R.id.resultTextView);
pointsTextView = (TextView)findViewById(R.id.pointsTextView);
timerTextView = (TextView)findViewById(R.id.timerTextView);
playAgainButton = (Button)findViewById(R.id.playAgainButton);
gameRelativeLayout = (RelativeLayout)findViewById(R.id.gameRelativeLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}