Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can you please tell me what the codes bellow mean in Card class of the solitaire game:


C++
deck.pile.remove (this);
    deck = value;
    deck.add(this); 
  
    if (DeckChanged != null)
        DeckChanged(this, null);
}


Here is the deck class

public class Card
   {
       public static boolean IsAceBiggest = true;

      private char rank;
       private String suit;
        public String Color;

        public String NumberString()
       {
               switch (rank)
               {
                   case 'A':
                       return "Ace";

                   case 'J':
                       return "Jack";

                   case 'Q':
                       return "Queen";

                   case 'K':
                       return "King";


                   default:
                       return ""+rank;
               }
           }

       private DeckPile deck;
       public DeckPile Deck()
       {
               if (deck.Game != value.Game)
                   throw new InvalidOperationException("The new deck must be in the same game like the old deck of the card.");

               if (deck != value)
               {
                   deck.pile.remove(this);
                   deck = value;
                   deck.add(this);

                   if (DeckChanged != null)
                       DeckChanged(this, null);
               }
       }

       private boolean visible = true;
       public boolean Visible
       {
           get
           {
               return visible;
           }
           set
           {
               if (visible != value)
               {
                   visible = value;

                   if (VisibleChanged != null)
                       VisibleChanged(this, null);
               }
           }
       }

       private boolean enabled = true;
       public boolean Enabled
       {
           get
           {
               return enabled;
           }
           set
           {
               enabled = value;
           }
       }

       private boolean isDragable = true;

       public boolean IsDragable
       {
          get { return isDragable; }
           set { isDragable = value; }
       }

       public Card(CardRank rank, CardSuit suit, Deck deck)
       {
           this.rank = rank;
           this.suit = suit;
           this.deck = deck;
           this.deck.Game.Cards.Add(this);
       }

       public Card(int number, CardSuit suit, Deck deck)
       {
           this.rank = (CardRank)number;
           this.suit = suit;
           this.deck = deck;
           this.deck.Game.Cards.Add(this);
       }

       public int CompareTo(Card other)
       {
           int value1 = this.Number;
           int value2 = other.Number;

           if(Card.IsAceBiggest)
           {
           if (value1 == 1)
               value1 = 14;

           if (value2 == 1)
               value2 = 14;
           }

           if (value1 > value2)
               return 1;
           else if (value1 < value2)
              return -1;
           else
               return 0;
       }

      public void MoveToFirst()
       {
           MoveToIndex(0);
       }

       public void MoveToLast()
       {
           MoveToIndex(Deck.Cards.Count);
       }

       public void Shuffle()
       {
           MoveToIndex(Deck.Game.random.Next(0, Deck.Cards.Count));
       }

       public void MoveToIndex(int index)
       {
           Deck.Cards.Remove(this);
           Deck.Cards.Insert(index, this);
       }


       public override string ToString()
       {
          return this.NumberString + " of " + this.Suit.ToString();
       }

   }
Posted
Updated 14-Mar-12 0:52am
v4
Comments
Chandrasekharan P 14-Mar-12 5:55am    
How do we know how the card class looks like?? Was there no explanation in the site where you got this code?
Keith Barrow 14-Mar-12 6:51am    
[Transferred from OP - not my reply]
Yes because it relates to codes of classes created such as CardPile and Solitaire built on Eclipse Java
Keith Barrow 14-Mar-12 6:46am    
I have formatted your code: please do this in future as it makes the question easier to read and an answer more likely!
Keith Barrow 14-Mar-12 7:04am    
Can you confirm: The code you have posted looks like c#, you said the question was in c++ before I edited it and you have commented that you are using Java. Which language are you using? Are you tranferring from one language to another?
mpondu 20-Mar-12 9:33am    
yes i have converted it from c# into java codes

1 solution

It removes the current card from its current deck to a different deck, passed as value. It then *looks* like it raises some Deckchanged event. What value is I don't know (See below), it makes sense if looks like the Deck() method is made into a property (which it probably should be):

C#
public DeckPile Deck
          {
get {return deck;}
set
{
        if (deck.Game != value.Game)
            throw new InvalidOperationException("The new deck must be in the same game like the old deck of the card.");

        if (deck != value)
        {
            deck.pile.remove(this);
            deck = value;
            deck.add(this);

            if (DeckChanged != null)
                DeckChanged(this, null);
        }
    }
}


The other thing is that the class os poorly designed: the move/shuffle methods properly belong in the Deck AFAICT. You also probably need a class that manages the game, looking after the various stacks/decks.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900