mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-09 03:43:01 -08:00
Complete game
This commit is contained in:
38
72_Queen/csharp/IOExtensions.cs
Normal file
38
72_Queen/csharp/IOExtensions.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace Queen;
|
||||
|
||||
internal static class IOExtensions
|
||||
{
|
||||
internal static bool ReadYesNo(this IReadWrite io, string prompt)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var answer = io.ReadString(prompt).ToLower();
|
||||
if (answer == "yes") { return true; }
|
||||
if (answer == "no") { return false; }
|
||||
|
||||
io.Write(Streams.YesOrNo);
|
||||
}
|
||||
}
|
||||
|
||||
internal static Position ReadPosition(
|
||||
this IReadWrite io,
|
||||
string prompt,
|
||||
Predicate<Position> isValid,
|
||||
Stream error,
|
||||
bool repeatPrompt = false)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var response = io.ReadNumber(prompt);
|
||||
var number = (int)response;
|
||||
var position = new Position(number);
|
||||
if (number == response && (position.IsZero || isValid(position)))
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
io.Write(error);
|
||||
if (!repeatPrompt) { prompt = ""; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user