54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using OECS;
|
|
|
|
namespace Blackjack;
|
|
|
|
/// <summary>
|
|
/// Dealer draws cards until reaching 17 or higher,
|
|
/// then resolves the round result.
|
|
/// </summary>
|
|
public class DealerSystem : ISystem
|
|
{
|
|
public void Run(World world)
|
|
{
|
|
var state = world.ReadSingleton<GameState>();
|
|
if (state.Phase != GamePhase.DealerTurn)
|
|
return;
|
|
|
|
ref var mutableState = ref world.GetSingleton<GameState>();
|
|
|
|
// Dealer must hit on 16 and below, stand on 17+.
|
|
int dealerTotal = HandUtil.CalculateHand(world, DealerHandTag.Instance);
|
|
while (dealerTotal < 17)
|
|
{
|
|
HitCommand.DrawCard<DealerHand>(world);
|
|
dealerTotal = HandUtil.CalculateHand(world, DealerHandTag.Instance);
|
|
}
|
|
|
|
int playerTotal = HandUtil.CalculateHand(world, PlayerHandTag.Instance);
|
|
|
|
mutableState.Phase = GamePhase.RoundOver;
|
|
|
|
if (dealerTotal > 21)
|
|
{
|
|
mutableState.Result = RoundResult.DealerBust;
|
|
mutableState.Chips += mutableState.CurrentBet * 2;
|
|
}
|
|
else if (playerTotal > dealerTotal)
|
|
{
|
|
mutableState.Result = RoundResult.PlayerWin;
|
|
mutableState.Chips += mutableState.CurrentBet * 2;
|
|
}
|
|
else if (dealerTotal > playerTotal)
|
|
{
|
|
mutableState.Result = RoundResult.DealerWin;
|
|
}
|
|
else
|
|
{
|
|
// Push: return the bet.
|
|
mutableState.Result = RoundResult.Push;
|
|
mutableState.Chips += mutableState.CurrentBet;
|
|
}
|
|
|
|
world.MarkModified<GameState>(World.SingletonEntity);
|
|
}
|
|
} |