mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-27 13:14:15 -08:00
Add game loops and betting
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user