From 3f14b212956b7138c5a85ab8eef1b1729f9b3b0c Mon Sep 17 00:00:00 2001 From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com> Date: Mon, 28 Oct 2024 11:19:02 +0100 Subject: [PATCH] Wrote the Trainer.cs methods and fixed a bug in VocabularyItem.cs --- VocabularyTrainer/Trainer.cs | 75 +++++++++++++++++++++++++++-- VocabularyTrainer/VocabularyItem.cs | 2 +- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/VocabularyTrainer/Trainer.cs b/VocabularyTrainer/Trainer.cs index 1e65c67..bd833ac 100644 --- a/VocabularyTrainer/Trainer.cs +++ b/VocabularyTrainer/Trainer.cs @@ -34,7 +34,28 @@ public sealed class Trainer var alreadyAsked = new bool[_vocabularyItems.Length]; for (var i = 0; i < CycleCount; i++) { - // TODO + Console.ForegroundColor = ConsoleColor.Yellow; + int nextWord = PickNextWord(alreadyAsked); + Console.Write($"{i + 1}: {_vocabularyItems[nextWord].NativeWord, -10} = "); + + Console.ResetColor(); + string translationInput = Console.ReadLine()!; + alreadyAsked[nextWord] = true; + + bool isTranslationCorrect = _vocabularyItems[nextWord].TestTranslation(translationInput); + if (isTranslationCorrect) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("OK!"); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"No, {_vocabularyItems[nextWord].NativeWord} = {_vocabularyItems[nextWord].Translation}"); + + return; + } + Console.ResetColor(); } Console.WriteLine(); @@ -69,8 +90,42 @@ public sealed class Trainer /// Index of next word to use private int PickNextWord(bool[] alreadyAsked) { - // TODO - return -1; + //Instead of a while loop trying every combination, we randomize only between the useAble words + int useAbleWords = UseAbleWords(alreadyAsked, out int[] indexOfUseAbleWords); + int randomIndex = RandomProvider.Random.Next(0, useAbleWords - 1); + + return indexOfUseAbleWords[randomIndex]; + + int UseAbleWords(bool[] alreadyAsked, out int[] indexOfUseAbleWords) + { + int count = CountUseAbleWords(alreadyAsked); + indexOfUseAbleWords = new int[count]; + int j = 0; + for (int i = 0; i < alreadyAsked.Length; i++) + { + if (!alreadyAsked[i]) + { + indexOfUseAbleWords[j] = i; + j++; + } + } + + return count; + } + + int CountUseAbleWords(bool[] alreadyAsked) + { + int count = 0; + for (int i = 0; i < alreadyAsked.Length; i++) + { + if (!alreadyAsked[i]) + { + count++; + } + } + + return count; + } } /// @@ -78,7 +133,19 @@ public sealed class Trainer /// private void Sort() { - // TODO + for (int i = 0; i < _vocabularyItems.Length - 1; i++) + { + int result = _vocabularyItems[i].CompareTo(_vocabularyItems[i + 1]); + if (result > 0) + { + Swap(i, i + 1); + } + } + + void Swap(int i, int j) + { + (_vocabularyItems[i], _vocabularyItems[j]) = (_vocabularyItems[j], _vocabularyItems[i]); + } } /// diff --git a/VocabularyTrainer/VocabularyItem.cs b/VocabularyTrainer/VocabularyItem.cs index c1fd457..6f6164f 100644 --- a/VocabularyTrainer/VocabularyItem.cs +++ b/VocabularyTrainer/VocabularyItem.cs @@ -24,7 +24,7 @@ public sealed class VocabularyItem public bool TestTranslation(string translationAttempt) { _countAsked++; - if (Translation == translationAttempt) + if (string.Equals(Translation, translationAttempt, StringComparison.CurrentCultureIgnoreCase)) { _countCorrect++; return true;