Works as intended
This commit is contained in:
parent
ced8bcdb46
commit
6d6e667003
3 changed files with 206 additions and 18 deletions
|
|
@ -1,10 +1,14 @@
|
||||||
namespace HorseRace;
|
using System.ComponentModel;
|
||||||
|
|
||||||
|
namespace HorseRace;
|
||||||
|
|
||||||
public sealed class Horse
|
public sealed class Horse
|
||||||
{
|
{
|
||||||
public Horse(string name, int age, int startNo)
|
public Horse(string name, int age, int startNo)
|
||||||
{
|
{
|
||||||
// TODO
|
Name = name;
|
||||||
|
Age = age;
|
||||||
|
StartNumber = startNo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Age { get; }
|
public int Age { get; }
|
||||||
|
|
@ -21,8 +25,15 @@ public sealed class Horse
|
||||||
/// <returns>The position difference between two horses</returns>
|
/// <returns>The position difference between two horses</returns>
|
||||||
public int CompareTo(Horse other)
|
public int CompareTo(Horse other)
|
||||||
{
|
{
|
||||||
// TODO
|
int positionDifference = other.Position - Position;
|
||||||
return -1;
|
int startNumberDifference = other.StartNumber - StartNumber;
|
||||||
|
|
||||||
|
if(positionDifference != 0)
|
||||||
|
{
|
||||||
|
return positionDifference;
|
||||||
|
}
|
||||||
|
|
||||||
|
return startNumberDifference;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -31,7 +42,15 @@ public sealed class Horse
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Draw()
|
public void Draw()
|
||||||
{
|
{
|
||||||
// TODO
|
const int MaxSteps = HorseRace.MaxSteps;
|
||||||
|
const char HorseSymbol = '*';
|
||||||
|
|
||||||
|
Console.Write($"Horse {StartNumber,2}:");
|
||||||
|
for (int i = 0; i < MaxSteps; i++)
|
||||||
|
{
|
||||||
|
Console.Write(Position == i ? HorseSymbol : " ");
|
||||||
|
}
|
||||||
|
Console.WriteLine("|");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -39,7 +58,12 @@ public sealed class Horse
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Move()
|
public void Move()
|
||||||
{
|
{
|
||||||
// TODO
|
if (RandomProvider.Random.Next(0, 3) != 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Position += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -52,8 +76,67 @@ public sealed class Horse
|
||||||
/// <returns>True if parsed successfully; false otherwise</returns>
|
/// <returns>True if parsed successfully; false otherwise</returns>
|
||||||
public static bool TryParse(string csvLine, int startNo, out Horse? horse)
|
public static bool TryParse(string csvLine, int startNo, out Horse? horse)
|
||||||
{
|
{
|
||||||
// TODO
|
|
||||||
horse = null;
|
horse = null;
|
||||||
return false;
|
if (Contains(csvLine, ';') != 1)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startNo <= 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string name = "";
|
||||||
|
int length = 0;
|
||||||
|
for (int i = 0; i < csvLine.Length; i++)
|
||||||
|
{
|
||||||
|
if (csvLine[i] == ';')
|
||||||
|
{
|
||||||
|
length = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
name += csvLine[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name == String.Empty)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string ageInString = "";
|
||||||
|
for (int i = length + 1; i < csvLine.Length; i++)
|
||||||
|
{
|
||||||
|
ageInString += csvLine[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!int.TryParse(ageInString, out int age))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (age < 0 || age > 10)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
horse = new Horse(name, age, startNo);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
int Contains(string line, char character)
|
||||||
|
{
|
||||||
|
int total = 0;
|
||||||
|
for (int i = 0; i < line.Length; i++)
|
||||||
|
{
|
||||||
|
if (line[i] == character)
|
||||||
|
{
|
||||||
|
total++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,11 +1,78 @@
|
||||||
namespace HorseRace;
|
using System.Security.AccessControl;
|
||||||
|
|
||||||
|
namespace HorseRace;
|
||||||
|
|
||||||
public static class HorseImporter
|
public static class HorseImporter
|
||||||
{
|
{
|
||||||
public static bool TryReadHorses(string filePath, out Horse[]? horses)
|
public static bool TryReadHorses(string filePath, out Horse[]? horses)
|
||||||
{
|
{
|
||||||
// TODO
|
|
||||||
horses = null;
|
horses = null;
|
||||||
return false;
|
if (!File.Exists(filePath))
|
||||||
}
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
string[] lines = File.ReadAllLines(filePath);
|
||||||
|
if (lines.Length <= 1)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FormatLines(lines, out horses);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool FormatLines(string[] lines, out Horse[]? horses)
|
||||||
|
{
|
||||||
|
lines = RemoveFirstLine(lines);
|
||||||
|
horses = new Horse[lines.Length];
|
||||||
|
for (int i = 0; i < lines.Length; i++)
|
||||||
|
{
|
||||||
|
Horse.TryParse(lines[i], i + 1, out horses[i]!);
|
||||||
|
}
|
||||||
|
|
||||||
|
horses = TrimNullHorses(horses);
|
||||||
|
|
||||||
|
if (horses.Length == 0)
|
||||||
|
{
|
||||||
|
horses = Array.Empty<Horse>();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
string[] RemoveFirstLine(string[] array)
|
||||||
|
{
|
||||||
|
string[] changedArray = new string[array.Length - 1];
|
||||||
|
for (int i = 0; i < changedArray.Length; i++)
|
||||||
|
{
|
||||||
|
changedArray[i] = array[i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return changedArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
Horse[] TrimNullHorses(Horse?[] horses)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
foreach (var horse in horses)
|
||||||
|
{
|
||||||
|
if (horse != null)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Horse[] result = new Horse[count];
|
||||||
|
int index = 0;
|
||||||
|
for (var i = 0; i < horses.Length; i++)
|
||||||
|
{
|
||||||
|
var horse = horses[i];
|
||||||
|
if (horse != null)
|
||||||
|
{
|
||||||
|
result[index++] = horse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ public sealed class HorseRace
|
||||||
|
|
||||||
public HorseRace(Horse[] horses)
|
public HorseRace(Horse[] horses)
|
||||||
{
|
{
|
||||||
// TODO
|
_horses = horses;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsFinished { get; set; }
|
private bool IsFinished { get; set; }
|
||||||
|
|
@ -53,7 +53,10 @@ public sealed class HorseRace
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void PrintResults()
|
public void PrintResults()
|
||||||
{
|
{
|
||||||
// TODO
|
foreach (var horse in _horses)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{horse.Rank,3}. {horse.Name,-10} (SN {horse.StartNumber} Position {horse.Position}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -61,7 +64,20 @@ public sealed class HorseRace
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void MoveHorses()
|
private void MoveHorses()
|
||||||
{
|
{
|
||||||
// TODO
|
if(IsFinished)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var horse in _horses)
|
||||||
|
{
|
||||||
|
horse.Move();
|
||||||
|
if (horse.Position >= MaxSteps)
|
||||||
|
{
|
||||||
|
IsFinished = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -70,7 +86,10 @@ public sealed class HorseRace
|
||||||
private void DrawHorses()
|
private void DrawHorses()
|
||||||
{
|
{
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
// TODO
|
foreach (var horse in _horses)
|
||||||
|
{
|
||||||
|
horse.Draw();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -79,7 +98,17 @@ public sealed class HorseRace
|
||||||
private void AssignRanks()
|
private void AssignRanks()
|
||||||
{
|
{
|
||||||
SortByPosition();
|
SortByPosition();
|
||||||
// TODO
|
for (int i = 0; i < _horses.Length; i++)
|
||||||
|
{
|
||||||
|
if (i > 0 && _horses[i].Position == _horses[i - 1].Position)
|
||||||
|
{
|
||||||
|
_horses[i].Rank = _horses[i - 1].Rank;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_horses[i].Rank = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -87,6 +116,15 @@ public sealed class HorseRace
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void SortByPosition()
|
private void SortByPosition()
|
||||||
{
|
{
|
||||||
// TODO
|
for (int i = 0; i < _horses.Length - 1; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < _horses.Length - i - 1; j++)
|
||||||
|
{
|
||||||
|
if (_horses[j].CompareTo(_horses[j + 1]) > 0)
|
||||||
|
{
|
||||||
|
(_horses[j], _horses[j + 1]) = (_horses[j + 1], _horses[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue