mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Add parameter input
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
26_Chomp/csharp/IOExtensions.cs
Normal file
24
26_Chomp/csharp/IOExtensions.cs
Normal file
@@ -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()}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user