-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmino_Acid_Quiz_III.java
More file actions
282 lines (251 loc) · 6.84 KB
/
Copy pathAmino_Acid_Quiz_III.java
File metadata and controls
282 lines (251 loc) · 6.84 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
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import java.awt.event.*;
import javax.swing.SwingUtilities;
import java.lang.reflect.InvocationTargetException;
public class Amino_Acid_Quiz_III extends JFrame
{
//declare gui elemetns
private JTextField aaQuestion = new JTextField();
private JTextField aaAnswer = new JTextField();
private JTextField aaTime = new JTextField();
private JTextField aaScore = new JTextField();
public JButton aaStartButton = new JButton("Start");
private JButton aaEndButton = new JButton("End");
private JPanel aaScore_Time = new JPanel();
//declare Engine so it can be referecned befroe it is started
private Engine engine1 = new Engine(1);
//this boolean tracks if the game has been ended
private boolean cancelled = false;
//this boolean tracks if a potential answer has been entered and needs scoring
//this was orignially meant to be chages by an action listener on the answer textfeild, whihc is why it isw declared here
private boolean answerEntered = false;
public Amino_Acid_Quiz_III()
{
super("Amino Acid Quiz");
//setting up gui
setSize(600,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set up sub-panel
aaScore_Time.setLayout(new BorderLayout());
aaScore_Time.add(aaTime, BorderLayout.EAST);
aaScore_Time.add(aaScore, BorderLayout.WEST);
//set layout
getContentPane().setLayout(new BorderLayout());
getContentPane().add(aaQuestion, BorderLayout.WEST);
getContentPane().add(aaAnswer, BorderLayout.CENTER);
getContentPane().add(aaScore_Time, BorderLayout.EAST);
getContentPane().add(aaStartButton, BorderLayout.NORTH);
getContentPane().add(aaEndButton, BorderLayout.SOUTH);
//set gui element attributes
aaStartButton.addActionListener(new buttonPress());
aaEndButton.addActionListener(new gameEnd());
aaQuestion.setColumns(20);
aaAnswer.setColumns(20);
aaTime.setColumns(10);
aaScore.setColumns(10);
aaQuestion.setText("Question");
aaAnswer.setText("Answer");
aaTime.setText("Time");
aaScore.setText("Score");
setVisible(true);
}
// the main method
public static void main(String[] args)
{
Amino_Acid_Quiz_III aaQuiz = new Amino_Acid_Quiz_III();
}
//governs the actions taken when the start button is pressed
private class buttonPress implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
new Thread(engine1).start();
}
}
//governs the actions taken when the end button is pressed
private class gameEnd implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
cancelled = true;
}
}
//updates the timer
private void timerUpdate(long currentTime)
{
aaTime.setText("Time: " + currentTime);
}
private void timerFinalUpdate(String message)
{
aaTime.setText(message);
}
//udates the score
private void scoreUpdate(int newScore)
{
aaScore.setText("Score: " + newScore);
}
//chages the test in aaQuestion
private void setQeustion(String newQuestion)
{
aaQuestion.setText(newQuestion);
}
//get the text in aaQuestion
private String getQuestion()
{
return(aaQuestion.getText());
}
//get the text in aaAnswer
private String getAnswer()
{
return(aaAnswer.getText());
}
//accesses value of answerEntered
private boolean checkAnswerEntered()
{
return(answerEntered);
}
//changes value of answerEntered
private void changeAnswerEntered(boolean newValue)
{
answerEntered = newValue;
}
//engine runs the game
private class Engine implements Runnable
{
private final int threadID;
private long startTime;
//constructor
public Engine(int threadID)
{
this.threadID = threadID;
}
//starts the clock
public void startClock()
{
startTime = System.currentTimeMillis();
}
//using to find what time should be displayed in the gui
public long currentTime()
{
return((System.currentTimeMillis() - startTime) / 1000);
}
// tells if time is up
public boolean isTimeUp()
{
if ((System.currentTimeMillis() - startTime) >= 30000)
{
return(true);
}
else
{
return(false);
}
}
//controls game flow and updates time from a separate thread
public void run()
{
startClock();
try {
//string to hold the contents of aaAnswer, so it can be checked for changes
String answerChange = getAnswer();
//int to hold the score be fore it is updated
int score = 0;
//random to generate new questions
Random rand = new Random();
//int to receive random numbers
int randPick = 0;
//boolean to keep trakc of whether a new question is needed
boolean nextQuestion = true;
//full names for selecting question
String[] FULL_NAMES =
{
"alanine","arginine", "asparagine",
"aspartic acid", "cysteine",
"glutamine", "glutamic acid",
"glycine" ,"histidine","isoleucine",
"leucine", "lysine", "methionine",
"phenylalanine", "proline",
"serine","threonine","tryptophan",
"tyrosine", "valine"
};
//short names for checking answer
String[] SHORT_NAMES =
{
"A","R", "N", "D", "C", "Q", "E",
"G", "H", "I", "L", "K", "M", "F",
"P", "S", "T", "W", "Y", "V"
};
//main game engine loop: repeatedly runs all needed procceses and check at 250ms intervals
while (true)
{
//update timer
timerUpdate(currentTime());
//check if time is up
if (isTimeUp())
{
break;
}
//check if game has been cancelled
if (cancelled)
{
break;
}
//check if answer has been entered
if (!(answerChange.equals(getAnswer())))
{
changeAnswerEntered(true);
answerChange = getAnswer();
}
//check if new question has to be chosen
if (nextQuestion == true)
{
randPick = rand.nextInt(20);
setQeustion(FULL_NAMES[randPick]);
nextQuestion = false;
}
//check if an answer has been entered needs to be scored
if (checkAnswerEntered() == true)
{
String answerHolder = getAnswer();
if (answerHolder.equals(SHORT_NAMES[randPick]))
{
//increment score
score++;
//signal that a new question is needed
nextQuestion = true;
//signal that a new answer must be entered before scoring
changeAnswerEntered(false);
}
else
{
//decrement score
score--;
//signal that a new question is needed
nextQuestion = true;
//signal that a new answer must be entered before scoring
changeAnswerEntered(false);
}
}
//update score
scoreUpdate(score);
//wait
Thread.sleep(250);
}
timerFinalUpdate("Time is up");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}