Wrote the Trainer.cs methods and fixed a bug in VocabularyItem.cs
This commit is contained in:
parent
bed0769995
commit
3f14b21295
2 changed files with 72 additions and 5 deletions
|
|
@ -34,7 +34,28 @@ public sealed class Trainer
|
||||||
var alreadyAsked = new bool[_vocabularyItems.Length];
|
var alreadyAsked = new bool[_vocabularyItems.Length];
|
||||||
for (var i = 0; i < CycleCount; i++)
|
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.ResetColor();
|
||||||
}
|
}
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|
@ -69,8 +90,42 @@ public sealed class Trainer
|
||||||
/// <returns>Index of next word to use</returns>
|
/// <returns>Index of next word to use</returns>
|
||||||
private int PickNextWord(bool[] alreadyAsked)
|
private int PickNextWord(bool[] alreadyAsked)
|
||||||
{
|
{
|
||||||
// TODO
|
//Instead of a while loop trying every combination, we randomize only between the useAble words
|
||||||
return -1;
|
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>
|
/// <summary>
|
||||||
|
|
@ -78,7 +133,19 @@ public sealed class Trainer
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void Sort()
|
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>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ public sealed class VocabularyItem
|
||||||
public bool TestTranslation(string translationAttempt)
|
public bool TestTranslation(string translationAttempt)
|
||||||
{
|
{
|
||||||
_countAsked++;
|
_countAsked++;
|
||||||
if (Translation == translationAttempt)
|
if (string.Equals(Translation, translationAttempt, StringComparison.CurrentCultureIgnoreCase))
|
||||||
{
|
{
|
||||||
_countCorrect++;
|
_countCorrect++;
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue