diff --git a/62 Mugwump/csharp/Mugwump.sln b/62 Mugwump/csharp/Mugwump.sln new file mode 100644 index 00000000..bc3cfbff --- /dev/null +++ b/62 Mugwump/csharp/Mugwump.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mugwump", "Mugwump\Mugwump.csproj", "{83F42802-4E7C-49B5-A022-DB9B6A65F2C6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Debug|x64.ActiveCfg = Debug|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Debug|x64.Build.0 = Debug|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Debug|x86.ActiveCfg = Debug|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Debug|x86.Build.0 = Debug|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Release|Any CPU.Build.0 = Release|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Release|x64.ActiveCfg = Release|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Release|x64.Build.0 = Release|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Release|x86.ActiveCfg = Release|Any CPU + {83F42802-4E7C-49B5-A022-DB9B6A65F2C6}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/62 Mugwump/csharp/Mugwump/Game.cs b/62 Mugwump/csharp/Mugwump/Game.cs new file mode 100644 index 00000000..bf72d44e --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Game.cs @@ -0,0 +1,36 @@ +using System; +using System.Linq; + +namespace Mugwump +{ + internal class Game + { + private readonly Grid _grid; + + private Game(Random random) + { + _grid = new Grid(Enumerable.Range(1, 4).Select(id => new Mugwump(id, random.Next(10), random.Next(10)))); + } + + public static void Play(Random random) => new Game(random).Play(); + + private void Play() + { + for (int turn = 1; turn <= 10; turn++) + { + var guess = Input.ReadGuess($"Turn no. {turn} -- what is your guess"); + + if (_grid.Check(guess)) + { + Console.WriteLine(); + Console.WriteLine($"You got them all in {turn} turns!"); + return; + } + } + + Console.WriteLine(); + Console.WriteLine("Sorry, that's 10 tries. Here is where they're hiding:"); + _grid.Reveal(); + } + } +} diff --git a/62 Mugwump/csharp/Mugwump/Grid.cs b/62 Mugwump/csharp/Mugwump/Grid.cs new file mode 100644 index 00000000..dd89c638 --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Grid.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Mugwump +{ + internal class Grid + { + private readonly List _mugwumps; + + public Grid(IEnumerable mugwumps) + { + _mugwumps = mugwumps.ToList(); + } + + public bool Check(Position guess) + { + foreach (var mugwump in _mugwumps.ToList()) + { + var (found, distance) = mugwump.FindFrom(guess); + + Console.WriteLine(found ? $"You have found {mugwump}" : $"You are {distance} units from {mugwump}"); + if (found) + { + _mugwumps.Remove(mugwump); + } + } + + return _mugwumps.Count == 0; + } + + public void Reveal() + { + foreach (var mugwump in _mugwumps.ToList()) + { + Console.WriteLine(mugwump.Reveal()); + } + } + } +} diff --git a/62 Mugwump/csharp/Mugwump/Input.cs b/62 Mugwump/csharp/Mugwump/Input.cs new file mode 100644 index 00000000..97222c3e --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Input.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; + +namespace Mugwump +{ + // Provides input methods which emulate the BASIC interpreter's keyboard input routines + internal static class Input + { + internal static Position ReadGuess(string prompt) + { + Console.WriteLine(); + Console.WriteLine(); + var input = ReadNumbers(prompt, 2); + return new Position(input[0], input[1]); + } + + private static void Prompt(string text = "") => Console.Write($"{text}? "); + + private static List ReadNumbers(string prompt, int requiredCount) + { + var numbers = new List(); + + while (!TryReadNumbers(prompt, requiredCount, numbers)) + { + prompt = ""; + } + + return numbers; + } + + private static bool TryReadNumbers(string prompt, int requiredCount, List numbers) + { + Prompt(prompt); + var inputValues = ReadStrings(); + + foreach (var value in inputValues) + { + if (numbers.Count == requiredCount) + { + Console.WriteLine("!Extra input ingored"); + return true; + } + + if (!TryParseNumber(value, out var number)) + { + return false; + } + + numbers.Add(number); + } + + return numbers.Count == requiredCount || TryReadNumbers("?", requiredCount, numbers); + } + + private static string[] ReadStrings() => Console.ReadLine().Split(',', StringSplitOptions.TrimEntries); + + private static bool TryParseNumber(string text, out float number) + { + if (float.TryParse(text, out number)) { return true; } + + Console.WriteLine("!Number expected - retry input line"); + number = default; + return false; + } + } +} diff --git a/62 Mugwump/csharp/Mugwump/Mugwump.cs b/62 Mugwump/csharp/Mugwump/Mugwump.cs new file mode 100644 index 00000000..bd117c63 --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Mugwump.cs @@ -0,0 +1,20 @@ +namespace Mugwump +{ + internal class Mugwump + { + private readonly int _id; + private readonly Position _position; + + public Mugwump(int id, int x, int y) + { + _id = id; + _position = new Position(x, y); + } + + public (bool, Distance) FindFrom(Position guess) => (guess == _position, guess - _position); + + public string Reveal() => $"{this} is at {_position}"; + + public override string ToString() => $"Mugwump {_id}"; + } +} diff --git a/62 Mugwump/csharp/Mugwump/Mugwump.csproj b/62 Mugwump/csharp/Mugwump/Mugwump.csproj new file mode 100644 index 00000000..fc2efa30 --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Mugwump.csproj @@ -0,0 +1,11 @@ + + + + Exe + net5.0 + + + + + + diff --git a/62 Mugwump/csharp/Mugwump/Offset.cs b/62 Mugwump/csharp/Mugwump/Offset.cs new file mode 100644 index 00000000..c62e3862 --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Offset.cs @@ -0,0 +1,16 @@ +using System; + +namespace Mugwump +{ + internal class Distance + { + private readonly float _value; + + public Distance(float deltaX, float deltaY) + { + _value = (float)Math.Sqrt(deltaX * deltaX + deltaY * deltaY); + } + + public override string ToString() => $"{_value:0.0}"; + } +} diff --git a/62 Mugwump/csharp/Mugwump/Position.cs b/62 Mugwump/csharp/Mugwump/Position.cs new file mode 100644 index 00000000..6005ae8b --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Position.cs @@ -0,0 +1,9 @@ +namespace Mugwump +{ + internal record Position(float X, float Y) + { + public override string ToString() => $"( {X} , {Y} )"; + + public static Distance operator -(Position p1, Position p2) => new(p1.X - p2.X, p1.Y - p2.Y); + } +} diff --git a/62 Mugwump/csharp/Mugwump/Program.cs b/62 Mugwump/csharp/Mugwump/Program.cs new file mode 100644 index 00000000..6121b81d --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Program.cs @@ -0,0 +1,33 @@ +using System; +using System.Reflection; + +namespace Mugwump +{ + class Program + { + static void Main(string[] args) + { + DisplayIntro(); + + var random = new Random(); + + while (true) + { + Game.Play(random); + + Console.WriteLine(); + Console.WriteLine("That was fun! Let's play again......."); + Console.WriteLine("Four more mugwumps are now in hiding."); + } + } + + private static void DisplayIntro() + { + using var stream = Assembly.GetExecutingAssembly() + .GetManifestResourceStream("Mugwump.Strings.Intro.txt"); + using var stdout = Console.OpenStandardOutput(); + + stream.CopyTo(stdout); + } + } +} diff --git a/62 Mugwump/csharp/Mugwump/Strings/Intro.txt b/62 Mugwump/csharp/Mugwump/Strings/Intro.txt new file mode 100644 index 00000000..30ede4d6 --- /dev/null +++ b/62 Mugwump/csharp/Mugwump/Strings/Intro.txt @@ -0,0 +1,15 @@ + Mugwump + Creative Computing Morristown, New Jersey + + + +The object of this game is to find four mugwumps +hidden on a 10 by 10 grid. Homebase is position 0,0. +Any guess you make must be two numbers with each +number between 0 and 9, inclusive. First number +is distance to right of homebase and second number +is distance above homebase. + +You get 10 tries. After each try, I will tell +you how far you are from each wugwump. +