Compares two cards according to a given announcement.
This is needed for sorting purposes. For example Jack is bigger than Ace in game of All Trumps but in game of No Trumps is smaller.
상속: IComparer
예제 #1
0
        /// <summary>
        /// Compares current combination to another one
        /// </summary>
        /// <param name="combination">combination to compare to</param>
        /// <returns>1 if current combination is bigger, -1 if second combination is bigger, 0 if both combination are equal</returns>
        public override int CompareTo(object combination)
        {
            if (!(combination is SequentialCombination))
            {
                throw new InvalidOperationException("Cannot compare SequentialCombination to an object of different type");
            }

            int result = 0;

            SequentialCombination comb = combination as SequentialCombination;

            if (this.Points > comb.Points)
            {
                result = 1;
            }
            else if (this.Points < comb.Points)
            {
                result = -1;
            }
            else
            {
                // both combinations are sequential. See biggest card
                CardComparer comparer = new CardComparer( );
                this.Cards.Sort(comparer);
                comb.Cards.Sort(comparer);

                result = comparer.Compare(this.Cards[0], comb.Cards[0]);
            }
            return(result);
        }
        /// <summary>
        /// Compares current combination to another one
        /// </summary>
        /// <param name="combination">combination to compare to</param>
        /// <returns>1 if current combination is bigger, -1 if second combination is bigger, 0 if both combination are equal</returns>
        public override int CompareTo( object combination )
        {
            if( !(combination is SequentialCombination) )
            {
                throw new InvalidOperationException( "Cannot compare SequentialCombination to an object of different type" );
            }

            int result = 0;

            SequentialCombination comb = combination as SequentialCombination;
            if( this.Points > comb.Points )
            {
                result = 1;
            }
            else if( this.Points < comb.Points )
            {
                result = -1;
            }
            else
            {
                // both combinations are sequential. See biggest card
                CardComparer comparer = new CardComparer( );
                this.Cards.Sort( comparer );
                comb.Cards.Sort( comparer );

                result = comparer.Compare( this.Cards[0], comb.Cards[0] );
            }
            return result;
        }
예제 #3
0
        private void FindSequentialForColor(CardsCollection cards)
        {
            CardComparer comparer = new CardComparer( );

            cards.Sort(comparer);               // we have cards sorted like A,K,Q,J,10,9,8,7

            CardsCollection foundCards = new CardsCollection();

            for (int i = 0; i < cards.Count - 1; i++)
            {
                if (IsConsequent(cards[i], cards[i + 1]))
                {
                    if (foundCards.IsEmpty)
                    {
                        foundCards.Add(cards[i]);
                    }

                    foundCards.Add(cards[i + 1]);
                }
                else
                {
                    if (!foundCards.IsEmpty)
                    {
                        AddSequentialCombination(foundCards);
                        foundCards = new CardsCollection();
                    }
                }
            }

            if (!foundCards.IsEmpty)
            {
                AddSequentialCombination(foundCards);
            }
        }
예제 #4
0
        /// <summary>
        /// Constructor for the class
        /// </summary>
        internal PlayingManager(Announcement current, BelotGame game, CardsCollection _allCards)
        {
            _game                = game;
            _playedHands         = new List <Hand>();
            _cardToPlayerMap     = new Dictionary <Card, Player>();
            _currentAnnouncement = current;
            _currentHand         = new Hand();
            _isHandClosed        = false;
            _cardComparer        = new CardComparer(current.Type);

            _playedCards    = new CardsCollection();
            _remainingCards = new CardsCollection();

            foreach (Card card in _allCards)
            {
                _remainingCards.Add(card);
            }
        }
예제 #5
0
        /// <summary>
        /// Constructor for the class
        /// </summary>
        internal PlayingManager( Announcement current, BelotGame game, CardsCollection _allCards )
        {
            _game = game;
            _playedHands = new List< Hand >();
            _cardToPlayerMap = new Dictionary<Card, Player>();
            _currentAnnouncement = current;
            _currentHand = new Hand();
            _isHandClosed = false;
            _cardComparer = new CardComparer( current.Type );

            _playedCards = new CardsCollection();
            _remainingCards = new CardsCollection();

            foreach( Card card in _allCards )
            {
                _remainingCards.Add( card );
            }
        }
예제 #6
0
        private void DealCards(int count)
        {
            System.Random rand = new Random(DateTime.Now.Millisecond * DateTime.Now.Second);

            Player current = this._firstPlayer;

            for (int i = count; i > 0; i--)
            {
                int index = rand.Next(i);

                current.Cards.Add(_cards[index]);
                current = this._game.GetNextPlayer(current);

                _cards.RemoveAt(index);
            }

            CardComparer comparer = new CardComparer(this._currentAnnouncement.Type);

            this._game.GetPlayer(PlayerPosition.South).Cards.Sort(comparer);
            this._game.GetPlayer(PlayerPosition.East).Cards.Sort(comparer);
            this._game.GetPlayer(PlayerPosition.North).Cards.Sort(comparer);
            this._game.GetPlayer(PlayerPosition.West).Cards.Sort(comparer);
        }
예제 #7
0
        private bool IsCurrentMaxCardInPlayingColor( Card targetCard )
        {
            CardComparer comparer = new CardComparer( _playingManager.CurrentAnnouncement.Type );
            bool foundBigger = false;

            foreach ( Card remCard in _playingManager.RemainingCards )
            {
                if ( targetCard.CardColor == remCard.CardColor )
                {
                    if ( comparer.Compare( targetCard, remCard ) < 0 )
                    {
                        foundBigger = true;
                        break;
                    }
                }
            }

            if ( !foundBigger )
            {
                foreach ( Card remCard in _playingManager.CurrentHand )
                {
                    if ( targetCard.CardColor == remCard.CardColor )
                    {
                        if ( comparer.Compare( targetCard, remCard ) < 0 )
                        {
                            foundBigger = true;
                            break;
                        }
                    }
                }
            }

            return !foundBigger;
        }
예제 #8
0
        private Card GetCurrentMaxCardInColor( CardColor color )
        {
            Card maxCard = null;
            CardComparer comparer = new CardComparer( _playingManager.CurrentAnnouncement.Type );

            foreach ( Card card in _playingManager.RemainingCards )
            {
                if ( card.CardColor == color )
                {
                    if ( maxCard == null )
                    {
                        maxCard = card;
                    }

                    if ( comparer.Compare( maxCard, card ) < 0 )
                    {
                        maxCard = card;
                    }
                }
            }

            foreach ( Card card in _playingManager.CurrentHand )
            {
                if ( card.CardColor == color )
                {
                    if ( maxCard == null )
                    {
                        maxCard = card;
                    }

                    if ( comparer.Compare( maxCard, card ) < 0 )
                    {
                        maxCard = card;
                    }
                }
            }

            return maxCard;
        }
        private void FindSequentialForColor( CardsCollection cards )
        {
            CardComparer comparer = new CardComparer( );
            cards.Sort( comparer ); // we have cards sorted like A,K,Q,J,10,9,8,7

            CardsCollection foundCards = new CardsCollection();

            for( int i = 0; i < cards.Count-1; i++ )
            {
                if( IsConsequent( cards[i], cards[i+1] ) )
                {
                    if( foundCards.IsEmpty )
                    {
                        foundCards.Add ( cards[i] );
                    }

                    foundCards.Add ( cards[i+1] );
                }
                else
                {
                    if( !foundCards.IsEmpty )
                    {

                        AddSequentialCombination( foundCards );
                        foundCards = new CardsCollection();
                    }
                }
            }

            if( !foundCards.IsEmpty )
            {
                AddSequentialCombination( foundCards );
            }
        }
예제 #10
0
        private void DealCards( int count )
        {
            System.Random rand = new Random( DateTime.Now.Millisecond * DateTime.Now.Second );

            Player current = this._firstPlayer;

            for( int i = count; i > 0; i-- )
            {
                int index = rand.Next( i );

                current.Cards.Add( _cards[index] );
                current = this._game.GetNextPlayer( current );

                _cards.RemoveAt( index );
            }

            CardComparer comparer = new CardComparer( this._currentAnnouncement.Type );

            this._game.GetPlayer( PlayerPosition.South ).Cards.Sort( comparer );
            this._game.GetPlayer( PlayerPosition.East ).Cards.Sort( comparer );
            this._game.GetPlayer( PlayerPosition.North ).Cards.Sort( comparer );
            this._game.GetPlayer( PlayerPosition.West ).Cards.Sort( comparer );
        }
예제 #11
0
 internal void Sort(CardComparer comparer)
 {
     this.InnerList.Sort(comparer);
     this.InnerList.Reverse();
     RaiseChanged( );
 }
예제 #12
0
 internal void Sort( CardComparer comparer )
 {
     this.InnerList.Sort( comparer );
     this.InnerList.Reverse();
     RaiseChanged( );
 }