diff --git a/30_Cube/csharp/Game.cs b/30_Cube/csharp/Game.cs index f02fa07e..70ada599 100644 --- a/30_Cube/csharp/Game.cs +++ b/30_Cube/csharp/Game.cs @@ -2,6 +2,7 @@ namespace Cube; internal class Game { + private const int _initialBalance = 500; private readonly IReadWrite _io; private readonly IRandom _random; @@ -14,5 +15,42 @@ internal class Game public void Play() { _io.Write(Streams.Introduction); + + if (_io.ReadNumber("") != 0) + { + _io.Write(Streams.Instructions); + } + + PlaySeries(_initialBalance); + + _io.Write(Streams.Goodbye); } -} \ No newline at end of file + + private void PlaySeries(float balance) + { + while (true) + { + var wager = _io.ReadWager(balance); + + var gameWon = PlayGame(); + + if (wager.HasValue) + { + balance = gameWon ? (balance + wager.Value) : (balance - wager.Value); + if (balance <= 0) + { + _io.Write(Streams.Bust); + return; + } + _io.WriteLine(Formats.Balance, balance); + } + + if (_io.ReadNumber(Prompts.TryAgain) != 1) { return; } + } + } + + private bool PlayGame() + { + return true; + } +} diff --git a/30_Cube/csharp/IOExtensions.cs b/30_Cube/csharp/IOExtensions.cs new file mode 100644 index 00000000..a221ca5d --- /dev/null +++ b/30_Cube/csharp/IOExtensions.cs @@ -0,0 +1,20 @@ +namespace Cube; + +internal static class IOExtensions +{ + internal static float? ReadWager(this IReadWrite io, float balance) + { + io.Write(Streams.Wager); + if (io.ReadNumber("") == 0) { return null; } + + var prompt = Prompts.HowMuch; + + while(true) + { + var wager = io.ReadNumber(prompt); + if (wager <= balance) { return wager; } + + prompt = Prompts.BetAgain; + } + } +} \ No newline at end of file diff --git a/30_Cube/csharp/Resources/NextMove.txt b/30_Cube/csharp/Resources/NextMove.txt index 4cbe5496..2c3c0611 100644 --- a/30_Cube/csharp/Resources/NextMove.txt +++ b/30_Cube/csharp/Resources/NextMove.txt @@ -1 +1 @@ -Next move: \ No newline at end of file +Next move: \ No newline at end of file