diff --git a/71_Poker/csharp/Game.cs b/71_Poker/csharp/Game.cs index c902ffeb..8fa11b7b 100644 --- a/71_Poker/csharp/Game.cs +++ b/71_Poker/csharp/Game.cs @@ -9,10 +9,7 @@ internal class Game private readonly IReadWrite _io; private readonly IRandom _random; - private int _playerBet; - private int _playerTotalBet; private int Z; - private int _computerTotalBet; private int V; public Game(IReadWrite io, IRandom random) @@ -70,7 +67,6 @@ internal class Game }; if (Z <= 1) { - _computerTotalBet = 0; _io.WriteLine("I check."); } else @@ -78,8 +74,7 @@ internal class Game V=Z+Get0To9(); if (ComputerIsBroke()) { return false; } _io.WriteLine($"I'll open with ${V}"); - _computerTotalBet = V; - _playerTotalBet = 0; + table.Computer.Bet = V; } if (GetWager()) { return false; } if (table.SomeoneHasFolded()) { return ShouldContinue(); } @@ -95,10 +90,8 @@ internal class Game _ => 2 }; - _computerTotalBet = 0; - _playerTotalBet = 0; if (GetWager()) { return false; } - if (_playerBet != 0) + if (table.Human.HasBet) { if (table.SomeoneHasFolded()) { return ShouldContinue(); } } @@ -111,7 +104,7 @@ internal class Game V=Z+Get0To9(); if (ComputerIsBroke()) { return false; } _io.WriteLine($"I'll bet ${V}"); - _computerTotalBet = V; + table.Computer.Bet = V; if (GetWager()) { return false; } if (table.SomeoneHasFolded()) { return ShouldContinue(); } } @@ -132,20 +125,20 @@ internal class Game { while (true) { - _playerBet = 0; + table.Human.HasBet = false; while(true) { - if (_io.ReadPlayerAction(_computerTotalBet == 0 && _playerTotalBet == 0) is Bet bet) + if (_io.ReadPlayerAction(table.Computer.Bet == 0 && table.Human.Bet == 0) is Bet bet) { - if (_playerTotalBet + bet.Amount < _computerTotalBet) + if (table.Human.Bet + bet.Amount < table.Computer.Bet) { _io.WriteLine("If you can't see my bet, then fold."); continue; } - if (table.Human.Balance - _playerTotalBet - bet.Amount >= 0) + if (table.Human.Balance - table.Human.Bet - bet.Amount >= 0) { - _playerBet = bet.Amount; - _playerTotalBet += bet.Amount; + table.Human.HasBet = true; + table.Human.Bet += bet.Amount; break; } if (table.Human.IsBroke()) { return true; } @@ -158,14 +151,14 @@ internal class Game return false; } } - if (_playerTotalBet == _computerTotalBet) + if (table.Human.Bet == table.Computer.Bet) { UpdatePot(); return false; } if (Z == 1) { - if (_playerTotalBet > 5) + if (table.Human.Bet > 5) { table.Computer.Fold(); _io.WriteLine("I fold."); @@ -173,42 +166,42 @@ internal class Game } V = 5; } - if (_playerTotalBet > 3 * Z) + if (table.Human.Bet > 3 * Z) { if (Z != 2) { _io.WriteLine("I'll see you."); - _computerTotalBet = _playerTotalBet; + table.Computer.Bet = table.Human.Bet; UpdatePot(); return false; } } - V = _playerTotalBet - _computerTotalBet + Get0To9(); + V = table.Human.Bet - table.Computer.Bet + Get0To9(); if (ComputerIsBroke()) { return true; } _io.WriteLine($"I'll see you, and raise you{V}"); - _computerTotalBet = _playerTotalBet + V; + table.Computer.Bet = table.Human.Bet + V; } } void UpdatePot() { - table.Human.Balance -= _playerTotalBet; - table.Computer.Balance -= _computerTotalBet; - table.Pot += _playerTotalBet + _computerTotalBet; + table.Human.Balance -= table.Human.Bet; + table.Computer.Balance -= table.Computer.Bet; + table.Pot += table.Human.Bet + table.Computer.Bet; } bool ComputerIsBroke() { - if (table.Computer.Balance - _playerTotalBet - V >= 0) { return false; } - if (_playerTotalBet == 0) + if (table.Computer.Balance - table.Human.Bet - V >= 0) { return false; } + if (table.Human.Bet == 0) { V = table.Computer.Balance; } - else if (table.Computer.Balance - _playerTotalBet >= 0) + else if (table.Computer.Balance - table.Human.Bet >= 0) { _io.WriteLine("I'll see you."); - _computerTotalBet = _playerTotalBet; + table.Computer.Bet = table.Human.Bet; UpdatePot(); return false; } diff --git a/71_Poker/csharp/Players/Computer.cs b/71_Poker/csharp/Players/Computer.cs index 11e2005e..a5506853 100644 --- a/71_Poker/csharp/Players/Computer.cs +++ b/71_Poker/csharp/Players/Computer.cs @@ -18,9 +18,9 @@ internal class Computer : Player public bool IsBluffing => _isBluffing; - public override void NewHand(Hand hand) + public override void NewHand() { - base.NewHand(hand); + base.NewHand(); _isBluffing = false; } @@ -33,7 +33,7 @@ internal class Computer : Player return 23; } - public void DrawCards(Deck deck) + protected override void DrawCards(Deck deck) { var keepMask = Hand.KeepMask; var count = 0; diff --git a/71_Poker/csharp/Players/Human.cs b/71_Poker/csharp/Players/Human.cs index f4a4af1a..0a3f4234 100644 --- a/71_Poker/csharp/Players/Human.cs +++ b/71_Poker/csharp/Players/Human.cs @@ -15,7 +15,7 @@ internal class Human : Player public bool HasWatch { get; set; } - public void DrawCards(Deck deck) + protected override void DrawCards(Deck deck) { var count = _io.ReadNumber("How many cards do you want", 3, "You can't draw more than three cards."); if (count == 0) { return; } diff --git a/71_Poker/csharp/Players/Player.cs b/71_Poker/csharp/Players/Player.cs index 313dc0ab..4e9f7faf 100644 --- a/71_Poker/csharp/Players/Player.cs +++ b/71_Poker/csharp/Players/Player.cs @@ -15,7 +15,8 @@ internal abstract class Player public Hand Hand { get; set; } public int Balance { get; set; } - public int Bet { get; private set; } + public bool HasBet { get; set; } + public int Bet { get; set; } public bool HasFolded => _hasFolded; protected Table Table => @@ -23,9 +24,10 @@ internal abstract class Player public void Sit(Table table) => _table = table; - public virtual void NewHand(Hand hand) + public virtual void NewHand() { - Hand = hand; + Bet = 0; + Hand = Table.Deck.DealHand(); _hasFolded = false; } @@ -35,6 +37,14 @@ internal abstract class Player return Table.Ante; } + public void DrawCards() + { + Bet = 0; + DrawCards(Table.Deck); + } + + protected abstract void DrawCards(Deck deck); + public virtual void TakeWinnings() { Balance += Table.Pot; diff --git a/71_Poker/csharp/Table.cs b/71_Poker/csharp/Table.cs index 28cf6919..cb5bfc44 100644 --- a/71_Poker/csharp/Table.cs +++ b/71_Poker/csharp/Table.cs @@ -7,12 +7,11 @@ internal class Table { private IReadWrite _io; public int Pot; - private Deck _deck; public Table(IReadWrite io, Deck deck, Human human, Computer computer) { _io = io; - _deck = deck; + Deck = deck; Human = human; Computer = computer; @@ -21,6 +20,7 @@ internal class Table } public int Ante { get; } = 5; + public Deck Deck { get; } public Human Human { get; } public Computer Computer { get; } @@ -28,8 +28,8 @@ internal class Table { Pot = Human.AnteUp() + Computer.AnteUp(); - Human.NewHand(_deck.DealHand()); - Computer.NewHand(_deck.DealHand()); + Human.NewHand(); + Computer.NewHand(); _io.WriteLine("Your hand:"); _io.Write(Human.Hand); @@ -39,8 +39,8 @@ internal class Table { _io.WriteLine(); _io.Write("Now we draw -- "); - Human.DrawCards(_deck); - Computer.DrawCards(_deck); + Human.DrawCards(); + Computer.DrawCards(); _io.WriteLine(); }