-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatching.java
More file actions
277 lines (262 loc) · 11.2 KB
/
Matching.java
File metadata and controls
277 lines (262 loc) · 11.2 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
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
import javafx.scene.text.Font;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.scene.Node;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.Animation;
public class Matching extends VBox {
private ArrayList<String> words;
private ArrayList<String> meanings;
private ArrayList<String> randomized;
private Button wordButton = new Button();
private Button meaningButton = new Button();
private Button startButton = new Button("Start");
private ArrayList<Button> wordButtonList = new ArrayList<Button>();
private ArrayList<Button> meaningButtonList = new ArrayList<Button>();
private ArrayList<Button> randomizedButtonList = new ArrayList<Button>();
private Button selectedWord;
private Button selectedMeaning;
private Button randomizedButton;
private int selectedWordIndex = -1;
private int selectedMeaningIndex = -1;
private boolean check1 = false;
private boolean check2 = false;
private int rand;
private EXPBarUI xp;
private Timeline timeline;
private double time;
private double avgTime;
// gui components
private Label matchingLabel = new Label("Matching");
private Label feedback = new Label("Click start to begin!");
private Label showTimer = new Label();
private Label fail = new Label("unequal # of words and definitions");
private VBox bvox = new VBox(16);
private HBox timer = new HBox(2);
Font microwave;
private Soundplayer soundplayer = new Soundplayer();
public Matching(ArrayList<String> w, ArrayList<String> m, EXPBarUI xpBar) {
words = w;
meanings = m;
xp = xpBar;
if (words == null) words = new ArrayList<String>();
if (meanings == null) meanings = new ArrayList<String>();
randomized = new ArrayList<String>(words);
time = 0;
avgTime = words.size() * 1.5;
if (words.size() == 0 || meanings.size() == 0) {
startButton.setDisable(true);
feedback.setText("No cards in the selected set");
}
setSpacing(10);
setPadding(new Insets(16));
this.getStyleClass().add("app-container");
matchingLabel.getStyleClass().add("app-header");
feedback.getStyleClass().add("sub-title");
microwave = Font.loadFont("file:fonts/microwave.ttf", 36);
showTimer.setText(String.format("%.2f", time));
showTimer.getStyleClass().add("timer");
showTimer.setFont(microwave);
if (words.size() != meanings.size())
bvox.getChildren().add(fail);
else {
for (int i = 0; i < words.size(); i++) {
wordButton = new Button(words.get(i));
meaningButton = new Button(meanings.get(i));
wordButton.getStyleClass().addAll("match-button", "match-word");
meaningButton.getStyleClass().addAll("match-button", "match-meaning");
Animations.applyButtonHover(wordButton);
Animations.applyButtonHover(meaningButton);
wordButtonList.add(wordButton);
meaningButtonList.add(meaningButton);
rand = (int)(Math.random() * randomized.size());
randomizedButton = new Button(randomized.get(rand));
randomizedButton.getStyleClass().addAll("match-button", "match-meaning");
Animations.applyButtonHover(randomizedButton);
randomizedButtonList.add(randomizedButton);
HBox setWords = new HBox(2);
setWords.getChildren().addAll(randomizedButton, meaningButton);
bvox.getChildren().add(setWords);
randomized.remove(rand);
}
}
timer.getChildren().addAll(showTimer, startButton);
getChildren().addAll(matchingLabel, timer, feedback);
Animations.fadeIn(this);
startButton.setOnAction(e -> {
getChildren().add(2, bvox);
startButton.setVisible(false);
stopwatch();
});
for (int i = 0; i < meaningButtonList.size(); i++) {
Button mb = meaningButtonList.get(i);
mb.setOnAction(e -> {
feedback.setText("Selected " + mb.getText());
if (selectedMeaning != null) {
selectedMeaning.getStyleClass().remove("selected");
}
selectedMeaning = mb;
mb.getStyleClass().add("selected");
if (selectedWord != null) {
match(selectedWord, selectedMeaning);
}
});
}
for (int i = 0; i < randomizedButtonList.size(); i++) {
Button wb = randomizedButtonList.get(i);
wb.setOnAction(e -> {
feedback.setText("Selected " + wb.getText());
if (selectedWord != null) {
selectedWord.getStyleClass().remove("selected");
}
selectedWord = wb;
wb.getStyleClass().add("selected");
if (selectedMeaning != null) {
match(selectedWord, selectedMeaning);
}
});
}
}
public void stopwatch() {
timeline = new Timeline(new KeyFrame(Duration.millis(10), ev -> {
time += 0.01;
showTimer.setText(String.format("%.2f", time));
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
/** Needs to acount for:
* Creating buttons
* Actions that occur on button click
* Setting buttons equal to term / definition
* Setting button pairs (one term on the left side that corresponds with term on the right side)
* Removal of buttons that were clicked
* E
* Match action (clicking one button, then clicking another on the other side, runs check after completed)
* Check action (Checking to see if buttons that were clicked are a correct pair)
* */
public boolean check(String wordClicked, String definitionClicked){
int ind1 = words.indexOf(wordClicked);
int ind2 = meanings.indexOf(definitionClicked);
return(ind1 == ind2);
}
public void match(Button button1, Button button2){
String term = button1.getText();
String definition = button2.getText();
boolean correct = check(term, definition);
if (correct){
xp.addXP(10);
feedback.setText("Well Done!");
soundplayer.playCorrect();
selectedWord.setVisible(false);
selectedMeaning.setVisible(false);
meaningButtonList.remove(selectedMeaning);
if (meaningButtonList.size() == 0) {
xp.addXP(50);
feedback.setText("All matched!\nBonus 50 XP!\nClick Matching to try again.");
timeline.stop();
if (time < avgTime) {
xp.addXP(50);
feedback.setText("All matched quickly!\nBonus 100 XP!\nClick Matching to try again.");
}
}
}
else{
feedback.setText("Incorrect! Try again.");
soundplayer.playWrong();
// do NOT remove button1 and button2
}
selectedMeaning = null;
selectedWord = null;
button1.getStyleClass().remove("selected");
button2.getStyleClass().remove("selected");
}
public boolean hasCards() {
return this.words != null && this.meanings != null && this.words.size() > 0 && this.meanings.size() > 0;
}
public void disableStartIfNoCards(Button startBtn) {
if (startBtn == null) return;
startBtn.setDisable(!hasCards());
}
}
// for (int i = 0; i < wordButtonList.size(); i++) {
// final int idx = i;
// Button wb = wordButtonList.get(i);
// wb.setOnAction(e -> {
// if (selectedWord != null && selectedWord != wb) {
// selectedWord.getStyleClass().remove("selected");
// }
// selectedWordIndex = idx;
// selectedWord = wb;
// wb.getStyleClass().add("selected");
// if (selectedMeaningIndex >= 0 && selectedMeaning != null) {
// boolean correct = check(selectedWord.getText(), selectedMeaning.getText());
// if (correct) {
// xpBar.addXP(10);
// selectedWord.getStyleClass().add("matched");
// selectedMeaning.getStyleClass().add("matched");
// selectedWord.setVisible(false);
// selectedMeaning.setVisible(false);
// } else {
// selectedWord.getStyleClass().remove("selected");
// selectedMeaning.getStyleClass().remove("selected");
// }
// selectedWord = null;
// selectedMeaning = null;
// selectedWordIndex = -1;
// selectedMeaningIndex = -1;
// }
// });
// }
// for (int i = 0; i < meaningButtonList.size(); i++) {
// final int idx = i;
// Button mb = meaningButtonList.get(i);
// mb.setOnAction(e -> {
// if (selectedMeaning != null && selectedMeaning != mb) {
// selectedMeaning.getStyleClass().remove("selected");
// }
// selectedMeaningIndex = idx;
// selectedMeaning = mb;
// mb.getStyleClass().add("selected");
// if (selectedWordIndex >= 0 && selectedWord != null) {
// boolean correct = check(selectedWord.getText(), selectedMeaning.getText());
// if (correct) {
// xpBar.addXP(10);
// selectedWord.getStyleClass().add("matched");
// selectedMeaning.getStyleClass().add("matched");
// selectedWord.setVisible(false);
// selectedMeaning.setVisible(false);
// } else {
// selectedWord.getStyleClass().remove("selected");
// selectedMeaning.getStyleClass().remove("selected");
// }
// selectedWord = null;
// selectedMeaning = null;
// selectedWordIndex = -1;
// selectedMeaningIndex = -1;
// }
// });
// }