From 1558bfbfe876ae81709d3d9cfa3337ba11ee2056 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Fri, 1 Jul 2022 17:20:13 +1000 Subject: [PATCH] CSHARP-71 Move Human GetWager logic --- 71_Poker/csharp/Players/Computer.cs | 2 +- 71_Poker/csharp/Players/Human.cs | 27 ++++++++++++++++++++++++ 71_Poker/csharp/Table.cs | 32 +++++------------------------ 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/71_Poker/csharp/Players/Computer.cs b/71_Poker/csharp/Players/Computer.cs index af82eef8..5ab2e77d 100644 --- a/71_Poker/csharp/Players/Computer.cs +++ b/71_Poker/csharp/Players/Computer.cs @@ -76,7 +76,7 @@ internal class Computer : Player { _io.WriteLine("I'll see you."); Bet = Table.Human.Bet; - Table.UpdatePot(); + Table.CollectBets(); } else { diff --git a/71_Poker/csharp/Players/Human.cs b/71_Poker/csharp/Players/Human.cs index cf42413e..ec3e7d3c 100644 --- a/71_Poker/csharp/Players/Human.cs +++ b/71_Poker/csharp/Players/Human.cs @@ -1,4 +1,5 @@ using Poker.Cards; +using Poker.Strategies; namespace Poker.Players; @@ -30,6 +31,32 @@ internal class Human : Player _io.Write(Hand); } + internal bool SetWager() + { + var strategy = _io.ReadHumanStrategy(Table.Computer.Bet == 0 && Bet == 0); + if (strategy is Strategies.Bet or Check) + { + if (Bet + strategy.Value < Table.Computer.Bet) + { + _io.WriteLine("If you can't see my bet, then fold."); + return false; + } + if (Balance - Bet - strategy.Value >= 0) + { + HasBet = true; + Bet += strategy.Value; + return true; + } + RaiseFunds(); + } + else + { + Fold(); + Table.CollectBets(); + } + return false; + } + public void RaiseFunds() { _io.WriteLine(); diff --git a/71_Poker/csharp/Table.cs b/71_Poker/csharp/Table.cs index b0510a8e..da819801 100644 --- a/71_Poker/csharp/Table.cs +++ b/71_Poker/csharp/Table.cs @@ -113,34 +113,12 @@ internal class Table Human.HasBet = false; while (true) { - var humanStrategy = _io.ReadHumanStrategy(Computer.Bet == 0 && Human.Bet == 0); - if (humanStrategy is Bet or Check) - { - if (Human.Bet + humanStrategy.Value < Computer.Bet) - { - _io.WriteLine("If you can't see my bet, then fold."); - continue; - } - if (Human.Balance - Human.Bet - humanStrategy.Value >= 0) - { - Human.HasBet = true; - Human.Bet += humanStrategy.Value; - break; - } - Human.RaiseFunds(); - if (Human.IsBroke) { return; } - continue; - } - else - { - Human.Fold(); - UpdatePot(); - return; - } + if (Human.SetWager()) { break; } + if (Human.IsBroke || Human.HasFolded) { return; } } if (Human.Bet == Computer.Bet) { - UpdatePot(); + CollectBets(); return; } if (Computer.Strategy is Fold) @@ -158,7 +136,7 @@ internal class Table { _io.WriteLine("I'll see you."); Computer.Bet = Human.Bet; - UpdatePot(); + CollectBets(); return; } } @@ -170,7 +148,7 @@ internal class Table } } - private void UpdatePot() + internal void CollectBets() { Human.Balance -= Human.Bet; Computer.Balance -= Computer.Bet;