public void Validate(PlayedCards activeCards) { if (activeCards == null || activeCards[0] == null || this[0] == null) { return; } if (activeCards.Count != this.Count) { throw new InvalidHandException(this, "You must play the same number of cards."); } if (this.Count == 1) { // Validate single card hand var nextCard = this[0]; var activeCard = activeCards[0]; if (nextCard.GameValue < activeCard.GameValue) { throw new InvalidHandException(this, "Cannot play a card of lesser value."); } if (nextCard.GameValue == activeCard.GameValue && nextCard.Suit < activeCard.Suit) { throw new InvalidHandException(this, "Cannot play an equal value card of a lesser suit."); } } // todo: validate poker style hands }
public void Start() { GameStarted.Raise(this, new BigTwoEventArgs()); DealHands(); var activePlayerIndex = 0; PlayedCards activeCards = null; // while all players have at least one card the game continues while (players.All(p => p.CardCount > 0)) { IPlayer activePlayer = players[activePlayerIndex]; PlayerTurnStart.Raise(this, new BigTwoPlayerEventArgs(activePlayer)); // If the active cards belong to the active player, It means that // all other players have passed meaning this player has won the sequence. if (activeCards != null && activePlayer == activeCards.Player) { // clear the active card to start a new sequence. SequenceCompleted.Raise(this, new BigTwoPlayerEventArgs(activeCards.Player)); activeCards = null; } PlayedCards nextCards = activePlayer.PlayTurn(activeCards); PlayerPlayedTurn.Raise(this, new BigTwoPlayerHandEventArgs(activePlayer, nextCards)); // null is a passed turn if (nextCards != null) { // Ensure the cards the player is trying to play are valid nextCards.Validate(activeCards); activeCards = nextCards; // Remove the played cards from the players hand activePlayer.RemoveCards(nextCards); } // Advance to next player activePlayerIndex++; if (activePlayerIndex >= players.Count) { activePlayerIndex = 0; } PlayerTurnEnd.Raise(this, new BigTwoPlayerEventArgs(activePlayer)); } // Get the winner and notify everyone that the game is over! IPlayer winner = players.Single(p => p.CardCount == 0); GameCompleted.Raise(this, new BigTwoPlayerEventArgs(winner)); }
public BigTwoPlayerHandEventArgs(IPlayer activePlayer, PlayedCards nextCards) : base(activePlayer) { PlayedCards = nextCards; }