Encapsulate shot number and name

This commit is contained in:
Andrew Cooper
2022-04-10 13:10:46 +10:00
parent 1ce5f7b3a7
commit 11ee731832
7 changed files with 89 additions and 36 deletions

View File

@@ -13,16 +13,22 @@ internal static class IReadWriteExtensions
}
}
public static int ReadShot(this IReadWrite io, string prompt)
private static bool TryReadInteger(this IReadWrite io, string prompt, out int value)
{
var floatValue = io.ReadNumber(prompt);
value = (int)floatValue;
return value == floatValue;
}
public static Shot? ReadShot(this IReadWrite io, string prompt)
{
while (true)
{
var shot = io.ReadNumber(prompt);
if ((int)shot == shot && shot >= 0 && shot <= 4) { return (int)shot; }
if (io.TryReadInteger(prompt, out var value) && Shot.TryGet(value, out var shot))
{
return shot;
}
io.Write("Incorrect answer. Retype it. ");
}
}
public static void WriteScore(this IReadWrite io, string format, string opponent, Dictionary<string ,int> score) =>
io.WriteLine(format, "Dartmouth", score["Dartmouth"], opponent, score[opponent]);
}
}