diff --git a/16_Bug/csharp/Game.cs b/16_Bug/csharp/Game.cs index c98c44f5..2b5c0711 100644 --- a/16_Bug/csharp/Game.cs +++ b/16_Bug/csharp/Game.cs @@ -1,7 +1,8 @@ +using BugGame.Parts; using BugGame.Resources; using Games.Common.IO; using Games.Common.Randomness; - +using static System.StringComparison; namespace BugGame; internal class Game @@ -18,10 +19,59 @@ internal class Game public void Play() { _io.Write(Resource.Streams.Introduction); - var response = _io.ReadString("Do you want instructions"); - if (!response.Equals("no", StringComparison.InvariantCultureIgnoreCase)) + if (!_io.ReadString("Do you want instructions").Equals("no", InvariantCultureIgnoreCase)) { _io.Write(Resource.Streams.Instructions); } + + BuildBugs(); + + _io.Write(Resource.Streams.PlayAgain); + } + + private void BuildBugs() + { + var yourBug = new Bug(); + var myBug = new Bug(); + + while (true) + { + if (TryBuild(yourBug, m => m.You) || TryBuild(myBug, m => m.I)) + { + if (yourBug.IsComplete) { _io.WriteLine(Message.Complete.You); } + if (myBug.IsComplete) { _io.WriteLine(Message.Complete.I); } + + if (!_io.ReadString("Do you want the picture").Equals("no", InvariantCultureIgnoreCase)) + { + _io.WriteLine(yourBug.ToString("Your", 'A')); + _io.WriteLine(myBug.ToString("My", 'F')); + } + } + + if (yourBug.IsComplete || myBug.IsComplete) { break; } + } + } + + private bool TryBuild(Bug bug, Func messageTransform) + { + var roll = _random.Next(6) + 1; + _io.WriteLine(messageTransform(Message.Rolled.ForValue(roll))); + + IPart part = roll switch + { + 1 => new Body(), + 2 => new Neck(), + 3 => new Head(), + 4 => new Feeler(), + 5 => new Tail(), + 6 => new Leg(), + _ => throw new Exception("Unexpected roll value") + }; + _io.WriteLine($"{roll}={part.GetType().Name}"); + + var partAdded = bug.TryAdd(part, out var message); + _io.WriteLine(messageTransform.Invoke(message)); + + return partAdded; } } \ No newline at end of file diff --git a/16_Bug/csharp/Resources/Message.cs b/16_Bug/csharp/Resources/Message.cs index be85faf8..4c719475 100644 --- a/16_Bug/csharp/Resources/Message.cs +++ b/16_Bug/csharp/Resources/Message.cs @@ -24,6 +24,8 @@ internal class Message public static Message LegAdded = new("now have {0} legs"); public static Message LegsFull = new("I have 6 feet.", "You have 6 feet already"); + public static Message Complete = new("bug is finished."); + private Message(string common) : this("I " + common, "You" + common) { @@ -40,5 +42,5 @@ internal class Message public static Message DoNotHaveA(Part part) => new($"do no have a {part.Name}"); - public Message ForQuantity(int quantity) => new(string.Format(I, quantity), string.Format(You, quantity)); + public Message ForValue(int quantity) => new(string.Format(I, quantity), string.Format(You, quantity)); } \ No newline at end of file diff --git a/16_Bug/csharp/Resources/PlayAgain.txt b/16_Bug/csharp/Resources/PlayAgain.txt new file mode 100644 index 00000000..2380e130 --- /dev/null +++ b/16_Bug/csharp/Resources/PlayAgain.txt @@ -0,0 +1 @@ +I hope you enjoyed the game, play it again soon!! diff --git a/16_Bug/csharp/Resources/Resource.cs b/16_Bug/csharp/Resources/Resource.cs index 57f05438..2b34a4e6 100644 --- a/16_Bug/csharp/Resources/Resource.cs +++ b/16_Bug/csharp/Resources/Resource.cs @@ -9,17 +9,11 @@ internal static class Resource { public static Stream Introduction => GetStream(); public static Stream Instructions => GetStream(); - } - - private static string GetString([CallerMemberName] string? name = null) - { - using var stream = GetStream(name); - using var reader = new StreamReader(stream); - return reader.ReadToEnd(); + public static Stream PlayAgain => GetStream(); } private static Stream GetStream([CallerMemberName] string? name = null) => Assembly.GetExecutingAssembly() - .GetManifestResourceStream($"{typeof(Resource).Namespace}.{name}.txt") + .GetManifestResourceStream($"Bug.Resources.{name}.txt") ?? throw new Exception($"Could not find embedded resource stream '{name}'."); } \ No newline at end of file