Add replay loop

This commit is contained in:
Andrew Cooper
2023-01-27 08:32:16 +11:00
parent 29636d5696
commit 1c911e9b30
4 changed files with 95 additions and 6 deletions

View File

@@ -1,11 +1,11 @@
namespace Queen;
internal class Game
internal class Games
{
private readonly IReadWrite _io;
private readonly IRandom _random;
public Game(IReadWrite io, IRandom random)
public Games(IReadWrite io, IRandom random)
{
_io = io;
_random = random;
@@ -14,21 +14,83 @@ internal class Game
internal void Play()
{
_io.Write(Streams.Title);
if (_io.ShouldDisplayInstructions()) { _io.Write(Streams.Instructions); }
if (_io.ReadYesNo(Prompts.Instructions)) { _io.Write(Streams.Instructions); }
while (true)
{
PlayGame();
if (!_io.ReadYesNo(Prompts.Anyone))
{
_io.Write(Streams.Thanks);
return;
}
}
}
internal void PlayGame()
{
_io.Write(Streams.Board);
var humanPosition = _io.ReadPosition(Prompts.Start, p => p.IsStart, Streams.IllegalStart, repeatPrompt: true)
if (humanPosition.IsZero)
{
_io.Write(Streams.Forfeit);
return;
}
}
}
internal static class IOExtensions
{
internal static bool ShouldDisplayInstructions(this IReadWrite io)
internal static bool ReadYesNo(this IReadWrite io, string prompt)
{
while (true)
{
var answer = io.ReadString(Prompts.Instructions).ToLower();
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 = ""; }
}
}
}
internal record struct Position(int Diagonal, int Row)
{
public static readonly Position Zero = new(0);
public Position(int number)
: this(Diagonal: number / 10, Row: number % 10)
{
}
public bool IsZero => Row == 0 && Diagonal == 0;
public bool IsStart => Row == 1 || Row == Diagonal;
public bool IsEnd => Row == 8 && Diagonal == 15;
public override string ToString() => $"{Diagonal}{Row}";
}

View File

@@ -4,4 +4,4 @@ global using static Queen.Resources.Resource;
using Queen;
new Game(new ConsoleIO(), new RandomNumberGenerator()).Play();
new Games(new ConsoleIO(), new RandomNumberGenerator()).Play();

View File

@@ -0,0 +1,26 @@
81 71 61 51 41 31 21 11
92 82 72 62 52 42 32 22
103 93 83 73 63 53 43 33
114 104 94 84 74 64 54 44
125 115 105 95 85 75 65 55
136 126 116 106 96 86 76 66
147 137 127 117 107 97 87 77
158 148 138 128 118 108 98 88

View File

@@ -10,6 +10,7 @@ internal static class Resource
public static Stream Title => GetStream();
public static Stream Instructions => GetStream();
public static Stream YesOrNo => GetStream();
public static Stream Board => GetStream();
public static Stream IllegalStart => GetStream();
public static Stream ComputerMove => GetStream();
public static Stream IllegalMove => GetStream();