Encapsulate formulae

This commit is contained in:
Andrew Cooper
2022-04-16 17:00:06 +10:00
parent 6927c5ad14
commit 5fbf0dc37f
3 changed files with 38 additions and 16 deletions

View File

@@ -14,17 +14,11 @@ internal class Game
internal void Play() internal void Play()
{ {
_io.Write(Streams.Title); DoIntroduction();
if (!_io.ReadYes(Prompts.Ready))
{
_io.Write(Streams.ShutUp);
}
_io.Write(Streams.Instructions);
var result = _io.ReadNumber(Prompts.Answer); var result = _io.ReadNumber(Prompts.Answer);
if (_io.ReadYes(Formats.Bet, (result + 1 - 5) * 5 / 8 * 5 - 3)) if (_io.ReadYes(Formats.Bet, Math.CalculateOriginal(result)))
{ {
_io.Write(Streams.Bye); _io.Write(Streams.Bye);
return; return;
@@ -32,7 +26,7 @@ internal class Game
var original = _io.ReadNumber(Prompts.Original); var original = _io.ReadNumber(Prompts.Original);
_io.WriteLine(Formats.Working, GetStepValues(original).ToArray()); _io.WriteLine(Math.ShowWorking(original));
if (_io.ReadYes(Prompts.Believe)) if (_io.ReadYes(Prompts.Believe))
{ {
@@ -43,13 +37,14 @@ internal class Game
_io.Write(Streams.Lightning); _io.Write(Streams.Lightning);
} }
private static IEnumerable<object> GetStepValues(float value) private void DoIntroduction()
{ {
yield return value; _io.Write(Streams.Title);
yield return value += 3; if (!_io.ReadYes(Prompts.Ready))
yield return value /= 5; {
yield return value *= 8; _io.Write(Streams.ShutUp);
yield return value = value / 5 + 5; }
yield return value - 1;
_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, float 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(float value) =>
string.Format(
Formats.Working,
value,
value += 3,
value /= 5,
value *= 8,
value = value / 5 + 5,
value - 1);
}