From 487b7ded98a91ed1a716648210fec038a30183a7 Mon Sep 17 00:00:00 2001 From: drewjcooper Date: Tue, 31 Jan 2023 07:40:47 +1100 Subject: [PATCH 1/7] Configure project --- 75_Roulette/csharp/Roulette.csproj | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/75_Roulette/csharp/Roulette.csproj b/75_Roulette/csharp/Roulette.csproj index d3fe4757..23d27b76 100644 --- a/75_Roulette/csharp/Roulette.csproj +++ b/75_Roulette/csharp/Roulette.csproj @@ -1,9 +1,17 @@ Exe - net6.0 + net7.0 10 enable enable + + + + + + + + From 3c9c04429d73109999eb17953d5084a6c168cc3c Mon Sep 17 00:00:00 2001 From: drewjcooper Date: Sun, 5 Feb 2023 19:05:23 +1100 Subject: [PATCH 2/7] Add string resources --- 75_Roulette/csharp/Resources/AgainPrompt.txt | 0 75_Roulette/csharp/Resources/BetAlready.txt | 1 + 75_Roulette/csharp/Resources/BetPrompt.txt | 1 + 75_Roulette/csharp/Resources/BrokeHouse.txt | 1 + 75_Roulette/csharp/Resources/Check.txt | 14 +++++ 75_Roulette/csharp/Resources/CheckPrompt.txt | 1 + .../csharp/Resources/HowManyBetsPrompt.txt | 1 + 75_Roulette/csharp/Resources/Instructions.txt | 48 +++++++++++++++ .../csharp/Resources/InstructionsPrompt.txt | 1 + 75_Roulette/csharp/Resources/LastDollar.txt | 1 + 75_Roulette/csharp/Resources/Outcome.txt | 1 + 75_Roulette/csharp/Resources/Resource.cs | 59 +++++++++++++++++++ 75_Roulette/csharp/Resources/Slot.txt | 2 + 75_Roulette/csharp/Resources/Spinning.txt | 3 + 75_Roulette/csharp/Resources/Thanks.txt | 3 + 75_Roulette/csharp/Resources/Title.txt | 7 +++ 75_Roulette/csharp/Resources/Totals.txt | 2 + 17 files changed, 146 insertions(+) create mode 100644 75_Roulette/csharp/Resources/AgainPrompt.txt create mode 100644 75_Roulette/csharp/Resources/BetAlready.txt create mode 100644 75_Roulette/csharp/Resources/BetPrompt.txt create mode 100644 75_Roulette/csharp/Resources/BrokeHouse.txt create mode 100644 75_Roulette/csharp/Resources/Check.txt create mode 100644 75_Roulette/csharp/Resources/CheckPrompt.txt create mode 100644 75_Roulette/csharp/Resources/HowManyBetsPrompt.txt create mode 100644 75_Roulette/csharp/Resources/Instructions.txt create mode 100644 75_Roulette/csharp/Resources/InstructionsPrompt.txt create mode 100644 75_Roulette/csharp/Resources/LastDollar.txt create mode 100644 75_Roulette/csharp/Resources/Outcome.txt create mode 100644 75_Roulette/csharp/Resources/Resource.cs create mode 100644 75_Roulette/csharp/Resources/Slot.txt create mode 100644 75_Roulette/csharp/Resources/Spinning.txt create mode 100644 75_Roulette/csharp/Resources/Thanks.txt create mode 100644 75_Roulette/csharp/Resources/Title.txt create mode 100644 75_Roulette/csharp/Resources/Totals.txt diff --git a/75_Roulette/csharp/Resources/AgainPrompt.txt b/75_Roulette/csharp/Resources/AgainPrompt.txt new file mode 100644 index 00000000..e69de29b diff --git a/75_Roulette/csharp/Resources/BetAlready.txt b/75_Roulette/csharp/Resources/BetAlready.txt new file mode 100644 index 00000000..be86cbea --- /dev/null +++ b/75_Roulette/csharp/Resources/BetAlready.txt @@ -0,0 +1 @@ +You made that bet once already,dum-dum \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/BetPrompt.txt b/75_Roulette/csharp/Resources/BetPrompt.txt new file mode 100644 index 00000000..50039840 --- /dev/null +++ b/75_Roulette/csharp/Resources/BetPrompt.txt @@ -0,0 +1 @@ +Number {0} \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/BrokeHouse.txt b/75_Roulette/csharp/Resources/BrokeHouse.txt new file mode 100644 index 00000000..638dafb4 --- /dev/null +++ b/75_Roulette/csharp/Resources/BrokeHouse.txt @@ -0,0 +1 @@ +You broke the house! diff --git a/75_Roulette/csharp/Resources/Check.txt b/75_Roulette/csharp/Resources/Check.txt new file mode 100644 index 00000000..f2be95cd --- /dev/null +++ b/75_Roulette/csharp/Resources/Check.txt @@ -0,0 +1,14 @@ +------------------------------------------------------------------------Check No. {0} + + {1:mmmm d',' yyyy} + + +Pay to the order of-----{2}-----$ {3} + + + The Memory Bank of New YORK + + The Computer + ----------X----- + +--------------------------------------------------------------Come back soon! diff --git a/75_Roulette/csharp/Resources/CheckPrompt.txt b/75_Roulette/csharp/Resources/CheckPrompt.txt new file mode 100644 index 00000000..16321fe9 --- /dev/null +++ b/75_Roulette/csharp/Resources/CheckPrompt.txt @@ -0,0 +1 @@ +To whom shall I make the check \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/HowManyBetsPrompt.txt b/75_Roulette/csharp/Resources/HowManyBetsPrompt.txt new file mode 100644 index 00000000..0b6abdfa --- /dev/null +++ b/75_Roulette/csharp/Resources/HowManyBetsPrompt.txt @@ -0,0 +1 @@ +How many bets \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/Instructions.txt b/75_Roulette/csharp/Resources/Instructions.txt new file mode 100644 index 00000000..8ced724b --- /dev/null +++ b/75_Roulette/csharp/Resources/Instructions.txt @@ -0,0 +1,48 @@ + +This is the betting layout + (*=RED) + + 1* 2 3* + 4 5* 6 + 7* 8 9* +10 11 12* +--------------- +13 14* 15 +16* 17 18* +19* 20 21* +22 23* 24 +--------------- +25* 26 27* +28 29 30* +31 32* 33 +34* 35 36* +--------------- + 00 0 + +Types of bets + +The numbers 1 to 36 signify a straight bet +on that number. +These pay off 35:1 + +The 2:1 bets are: + 37) 1-12 40) First column + 38) 13-24 41) Second column + 39) 25-36 42) Third column + +The even money bets are: + 43) 1-18 46) Odd + 44) 19-36 47) Red + 45) Even 48) Black + +49)0 and 50)00 pay off 35:1 +Note: 0 and 00 do not count under any + bets except their own. + +When I ask for each bet, type the number +and the amount, separated by a comma. +For example: to bet $500 on Black, type 48,500 +when I ask for a bet. + +The minimum bet is $5, the maximum is $500. + diff --git a/75_Roulette/csharp/Resources/InstructionsPrompt.txt b/75_Roulette/csharp/Resources/InstructionsPrompt.txt new file mode 100644 index 00000000..0d311b60 --- /dev/null +++ b/75_Roulette/csharp/Resources/InstructionsPrompt.txt @@ -0,0 +1 @@ +Do you want instructions \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/LastDollar.txt b/75_Roulette/csharp/Resources/LastDollar.txt new file mode 100644 index 00000000..632516de --- /dev/null +++ b/75_Roulette/csharp/Resources/LastDollar.txt @@ -0,0 +1 @@ +Oops! you just spent your last dollar! diff --git a/75_Roulette/csharp/Resources/Outcome.txt b/75_Roulette/csharp/Resources/Outcome.txt new file mode 100644 index 00000000..7926cb87 --- /dev/null +++ b/75_Roulette/csharp/Resources/Outcome.txt @@ -0,0 +1 @@ +You {0} {1} dollars on bet {2} \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/Resource.cs b/75_Roulette/csharp/Resources/Resource.cs new file mode 100644 index 00000000..aca0aaf5 --- /dev/null +++ b/75_Roulette/csharp/Resources/Resource.cs @@ -0,0 +1,59 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using Games.Common.Randomness; + +namespace Roulette.Resources; + +internal static class Resource +{ + internal static class Streams + { + public static Stream Title => GetStream(); + public static Stream Instructions => GetStream(); + public static Stream BetAlready => GetStream(); + public static Stream Spinning => GetStream(); + public static Stream LastDollar => GetStream(); + public static Stream BrokeHouse => GetStream(); + public static Stream Thanks => GetStream(); + } + + internal static class Strings + { + public static string Black(int number) => Slot(number); + public static string Red(int number) => Slot(number); + private static string Slot(int number, [CallerMemberName] string? colour = null) + => string.Format(GetString(), number, colour); + + public static string Lose(int amount, int bet) => Outcome(amount, bet); + public static string Win(int amount, int bet) => Outcome(amount, bet); + private static string Outcome(int amount, int bet, [CallerMemberName] string? winlose = null) + => string.Format(GetString(), winlose, amount, bet); + + public static string Totals(int me, int you) => string.Format(GetString(), me, you); + + public static string Check(IRandom random, string payee, int amount) + => string.Format(GetString(), random.Next(100), DateTime.Now, payee, amount); + } + + internal static class Prompts + { + public static string Instructions => GetPrompt(); + public static string HowManyBets => GetPrompt(); + public static string Bet(int number) => string.Format(GetPrompt(), number); + public static string Again => GetPrompt(); + public static string Check => GetPrompt(); + } + + private static string GetPrompt([CallerMemberName] string? name = null) => GetString($"{name}Prompt"); + + 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}'."); +} \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/Slot.txt b/75_Roulette/csharp/Resources/Slot.txt new file mode 100644 index 00000000..d45e103b --- /dev/null +++ b/75_Roulette/csharp/Resources/Slot.txt @@ -0,0 +1,2 @@ + {0} {1} + \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/Spinning.txt b/75_Roulette/csharp/Resources/Spinning.txt new file mode 100644 index 00000000..13514a0a --- /dev/null +++ b/75_Roulette/csharp/Resources/Spinning.txt @@ -0,0 +1,3 @@ +SPINNING + + diff --git a/75_Roulette/csharp/Resources/Thanks.txt b/75_Roulette/csharp/Resources/Thanks.txt new file mode 100644 index 00000000..0b835237 --- /dev/null +++ b/75_Roulette/csharp/Resources/Thanks.txt @@ -0,0 +1,3 @@ +Thanks for you money. +I'll use it to buy a solid gold roulette WHEEL + diff --git a/75_Roulette/csharp/Resources/Title.txt b/75_Roulette/csharp/Resources/Title.txt new file mode 100644 index 00000000..0d53f1a8 --- /dev/null +++ b/75_Roulette/csharp/Resources/Title.txt @@ -0,0 +1,7 @@ + Roulette + Creative Computing Morristown, New Jersey + + + +Welcome to the roulette table + diff --git a/75_Roulette/csharp/Resources/Totals.txt b/75_Roulette/csharp/Resources/Totals.txt new file mode 100644 index 00000000..26f35724 --- /dev/null +++ b/75_Roulette/csharp/Resources/Totals.txt @@ -0,0 +1,2 @@ +Totals Me You + {0,-14}{1} From 9a1e8e88752b26c3b3e3c7c7d1adccd9806b4b1b Mon Sep 17 00:00:00 2001 From: drewjcooper Date: Sun, 5 Feb 2023 21:28:51 +1100 Subject: [PATCH 3/7] Add game start --- 75_Roulette/csharp/Game.cs | 18 ++++++++++++++++++ 75_Roulette/csharp/Program.cs | 6 ++++++ 2 files changed, 24 insertions(+) create mode 100644 75_Roulette/csharp/Game.cs create mode 100644 75_Roulette/csharp/Program.cs diff --git a/75_Roulette/csharp/Game.cs b/75_Roulette/csharp/Game.cs new file mode 100644 index 00000000..efb4b966 --- /dev/null +++ b/75_Roulette/csharp/Game.cs @@ -0,0 +1,18 @@ +namespace Roulette; + +internal class Game +{ + private readonly IReadWrite _io; + private readonly IRandom _random; + + public Game(IReadWrite io, IRandom random) + { + _io = io; + _random = random; + } + + public void Play() + { + + } +} \ No newline at end of file diff --git a/75_Roulette/csharp/Program.cs b/75_Roulette/csharp/Program.cs new file mode 100644 index 00000000..4be74962 --- /dev/null +++ b/75_Roulette/csharp/Program.cs @@ -0,0 +1,6 @@ +global using Games.Common.IO; +global using Games.Common.Randomness; +global using static Roulette.Resources.Resource; +using Roulette; + +new Game(new ConsoleIO(), new RandomNumberGenerator()).Play(); From 25dba634c5f80d33d4d0af458ce2e288696a73b9 Mon Sep 17 00:00:00 2001 From: drewjcooper Date: Sun, 5 Feb 2023 22:47:02 +1100 Subject: [PATCH 4/7] Add game loop and objects --- 75_Roulette/csharp/Game.cs | 140 ++++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 1 deletion(-) diff --git a/75_Roulette/csharp/Game.cs b/75_Roulette/csharp/Game.cs index efb4b966..c9570ca1 100644 --- a/75_Roulette/csharp/Game.cs +++ b/75_Roulette/csharp/Game.cs @@ -1,18 +1,156 @@ +using System.Collections.Immutable; + namespace Roulette; internal class Game { private readonly IReadWrite _io; private readonly IRandom _random; + private readonly Table _table; public Game(IReadWrite io, IRandom random) { _io = io; _random = random; + _table = new Table(io, random); } public void Play() { - + _io.Write(Streams.Title); + if (!_io.ReadString(Prompts.Instructions).ToLowerInvariant().StartsWith('n')) + { + _io.Write(Streams.Instructions); + } + + while (_table.Play()); + + if (_table.Balance > 0) + { + var name = _io.ReadString(Prompts.Check); + _io.Write(Strings.Check(_random, name, _table.Balance)); + } + else + { + _io.Write(Streams.Thanks); + } } +} + +internal class Wheel +{ + private static readonly ImmutableArray _slots = ImmutableArray.Create( + new Slot(Strings.Red(1)), + new Slot(Strings.Black(2)), + new Slot(Strings.Red(3)), + new Slot(Strings.Black(4)), + new Slot(Strings.Red(5)), + new Slot(Strings.Black(6)), + new Slot(Strings.Red(7)), + new Slot(Strings.Black(8)), + new Slot(Strings.Red(9)), + new Slot(Strings.Black(10)), + new Slot(Strings.Black(11)), + new Slot(Strings.Red(12)), + new Slot(Strings.Black(13)), + new Slot(Strings.Red(14)), + new Slot(Strings.Black(15)), + new Slot(Strings.Red(16)), + new Slot(Strings.Black(17)), + new Slot(Strings.Red(18)), + new Slot(Strings.Red(19)), + new Slot(Strings.Black(20)), + new Slot(Strings.Red(21)), + new Slot(Strings.Black(22)), + new Slot(Strings.Red(23)), + new Slot(Strings.Black(24)), + new Slot(Strings.Red(25)), + new Slot(Strings.Black(26)), + new Slot(Strings.Red(27)), + new Slot(Strings.Black(28)), + new Slot(Strings.Black(29)), + new Slot(Strings.Red(30)), + new Slot(Strings.Black(31)), + new Slot(Strings.Red(32)), + new Slot(Strings.Black(33)), + new Slot(Strings.Red(34)), + new Slot(Strings.Black(35)), + new Slot(Strings.Red(36)), + new Slot("0"), + new Slot("00")); + + private readonly IRandom _random; + + public Wheel(IRandom random) => _random = random; + + public Slot Spin() => _slots[_random.Next(_slots.Length)]; +} + +internal record struct Slot(string Name); + +internal record struct Bet(int Number, int Amount) +{ + public Bet(int number) : this(number, 0) { } + + public bool Equals(Bet? other) => Number == other?.Number; +} + +public class Table +{ + private readonly IReadWrite _io; + private readonly Wheel _wheel; + + private int _houseBalance = 100_000; + private int _playerBalance = 1_000; + + public Table(IReadWrite io, IRandom random) + { + _io = io; + _wheel = new(random); + } + + public int Balance => _playerBalance; + + public bool Play() + { + var betCount = _io.ReadBetCount(); + var bets = new HashSet(); + for (int i = 0; i < betCount; i++) + { + while (!bets.Add(_io.ReadBet(i))) + { + _io.Write(Streams.BetAlready); + } + } + + return _io.ReadString(Prompts.Again).ToLowerInvariant().StartsWith('y'); + } +} + +internal static class IOExtensions +{ + internal static int ReadBetCount(this IReadWrite io) + { + while (true) + { + var betCount = io.ReadNumber(Prompts.HowManyBets); + if (betCount.IsValidInt(1)) { return (int)betCount; } + } + } + + internal static Bet ReadBet(this IReadWrite io, int number) + { + while (true) + { + var (bet, amount) = io.Read2Numbers(Prompts.Bet(number)); + + if (bet.IsValidInt(1, 50) && amount.IsValidInt(5, 500)) + { + return new((int)bet, (int)amount); + } + } + } + + internal static bool IsValidInt(this float value, int minValue, int maxValue = int.MaxValue) + => value == (int)value && value >= minValue && value <= maxValue; } \ No newline at end of file From 2d9d890269b79e6177c3370fd2d431a0cf520cea Mon Sep 17 00:00:00 2001 From: drewjcooper Date: Wed, 15 Feb 2023 17:49:22 +1100 Subject: [PATCH 5/7] Finish game logic --- 75_Roulette/csharp/Game.cs | 243 ++++++++++++++----- 75_Roulette/csharp/Resources/AgainPrompt.txt | 1 + 75_Roulette/csharp/Resources/Check.txt | 4 +- 75_Roulette/csharp/Resources/Outcome.txt | 2 +- 75_Roulette/csharp/Resources/Resource.cs | 8 +- 75_Roulette/csharp/Resources/Slot.txt | 2 +- 75_Roulette/csharp/Resources/Spinning.txt | 2 +- 75_Roulette/csharp/Resources/Totals.txt | 1 + 8 files changed, 193 insertions(+), 70 deletions(-) diff --git a/75_Roulette/csharp/Game.cs b/75_Roulette/csharp/Game.cs index c9570ca1..4543737f 100644 --- a/75_Roulette/csharp/Game.cs +++ b/75_Roulette/csharp/Game.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; namespace Roulette; @@ -7,12 +8,14 @@ internal class Game private readonly IReadWrite _io; private readonly IRandom _random; private readonly Table _table; + private readonly House _house; public Game(IReadWrite io, IRandom random) { _io = io; _random = random; - _table = new Table(io, random); + _house = new(); + _table = new(_house, io, random); } public void Play() @@ -25,10 +28,9 @@ internal class Game while (_table.Play()); - if (_table.Balance > 0) + if (!_house.PlayerIsBroke) { - var name = _io.ReadString(Prompts.Check); - _io.Write(Strings.Check(_random, name, _table.Balance)); + _house.CutCheck(_io, _random); } else { @@ -40,44 +42,44 @@ internal class Game internal class Wheel { private static readonly ImmutableArray _slots = ImmutableArray.Create( - new Slot(Strings.Red(1)), - new Slot(Strings.Black(2)), - new Slot(Strings.Red(3)), - new Slot(Strings.Black(4)), - new Slot(Strings.Red(5)), - new Slot(Strings.Black(6)), - new Slot(Strings.Red(7)), - new Slot(Strings.Black(8)), - new Slot(Strings.Red(9)), - new Slot(Strings.Black(10)), - new Slot(Strings.Black(11)), - new Slot(Strings.Red(12)), - new Slot(Strings.Black(13)), - new Slot(Strings.Red(14)), - new Slot(Strings.Black(15)), - new Slot(Strings.Red(16)), - new Slot(Strings.Black(17)), - new Slot(Strings.Red(18)), - new Slot(Strings.Red(19)), - new Slot(Strings.Black(20)), - new Slot(Strings.Red(21)), - new Slot(Strings.Black(22)), - new Slot(Strings.Red(23)), - new Slot(Strings.Black(24)), - new Slot(Strings.Red(25)), - new Slot(Strings.Black(26)), - new Slot(Strings.Red(27)), - new Slot(Strings.Black(28)), - new Slot(Strings.Black(29)), - new Slot(Strings.Red(30)), - new Slot(Strings.Black(31)), - new Slot(Strings.Red(32)), - new Slot(Strings.Black(33)), - new Slot(Strings.Red(34)), - new Slot(Strings.Black(35)), - new Slot(Strings.Red(36)), - new Slot("0"), - new Slot("00")); + new Slot(Strings.Red(1), 1, 37, 40, 43, 46, 47), + new Slot(Strings.Black(2), 2, 37, 41, 43, 45, 48), + new Slot(Strings.Red(3), 3, 37, 42, 43, 46, 47), + new Slot(Strings.Black(4), 4, 37, 40, 43, 45, 48), + new Slot(Strings.Red(5), 5, 37, 41, 43, 46, 47), + new Slot(Strings.Black(6), 6, 37, 42, 43, 45, 48), + new Slot(Strings.Red(7), 7, 37, 40, 43, 46, 47), + new Slot(Strings.Black(8), 8, 37, 41, 43, 45, 48), + new Slot(Strings.Red(9), 9, 37, 42, 43, 46, 47), + new Slot(Strings.Black(10), 10, 37, 40, 43, 45, 48), + new Slot(Strings.Black(11), 11, 37, 41, 43, 46, 48), + new Slot(Strings.Red(12), 12, 37, 42, 43, 45, 47), + new Slot(Strings.Black(13), 13, 38, 40, 43, 46, 48), + new Slot(Strings.Red(14), 14, 38, 41, 43, 45, 47), + new Slot(Strings.Black(15), 15, 38, 42, 43, 46, 48), + new Slot(Strings.Red(16), 16, 38, 40, 43, 45, 47), + new Slot(Strings.Black(17), 17, 38, 41, 43, 46, 48), + new Slot(Strings.Red(18), 18, 38, 42, 43, 45, 47), + new Slot(Strings.Red(19), 19, 38, 40, 44, 46, 47), + new Slot(Strings.Black(20), 20, 38, 41, 44, 45, 48), + new Slot(Strings.Red(21), 21, 38, 42, 44, 46, 47), + new Slot(Strings.Black(22), 22, 38, 40, 44, 45, 48), + new Slot(Strings.Red(23), 23, 38, 41, 44, 46, 47), + new Slot(Strings.Black(24), 24, 38, 42, 44, 45, 48), + new Slot(Strings.Red(25), 25, 39, 40, 44, 46, 47), + new Slot(Strings.Black(26), 26, 39, 41, 44, 45, 48), + new Slot(Strings.Red(27), 27, 39, 42, 44, 46, 47), + new Slot(Strings.Black(28), 28, 39, 40, 44, 45, 48), + new Slot(Strings.Black(29), 29, 39, 41, 44, 46, 48), + new Slot(Strings.Red(30), 30, 39, 42, 44, 45, 47), + new Slot(Strings.Black(31), 31, 39, 40, 44, 46, 48), + new Slot(Strings.Red(32), 32, 39, 41, 44, 45, 47), + new Slot(Strings.Black(33), 33, 39, 42, 44, 46, 48), + new Slot(Strings.Red(34), 34, 39, 40, 44, 45, 47), + new Slot(Strings.Black(35), 35, 39, 41, 44, 46, 48), + new Slot(Strings.Red(36), 36, 39, 42, 44, 45, 47), + new Slot("0", 49), + new Slot("00", 50)); private readonly IRandom _random; @@ -86,44 +88,156 @@ internal class Wheel public Slot Spin() => _slots[_random.Next(_slots.Length)]; } -internal record struct Slot(string Name); - -internal record struct Bet(int Number, int Amount) +internal class Slot { - public Bet(int number) : this(number, 0) { } + private readonly ImmutableHashSet _coveringBets; - public bool Equals(Bet? other) => Number == other?.Number; + public Slot (string name, params BetType[] coveringBets) + { + Name = name; + _coveringBets = coveringBets.ToImmutableHashSet(); + } + + public string Name { get; } + + public bool IsCoveredBy(Bet bet) => _coveringBets.Contains(bet.Type); +} + +internal record struct BetType(int Value) +{ + public static implicit operator BetType(int value) => new(value); + + public int Payout => Value switch + { + <= 36 or >= 49 => 35, + <= 42 => 2, + <= 48 => 1 + }; +} + +internal record struct Bet(BetType Type, int Number, int Wager) +{ + public int Payout => Wager * Type.Payout; } public class Table { private readonly IReadWrite _io; private readonly Wheel _wheel; + private readonly House _house; - private int _houseBalance = 100_000; - private int _playerBalance = 1_000; - - public Table(IReadWrite io, IRandom random) + public Table(House house, IReadWrite io, IRandom random) { + _house = house; _io = io; _wheel = new(random); } - public int Balance => _playerBalance; - public bool Play() { - var betCount = _io.ReadBetCount(); - var bets = new HashSet(); - for (int i = 0; i < betCount; i++) + var bets = AcceptBets(); + var slot = SpinWheel(); + SettleBets(bets, slot); + + _io.Write(_house.Totals); + + if (_house.PlayerIsBroke) { - while (!bets.Add(_io.ReadBet(i))) + _io.Write(Streams.LastDollar); + _io.Write(Streams.Thanks); + return false; + } + + if (_house.HouseIsBroke) + { + _io.Write(Streams.BrokeHouse); + return false; + } + + return _io.ReadString(Prompts.Again).ToLowerInvariant().StartsWith('y'); + } + + private Slot SpinWheel() + { + _io.Write(Streams.Spinning); + var slot = _wheel.Spin(); + _io.Write(slot.Name); + return slot; + } + + private IReadOnlyList AcceptBets() + { + var betCount = _io.ReadBetCount(); + var betTypes = new HashSet(); + var bets = new List(); + for (int i = 1; i <= betCount; i++) + { + while (!TryAdd(_io.ReadBet(i))) { _io.Write(Streams.BetAlready); } } - - return _io.ReadString(Prompts.Again).ToLowerInvariant().StartsWith('y'); + + return bets.AsReadOnly(); + + bool TryAdd(Bet bet) + { + if (betTypes.Add(bet.Type)) + { + bets.Add(bet); + return true; + } + + return false; + } + } + + private void SettleBets(IReadOnlyList bets, Slot slot) + { + foreach (var bet in bets) + { + _io.Write(slot.IsCoveredBy(bet) ? _house.Pay(bet) : _house.Take(bet)); + } + } +} + +public class House +{ + private const int _initialHouse = 100_000; + private const int _initialPlayer = 1_000; + + private int _balance = _initialHouse; + private int _player = _initialPlayer; + + public string Totals => Strings.Totals(_balance, _player); + public bool PlayerIsBroke => _player <= 0; + public bool HouseIsBroke => _balance <= 0; + + internal string Pay(Bet bet) + { + _balance -= bet.Payout; + _player += bet.Payout; + + if (_balance <= 0) + { + _player = _initialHouse + _initialPlayer; + } + + return Strings.Win(bet); + } + + internal string Take(Bet bet) + { + _balance += bet.Wager; + _player -= bet.Wager; + + return Strings.Lose(bet); + } + + public void CutCheck(IReadWrite io, IRandom random) + { + var name = io.ReadString(Prompts.Check); + io.Write(Strings.Check(random, name, _player)); } } @@ -142,11 +256,16 @@ internal static class IOExtensions { while (true) { - var (bet, amount) = io.Read2Numbers(Prompts.Bet(number)); + var (type, amount) = io.Read2Numbers(Prompts.Bet(number)); - if (bet.IsValidInt(1, 50) && amount.IsValidInt(5, 500)) + if (type.IsValidInt(1, 50) && amount.IsValidInt(5, 500)) { - return new((int)bet, (int)amount); + return new() + { + Type = (int)type, + Number = number, + Wager = (int)amount + }; } } } diff --git a/75_Roulette/csharp/Resources/AgainPrompt.txt b/75_Roulette/csharp/Resources/AgainPrompt.txt index e69de29b..bd1d18b6 100644 --- a/75_Roulette/csharp/Resources/AgainPrompt.txt +++ b/75_Roulette/csharp/Resources/AgainPrompt.txt @@ -0,0 +1 @@ +Again \ No newline at end of file diff --git a/75_Roulette/csharp/Resources/Check.txt b/75_Roulette/csharp/Resources/Check.txt index f2be95cd..ffdd640a 100644 --- a/75_Roulette/csharp/Resources/Check.txt +++ b/75_Roulette/csharp/Resources/Check.txt @@ -1,6 +1,7 @@ + ------------------------------------------------------------------------Check No. {0} - {1:mmmm d',' yyyy} + {1:MMMM d',' yyyy} Pay to the order of-----{2}-----$ {3} @@ -12,3 +13,4 @@ Pay to the order of-----{2}-----$ {3} ----------X----- --------------------------------------------------------------Come back soon! + diff --git a/75_Roulette/csharp/Resources/Outcome.txt b/75_Roulette/csharp/Resources/Outcome.txt index 7926cb87..30e227f7 100644 --- a/75_Roulette/csharp/Resources/Outcome.txt +++ b/75_Roulette/csharp/Resources/Outcome.txt @@ -1 +1 @@ -You {0} {1} dollars on bet {2} \ No newline at end of file +You {0} {1} dollars on bet {2} diff --git a/75_Roulette/csharp/Resources/Resource.cs b/75_Roulette/csharp/Resources/Resource.cs index aca0aaf5..dd9c8672 100644 --- a/75_Roulette/csharp/Resources/Resource.cs +++ b/75_Roulette/csharp/Resources/Resource.cs @@ -24,10 +24,10 @@ internal static class Resource private static string Slot(int number, [CallerMemberName] string? colour = null) => string.Format(GetString(), number, colour); - public static string Lose(int amount, int bet) => Outcome(amount, bet); - public static string Win(int amount, int bet) => Outcome(amount, bet); - private static string Outcome(int amount, int bet, [CallerMemberName] string? winlose = null) - => string.Format(GetString(), winlose, amount, bet); + public static string Lose(Bet bet) => Outcome(bet.Wager, bet.Number); + public static string Win(Bet bet) => Outcome(bet.Payout, bet.Number); + private static string Outcome(int amount, int number, [CallerMemberName] string? winlose = null) + => string.Format(GetString(), winlose, amount, number); public static string Totals(int me, int you) => string.Format(GetString(), me, you); diff --git a/75_Roulette/csharp/Resources/Slot.txt b/75_Roulette/csharp/Resources/Slot.txt index d45e103b..de02695d 100644 --- a/75_Roulette/csharp/Resources/Slot.txt +++ b/75_Roulette/csharp/Resources/Slot.txt @@ -1,2 +1,2 @@ {0} {1} - \ No newline at end of file + diff --git a/75_Roulette/csharp/Resources/Spinning.txt b/75_Roulette/csharp/Resources/Spinning.txt index 13514a0a..0d87fe39 100644 --- a/75_Roulette/csharp/Resources/Spinning.txt +++ b/75_Roulette/csharp/Resources/Spinning.txt @@ -1,3 +1,3 @@ -SPINNING +Spinning diff --git a/75_Roulette/csharp/Resources/Totals.txt b/75_Roulette/csharp/Resources/Totals.txt index 26f35724..4cecae3f 100644 --- a/75_Roulette/csharp/Resources/Totals.txt +++ b/75_Roulette/csharp/Resources/Totals.txt @@ -1,2 +1,3 @@ + Totals Me You {0,-14}{1} From 699412adb81c8524f435f6b4b3042a496847c60f Mon Sep 17 00:00:00 2001 From: drewjcooper Date: Thu, 16 Feb 2023 12:22:59 +1100 Subject: [PATCH 6/7] Reorganise code --- 75_Roulette/csharp/Bet.cs | 6 + 75_Roulette/csharp/BetType.cs | 13 ++ 75_Roulette/csharp/Croupier.cs | 41 +++++ 75_Roulette/csharp/Game.cs | 238 +---------------------------- 75_Roulette/csharp/IOExtensions.cs | 34 +++++ 75_Roulette/csharp/Slot.cs | 18 +++ 75_Roulette/csharp/Table.cs | 82 ++++++++++ 75_Roulette/csharp/Wheel.cs | 52 +++++++ 8 files changed, 247 insertions(+), 237 deletions(-) create mode 100644 75_Roulette/csharp/Bet.cs create mode 100644 75_Roulette/csharp/BetType.cs create mode 100644 75_Roulette/csharp/Croupier.cs create mode 100644 75_Roulette/csharp/IOExtensions.cs create mode 100644 75_Roulette/csharp/Slot.cs create mode 100644 75_Roulette/csharp/Table.cs create mode 100644 75_Roulette/csharp/Wheel.cs diff --git a/75_Roulette/csharp/Bet.cs b/75_Roulette/csharp/Bet.cs new file mode 100644 index 00000000..da0e31b7 --- /dev/null +++ b/75_Roulette/csharp/Bet.cs @@ -0,0 +1,6 @@ +namespace Roulette; + +internal record struct Bet(BetType Type, int Number, int Wager) +{ + public int Payout => Wager * Type.Payout; +} diff --git a/75_Roulette/csharp/BetType.cs b/75_Roulette/csharp/BetType.cs new file mode 100644 index 00000000..79b3b67c --- /dev/null +++ b/75_Roulette/csharp/BetType.cs @@ -0,0 +1,13 @@ +namespace Roulette; + +internal record struct BetType(int Value) +{ + public static implicit operator BetType(int value) => new(value); + + public int Payout => Value switch + { + <= 36 or >= 49 => 35, + <= 42 => 2, + <= 48 => 1 + }; +} diff --git a/75_Roulette/csharp/Croupier.cs b/75_Roulette/csharp/Croupier.cs new file mode 100644 index 00000000..32f76c33 --- /dev/null +++ b/75_Roulette/csharp/Croupier.cs @@ -0,0 +1,41 @@ +namespace Roulette; + +internal class Croupier +{ + private const int _initialHouse = 100_000; + private const int _initialPlayer = 1_000; + + private int _house = _initialHouse; + private int _player = _initialPlayer; + + public string Totals => Strings.Totals(_house, _player); + public bool PlayerIsBroke => _player <= 0; + public bool HouseIsBroke => _house <= 0; + + internal string Pay(Bet bet) + { + _house -= bet.Payout; + _player += bet.Payout; + + if (_house <= 0) + { + _player = _initialHouse + _initialPlayer; + } + + return Strings.Win(bet); + } + + internal string Take(Bet bet) + { + _house += bet.Wager; + _player -= bet.Wager; + + return Strings.Lose(bet); + } + + public void CutCheck(IReadWrite io, IRandom random) + { + var name = io.ReadString(Prompts.Check); + io.Write(Strings.Check(random, name, _player)); + } +} diff --git a/75_Roulette/csharp/Game.cs b/75_Roulette/csharp/Game.cs index 4543737f..aa077042 100644 --- a/75_Roulette/csharp/Game.cs +++ b/75_Roulette/csharp/Game.cs @@ -1,4 +1,3 @@ -using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; namespace Roulette; @@ -8,7 +7,7 @@ internal class Game private readonly IReadWrite _io; private readonly IRandom _random; private readonly Table _table; - private readonly House _house; + private readonly Croupier _house; public Game(IReadWrite io, IRandom random) { @@ -38,238 +37,3 @@ internal class Game } } } - -internal class Wheel -{ - private static readonly ImmutableArray _slots = ImmutableArray.Create( - new Slot(Strings.Red(1), 1, 37, 40, 43, 46, 47), - new Slot(Strings.Black(2), 2, 37, 41, 43, 45, 48), - new Slot(Strings.Red(3), 3, 37, 42, 43, 46, 47), - new Slot(Strings.Black(4), 4, 37, 40, 43, 45, 48), - new Slot(Strings.Red(5), 5, 37, 41, 43, 46, 47), - new Slot(Strings.Black(6), 6, 37, 42, 43, 45, 48), - new Slot(Strings.Red(7), 7, 37, 40, 43, 46, 47), - new Slot(Strings.Black(8), 8, 37, 41, 43, 45, 48), - new Slot(Strings.Red(9), 9, 37, 42, 43, 46, 47), - new Slot(Strings.Black(10), 10, 37, 40, 43, 45, 48), - new Slot(Strings.Black(11), 11, 37, 41, 43, 46, 48), - new Slot(Strings.Red(12), 12, 37, 42, 43, 45, 47), - new Slot(Strings.Black(13), 13, 38, 40, 43, 46, 48), - new Slot(Strings.Red(14), 14, 38, 41, 43, 45, 47), - new Slot(Strings.Black(15), 15, 38, 42, 43, 46, 48), - new Slot(Strings.Red(16), 16, 38, 40, 43, 45, 47), - new Slot(Strings.Black(17), 17, 38, 41, 43, 46, 48), - new Slot(Strings.Red(18), 18, 38, 42, 43, 45, 47), - new Slot(Strings.Red(19), 19, 38, 40, 44, 46, 47), - new Slot(Strings.Black(20), 20, 38, 41, 44, 45, 48), - new Slot(Strings.Red(21), 21, 38, 42, 44, 46, 47), - new Slot(Strings.Black(22), 22, 38, 40, 44, 45, 48), - new Slot(Strings.Red(23), 23, 38, 41, 44, 46, 47), - new Slot(Strings.Black(24), 24, 38, 42, 44, 45, 48), - new Slot(Strings.Red(25), 25, 39, 40, 44, 46, 47), - new Slot(Strings.Black(26), 26, 39, 41, 44, 45, 48), - new Slot(Strings.Red(27), 27, 39, 42, 44, 46, 47), - new Slot(Strings.Black(28), 28, 39, 40, 44, 45, 48), - new Slot(Strings.Black(29), 29, 39, 41, 44, 46, 48), - new Slot(Strings.Red(30), 30, 39, 42, 44, 45, 47), - new Slot(Strings.Black(31), 31, 39, 40, 44, 46, 48), - new Slot(Strings.Red(32), 32, 39, 41, 44, 45, 47), - new Slot(Strings.Black(33), 33, 39, 42, 44, 46, 48), - new Slot(Strings.Red(34), 34, 39, 40, 44, 45, 47), - new Slot(Strings.Black(35), 35, 39, 41, 44, 46, 48), - new Slot(Strings.Red(36), 36, 39, 42, 44, 45, 47), - new Slot("0", 49), - new Slot("00", 50)); - - private readonly IRandom _random; - - public Wheel(IRandom random) => _random = random; - - public Slot Spin() => _slots[_random.Next(_slots.Length)]; -} - -internal class Slot -{ - private readonly ImmutableHashSet _coveringBets; - - public Slot (string name, params BetType[] coveringBets) - { - Name = name; - _coveringBets = coveringBets.ToImmutableHashSet(); - } - - public string Name { get; } - - public bool IsCoveredBy(Bet bet) => _coveringBets.Contains(bet.Type); -} - -internal record struct BetType(int Value) -{ - public static implicit operator BetType(int value) => new(value); - - public int Payout => Value switch - { - <= 36 or >= 49 => 35, - <= 42 => 2, - <= 48 => 1 - }; -} - -internal record struct Bet(BetType Type, int Number, int Wager) -{ - public int Payout => Wager * Type.Payout; -} - -public class Table -{ - private readonly IReadWrite _io; - private readonly Wheel _wheel; - private readonly House _house; - - public Table(House house, IReadWrite io, IRandom random) - { - _house = house; - _io = io; - _wheel = new(random); - } - - public bool Play() - { - var bets = AcceptBets(); - var slot = SpinWheel(); - SettleBets(bets, slot); - - _io.Write(_house.Totals); - - if (_house.PlayerIsBroke) - { - _io.Write(Streams.LastDollar); - _io.Write(Streams.Thanks); - return false; - } - - if (_house.HouseIsBroke) - { - _io.Write(Streams.BrokeHouse); - return false; - } - - return _io.ReadString(Prompts.Again).ToLowerInvariant().StartsWith('y'); - } - - private Slot SpinWheel() - { - _io.Write(Streams.Spinning); - var slot = _wheel.Spin(); - _io.Write(slot.Name); - return slot; - } - - private IReadOnlyList AcceptBets() - { - var betCount = _io.ReadBetCount(); - var betTypes = new HashSet(); - var bets = new List(); - for (int i = 1; i <= betCount; i++) - { - while (!TryAdd(_io.ReadBet(i))) - { - _io.Write(Streams.BetAlready); - } - } - - return bets.AsReadOnly(); - - bool TryAdd(Bet bet) - { - if (betTypes.Add(bet.Type)) - { - bets.Add(bet); - return true; - } - - return false; - } - } - - private void SettleBets(IReadOnlyList bets, Slot slot) - { - foreach (var bet in bets) - { - _io.Write(slot.IsCoveredBy(bet) ? _house.Pay(bet) : _house.Take(bet)); - } - } -} - -public class House -{ - private const int _initialHouse = 100_000; - private const int _initialPlayer = 1_000; - - private int _balance = _initialHouse; - private int _player = _initialPlayer; - - public string Totals => Strings.Totals(_balance, _player); - public bool PlayerIsBroke => _player <= 0; - public bool HouseIsBroke => _balance <= 0; - - internal string Pay(Bet bet) - { - _balance -= bet.Payout; - _player += bet.Payout; - - if (_balance <= 0) - { - _player = _initialHouse + _initialPlayer; - } - - return Strings.Win(bet); - } - - internal string Take(Bet bet) - { - _balance += bet.Wager; - _player -= bet.Wager; - - return Strings.Lose(bet); - } - - public void CutCheck(IReadWrite io, IRandom random) - { - var name = io.ReadString(Prompts.Check); - io.Write(Strings.Check(random, name, _player)); - } -} - -internal static class IOExtensions -{ - internal static int ReadBetCount(this IReadWrite io) - { - while (true) - { - var betCount = io.ReadNumber(Prompts.HowManyBets); - if (betCount.IsValidInt(1)) { return (int)betCount; } - } - } - - internal static Bet ReadBet(this IReadWrite io, int number) - { - while (true) - { - var (type, amount) = io.Read2Numbers(Prompts.Bet(number)); - - if (type.IsValidInt(1, 50) && amount.IsValidInt(5, 500)) - { - return new() - { - Type = (int)type, - Number = number, - Wager = (int)amount - }; - } - } - } - - internal static bool IsValidInt(this float value, int minValue, int maxValue = int.MaxValue) - => value == (int)value && value >= minValue && value <= maxValue; -} \ No newline at end of file diff --git a/75_Roulette/csharp/IOExtensions.cs b/75_Roulette/csharp/IOExtensions.cs new file mode 100644 index 00000000..49326bef --- /dev/null +++ b/75_Roulette/csharp/IOExtensions.cs @@ -0,0 +1,34 @@ +namespace Roulette; + +internal static class IOExtensions +{ + internal static int ReadBetCount(this IReadWrite io) + { + while (true) + { + var betCount = io.ReadNumber(Prompts.HowManyBets); + if (betCount.IsValidInt(1)) { return (int)betCount; } + } + } + + internal static Bet ReadBet(this IReadWrite io, int number) + { + while (true) + { + var (type, amount) = io.Read2Numbers(Prompts.Bet(number)); + + if (type.IsValidInt(1, 50) && amount.IsValidInt(5, 500)) + { + return new() + { + Type = (int)type, + Number = number, + Wager = (int)amount + }; + } + } + } + + internal static bool IsValidInt(this float value, int minValue, int maxValue = int.MaxValue) + => value == (int)value && value >= minValue && value <= maxValue; +} \ No newline at end of file diff --git a/75_Roulette/csharp/Slot.cs b/75_Roulette/csharp/Slot.cs new file mode 100644 index 00000000..df30cea1 --- /dev/null +++ b/75_Roulette/csharp/Slot.cs @@ -0,0 +1,18 @@ +using System.Collections.Immutable; + +namespace Roulette; + +internal class Slot +{ + private readonly ImmutableHashSet _coveringBets; + + public Slot (string name, params BetType[] coveringBets) + { + Name = name; + _coveringBets = coveringBets.ToImmutableHashSet(); + } + + public string Name { get; } + + public bool IsCoveredBy(Bet bet) => _coveringBets.Contains(bet.Type); +} diff --git a/75_Roulette/csharp/Table.cs b/75_Roulette/csharp/Table.cs new file mode 100644 index 00000000..fbc6341e --- /dev/null +++ b/75_Roulette/csharp/Table.cs @@ -0,0 +1,82 @@ +namespace Roulette; + +internal class Table +{ + private readonly IReadWrite _io; + private readonly Wheel _wheel; + private readonly Croupier _house; + + public Table(Croupier house, IReadWrite io, IRandom random) + { + _house = house; + _io = io; + _wheel = new(random); + } + + public bool Play() + { + var bets = AcceptBets(); + var slot = SpinWheel(); + SettleBets(bets, slot); + + _io.Write(_house.Totals); + + if (_house.PlayerIsBroke) + { + _io.Write(Streams.LastDollar); + _io.Write(Streams.Thanks); + return false; + } + + if (_house.HouseIsBroke) + { + _io.Write(Streams.BrokeHouse); + return false; + } + + return _io.ReadString(Prompts.Again).ToLowerInvariant().StartsWith('y'); + } + + private Slot SpinWheel() + { + _io.Write(Streams.Spinning); + var slot = _wheel.Spin(); + _io.Write(slot.Name); + return slot; + } + + private IReadOnlyList AcceptBets() + { + var betCount = _io.ReadBetCount(); + var betTypes = new HashSet(); + var bets = new List(); + for (int i = 1; i <= betCount; i++) + { + while (!TryAdd(_io.ReadBet(i))) + { + _io.Write(Streams.BetAlready); + } + } + + return bets.AsReadOnly(); + + bool TryAdd(Bet bet) + { + if (betTypes.Add(bet.Type)) + { + bets.Add(bet); + return true; + } + + return false; + } + } + + private void SettleBets(IReadOnlyList bets, Slot slot) + { + foreach (var bet in bets) + { + _io.Write(slot.IsCoveredBy(bet) ? _house.Pay(bet) : _house.Take(bet)); + } + } +} diff --git a/75_Roulette/csharp/Wheel.cs b/75_Roulette/csharp/Wheel.cs new file mode 100644 index 00000000..dfcecd29 --- /dev/null +++ b/75_Roulette/csharp/Wheel.cs @@ -0,0 +1,52 @@ +using System.Collections.Immutable; + +namespace Roulette; + +internal class Wheel +{ + private static readonly ImmutableArray _slots = ImmutableArray.Create( + new Slot(Strings.Red(1), 1, 37, 40, 43, 46, 47), + new Slot(Strings.Black(2), 2, 37, 41, 43, 45, 48), + new Slot(Strings.Red(3), 3, 37, 42, 43, 46, 47), + new Slot(Strings.Black(4), 4, 37, 40, 43, 45, 48), + new Slot(Strings.Red(5), 5, 37, 41, 43, 46, 47), + new Slot(Strings.Black(6), 6, 37, 42, 43, 45, 48), + new Slot(Strings.Red(7), 7, 37, 40, 43, 46, 47), + new Slot(Strings.Black(8), 8, 37, 41, 43, 45, 48), + new Slot(Strings.Red(9), 9, 37, 42, 43, 46, 47), + new Slot(Strings.Black(10), 10, 37, 40, 43, 45, 48), + new Slot(Strings.Black(11), 11, 37, 41, 43, 46, 48), + new Slot(Strings.Red(12), 12, 37, 42, 43, 45, 47), + new Slot(Strings.Black(13), 13, 38, 40, 43, 46, 48), + new Slot(Strings.Red(14), 14, 38, 41, 43, 45, 47), + new Slot(Strings.Black(15), 15, 38, 42, 43, 46, 48), + new Slot(Strings.Red(16), 16, 38, 40, 43, 45, 47), + new Slot(Strings.Black(17), 17, 38, 41, 43, 46, 48), + new Slot(Strings.Red(18), 18, 38, 42, 43, 45, 47), + new Slot(Strings.Red(19), 19, 38, 40, 44, 46, 47), + new Slot(Strings.Black(20), 20, 38, 41, 44, 45, 48), + new Slot(Strings.Red(21), 21, 38, 42, 44, 46, 47), + new Slot(Strings.Black(22), 22, 38, 40, 44, 45, 48), + new Slot(Strings.Red(23), 23, 38, 41, 44, 46, 47), + new Slot(Strings.Black(24), 24, 38, 42, 44, 45, 48), + new Slot(Strings.Red(25), 25, 39, 40, 44, 46, 47), + new Slot(Strings.Black(26), 26, 39, 41, 44, 45, 48), + new Slot(Strings.Red(27), 27, 39, 42, 44, 46, 47), + new Slot(Strings.Black(28), 28, 39, 40, 44, 45, 48), + new Slot(Strings.Black(29), 29, 39, 41, 44, 46, 48), + new Slot(Strings.Red(30), 30, 39, 42, 44, 45, 47), + new Slot(Strings.Black(31), 31, 39, 40, 44, 46, 48), + new Slot(Strings.Red(32), 32, 39, 41, 44, 45, 47), + new Slot(Strings.Black(33), 33, 39, 42, 44, 46, 48), + new Slot(Strings.Red(34), 34, 39, 40, 44, 45, 47), + new Slot(Strings.Black(35), 35, 39, 41, 44, 46, 48), + new Slot(Strings.Red(36), 36, 39, 42, 44, 45, 47), + new Slot("0", 49), + new Slot("00", 50)); + + private readonly IRandom _random; + + public Wheel(IRandom random) => _random = random; + + public Slot Spin() => _slots[_random.Next(_slots.Length)]; +} From 4722cbded8d3d39b76e74c839a9b8608a20812e6 Mon Sep 17 00:00:00 2001 From: drewjcooper Date: Thu, 16 Feb 2023 18:02:04 +1100 Subject: [PATCH 7/7] Code cleanup --- 75_Roulette/csharp/Game.cs | 23 +++++++++++++---------- 75_Roulette/csharp/Table.cs | 23 ++++++----------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/75_Roulette/csharp/Game.cs b/75_Roulette/csharp/Game.cs index aa077042..75a6d7e5 100644 --- a/75_Roulette/csharp/Game.cs +++ b/75_Roulette/csharp/Game.cs @@ -1,5 +1,3 @@ -using System.Diagnostics.CodeAnalysis; - namespace Roulette; internal class Game @@ -7,14 +5,14 @@ internal class Game private readonly IReadWrite _io; private readonly IRandom _random; private readonly Table _table; - private readonly Croupier _house; + private readonly Croupier _croupier; public Game(IReadWrite io, IRandom random) { _io = io; _random = random; - _house = new(); - _table = new(_house, io, random); + _croupier = new(); + _table = new(_croupier, io, random); } public void Play() @@ -27,13 +25,18 @@ internal class Game while (_table.Play()); - if (!_house.PlayerIsBroke) - { - _house.CutCheck(_io, _random); - } - else + if (_croupier.PlayerIsBroke) { + _io.Write(Streams.LastDollar); _io.Write(Streams.Thanks); + return; } + + if (_croupier.HouseIsBroke) + { + _io.Write(Streams.BrokeHouse); + } + + _croupier.CutCheck(_io, _random); } } diff --git a/75_Roulette/csharp/Table.cs b/75_Roulette/csharp/Table.cs index fbc6341e..026152d1 100644 --- a/75_Roulette/csharp/Table.cs +++ b/75_Roulette/csharp/Table.cs @@ -4,11 +4,11 @@ internal class Table { private readonly IReadWrite _io; private readonly Wheel _wheel; - private readonly Croupier _house; + private readonly Croupier _croupier; - public Table(Croupier house, IReadWrite io, IRandom random) + public Table(Croupier croupier, IReadWrite io, IRandom random) { - _house = house; + _croupier = croupier; _io = io; _wheel = new(random); } @@ -19,20 +19,9 @@ internal class Table var slot = SpinWheel(); SettleBets(bets, slot); - _io.Write(_house.Totals); + _io.Write(_croupier.Totals); - if (_house.PlayerIsBroke) - { - _io.Write(Streams.LastDollar); - _io.Write(Streams.Thanks); - return false; - } - - if (_house.HouseIsBroke) - { - _io.Write(Streams.BrokeHouse); - return false; - } + if (_croupier.PlayerIsBroke || _croupier.HouseIsBroke) { return false; } return _io.ReadString(Prompts.Again).ToLowerInvariant().StartsWith('y'); } @@ -76,7 +65,7 @@ internal class Table { foreach (var bet in bets) { - _io.Write(slot.IsCoveredBy(bet) ? _house.Pay(bet) : _house.Take(bet)); + _io.Write(slot.IsCoveredBy(bet) ? _croupier.Pay(bet) : _croupier.Take(bet)); } } }