I'm implementing an event-driven game in WPF and have the following Blackjack class:
public class Blackjack
{
public Dealer Dealer { get; } = new Dealer();
public Player Player { get; } = new Player();
public IList<Player> AiPlayers { get; } = new List<Player>();
public event EventHandler<EventArgs>? NewGameStarted;
public event EventHandler<EventArgs>? HandStarted;
public event EventHandler<GameOverEventArgs>? HandEnded;
public Blackjack()
{
Dealer.Hand.Busted += DealerBusted;
Player.BetPlaced += BetPlaced;
Player.PlayerHit += PlayerHit;
Player.PlayerStand += PlayerStand;
Player.Hand.Blackjack += PlayerBlackjack;
Player.Hand.Busted += PlayerBusted;
_gameStates[Player] = GameStates.PLAYING;
}
public void StartNewGame(int numAiPlayers)
{
for (var i = 0; i < numAiPlayers; i++)
{
var ai = new Player();
AiPlayers.Add(ai);
ai.PlayerHit += PlayerHit;
ai.PlayerStand += PlayerStand;
ai.Hand.Blackjack += PlayerBlackjack;
ai.Hand.Busted += PlayerBusted;
_gameStates[ai] = GameStates.PLAYING;
}
OnNewGameStarted(new EventArgs());
}
// More methods
}
But as you can see, the player/ai creation is starting to pollute my constructor and StartNewGame
method. Additionally, I want to be able to use different AIs to simulate different strategies.
I thought about moving this to a factory, but realized I would be subscribing to the events from Blackjack
inside the factory. This feels odd to me... Is this a normal thing to do or is it best to just create methods in my Blackjack
class that handle player/ai creation and wiring up the events.