-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccuracy.java
More file actions
207 lines (200 loc) · 7.27 KB
/
Accuracy.java
File metadata and controls
207 lines (200 loc) · 7.27 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
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.util.ArrayList;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.util.Duration;
import javafx.scene.text.Font;
import java.util.Random;
public class Accuracy extends VBox {
private ArrayList<String> words;
private ArrayList<String> meanings;
private ArrayList<Double> weights;
private int questionsCorrect;
private double time;
private double startTime = 7.0;
private EXPBarUI expBar;
private String correctAnswer;
private String ans;
private Timeline timeline;
private boolean started;
private Random rng = new Random();
private int lastIndex = -1;
private double addTime;
private int XPadd;
// more gui components
private Label accuracylabel = new Label("Accuracy");
private VBox container = new VBox(8);
private Label feedback = new Label();
private TextField answer = new TextField();
private Button submit = new Button("Submit");
private Button start = new Button("Start");
private Label showTimer = new Label();
private HBox buttonBox = new HBox(2);
private Label score = new Label("Score: " + questionsCorrect);
private Label definition = new Label("Definition: ");
private Soundplayer soundplayer = new Soundplayer();
private Button next = new Button("Next");
Font microwave;
private HBox timerButtons = new HBox(4);
{
microwave = Font.loadFont("file:fonts/microwave.ttf", 36);
}
public Accuracy(ArrayList<String> w, ArrayList<String> m, ArrayList<Double> we, EXPBarUI exp) {
words = w;
meanings = m;
weights = we;
questionsCorrect = 0;
time = startTime;
started = false;
expBar = exp;
timeline = new Timeline();
addTime = 5;
XPadd = 20;
setSpacing(10);
setPadding(new Insets(16));
this.getStyleClass().add("app-container");
accuracylabel.getStyleClass().add("app-header");
answer.getStyleClass().add("answer");
answer.getStyleClass().add("text-field");
answer.setDisable(!started);
answer.setOnAction(ev -> {
if (!submit.isDisabled()) submit.fire();
});
next.setVisible(started);
showTimer.setText(String.format("%.2f", time));
showTimer.getStyleClass().add("timer");
score.getStyleClass().add("score");
submit.setDisable(!started);
submit.getStyleClass().add("primary");
start.getStyleClass().add("accent");
next.getStyleClass().add("primary");
if (microwave != null) {
showTimer.setFont(microwave);
}
buttonBox.getChildren().addAll(submit, next);
container.getChildren().addAll(answer, definition, feedback, buttonBox);
timerButtons.getChildren().addAll(showTimer, start);
getChildren().addAll(accuracylabel, score, timerButtons, container);
Animations.fadeIn(this);
Animations.applyButtonHover(submit);
Animations.applyButtonHover(start);
Animations.applyButtonHover(next);
if (words == null) words = new ArrayList<String>();
if (meanings == null) meanings = new ArrayList<String>();
if (weights == null) weights = new ArrayList<Double>();
if (words.size() == 0 || meanings.size() == 0) {
feedback.setText("No cards in the selected set");
answer.setDisable(true);
submit.setDisable(true);
start.setDisable(true);
start.setVisible(false);
next.setDisable(true);
next.setVisible(false);
time = 0;
showTimer.setText(String.format("%.2f", time));
return;
}
submit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
timeline.stop();
ans = "";
if (!(answer.getText() == null)) {
ans = answer.getText();
} else {
ans = "";
}
answer.setText(ans);
if (ans.toLowerCase().equals(correctAnswer.toLowerCase())) {
if (addTime > 0.25) {
addTime -= 0.25;
}
questionsCorrect++;
time += addTime;
score.setText("Score: " + questionsCorrect);
expBar.addXP(XPadd);
XPadd += 5;
feedback.setText("Correct!");
soundplayer.playCorrect();
next.setVisible(true);
submit.setDisable(true);
next.requestFocus();
} else {
feedback.setText("Incorrect! The correct answer was: " + correctAnswer);
soundplayer.playWrong();
next.setVisible(true);
submit.setDisable(true);
next.requestFocus();
}
}
});
next.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
timer();
next.setVisible(false);
answer.clear();
feedback.setText("");
submit.setDisable(false);
answer.requestFocus();
mainLoop();
}
});
start.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
time = startTime;
timer();
}
});
}
public void timer() {
mainLoop();
started = true;
answer.setDisable(false);
submit.setDisable(false);
start.setDisable(true);
answer.requestFocus();
if (timeline != null) {
timeline.stop();
}
timeline = new Timeline(new KeyFrame(Duration.millis(10), ev -> {
time -= 0.01;
if (time <= 0) {
showTimer.setText("0.00");
timeline.stop();
feedback.setText("Time's Up!");
answer.setDisable(true);
submit.setDisable(true);
start.setDisable(false);
started = false;
} else {
showTimer.setText(String.format("%.2f", time));
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
private void mainLoop() {
int index = Utils.weightedIndex(weights, words);
int attempts = 0;
while (index == lastIndex && attempts < 8) {
index = Utils.weightedIndex(weights, words);
attempts++;
}
if (index < 0 || index >= words.size()) {
if (words == null || words.size() == 0) return;
index = rng.nextInt(words.size());
}
lastIndex = index;
correctAnswer = words.get(index);
definition.setText("Definition: " + meanings.get(index));
}
}