mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-10 04:07:28 -08:00
Reorganise classes
This commit is contained in:
95
71_Poker/csharp/Players/Computer.cs
Normal file
95
71_Poker/csharp/Players/Computer.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using Poker.Cards;
|
||||
using static System.StringComparison;
|
||||
|
||||
namespace Poker.Players;
|
||||
|
||||
internal class Computer : Player
|
||||
{
|
||||
private readonly IReadWrite _io;
|
||||
private readonly IRandom _random;
|
||||
private bool _isBluffing;
|
||||
|
||||
public Computer(int bank, IReadWrite io, IRandom random)
|
||||
: base(bank)
|
||||
{
|
||||
_io = io;
|
||||
_random = random;
|
||||
}
|
||||
|
||||
public bool IsBluffing => _isBluffing;
|
||||
|
||||
public override void NewHand(Hand hand)
|
||||
{
|
||||
base.NewHand(hand);
|
||||
_isBluffing = false;
|
||||
}
|
||||
|
||||
public int? BluffIf(bool shouldBluff, int? keepMask = null)
|
||||
{
|
||||
if (!shouldBluff) { return null; }
|
||||
|
||||
_isBluffing = true;
|
||||
Hand.KeepMask = keepMask ?? Hand.KeepMask;
|
||||
return 23;
|
||||
}
|
||||
|
||||
public void DrawCards(Deck deck)
|
||||
{
|
||||
var keepMask = Hand.KeepMask;
|
||||
var count = 0;
|
||||
for (var i = 1; i <= 5; i++)
|
||||
{
|
||||
if ((keepMask & (1 << (i - 1))) == 0)
|
||||
{
|
||||
Hand = Hand.Replace(i, deck.DealCard());
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
_io.WriteLine();
|
||||
_io.Write($"I am taking {count} card");
|
||||
if (count != 1)
|
||||
{
|
||||
_io.WriteLine("s");
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryBuyWatch(Human human)
|
||||
{
|
||||
if (!human.HasWatch) { return false; }
|
||||
|
||||
var response = _io.ReadString("Would you like to sell your watch");
|
||||
if (response.StartsWith("N", InvariantCultureIgnoreCase)) { return false; }
|
||||
|
||||
var (value, message) = (_random.Next(10) < 7) switch
|
||||
{
|
||||
true => (75, "I'll give you $75 for it."),
|
||||
false => (25, "That's a pretty crummy watch - I'll give you $25.")
|
||||
};
|
||||
|
||||
_io.WriteLine(message);
|
||||
human.SellWatch(value);
|
||||
// The original code does not have the computer part with any money
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TrySellWatch(Human human)
|
||||
{
|
||||
if (human.HasWatch) { return false; }
|
||||
|
||||
var response = _io.ReadString("Would you like to buy back your watch for $50");
|
||||
if (response.StartsWith("N", InvariantCultureIgnoreCase)) { return false; }
|
||||
|
||||
// The original code does not deduct $50 from the player
|
||||
Balance += 50;
|
||||
human.ReceiveWatch();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void TakeWinnings()
|
||||
{
|
||||
_io.WriteLine("I win.");
|
||||
base.TakeWinnings();
|
||||
}
|
||||
}
|
||||
64
71_Poker/csharp/Players/Human.cs
Normal file
64
71_Poker/csharp/Players/Human.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Poker.Cards;
|
||||
|
||||
namespace Poker.Players;
|
||||
|
||||
internal class Human : Player
|
||||
{
|
||||
private readonly IReadWrite _io;
|
||||
|
||||
public Human(int bank, IReadWrite io)
|
||||
: base(bank)
|
||||
{
|
||||
HasWatch = true;
|
||||
_io = io;
|
||||
}
|
||||
|
||||
public bool HasWatch { get; set; }
|
||||
|
||||
public 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; }
|
||||
|
||||
_io.WriteLine("What are their numbers:");
|
||||
for (var i = 1; i <= count; i++)
|
||||
{
|
||||
Hand = Hand.Replace((int)_io.ReadNumber(), deck.DealCard());
|
||||
}
|
||||
|
||||
_io.WriteLine("Your new hand:");
|
||||
_io.Write(Hand);
|
||||
}
|
||||
|
||||
public bool IsBroke()
|
||||
{
|
||||
_io.WriteLine();
|
||||
_io.WriteLine("You can't bet with what you haven't got.");
|
||||
|
||||
if (Table.Computer.TryBuyWatch(this)) { return false; }
|
||||
|
||||
// The original program had some code about selling a tie tack, but due to a fault
|
||||
// in the logic the code was unreachable. I've omitted it in this port.
|
||||
|
||||
_io.WriteLine("Your wad is shot. So long, sucker!");
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ReceiveWatch()
|
||||
{
|
||||
// In the original code the player does not pay any money to receive the watch back.
|
||||
HasWatch = true;
|
||||
}
|
||||
|
||||
public void SellWatch(int amount)
|
||||
{
|
||||
HasWatch = false;
|
||||
Balance += amount;
|
||||
}
|
||||
|
||||
public override void TakeWinnings()
|
||||
{
|
||||
_io.WriteLine("You win.");
|
||||
base.TakeWinnings();
|
||||
}
|
||||
}
|
||||
47
71_Poker/csharp/Players/Player.cs
Normal file
47
71_Poker/csharp/Players/Player.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Poker.Cards;
|
||||
|
||||
namespace Poker.Players;
|
||||
|
||||
internal abstract class Player
|
||||
{
|
||||
private Table? _table;
|
||||
private bool _hasFolded;
|
||||
|
||||
protected Player(int bank)
|
||||
{
|
||||
Hand = Hand.Empty;
|
||||
Balance = bank;
|
||||
}
|
||||
|
||||
public Hand Hand { get; set; }
|
||||
public int Balance { get; set; }
|
||||
public int Bet { get; private set; }
|
||||
public bool HasFolded => _hasFolded;
|
||||
|
||||
protected Table Table =>
|
||||
_table ?? throw new InvalidOperationException("The player must be sitting at the table.");
|
||||
|
||||
public void Sit(Table table) => _table = table;
|
||||
|
||||
public virtual void NewHand(Hand hand)
|
||||
{
|
||||
Hand = hand;
|
||||
_hasFolded = false;
|
||||
}
|
||||
|
||||
public void Pay(int amount)
|
||||
{
|
||||
Balance -= amount;
|
||||
}
|
||||
|
||||
public virtual void TakeWinnings()
|
||||
{
|
||||
Balance += Table.Pot;
|
||||
Table.Pot = 0;
|
||||
}
|
||||
|
||||
public void Fold()
|
||||
{
|
||||
_hasFolded = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user