diff --git a/72_Queen/csharp/Games.cs b/72_Queen/csharp/Games.cs index 73a0b80b..4504c771 100644 --- a/72_Queen/csharp/Games.cs +++ b/72_Queen/csharp/Games.cs @@ -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 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}"; +} \ No newline at end of file diff --git a/72_Queen/csharp/Program.cs b/72_Queen/csharp/Program.cs index a32aded0..a03fe364 100644 --- a/72_Queen/csharp/Program.cs +++ b/72_Queen/csharp/Program.cs @@ -4,4 +4,4 @@ global using static Queen.Resources.Resource; using Queen; -new Game(new ConsoleIO(), new RandomNumberGenerator()).Play(); \ No newline at end of file +new Games(new ConsoleIO(), new RandomNumberGenerator()).Play(); \ No newline at end of file diff --git a/72_Queen/csharp/Resources/Board.txt b/72_Queen/csharp/Resources/Board.txt new file mode 100644 index 00000000..45a8ab0a --- /dev/null +++ b/72_Queen/csharp/Resources/Board.txt @@ -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 + + + diff --git a/72_Queen/csharp/Resources/Resource.cs b/72_Queen/csharp/Resources/Resource.cs index 5290766e..9ff43842 100644 --- a/72_Queen/csharp/Resources/Resource.cs +++ b/72_Queen/csharp/Resources/Resource.cs @@ -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();