Exemplo n.º 1
0
        private void OnRoundClosed(Round round)
        {
            var winningCard = round.GetWinningCard();
            var winningPlayer = _cardsPlayers[winningCard];
            var bid = _bidPlayerTeam.First;
            var biddingTeam = _bidPlayerTeam.Third;
            var nonBiddingTeam = GetNoneBiddingTeam(_bidPlayerTeam.Second.Name);

            winningPlayer.Score++;
            _round = null;
            _eventHandlerList[RoundEndedConstant].SafelyInvoke(this, new CardPlayerArgs
                                                                         {
                                                                             Card = winningCard,
                                                                             Player = winningPlayer
                                                                         });

            if (_cardsPlayers.Keys.Any(c => !c.IsPlayed))
                return;

            if (bid.IsBidSatisfied(biddingTeam.PlayersScore))
                biddingTeam.TeamScore += _isDouble
                                             ? biddingTeam.PlayersScore * 2
                                             : biddingTeam.PlayersScore;
            else
            {
                if (!_isDouble)
                {
                    biddingTeam.TeamScore -= bid.TricksRequired;
                    nonBiddingTeam.TeamScore += nonBiddingTeam.PlayersScore;
                }
                else if (_isDouble && nonBiddingTeam.PlayersScore >= bid.TricksRequired)
                {
                    biddingTeam.TeamScore -= bid.TricksRequired * 2;
                    nonBiddingTeam.TeamScore += nonBiddingTeam.PlayersScore * 2;
                }
                else
                {
                    // TODO: buisness logic, asking for up and down score calculation
                    return;
                }
            }

            var scoreArgs = new ScoreArgs
                                {
                                    Teams = _teams
                                };

            if (IsGameScoreLimitReached(biddingTeam) || IsGameScoreLimitReached(nonBiddingTeam))
            {
                _eventHandlerList[GameEndedConstant].SafelyInvoke(this, scoreArgs);
            }
            else
            {
                _eventHandlerList[RoundsEndedConstant].SafelyInvoke(this, scoreArgs);
                SetupGame();
            }
        }
Exemplo n.º 2
0
        private void SetupGame()
        {
            _cardsPlayers.Clear();
            _bidPlayerTeam = null;
            _round = null;
            _biddingPlayerIndex = 0;
            _isDouble = false;
            _players.ForEach(p => p.Score = 0);
            var playerIndex = 0;
            var cardsShuffler = new CardsShuffler();
            var deck = cardsShuffler.GetShuffledDeck();

            for (var i = 0; i < deck.Count; i++)
            {
                if (i != 0 && i % 13 == 0)
                    playerIndex++;
                _cardsPlayers.Add(deck[i], _players[playerIndex]);
            }

            _eventHandlerList[GameSetupCompletedConstant].SafelyInvoke(this,
                                                                       new GameSetupCompletedEventArgs { CardsPlayers = _cardsPlayers });
            _eventHandlerList[BiddingStartedConstant].SafelyInvoke(this,
                                                                   new BidEventArgs { NextCaller = _players[_biddingPlayerIndex] });
        }
Exemplo n.º 3
0
        public void PlayCard(Card card)
        {
            if (card.IsPlayed)
                throw new InvalidOperationException("This card has been player before.");

            if (_round == null)
            {
                _round = new Round { BaseSuit = card.Suit };
                _round.RoundClosed += OnRoundClosed;
            }

            card.IsPlayed = true;
            var player = _cardsPlayers[card];
            _eventHandlerList[CardPlayedConstant].SafelyInvoke(this, new CardPlayerArgs
                                                                         {
                                                                             Card = card,
                                                                             Player = player,
                                                                             NextPlayer =
                                                                                 _players[
                                                                                     (_players.IndexOf(player) + 1) % 4]
                                                                         });
            _round.PlayCard(card);
        }