mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-02 08:10:45 -08:00
Convert Hexapawn to common library
This commit is contained in:
42
46_Hexapawn/csharp/IReadWriteExtensions.cs
Normal file
42
46_Hexapawn/csharp/IReadWriteExtensions.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Games.Common.IO;
|
||||
|
||||
namespace Hexapawn;
|
||||
|
||||
// Provides input methods which emulate the BASIC interpreter's keyboard input routines
|
||||
internal static class IReadWriteExtensions
|
||||
{
|
||||
internal static char GetYesNo(this IReadWrite io, string prompt)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var response = io.ReadString($"{prompt} (Y-N)").FirstOrDefault();
|
||||
if ("YyNn".Contains(response))
|
||||
{
|
||||
return char.ToUpperInvariant(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implements original code:
|
||||
// 120 PRINT "YOUR MOVE";
|
||||
// 121 INPUT M1,M2
|
||||
// 122 IF M1=INT(M1)AND M2=INT(M2)AND M1>0 AND M1<10 AND M2>0 AND M2<10 THEN 130
|
||||
// 123 PRINT "ILLEGAL CO-ORDINATES."
|
||||
// 124 GOTO 120
|
||||
internal static Move ReadMove(this IReadWrite io, string prompt)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
var (from, to) = io.Read2Numbers(prompt);
|
||||
|
||||
if (Move.TryCreate(from, to, out var move))
|
||||
{
|
||||
return move;
|
||||
}
|
||||
|
||||
io.WriteLine("Illegal Coordinates.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user