Esempio n. 1
0
 private void CleanCurrentTrick()
 {
     CurrentTrick.Remove(PlayerPosition.North);
     CurrentTrick.Remove(PlayerPosition.South);
     CurrentTrick.Remove(PlayerPosition.East);
     CurrentTrick.Remove(PlayerPosition.West);
 }
Esempio n. 2
0
        public void CardWithdrawn(string withdrawingUser)
        {
            if (!CurrentTrick.ContainsKey(withdrawingUser))
            {
                throw new System.InvalidOperationException("there is no card to withdraw");
            }
            var card = CurrentTrick[withdrawingUser];

            PlayerStates[withdrawingUser].Hand.Add(new Card(card));
            CurrentTrick.Remove(withdrawingUser);
        }
Esempio n. 3
0
        public void CardPlayed(string playingUser, Card c)
        {
            if (CurrentTrick.ContainsKey(playingUser))
            {
                throw new System.InvalidOperationException("another card had already been played");
            }

            CurrentTrick.Add(playingUser, new Card(c));

            try
            {
                var cardToRemove = PlayerStates[playingUser].Hand.First(x => x.cardColor == c.cardColor && x.cardType == c.cardType);
                PlayerStates[playingUser].Hand.Remove(cardToRemove);
            }
            catch
            {
                throw new System.Exception("could not remove card although it was played");
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Place the specified card by the specified player in the current trick.
        /// And evaluate the trick to determine the winner
        /// </summary>
        public void PlaceCard(Player player, Card card)
        {
            PlayerPosition position = gameSession.GetPlayerPosition(player);

            if (position == CurrentTurn)
            {
                Card tempCard;
                if (!CurrentTrick.TryGetValue(position, out tempCard))
                {
                    if (ValidatePlay(position, card))
                    {
                        CurrentTrick[position] = card;
                        PlayerCards[position].Remove(card);
                        if (CurrentTrick.Values.Count == 1)
                        {
                            //First card in the trick
                            CurrentTrickBaseSuit = card.Suit;
                        }

                        if (CurrentTrick.Values.Count >= 4)
                        {
                            //Trick is complete -> evalueate
                            PlayerPosition trickWinner = EvaluateCurrentTrick();
                            CurrentTurn = trickWinner;

                            switch (trickWinner)
                            {
                            case PlayerPosition.North:
                            case PlayerPosition.South:
                                TricksWon[TeamPosition.NorthSouth]++;
                                break;

                            case PlayerPosition.East:
                            case PlayerPosition.West:
                                TricksWon[TeamPosition.EastWest]++;
                                break;
                            }
                            CleanCurrentTrick();
                            currentNumberOfTricks++;

                            if (IsComplete)
                            {
                                gameSession.GameComplete();
                            }
                        }
                        else
                        {
                            NextTurn();
                        }
                    }
                    else
                    {
                        throw new ArgumentException("The played card is invalid");
                    }
                }
                else
                {
                    throw new InvalidOperationException("The player already placed card for the current trick");
                }
            }
            else
            {
                throw new InvalidOperationException("The player has played not in the right turn");
            }
        }
Esempio n. 5
0
        //player unique identifies and validates the entity making the play
        //if it is dummy, it must be rejected.  If it is Declarer, then it
        //is valid when it is dummy or declareres turn.
        public void PlayCard(IPlayer player, Card card)
        {
            lock (_tableLock)
            {
                if (!_seats.ContainsKey(player))
                {
                    throw new PlayException("Player is not seated at the table.");
                }

                if (Contract == null || CurrentTrick == null)
                {
                    throw new PlayException("You cannot play a card until bidding is done.");
                }

                if (_seats[player] == Dummy)
                {
                    throw new PlayException("Dummy is not allowed to play his cards.");
                }

                bool correctPlayer = player == _players[HotSeat] ||
                                     (HotSeat == Dummy && player == _players[Declarer]);
                if (!correctPlayer)
                {
                    throw new PlayException("It is not your turn to play a card.");
                }

                //Make sure I get Dummies hand when appropriate
                Hand hand = _hands[_players[HotSeat]];
                if (hand.Count == 0)
                {
                    throw new PlayException("You have no cards to play.");
                }

                if (!hand.Contains(card))
                {
                    throw new PlayException("The " + card + " is not yours to play.");
                }

                if (!CurrentTrick.IsLegalPlay(card, hand))
                {
                    if (CurrentTrick.Done)
                    {
                        throw new PlayException("The current trick is finished.");
                    }
                    throw new PlayException(CurrentTrick.Suit + " were lead. You can and must follow suit.");
                }

                //We have a legal card from the correct player
                hand.Remove(card);
                CurrentTrick.AddCard(card, HotSeat);
                //TODO Resharper says Table is redundant
                OnCardHasBeenPlayed(new Table.CardHasBeenPlayedEventArgs(HotSeat, card));
                if (IsOpeningLead())
                {
                    OnDummyHasExposedHand(new Table.DummyHasExposedHandEventArgs(DummiesCards));
                }
                if (CurrentTrick.Done)
                {
                    OnTrickHasBeenWon(new Table.TrickHasBeenWonEventArgs(CurrentTrick.Winner, CurrentTrick));
                    if (_tricks.Count == 13)
                    {
                        FinishDeal();
                    }
                    else
                    {
                        // Law 44 - "The player who has won the trick leads to the next trick."
                        HotSeat = CurrentTrick.Winner;
                        _tricks.Add(new Trick(Trump));
                    }
                }
                else
                {
                    HotSeat = NextSeat(HotSeat);
                }
            }
        }