mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Merge pull request #314 from drewjcooper/csharp-62-mugwump
Add C# implementation of 62 Mugwump
This commit is contained in:
34
62 Mugwump/csharp/Mugwump.sln
Normal file
34
62 Mugwump/csharp/Mugwump.sln
Normal file
@@ -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
|
||||||
36
62 Mugwump/csharp/Mugwump/Game.cs
Normal file
36
62 Mugwump/csharp/Mugwump/Game.cs
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
40
62 Mugwump/csharp/Mugwump/Grid.cs
Normal file
40
62 Mugwump/csharp/Mugwump/Grid.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Mugwump
|
||||||
|
{
|
||||||
|
internal class Grid
|
||||||
|
{
|
||||||
|
private readonly List<Mugwump> _mugwumps;
|
||||||
|
|
||||||
|
public Grid(IEnumerable<Mugwump> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
66
62 Mugwump/csharp/Mugwump/Input.cs
Normal file
66
62 Mugwump/csharp/Mugwump/Input.cs
Normal file
@@ -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<float> ReadNumbers(string prompt, int requiredCount)
|
||||||
|
{
|
||||||
|
var numbers = new List<float>();
|
||||||
|
|
||||||
|
while (!TryReadNumbers(prompt, requiredCount, numbers))
|
||||||
|
{
|
||||||
|
prompt = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryReadNumbers(string prompt, int requiredCount, List<float> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
62 Mugwump/csharp/Mugwump/Mugwump.cs
Normal file
20
62 Mugwump/csharp/Mugwump/Mugwump.cs
Normal file
@@ -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}";
|
||||||
|
}
|
||||||
|
}
|
||||||
11
62 Mugwump/csharp/Mugwump/Mugwump.csproj
Normal file
11
62 Mugwump/csharp/Mugwump/Mugwump.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Strings\Intro.txt" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
16
62 Mugwump/csharp/Mugwump/Offset.cs
Normal file
16
62 Mugwump/csharp/Mugwump/Offset.cs
Normal file
@@ -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}";
|
||||||
|
}
|
||||||
|
}
|
||||||
9
62 Mugwump/csharp/Mugwump/Position.cs
Normal file
9
62 Mugwump/csharp/Mugwump/Position.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
62 Mugwump/csharp/Mugwump/Program.cs
Normal file
33
62 Mugwump/csharp/Mugwump/Program.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
62 Mugwump/csharp/Mugwump/Strings/Intro.txt
Normal file
15
62 Mugwump/csharp/Mugwump/Strings/Intro.txt
Normal file
@@ -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.
|
||||||
|
|
||||||
Reference in New Issue
Block a user