142 lines
No EOL
3.6 KiB
C#
142 lines
No EOL
3.6 KiB
C#
using System.ComponentModel;
|
|
|
|
namespace HorseRace;
|
|
|
|
public sealed class Horse
|
|
{
|
|
public Horse(string name, int age, int startNo)
|
|
{
|
|
Name = name;
|
|
Age = age;
|
|
StartNumber = startNo;
|
|
}
|
|
|
|
public int Age { get; }
|
|
public string Name { get; }
|
|
public int Position { get; private set; }
|
|
public int Rank { get; set; }
|
|
public int StartNumber { get; }
|
|
|
|
/// <summary>
|
|
/// Compares this <see cref="Horse" />, first by position and then by start number,
|
|
/// to the supplied <see cref="Horse" />
|
|
/// </summary>
|
|
/// <param name="other"><see cref="Horse" /> to compare to</param>
|
|
/// <returns>The position difference between two horses</returns>
|
|
public int CompareTo(Horse other)
|
|
{
|
|
int positionDifference = other.Position - Position;
|
|
int startNumberDifference = other.StartNumber - StartNumber;
|
|
|
|
if(positionDifference != 0)
|
|
{
|
|
return positionDifference;
|
|
}
|
|
|
|
return startNumberDifference;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draws the current position of this <see cref="Horse" /> to the console, including
|
|
/// its label and the finish line.
|
|
/// </summary>
|
|
public void Draw()
|
|
{
|
|
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>
|
|
/// Randomly either increases or keeps the position of the <see cref="Horse" />.
|
|
/// </summary>
|
|
public void Move()
|
|
{
|
|
if (RandomProvider.Random.Next(0, 3) != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Position += 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to parse a CSV formatted string to a <see cref="Horse" /> instance.
|
|
/// Also sets the supplied <see cref="startNo" /> if parsing succeeds.
|
|
/// </summary>
|
|
/// <param name="csvLine">Text to parse</param>
|
|
/// <param name="startNo">Start number to set; must not be negative</param>
|
|
/// <param name="horse">Set to the parsed instance; null if parsing fails</param>
|
|
/// <returns>True if parsed successfully; false otherwise</returns>
|
|
public static bool TryParse(string csvLine, int startNo, out Horse? horse)
|
|
{
|
|
horse = null;
|
|
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;
|
|
}
|
|
}
|
|
} |