From bbbf5560f40b5bbc89da04b91c0a0ba9cf176fd3 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Sun, 17 Jul 2022 17:17:29 +1000 Subject: [PATCH] Add parameter input --- 26_Chomp/csharp/Game.cs | 5 ++++- 26_Chomp/csharp/IOExtensions.cs | 24 ++++++++++++++++++++++++ 26_Chomp/csharp/Resources/Resource.cs | 7 +++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 26_Chomp/csharp/IOExtensions.cs diff --git a/26_Chomp/csharp/Game.cs b/26_Chomp/csharp/Game.cs index 943eb95a..96ea19b4 100644 --- a/26_Chomp/csharp/Game.cs +++ b/26_Chomp/csharp/Game.cs @@ -17,11 +17,14 @@ internal class Game _io.Write(Resource.Streams.Rules); } + while (true) { _io.Write(Resource.Streams.HereWeGo); + var (playerCount, rowCount, columnCount) = _io.ReadParameters(); + if (_io.ReadNumber("Again (1=Yes, 0=No!)") != 1) { break; } } } -} \ No newline at end of file +} diff --git a/26_Chomp/csharp/IOExtensions.cs b/26_Chomp/csharp/IOExtensions.cs new file mode 100644 index 00000000..c94053a9 --- /dev/null +++ b/26_Chomp/csharp/IOExtensions.cs @@ -0,0 +1,24 @@ +namespace Chomp; + +internal static class IOExtensions +{ + public static (float, int, int) ReadParameters(this IReadWrite io) + => ( + (int)io.ReadNumber(Resource.Prompts.HowManyPlayers), + io.ReadNumberWithMax(Resource.Prompts.HowManyRows, 9, Resource.Strings.TooManyRows), + io.ReadNumberWithMax(Resource.Prompts.HowManyColumns, 9, Resource.Strings.TooManyColumns) + ); + + private static int ReadNumberWithMax(this IReadWrite io, string initialPrompt, int max, string reprompt) + { + var prompt = initialPrompt; + + while (true) + { + var response = io.ReadNumber(prompt); + if (response <= 9) { return (int)response; } + + prompt = $"{reprompt} {initialPrompt.ToLowerInvariant()}"; + } + } +} \ No newline at end of file diff --git a/26_Chomp/csharp/Resources/Resource.cs b/26_Chomp/csharp/Resources/Resource.cs index e1f06e39..efa933dc 100644 --- a/26_Chomp/csharp/Resources/Resource.cs +++ b/26_Chomp/csharp/Resources/Resource.cs @@ -26,6 +26,13 @@ internal static class Resource public static string HowManyPlayers => GetString(); public static string HowManyRows => GetString(); public static string HowManyColumns => GetString(); + public static string TooManyColumns => GetString(); + } + + internal static class Strings + { + public static string TooManyColumns => GetString(); + public static string TooManyRows => GetString(); } private static string GetString([CallerMemberName] string? name = null)