Merge pull request #786 from drewjcooper/csharp-41-guess

C# 41 guess
This commit is contained in:
Jeff Atwood
2022-08-25 14:40:51 -07:00
committed by GitHub
14 changed files with 160 additions and 0 deletions

78
41_Guess/csharp/Game.cs Normal file
View File

@@ -0,0 +1,78 @@
namespace Guess;
internal class Game
{
private readonly IReadWrite _io;
private readonly IRandom _random;
public Game(IReadWrite io, IRandom random)
{
_io = io;
_random = random;
}
public void Play()
{
while (true)
{
_io.Write(Streams.Introduction);
var limit = _io.ReadNumber(Prompts.Limit);
_io.WriteLine();
// There's a bug here that exists in the original code.
// If the limit entered is <= 0 then the program will crash.
var targetGuessCount = checked((int)Math.Log2(limit) + 1);
PlayGuessingRounds(limit, targetGuessCount);
_io.Write(Streams.BlankLines);
}
}
private void PlayGuessingRounds(float limit, int targetGuessCount)
{
while (true)
{
_io.WriteLine(Formats.Thinking, limit);
// There's a bug here that exists in the original code. If a non-integer is entered as the limit
// then it's possible for the secret number to be the next integer greater than the limit.
var secretNumber = (int)_random.NextFloat(limit) + 1;
var guessCount = 0;
while (true)
{
var guess = _io.ReadNumber("");
if (guess <= 0) { return; }
guessCount++;
if (IsGuessCorrect(guess, secretNumber)) { break; }
}
ReportResult(guessCount, targetGuessCount);
_io.Write(Streams.BlankLines);
}
}
private bool IsGuessCorrect(float guess, int secretNumber)
{
if (guess < secretNumber) { _io.Write(Streams.TooLow); }
if (guess > secretNumber) { _io.Write(Streams.TooHigh); }
return guess == secretNumber;
}
private void ReportResult(int guessCount, int targetGuessCount)
{
_io.WriteLine(Formats.ThatsIt, guessCount);
_io.WriteLine(
(guessCount - targetGuessCount) switch
{
< 0 => Strings.VeryGood,
0 => Strings.Good,
> 0 => string.Format(Formats.ShouldHave, targetGuessCount)
});
}
}

View File

@@ -6,4 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Resources/*.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\00_Common\dotnet\Games.Common\Games.Common.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,7 @@
global using Games.Common.IO;
global using Games.Common.Randomness;
global using static Guess.Resources.Resource;
using Guess;
new Game(new ConsoleIO(), new RandomNumberGenerator()).Play();

View File

@@ -0,0 +1,5 @@

View File

@@ -0,0 +1 @@
Good.

View File

@@ -0,0 +1,9 @@
Guess
Creative Computing Morristown, New Jersey
This is a number guessing game. I'll think
of a number between 1 and any limit you want.
The you have to guess what it is.

View File

@@ -0,0 +1 @@
What limit do you want

View File

@@ -0,0 +1,44 @@
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Guess.Resources;
internal static class Resource
{
internal static class Streams
{
public static Stream Introduction => GetStream();
public static Stream TooLow => GetStream();
public static Stream TooHigh => GetStream();
public static Stream BlankLines => GetStream();
}
internal static class Formats
{
public static string Thinking => GetString();
public static string ThatsIt => GetString();
public static string ShouldHave => GetString();
}
internal static class Prompts
{
public static string Limit => GetString();
}
internal static class Strings
{
public static string Good => GetString();
public static string VeryGood => GetString();
}
private static string GetString([CallerMemberName] string? name = null)
{
using var stream = GetStream(name);
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
private static Stream GetStream([CallerMemberName] string? name = null) =>
Assembly.GetExecutingAssembly().GetManifestResourceStream($"{typeof(Resource).Namespace}.{name}.txt")
?? throw new Exception($"Could not find embedded resource stream '{name}'.");
}

View File

@@ -0,0 +1 @@
You should have been able to get it in only {0}

View File

@@ -0,0 +1 @@
That's it! You got it in {0} tries.

View File

@@ -0,0 +1,2 @@
I'm thinking of a number between 1 and {0}
Now you try to guess what it is.

View File

@@ -0,0 +1 @@
Too high. Try a smaller answer.

View File

@@ -0,0 +1 @@
Too low. Try a bigger answer.

View File

@@ -0,0 +1 @@
Very good.