-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuizWindow.xaml.cs
639 lines (512 loc) · 18.3 KB
/
QuizWindow.xaml.cs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
namespace MyDictionary
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MyDictionary.Classes;
using MyDictionary.Enums;
using MyDictionary.Helpers;
using MyDictionary.Interfaces;
using System.Text.RegularExpressions;
/// <summary>
/// Interaction logic for QuizWindow.xaml
/// </summary>
public partial class QuizWindow : Window
{
private readonly string defaultAnswerText = "";
private const string AnswerJoinSymbol = "$";
private TextBox lastActiveTB;
private DateTime? startDate;
private DateTime? endDate;
private Random rnd;
private IAnnouncer announcer;
private bool[] correctness;
private IList<IWord> questionWords;
private string[] answers;
private QuizType quizType;
private int numOfQuestions;
private int currQind;
private IDictionaryEngine engine;
private string[] questions;
private string[] correctAnswers;
public QuizWindow(
string quizType, int numOfQuestions, IDictionaryEngine engine,
DateTime? startDate, DateTime? endDate)
{
this.InitializeComponent();
this.engine = engine;
this.NumOfQuestions = numOfQuestions;
this.quizType = Helper.ParseQuizType(quizType);
this.startDate = startDate;
this.endDate = endDate;
this.announcer = new WPFannouncer();
this.rnd = new Random();
this.Title = quizType + " - " + this.numOfQuestions + " Questions";
this.Title += string.Format(Helper.SignedInMessage, this.engine.User.Name);
this.InitializeData();
}
private int NumOfQuestions
{
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(
"Questions number cannot be 0 or less.");
}
this.numOfQuestions = value;
}
}
private void InitializeData()
{
this.answerTB.Text = defaultAnswerText;
var acceptedTypes = this.DetermineAcceptedTypes();
this.questionWords = this.engine.GenerateQuiz(
this.numOfQuestions, acceptedTypes,
this.quizType, this.startDate, this.endDate);
if (this.questionWords == null ||
this.questionWords.Count == 0)
{
throw new QuizException(
"Not enough appropriate questions in dictionary.");
}
this.questions = new string[this.questionWords.Count];
this.answers = new string[this.questionWords.Count];
this.correctness = new bool[this.questionWords.Count];
this.correctAnswers = new string[this.questionWords.Count];
this.qNumIndicator.Text = "1";
this.totalQs.Text = this.questionWords.Count.ToString();
var currWord = this.questionWords[this.currQind];
this.HandleAnswerTBsVisibility();
this.RenderAppropriately(currWord);
}
private IEnumerable<WordType> DetermineAcceptedTypes()
{
var result = new List<WordType>()
{
WordType.Adjective,
WordType.Noun,
WordType.Verb
};
switch (this.quizType)
{
case QuizType.Article:
result.Remove(WordType.Verb);
result.Remove(WordType.Adjective);
break;
case QuizType.VerbPastTenses:
result.Remove(WordType.Adjective);
result.Remove(WordType.Noun);
break;
case QuizType.Preposition:
result.Remove(WordType.Noun);
result.Remove(WordType.Adjective);
break;
}
return result;
}
private void RenderAppropriately(IWord word)
{
switch (this.quizType)
{
case QuizType.ForeignToNative:
this.RenderForeignToNative(word);
break;
case QuizType.Article:
this.RenderArticle(word);
break;
case QuizType.VerbPastTenses:
this.RenderVerbPastTenses(word);
break;
case QuizType.Preposition:
this.RenderPrepositions(word);
break;
case QuizType.NativeToForeign:
this.RenderNativeToForeign(word);
break;
}
this.questions[currQind] = this.questionTBL.Text;
this.answerTB.Focus();
}
private void RenderNativeToForeign(IWord word)
{
int ind = this.rnd.Next() % word.Definition.Count;
this.questionTBL.Text = word.Definition[ind];
}
private void RenderForeignToNative(IWord wordToRender)
{
this.questionTBL.Text = string.Empty;
// Add article if noun
if (wordToRender.Type == WordType.Noun)
{
this.questionTBL.Text += wordToRender.Article + " ";
}
// Add content
this.questionTBL.Text += wordToRender.Content;
// Add plural if noun
if (wordToRender.Type == WordType.Noun)
{
this.questionTBL.Text += " " + wordToRender.Plural;
}
}
private void RenderArticle(IWord wordToRender)
{
this.questionTBL.Text = wordToRender.Content;
}
private void RenderVerbPastTenses(IWord wordToRender)
{
this.questionTBL.Text = wordToRender.Content;
}
private void RenderPrepositions(IWord wordToRender)
{
this.questionTBL.Text = wordToRender.Content;
}
private void MoveToQuestion(int qIndex)
{
if (qIndex < 0 || qIndex >= this.questionWords.Count)
{
throw new ArgumentOutOfRangeException("Question number is outside the accepted range");
}
this.currQind = qIndex;
var wordToRender = this.questionWords[currQind]; // change from march 30th - ind changed to currQind from qIndex
this.qNumIndicator.Text = (this.currQind + 1).ToString();
this.AlternateAnswerButtonName();
this.RenderAppropriately(wordToRender);
}
private void qNumIndicator_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter && e.Key != Key.Return) return;
if (!string.IsNullOrWhiteSpace(qNumIndicator.Text))
{
try
{
int newQnum = int.Parse(qNumIndicator.Text);
this.MoveToQuestion(newQnum - 1);
}
catch (ArgumentOutOfRangeException ex)
{
MessageBox.Show(ex.ParamName, "Error!");
}
catch (FormatException) { }
}
}
private void Answer_ButtClick(object sender, RoutedEventArgs e)
{
string answer = string.Empty;
try
{
answer = this.answStackPanel
.Children
.OfType<TextBox>()
.Where(c => !string.IsNullOrWhiteSpace(c.Text))
.Select(c => c.Text)
.Aggregate((s1, s2) => s1 + AnswerJoinSymbol + s2)
.Trim()
.ToLower();
}
catch (SystemException)
{
this.announcer.InvalidAnswer();
return;
}
answer = Helper.CutMultipleSpaces(answer);
if (string.IsNullOrWhiteSpace(answer))
{
this.announcer.InvalidAnswer();
this.ResetAllTextBoxes();
return;
}
this.answers[this.currQind] = answer;
try
{
this.JudgeAnswer(answer);
}
catch (ArgumentException ex)
{
this.announcer.InvalidAnswer(ex.Message);
return;
}
this.ResetAllAnswerTBs();
this.HandleAnswerTBsVisibility();
this.answerTB.Focus();
if (this.currQind + 1 < this.questionWords.Count)
{
this.MoveToQuestion(this.currQind + 1);
}
else
{
this.QuizEnded();
}
}
private IEnumerable<string> SplitAndTrimAnswers(string answer)
{
var answers = answer
.Split(AnswerJoinSymbol.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
.Select(a => a.Trim());
return answers;
}
private void JudgeAnswer(string answer)
{
var currWord = this.questionWords[this.currQind];
switch (this.quizType)
{
case QuizType.ForeignToNative:
this.JudgeForeignToNative(currWord, answer);
break;
case QuizType.NativeToForeign:
this.JudgeNativeToForeign(currWord, answer);
break;
case QuizType.Article:
this.JudgeArticleQuiz(currWord, answer);
break;
case QuizType.VerbPastTenses:
this.JudgeVerbPastTensesQuiz(currWord, answer);
break;
case QuizType.Preposition:
this.JudgePrepositionsQuiz(currWord, answer);
break;
}
}
private void JudgeNativeToForeign(IWord currWord, string answer)
{
var answers = this.SplitAndTrimAnswers(answer).ToArray();
if (answers.Length > 1)
{
throw new ArgumentException("There must be only one answer");
}
string compareWith = currWord.Content.ToLower();
if (currWord.Type == WordType.Noun)
{
compareWith = currWord.Article + " " + compareWith;
}
if (compareWith == answers[0])
{
this.correctness[currQind] = true;
}
this.correctAnswers[currQind] = compareWith;
}
private void JudgeForeignToNative(IWord currWord, string answer)
{
double guessed = 0;
var answers = this.SplitAndTrimAnswers(answer);
foreach (var attempt in answers)
{
var defsToLower = currWord.Definition.Select(d => d.ToLower().Trim());
if (defsToLower.Contains(attempt))
{
guessed++;
}
}
double definitionsCount = (double)currWord.Definition.Count;
var malus = definitionsCount - answers.Count();
if (malus < 0)
{
guessed += malus;
}
if (guessed / definitionsCount > 0.5)
{
this.correctness[currQind] = true;
}
this.correctAnswers[currQind] = string.Join(", ", currWord.Definition);
}
private void JudgeArticleQuiz(IWord currWord, string answer)
{
var answers = this.SplitAndTrimAnswers(answer);
if (answers.Count() > 1)
{
throw new ArgumentException("There must only be one answer");
}
WordArticles answerType = WordArticles.NotApplicable;
try
{
answerType = (WordArticles)Enum.Parse(typeof(WordArticles), answer);
}
catch (ArgumentException ex)
{
throw new ArgumentOutOfRangeException("Please enter a valid word article.", ex);
}
if (answerType == currWord.Article)
{
this.correctness[currQind] = true;
}
this.correctAnswers[currQind] = currWord.Article.ToString();
}
private void JudgeVerbPastTensesQuiz(IWord currWord, string answer)
{
int guessed = 0;
var answers = this.SplitAndTrimAnswers(answer);
this.correctAnswers[currQind] =
currWord.Partizip2 + ", " + currWord.PSgPras + ", " + currWord.Pratitium;
if (answers.Count() < 2)
{
return;
}
if (answers.Contains(currWord.Partizip2.ToLower()))
{
guessed++;
}
if (answers.Contains(currWord.PSgPras.ToLower()))
{
guessed++;
}
if (answers.Contains(currWord.Pratitium.ToLower()))
{
guessed++;
}
if (guessed >= 2)
{
this.correctness[currQind] = true;
}
}
private void JudgePrepositionsQuiz(IWord currWord, string answer)
{
double guessed = 0;
var answers = this.SplitAndTrimAnswers(answer);
var preposToLower = currWord.Prepositions.Select(p => p.ToLower());
foreach (var answ in answers)
{
if (preposToLower.Contains(answ))
{
guessed++;
}
}
double numOfPrepos = (double)currWord.Prepositions.Count;
var malus = numOfPrepos - answers.Count();
if (malus < 0)
{
guessed += malus;
}
if (guessed / numOfPrepos > 0.5)
{
this.correctness[currQind] = true;
}
var correctAnswer = string.Join(", ", currWord.Prepositions);
this.correctAnswers[currQind] = correctAnswer;
}
private void AlternateAnswerButtonName()
{
if (this.currQind == this.questionWords.Count - 1)
{
(this.answerButt.Content as TextBlock).Text = "Complete Quiz";
}
else
{
(this.answerButt.Content as TextBlock).Text = "Answer";
}
}
private void QuizEnded()
{
var resultsWindow = new QuizResultsWindow(this.quizType, this.questionWords, this.questions, this.correctAnswers, this.answers, this.correctness);
resultsWindow.Owner = this.Owner;
resultsWindow.Activate();
resultsWindow.Show();
this.Close();
resultsWindow.Focus();
}
private void QuizWindow_Closed(object sender, EventArgs e)
{
this.Owner.Show();
}
private void ResetAllTextBoxes()
{
this.answerTB.Text = this.defaultAnswerText;
this.ResetAllAnswerTBs();
this.answerTB.Focus();
}
private void CharacterButtClick(object sender, RoutedEventArgs e)
{
var senderAsBtn = sender as Button;
if (senderAsBtn != null)
{
this.AddStringToContainer(senderAsBtn.Content as string, this.lastActiveTB);
}
}
private void AddStringToContainer(string str, object container)
{
var cntrAsTextBox = container as TextBox;
if (cntrAsTextBox != null)
{
cntrAsTextBox.Focus();
cntrAsTextBox.Text += str;
cntrAsTextBox.CaretIndex = int.MaxValue;
}
}
private void UpdateLastActiveTB(object sender, RoutedEventArgs e)
{
this.lastActiveTB = sender as TextBox;
}
private void AnswerTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Return &&
e.Key != Key.Enter)
{
return;
}
this.Answer_ButtClick(new object(), new RoutedEventArgs());
}
private void HideAllTextBoxes(Panel panel)
{
foreach (var item in panel.Children)
{
var tb = item as TextBox;
if (tb != null)
{
tb.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
private void ResetAllAnswerTBs()
{
foreach (var item in this.answStackPanel.Children)
{
var tb = item as TextBox;
tb.Text = string.Empty;
}
}
private void HandleAnswerTBsVisibility()
{
this.HideEmptyAnswerTBs();
var allTBs = this.answStackPanel.Children.OfType<TextBox>();
var visibleTBs = allTBs
.Where(tb => tb.Visibility == Visibility.Visible);
var visibleEmpties = visibleTBs.Where(tb => tb.Text == string.Empty);
if (visibleEmpties.Count() < 2)
{
var tbToShow = allTBs
.FirstOrDefault(tb => tb.Visibility == Visibility.Collapsed);
if (tbToShow != null)
{
tbToShow.Visibility = Visibility.Visible;
}
}
}
private void HideEmptyAnswerTBs()
{
foreach (var item in this.answStackPanel.Children)
{
var tb = item as TextBox;
if (tb != null)
{
if (string.IsNullOrWhiteSpace(tb.Text))
{
tb.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
}
private void HandleAnswerTBsVisibility(object sender, TextChangedEventArgs e)
{
this.HandleAnswerTBsVisibility();
}
}
}