diff --git a/HorseRace/Horse.cs b/HorseRace/Horse.cs index 79cf701..345f353 100644 --- a/HorseRace/Horse.cs +++ b/HorseRace/Horse.cs @@ -1,10 +1,14 @@ -namespace HorseRace; +using System.ComponentModel; + +namespace HorseRace; public sealed class Horse { public Horse(string name, int age, int startNo) { - // TODO + Name = name; + Age = age; + StartNumber = startNo; } public int Age { get; } @@ -21,8 +25,15 @@ public sealed class Horse /// The position difference between two horses public int CompareTo(Horse other) { - // TODO - return -1; + int positionDifference = other.Position - Position; + int startNumberDifference = other.StartNumber - StartNumber; + + if(positionDifference != 0) + { + return positionDifference; + } + + return startNumberDifference; } /// @@ -31,7 +42,15 @@ public sealed class Horse /// 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("|"); } /// @@ -39,7 +58,12 @@ public sealed class Horse /// public void Move() { - // TODO + if (RandomProvider.Random.Next(0, 3) != 0) + { + return; + } + + Position += 1; } /// @@ -52,8 +76,67 @@ public sealed class Horse /// True if parsed successfully; false otherwise public static bool TryParse(string csvLine, int startNo, out Horse? horse) { - // TODO 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; + } } } \ No newline at end of file diff --git a/HorseRace/HorseImporter.cs b/HorseRace/HorseImporter.cs index 25edf96..f087810 100644 --- a/HorseRace/HorseImporter.cs +++ b/HorseRace/HorseImporter.cs @@ -1,11 +1,78 @@ -namespace HorseRace; +using System.Security.AccessControl; + +namespace HorseRace; public static class HorseImporter { public static bool TryReadHorses(string filePath, out Horse[]? horses) { - // TODO 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(); + 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; + } + } } \ No newline at end of file diff --git a/HorseRace/HorseRace.cs b/HorseRace/HorseRace.cs index dc95820..ba630a1 100644 --- a/HorseRace/HorseRace.cs +++ b/HorseRace/HorseRace.cs @@ -8,7 +8,7 @@ public sealed class HorseRace public HorseRace(Horse[] horses) { - // TODO + _horses = horses; } private bool IsFinished { get; set; } @@ -53,7 +53,10 @@ public sealed class HorseRace /// public void PrintResults() { - // TODO + foreach (var horse in _horses) + { + Console.WriteLine($"{horse.Rank,3}. {horse.Name,-10} (SN {horse.StartNumber} Position {horse.Position}"); + } } /// @@ -61,7 +64,20 @@ public sealed class HorseRace /// private void MoveHorses() { - // TODO + if(IsFinished) + { + return; + } + + foreach (var horse in _horses) + { + horse.Move(); + if (horse.Position >= MaxSteps) + { + IsFinished = true; + return; + } + } } /// @@ -70,7 +86,10 @@ public sealed class HorseRace private void DrawHorses() { Console.Clear(); - // TODO + foreach (var horse in _horses) + { + horse.Draw(); + } } /// @@ -79,7 +98,17 @@ public sealed class HorseRace private void AssignRanks() { 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; + } + } } /// @@ -87,6 +116,15 @@ public sealed class HorseRace /// 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]); + } + } + } } }