Merge pull request #723 from drewjcooper/csharp-25-chief

C# 25 chief
This commit is contained in:
Jeff Atwood
2022-04-16 13:22:15 -07:00
committed by GitHub
23 changed files with 225 additions and 18 deletions

View File

@@ -1,7 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -1,5 +1,4 @@
using System;
using System.IO;
using Games.Common.Numbers;
namespace Games.Common.IO;
@@ -76,17 +75,16 @@ public interface IReadWrite
void WriteLine(string message = "");
/// <summary>
/// Writes a <see cref="float" /> to output, formatted per the BASIC interpreter, with leading and trailing spaces.
/// Writes a <see cref="Number" /> to output.
/// </summary>
/// <param name="value">The <see cref="float" /> to be written.</param>
void Write(float value);
/// <param name="value">The <see cref="Number" /> to be written.</param>
void Write(Number value);
/// <summary>
/// Writes a <see cref="float" /> to output, formatted per the BASIC interpreter, with leading and trailing spaces,
/// followed by a new-line.
/// Writes a <see cref="Number" /> to output.
/// </summary>
/// <param name="value">The <see cref="float" /> to be written.</param>
void WriteLine(float value);
/// <param name="value">The <see cref="Number" /> to be written.</param>
void WriteLine(Number value);
/// <summary>
/// Writes an <see cref="object" /> to output.

View File

@@ -0,0 +1,9 @@
namespace Games.Common.IO;
public class InsufficientInputException : Exception
{
public InsufficientInputException()
: base("Insufficient input was supplied")
{
}
}

View File

@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Games.Common.Numbers;
namespace Games.Common.IO;
@@ -93,16 +90,16 @@ public class TextIO : IReadWrite
internal string ReadLine(string prompt)
{
Write(prompt + "? ");
return _input.ReadLine();
return _input.ReadLine() ?? throw new InsufficientInputException();
}
public void Write(string value) => _output.Write(value);
public void WriteLine(string value = "") => _output.WriteLine(value);
public void Write(float value) => _output.Write(GetString(value));
public void Write(Number value) => _output.Write(value.ToString());
public void WriteLine(float value) => _output.WriteLine(GetString(value));
public void WriteLine(Number value) => _output.WriteLine(value.ToString());
public void Write(object value) => _output.Write(value.ToString());

View File

@@ -0,0 +1,20 @@
namespace Games.Common.Numbers;
/// <summary>
/// A single-precision floating-point number with string formatting equivalent to the BASIC interpreter.
/// </summary>
public struct Number
{
private readonly float _value;
public Number (float value)
{
_value = value;
}
public static implicit operator float(Number value) => value._value;
public static implicit operator Number(float value) => new Number(value);
public override string ToString() => _value < 0 ? $"{_value} " : $" {_value} ";
}

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>

49
25_Chief/csharp/Game.cs Normal file
View File

@@ -0,0 +1,49 @@
using static Chief.Resources.Resource;
namespace Chief;
internal class Game
{
private readonly IReadWrite _io;
public Game(IReadWrite io)
{
_io = io;
}
internal void Play()
{
DoIntroduction();
var result = _io.ReadNumber(Prompts.Answer);
if (_io.ReadYes(Formats.Bet, Math.CalculateOriginal(result)))
{
_io.Write(Streams.Bye);
return;
}
var original = _io.ReadNumber(Prompts.Original);
_io.WriteLine(Math.ShowWorking(original));
if (_io.ReadYes(Prompts.Believe))
{
_io.Write(Streams.Bye);
return;
}
_io.Write(Streams.Lightning);
}
private void DoIntroduction()
{
_io.Write(Streams.Title);
if (!_io.ReadYes(Prompts.Ready))
{
_io.Write(Streams.ShutUp);
}
_io.Write(Streams.Instructions);
}
}

View File

@@ -0,0 +1,9 @@
namespace Chief;
internal static class IReadWriteExtensions
{
internal static bool ReadYes(this IReadWrite io, string format, Number value) =>
io.ReadYes(string.Format(format, value));
internal static bool ReadYes(this IReadWrite io, string prompt) =>
io.ReadString(prompt).Equals("Yes", StringComparison.InvariantCultureIgnoreCase);
}

18
25_Chief/csharp/Math.cs Normal file
View File

@@ -0,0 +1,18 @@
using static Chief.Resources.Resource;
namespace Chief;
public static class Math
{
public static float CalculateOriginal(float result) => (result + 1 - 5) * 5 / 8 * 5 - 3;
public static string ShowWorking(Number value) =>
string.Format(
Formats.Working,
value,
value += 3,
value /= 5,
value *= 8,
value = value / 5 + 5,
value - 1);
}

View File

@@ -0,0 +1,4 @@
global using Games.Common.IO;
global using Chief;
new Game(new ConsoleIO()).Play();

View File

@@ -0,0 +1 @@
What do you have

View File

@@ -0,0 +1 @@
Now do you believe me

View File

@@ -0,0 +1 @@
I bet your number was{0}. Am I right

View File

@@ -0,0 +1 @@
Bye!!!

View File

@@ -0,0 +1,2 @@
Take a number and add 3. Divide this number by 5 and
multiply by 8. Divide by 5 and add the same. Subtract 1.

View File

@@ -0,0 +1,31 @@
You have made me mad!!!
There must be a great lightning bolt!!
X X
X X
X X
X X
X X
X X
X X
X X
X X
X XXX
X X
XX X
X X
X X
X X
X X
X X
X X
X X
X X
XX
X
*
#########################
I hope you believe me now, for your sake!!

View File

@@ -0,0 +1 @@
What was your original number

View File

@@ -0,0 +1,2 @@
I am Chief Numbers Freek, the great Indian math god.
Are you ready to take the test you called me out for

View File

@@ -0,0 +1,41 @@
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Chief.Resources;
internal static class Resource
{
internal static class Streams
{
public static Stream Bye => GetStream();
public static Stream Instructions => GetStream();
public static Stream Lightning => GetStream();
public static Stream ShutUp => GetStream();
public static Stream Title => GetStream();
}
internal static class Formats
{
public static string Bet => GetString();
public static string Working => GetString();
}
internal static class Prompts
{
public static string Answer => GetString();
public static string Believe => GetString();
public static string Original => GetString();
public static string Ready => 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($"Chief.Resources.{name}.txt")
?? throw new ArgumentException($"Resource stream {name} does not exist", nameof(name));
}

View File

@@ -0,0 +1 @@
Shut up, pale face with wise tongue.

View File

@@ -0,0 +1,5 @@
Chief
Creative Computing Morristown, New Jersey

View File

@@ -0,0 +1,5 @@
So you think you're so smart, eh?
Now watch.
{0}plus 3 equals{1}. This divided by 5 equals{2};
This times by 8 equals{3}. If we divide by 5 and add 5,
we get{4}, which, minus 1, equals{5}.

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>