mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-26 20:54:07 -08:00
Add parameter input
This commit is contained in:
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()}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user