-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearn.java
More file actions
290 lines (271 loc) · 9.93 KB
/
Learn.java
File metadata and controls
290 lines (271 loc) · 9.93 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
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class Learn extends VBox {
// instance variables
private boolean text;
private ArrayList<String> words;
private ArrayList<Double> weights;
private ArrayList<String> definitions;
// user interface components
private Label titleLabel = new Label("Learn");
private ToggleButton modeToggle = new ToggleButton("Multiple Choice");
private Label prompt = new Label("");
private Label feedback = new Label("");
private Label progressLabel = new Label("");
private Button nextBtn = new Button("Next");
private VBox choicesBox = new VBox(8);
private Button[] choiceBtns = new Button[] { new Button(), new Button(), new Button(), new Button() };
private HBox freeBox = new HBox(8);
private TextField answerField = new TextField();
private Button submitBtn = new Button("Submit");
private int currentIndex = -1;
private boolean askForDefinition = true;
private boolean awaitingAnswer = true;
private int totalAsked = 0;
private int totalCorrect = 0;
private EXPBarUI expBar;
private Random rng = new Random();
private Soundplayer soundPlayer = new Soundplayer();
private Label streak;
private int correctStreak;
public Learn(ArrayList<String> w, ArrayList<Double> we, ArrayList<String> d, EXPBarUI exp) {
text = false;
words = w;
weights = we;
definitions = d;
double maxWidth = 360;
expBar = exp;
correctStreak = 0;
streak = new Label("Start a Streak!");
titleLabel.getStyleClass().add("app-header");
prompt.getStyleClass().add("sub-title");
progressLabel.getStyleClass().add("xp-label");
streak.getStyleClass().add("sub-title");
setSpacing(10);
setPadding(new Insets(16));
modeToggle.getStyleClass().add("switch-button");
for (int i = 0; i < choiceBtns.length; i++) {
choiceBtns[i].setMaxWidth(maxWidth);
choiceBtns[i].getStyleClass().addAll("match-button");
Animations.applyButtonHover(choiceBtns[i]);
choicesBox.getChildren().add(choiceBtns[i]);
}
freeBox.getChildren().addAll(answerField, submitBtn);
answerField.getStyleClass().add("text-field");
submitBtn.getStyleClass().add("primary");
nextBtn.getStyleClass().add("primary");
Animations.applyButtonHover(submitBtn);
Animations.applyButtonHover(nextBtn);
// entrance animation for this view
Animations.fadeIn(this);
answerField.setOnAction(e -> submitBtn.fire());
modeToggle.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
switchText();
if (isText()) {
modeToggle.setText("Free Response");
} else {
modeToggle.setText("Multiple Choice");
}
feedback.setText("");
answerField.clear();
nextQuestion();
}
});
nextBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
nextQuestion();
}
});
EventHandler<ActionEvent> choiceHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
if (!awaitingAnswer) return;
Button b = (Button) e.getSource();
String chosen = b.getText();
evaluateAnswer(chosen);
}
};
for (int i = 0; i < choiceBtns.length; i++) {
choiceBtns[i].setOnAction(choiceHandler);
}
submitBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
if (!awaitingAnswer) return;
String ans;
if (answerField.getText() == null) {
ans = "";
} else {
ans = answerField.getText().trim();
}
evaluateAnswer(ans);
if (isText()) {
nextBtn.requestFocus();
}
}
});
getChildren().addAll(titleLabel, modeToggle, progressLabel, streak, prompt, choicesBox, freeBox, feedback, nextBtn);
nextQuestion();
}
public boolean isText() {
return text;
}
public void switchText() {
text = !text;
}
private void nextQuestion() {
if (words == null || definitions == null || words.size() < 1 || definitions.size() < 1) {
prompt.setText("No cards available.");
choicesBox.setVisible(false);
choicesBox.setManaged(false);
freeBox.setVisible(false);
freeBox.setManaged(false);
nextBtn.setDisable(true);
return;
}
awaitingAnswer = true;
feedback.setText("");
nextBtn.setDisable(true);
answerField.clear();
askForDefinition = rng.nextBoolean();
currentIndex = Utils.weightedIndex(weights, words);
if (currentIndex < 0) {
prompt.setText("No cards available.");
return;
}
String term = words.get(currentIndex);
String def = definitions.get(currentIndex);
if (askForDefinition) {
prompt.setText("Define: " + term);
} else {
prompt.setText("Term for: " + def);
}
if (isText()) {
freeBox.setVisible(true);
freeBox.setManaged(true);
choicesBox.setVisible(false);
choicesBox.setManaged(false);
// focus the free response input so the user can type immediately
answerField.requestFocus();
} else {
freeBox.setVisible(false);
freeBox.setManaged(false);
choicesBox.setVisible(true);
choicesBox.setManaged(true);
fillChoices(currentIndex, askForDefinition);
}
updateProgressLabel();
}
private void fillChoices(int correctIdx, boolean askDef) {
String correct;
if (askDef) {
correct = definitions.get(correctIdx);
} else {
correct = words.get(correctIdx);
}
ArrayList<String> options = new ArrayList<String>();
options.add(correct);
Set<Integer> used = new HashSet<Integer>();
used.add(Integer.valueOf(correctIdx));
while (options.size() < 4 && used.size() < words.size()) {
int candidate = rng.nextInt(words.size());
if (used.contains(Integer.valueOf(candidate))) continue;
used.add(Integer.valueOf(candidate));
String wrong;
if (askDef) {
wrong = definitions.get(candidate);
} else {
wrong = words.get(candidate);
}
if (!options.contains(wrong)) {
options.add(wrong);
}
}
Collections.shuffle(options);
for (int i = 0; i < choiceBtns.length; i++) {
if (i < options.size()) {
choiceBtns[i].setText(options.get(i));
choiceBtns[i].setDisable(false);
choiceBtns[i].setVisible(true);
choiceBtns[i].setManaged(true);
} else {
choiceBtns[i].setText("");
choiceBtns[i].setDisable(true);
choiceBtns[i].setVisible(false);
choiceBtns[i].setManaged(false);
}
}
}
private void evaluateAnswer(String given) {
if (currentIndex < 0) return;
String correct;
if (askForDefinition) {
correct = definitions.get(currentIndex);
} else {
correct = words.get(currentIndex);
}
boolean correctAns;
if (isText()) {
String normGiven;
if (given == null) {
normGiven = "";
} else {
normGiven = given.trim().toLowerCase();
}
String normCorrect = correct.trim().toLowerCase();
correctAns = normGiven.equals(normCorrect);
} else {
correctAns = correct.equals(given);
}
awaitingAnswer = false;
totalAsked++;
if (correctAns) {
totalCorrect++;
correctStreak++;
feedback.setText("Correct!");
adjustWeight(currentIndex, true);
soundPlayer.playCorrect();
expBar.addXP(10 + Math.max((correctStreak - 5) * 5, 0)); //temp value for actually leveling up
streak.setText("Current Streak: " + correctStreak);
if (isText()) {
expBar.addXP(10 + Math.max((correctStreak - 5) * 5, 0));
}
} else {
correctStreak = 0;
streak.setText("Streak reset to 0!");
feedback.setText("Incorrect. Correct answer: " + correct);
adjustWeight(currentIndex, false);
soundPlayer.playWrong();
}
nextBtn.setDisable(false);
updateProgressLabel();
}
private void adjustWeight(int idx, boolean correct) {
if (idx < 0 || idx >= weights.size()) return;
double w = weights.get(idx).doubleValue();
if (correct) {
w = w - 0.2;
} else {
w = w + 0.2;
}
weights.set(idx, Double.valueOf(w));
}
private void updateProgressLabel() {
progressLabel.setText("Progress: " + totalCorrect + " / " + totalAsked);
}
}