Belot.PlayingManager.IsValid C# (CSharp) Метод

IsValid() публичный Метод

If card played by this player is valid according to game rules
public IsValid ( Player player, Card card ) : bool
player Player
card Card
Результат bool
        public bool IsValid( Player player, Card card )
        {
            if( player == null)
                throw new ArgumentNullException( "Player", "Player cannot be null");

            if(	card == null)
                throw new ArgumentNullException( "Card", "Played card cannot be null");

            CardColor wantedColor;
            Card biggestCard;

            // if player plays first card in hand he can play any card
            if( _currentHand.IsEmpty )
            {
                return true;
            }
            else
            {
                wantedColor = GetPlayingColor( _currentHand );
                biggestCard = GetBiggestCard( _currentHand );
            }

            if( _currentAnnouncement.Type == AnnouncementTypeEnum.AllTrumps )
            {
                #region Game of all trumps
                if( card.CardColor == wantedColor )
                {
                    if( IsBigger( card, biggestCard ) )
                    {
                        // card is biggest for now in wanted color
                        return true;
                    }
                    else
                    {
                        // card is not biggest but player has no bigger one
                        return !HasBigger( biggestCard, player.Cards, wantedColor );
                    }
                }
                else if( !HasPlayingColor( wantedColor, player.Cards ) )
                {
                    // if player does not have currently wanted color he can play any card
                    return true;
                }
                #endregion
            }
            else if( _currentAnnouncement.Type == AnnouncementTypeEnum.NoTrumps )
            {
                #region Game of no trumps
                if( card.CardColor == wantedColor )
                {
                    //player may play any card in wanted color
                    return true;
                }
                else
                {
                    // player does not have currently wanted color he can play any card
                    return ( !HasPlayingColor( wantedColor, player.Cards ) );
                }
                #endregion
            }
            else
            {
                #region Game of color trumps
                CardColor trumpColor = GetTrumpColor( );

                if( card.CardColor == wantedColor )
                {
                    if( trumpColor == wantedColor )
                    {
                        if( IsBigger( card, biggestCard ) )
                        {
                            // card is biggest for now
                            return true;
                        }
                        else
                        {
                            // card is not biggest but player has no bigger one
                            return !HasBigger( biggestCard, player.Cards, wantedColor );
                        }
                    }
                    else
                    {
                        // card is not trump and may not be bigger
                        return true;
                    }
                }
                else
                {
                    if( HasPlayingColor( wantedColor, player.Cards ) )
                    {
                        // player has cards in wanted color but plays from different one
                        return false;
                    }
                    else
                    {
                        if( trumpColor == wantedColor )
                        {
                            // if player does not have currently wanted color or trump color he can play any card
                            return true;
                        }
                        else
                        {
                            biggestCard = GetBiggestCard( _currentHand, trumpColor );

                            Player biggestCardOwner = _cardToPlayerMap[biggestCard] as Player;

                            if( biggestCardOwner == _game.GetTeamPlayer( player ) )
                            {
                                //if hand is held by partner player may not give trump
                                return true;
                            }
                            else
                            {
                                // player must give bigger trump
                                if( !HasPlayingColor( trumpColor, player.Cards ) )
                                {
                                    // player does not have trumps
                                    return true;
                                }
                                else
                                {
                                    if ( biggestCard.CardColor != trumpColor )
                                    {
                                        // biggest card for now is not a trump
                                        // so player must play a trump
                                        return ( card.CardColor == trumpColor );
                                    }
                                    else
                                    {
                                        if ( IsBigger( card, biggestCard ) )
                                        {
                                            // card is biggest for now
                                            return true;
                                        }
                                        else
                                        {
                                            // card is not biggest but player has no bigger one
                                            return !HasBigger( biggestCard, player.Cards, trumpColor );
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion
            }

            return false;
        }

Usage Example

Пример #1
0
        private void PlayerHasTurn(Player player)
        {
            #region Announce combinations on first hand

            if (_playingManager.IsFirstHand && _currentAnnouncement.Type != AnnouncementTypeEnum.NoTrumps)
            {
                CombinationFinder finder = new CombinationFinder(player.Cards);
                foreach (CardCombination combination in finder.Combinations)
                {
                    bool isValidCombination = false;

                    if (combination is SequentialCombination)
                    {
                        Player          biggest            = FindBiggestSequentialCombinationHolder();
                        CardCombination biggestCombination = null;

                        foreach (DictionaryEntry de in _mapSequentialCombinationToPlayer)
                        {
                            if (biggest == de.Value as Player)
                            {
                                biggestCombination = de.Key as CardCombination;
                                break;
                            }
                        }

                        if ((biggest == null) ||
                            (combination.Points >= biggestCombination.Points) ||
                            (player.Position == biggest.Position || player.Position == _game.GetTeamPlayer(biggest).Position))
                        {
                            isValidCombination = true;
                        }
                    }
                    else
                    {
                        isValidCombination = true;
                    }

                    if (isValidCombination && player.AnnounceCombination(combination))
                    {
                        if (combination is SequentialCombination)
                        {
                            _mapSequentialCombinationToPlayer.Add(combination, player);
                        }
                        if (combination is FourEqualsCombination)
                        {
                            _mapEqualCombinationToPlayer.Add(combination, player);
                        }
                    }
                }
            }

            #endregion

            foreach (Card card in player.Cards)
            {
                card.IsSelectable = _playingManager.IsValid(player, card);
            }

            player.PlayCard(_playingManager);
        }