Wrote the Trainer.cs methods and fixed a bug in VocabularyItem.cs

This commit is contained in:
MarcUs7i 2024-10-28 11:19:02 +01:00
parent bed0769995
commit 3f14b21295
2 changed files with 72 additions and 5 deletions

View file

@ -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
/// <returns>Index of next word to use</returns>
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;
}
}
/// <summary>
@ -78,7 +133,19 @@ public sealed class Trainer
/// </summary>
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]);
}
}
/// <summary>

View file

@ -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;