mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-04-28 11:53:21 -07:00
Move PlayHand logic to table
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using Poker.Cards;
|
||||
using Poker.Players;
|
||||
using Poker.Resources;
|
||||
using Poker.Strategies;
|
||||
|
||||
namespace Poker;
|
||||
|
||||
@@ -16,217 +15,19 @@ internal class Game
|
||||
_random = random;
|
||||
}
|
||||
|
||||
private int Get0To9() => _random.Next(10);
|
||||
|
||||
internal void Play()
|
||||
{
|
||||
var deck = new Deck();
|
||||
var human = new Human(200, _io);
|
||||
var computer = new Computer(200, _io, _random);
|
||||
var table = new Table(_io, deck, human, computer);
|
||||
|
||||
_io.Write(Resource.Streams.Title);
|
||||
_io.Write(Resource.Streams.Instructions);
|
||||
|
||||
var deck = new Deck();
|
||||
var human = new Human(200, _io);
|
||||
var computer = new Computer(200, _io, _random);
|
||||
var table = new Table(_io, _random, deck, human, computer);
|
||||
|
||||
do
|
||||
{
|
||||
PlayHand(table);
|
||||
table.PlayHand();
|
||||
} while (table.ShouldPlayAnotherHand());
|
||||
}
|
||||
|
||||
internal void PlayHand(Table table)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
_io.WriteLine();
|
||||
table.Computer.CheckFunds();
|
||||
if (table.Computer.IsBroke) { return; }
|
||||
|
||||
_io.WriteLine($"The ante is ${table.Ante}. I will deal:");
|
||||
_io.WriteLine();
|
||||
if (table.Human.Balance <= table.Ante)
|
||||
{
|
||||
table.Human.RaiseFunds();
|
||||
if (table.Human.IsBroke) { return; }
|
||||
}
|
||||
|
||||
table.Deal(_random);
|
||||
|
||||
_io.WriteLine();
|
||||
table.Computer.Strategy = (table.Computer.Hand.IsWeak, table.Computer.Hand.Rank < HandRank.Three, table.Computer.Hand.Rank < HandRank.FullHouse) switch
|
||||
{
|
||||
(true, _, _) when Get0To9() < 2 => Strategy.Bluff(23, 0b11100),
|
||||
(true, _, _) when Get0To9() < 2 => Strategy.Bluff(23, 0b11110),
|
||||
(true, _, _) when Get0To9() < 1 => Strategy.Bluff(23, 0b11111),
|
||||
(true, _, _) => Strategy.Fold,
|
||||
(false, true, _) => Get0To9() < 2 ? Strategy.Bluff(23) : Strategy.Check,
|
||||
(false, false, true) => Strategy.Bet(35),
|
||||
(false, false, false) => Get0To9() < 1 ? Strategy.Bet(35) : Strategy.Raise
|
||||
};
|
||||
if (table.Computer.Strategy is Bet)
|
||||
{
|
||||
var bet = table.Computer.Strategy.Value + Get0To9();
|
||||
if (table.Computer.Balance - table.Human.Bet - bet < 0)
|
||||
{
|
||||
if (table.Human.Bet == 0)
|
||||
{
|
||||
bet = table.Computer.Balance;
|
||||
}
|
||||
else if (table.Computer.Balance - table.Human.Bet >= 0)
|
||||
{
|
||||
_io.WriteLine("I'll see you.");
|
||||
table.Computer.Bet = table.Human.Bet;
|
||||
table.UpdatePot();
|
||||
}
|
||||
else
|
||||
{
|
||||
table.Computer.RaiseFunds();
|
||||
if (table.Computer.IsBroke) { return; }
|
||||
}
|
||||
}
|
||||
_io.WriteLine($"I'll open with ${bet}");
|
||||
table.Computer.Bet = bet;
|
||||
}
|
||||
else
|
||||
{
|
||||
_io.WriteLine("I check.");
|
||||
}
|
||||
GetWager(table.Computer.Strategy);
|
||||
if (table.SomeoneIsBroke() || table.SomeoneHasFolded()) { return; }
|
||||
|
||||
table.Draw();
|
||||
|
||||
table.Computer.Strategy = (table.Computer.Hand.IsWeak, table.Computer.Hand.Rank < HandRank.Three, table.Computer.Hand.Rank < HandRank.FullHouse) switch
|
||||
{
|
||||
_ when table.Computer.Strategy is Bluff => Strategy.Bluff(28),
|
||||
(true, _, _) => Strategy.Fold,
|
||||
(false, true, _) => Get0To9() == 0 ? Strategy.Bet(19) : Strategy.Raise,
|
||||
(false, false, true) => Get0To9() == 0 ? Strategy.Bet(11) : Strategy.Bet(19),
|
||||
(false, false, false) => Strategy.Raise
|
||||
};
|
||||
|
||||
GetWager(table.Computer.Strategy);
|
||||
if (table.SomeoneIsBroke()) { return; }
|
||||
if (table.Human.HasBet)
|
||||
{
|
||||
if (table.SomeoneHasFolded()) { return; }
|
||||
}
|
||||
else if (table.Computer.Strategy is Bet)
|
||||
{
|
||||
var bet = table.Computer.Strategy.Value + Get0To9();
|
||||
if (table.Computer.Balance - table.Human.Bet - bet < 0)
|
||||
{
|
||||
if (table.Human.Bet == 0)
|
||||
{
|
||||
bet = table.Computer.Balance;
|
||||
}
|
||||
else if (table.Computer.Balance - table.Human.Bet >= 0)
|
||||
{
|
||||
_io.WriteLine("I'll see you.");
|
||||
table.Computer.Bet = table.Human.Bet;
|
||||
table.UpdatePot();
|
||||
}
|
||||
else
|
||||
{
|
||||
table.Computer.RaiseFunds();
|
||||
if (table.Computer.IsBroke) { return; }
|
||||
}
|
||||
}
|
||||
_io.WriteLine($"I'll bet ${bet}");
|
||||
table.Computer.Bet = bet;
|
||||
GetWager(table.Computer.Strategy);
|
||||
if (table.SomeoneIsBroke() || table.SomeoneHasFolded()) { return; }
|
||||
}
|
||||
else
|
||||
{
|
||||
_io.WriteLine("I'll check");
|
||||
}
|
||||
if (table.GetWinner() is { } winner)
|
||||
{
|
||||
winner.TakeWinnings();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void GetWager(Strategy computerStrategy)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
table.Human.HasBet = false;
|
||||
while (true)
|
||||
{
|
||||
var humanStrategy = _io.ReadHumanStrategy(table.Computer.Bet == 0 && table.Human.Bet == 0);
|
||||
if (humanStrategy is Bet or Check)
|
||||
{
|
||||
if (table.Human.Bet + humanStrategy.Value < table.Computer.Bet)
|
||||
{
|
||||
_io.WriteLine("If you can't see my bet, then fold.");
|
||||
continue;
|
||||
}
|
||||
if (table.Human.Balance - table.Human.Bet - humanStrategy.Value >= 0)
|
||||
{
|
||||
table.Human.HasBet = true;
|
||||
table.Human.Bet += humanStrategy.Value;
|
||||
break;
|
||||
}
|
||||
table.Human.RaiseFunds();
|
||||
if (table.Human.IsBroke) { return; }
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
table.Human.Fold();
|
||||
table.UpdatePot();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (table.Human.Bet == table.Computer.Bet)
|
||||
{
|
||||
table.UpdatePot();
|
||||
return;
|
||||
}
|
||||
if (computerStrategy is Fold)
|
||||
{
|
||||
if (table.Human.Bet > 5)
|
||||
{
|
||||
table.Computer.Fold();
|
||||
_io.WriteLine("I fold.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (table.Human.Bet > 3 * computerStrategy.Value)
|
||||
{
|
||||
if (computerStrategy is not Raise)
|
||||
{
|
||||
_io.WriteLine("I'll see you.");
|
||||
table.Computer.Bet = table.Human.Bet;
|
||||
table.UpdatePot();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var raise = table.Human.Bet - table.Computer.Bet + Get0To9();
|
||||
if (table.Computer.Balance - table.Human.Bet - raise < 0)
|
||||
{
|
||||
if (table.Human.Bet == 0)
|
||||
{
|
||||
raise = table.Computer.Balance;
|
||||
}
|
||||
else if (table.Computer.Balance - table.Human.Bet >= 0)
|
||||
{
|
||||
_io.WriteLine("I'll see you.");
|
||||
table.Computer.Bet = table.Human.Bet;
|
||||
table.UpdatePot();
|
||||
}
|
||||
else
|
||||
{
|
||||
table.Computer.RaiseFunds();
|
||||
if (table.Computer.IsBroke) { return; }
|
||||
}
|
||||
}
|
||||
_io.WriteLine($"I'll see you, and raise you {raise}");
|
||||
table.Computer.Bet = table.Human.Bet + raise;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,22 @@ internal class Computer : Player
|
||||
|
||||
public Strategy Strategy { get; set; }
|
||||
|
||||
public override void NewHand()
|
||||
{
|
||||
base.NewHand();
|
||||
|
||||
Strategy = (Hand.IsWeak, Hand.Rank < HandRank.Three, Hand.Rank < HandRank.FullHouse) switch
|
||||
{
|
||||
(true, _, _) when _random.Next(10) < 2 => Strategy.Bluff(23, 0b11100),
|
||||
(true, _, _) when _random.Next(10) < 2 => Strategy.Bluff(23, 0b11110),
|
||||
(true, _, _) when _random.Next(10) < 1 => Strategy.Bluff(23, 0b11111),
|
||||
(true, _, _) => Strategy.Fold,
|
||||
(false, true, _) => _random.Next(10) < 2 ? Strategy.Bluff(23) : Strategy.Check,
|
||||
(false, false, true) => Strategy.Bet(35),
|
||||
(false, false, false) => _random.Next(10) < 1 ? Strategy.Bet(35) : Strategy.Raise
|
||||
};
|
||||
}
|
||||
|
||||
protected override void DrawCards(Deck deck)
|
||||
{
|
||||
var keepMask = Strategy.KeepMask ?? Hand.KeepMask;
|
||||
@@ -38,6 +54,37 @@ internal class Computer : Player
|
||||
{
|
||||
_io.WriteLine("s");
|
||||
}
|
||||
|
||||
Strategy = (Hand.IsWeak, Hand.Rank < HandRank.Three, Hand.Rank < HandRank.FullHouse) switch
|
||||
{
|
||||
_ when Strategy is Bluff => Strategy.Bluff(28),
|
||||
(true, _, _) => Strategy.Fold,
|
||||
(false, true, _) => _random.Next(10) == 0 ? Strategy.Bet(19) : Strategy.Raise,
|
||||
(false, false, true) => _random.Next(10) == 0 ? Strategy.Bet(11) : Strategy.Bet(19),
|
||||
(false, false, false) => Strategy.Raise
|
||||
};
|
||||
}
|
||||
|
||||
public int GetWager(int wager)
|
||||
{
|
||||
wager += _random.Next(10);
|
||||
if (Balance < Table.Human.Bet + wager)
|
||||
{
|
||||
if (Table.Human.Bet == 0) { return Balance; }
|
||||
|
||||
if (Balance >= Table.Human.Bet)
|
||||
{
|
||||
_io.WriteLine("I'll see you.");
|
||||
Bet = Table.Human.Bet;
|
||||
Table.UpdatePot();
|
||||
}
|
||||
else
|
||||
{
|
||||
RaiseFunds();
|
||||
}
|
||||
}
|
||||
|
||||
return wager;
|
||||
}
|
||||
|
||||
public bool TryBuyWatch()
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
using Poker.Cards;
|
||||
using Poker.Players;
|
||||
using Poker.Strategies;
|
||||
|
||||
namespace Poker;
|
||||
|
||||
internal class Table
|
||||
{
|
||||
private readonly IReadWrite _io;
|
||||
private readonly IRandom _random;
|
||||
public int Pot;
|
||||
|
||||
public Table(IReadWrite io, Deck deck, Human human, Computer computer)
|
||||
public Table(IReadWrite io, IRandom random, Deck deck, Human human, Computer computer)
|
||||
{
|
||||
_io = io;
|
||||
_random = random;
|
||||
Deck = deck;
|
||||
Human = human;
|
||||
Computer = computer;
|
||||
@@ -24,7 +27,46 @@ internal class Table
|
||||
public Human Human { get; }
|
||||
public Computer Computer { get; }
|
||||
|
||||
public void Deal(IRandom random)
|
||||
internal void PlayHand()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
_io.WriteLine();
|
||||
Computer.CheckFunds();
|
||||
if (Computer.IsBroke) { return; }
|
||||
|
||||
_io.WriteLine($"The ante is ${Ante}. I will deal:");
|
||||
_io.WriteLine();
|
||||
if (Human.Balance <= Ante)
|
||||
{
|
||||
Human.RaiseFunds();
|
||||
if (Human.IsBroke) { return; }
|
||||
}
|
||||
|
||||
Deal(_random);
|
||||
|
||||
_io.WriteLine();
|
||||
GetWagers("I'll open with ${0}", "I check.", allowRaiseAfterCheck: true);
|
||||
if (SomeoneIsBroke() || SomeoneHasFolded()) { return; }
|
||||
|
||||
Draw();
|
||||
|
||||
GetWagers();
|
||||
if (SomeoneIsBroke()) { return; }
|
||||
if (!Human.HasBet)
|
||||
{
|
||||
GetWagers("I'll bet ${0}", "I'll check");
|
||||
}
|
||||
if (SomeoneIsBroke() || SomeoneHasFolded()) { return; }
|
||||
if (GetWinner() is { } winner)
|
||||
{
|
||||
winner.TakeWinnings();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Deal(IRandom random)
|
||||
{
|
||||
Deck.Shuffle(random);
|
||||
|
||||
@@ -37,7 +79,7 @@ internal class Table
|
||||
_io.Write(Human.Hand);
|
||||
}
|
||||
|
||||
public void Draw()
|
||||
private void Draw()
|
||||
{
|
||||
_io.WriteLine();
|
||||
_io.Write("Now we draw -- ");
|
||||
@@ -46,19 +88,96 @@ internal class Table
|
||||
_io.WriteLine();
|
||||
}
|
||||
|
||||
public void AcceptBets()
|
||||
private void GetWagers(string betFormat, string checkMessage, bool allowRaiseAfterCheck = false)
|
||||
{
|
||||
if (Computer.Strategy is Bet)
|
||||
{
|
||||
Computer.Bet = Computer.GetWager(Computer.Strategy.Value);
|
||||
if (Computer.IsBroke) { return; }
|
||||
|
||||
_io.WriteLine(betFormat, Computer.Bet);
|
||||
}
|
||||
else
|
||||
{
|
||||
_io.WriteLine(checkMessage);
|
||||
if (!allowRaiseAfterCheck) { return; }
|
||||
}
|
||||
|
||||
GetWagers();
|
||||
}
|
||||
|
||||
public void UpdatePot()
|
||||
private void GetWagers()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
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.Bet == Computer.Bet)
|
||||
{
|
||||
UpdatePot();
|
||||
return;
|
||||
}
|
||||
if (Computer.Strategy is Fold)
|
||||
{
|
||||
if (Human.Bet > 5)
|
||||
{
|
||||
Computer.Fold();
|
||||
_io.WriteLine("I fold.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (Human.Bet > 3 * Computer.Strategy.Value)
|
||||
{
|
||||
if (Computer.Strategy is not Raise)
|
||||
{
|
||||
_io.WriteLine("I'll see you.");
|
||||
Computer.Bet = Human.Bet;
|
||||
UpdatePot();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var raise = Computer.GetWager(Human.Bet - Computer.Bet);
|
||||
if (Computer.IsBroke) { return; }
|
||||
_io.WriteLine($"I'll see you, and raise you {raise}");
|
||||
Computer.Bet = Human.Bet + raise;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePot()
|
||||
{
|
||||
Human.Balance -= Human.Bet;
|
||||
Computer.Balance -= Computer.Bet;
|
||||
Pot += Human.Bet + Computer.Bet;
|
||||
}
|
||||
|
||||
public bool SomeoneHasFolded()
|
||||
private bool SomeoneHasFolded()
|
||||
{
|
||||
if (Human.HasFolded)
|
||||
{
|
||||
@@ -79,9 +198,9 @@ internal class Table
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool SomeoneIsBroke() => Human.IsBroke || Computer.IsBroke;
|
||||
private bool SomeoneIsBroke() => Human.IsBroke || Computer.IsBroke;
|
||||
|
||||
public Player? GetWinner()
|
||||
private Player? GetWinner()
|
||||
{
|
||||
_io.WriteLine();
|
||||
_io.WriteLine("Now we compare hands:");
|
||||
|
||||
Reference in New Issue
Block a user