2022 - Card

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

This is the base class for all cards. Remember, 'protected' means that it is available only within this class and any subclasses. Also remember a method marked as 'virtual' can be overridden in a subclass. The term 'static' means that it belongs to the class itself is not available in object of the class.

The Code

class Card
    {
        protected int CardNumber, Score;
        protected static int NextCardNumber = 1;

        public Card()
        {
            CardNumber = NextCardNumber;
            NextCardNumber += 1;
            Score = 0;
        }

        public virtual int GetScore()
        {
            return Score;
        }

        public virtual void Process(CardCollection deck, CardCollection discard,
            CardCollection hand, CardCollection sequence, Lock currentLock,
            string choice, int cardChoice)
        {
        }

        public virtual int GetCardNumber()
        {
            return CardNumber;
        }

        public virtual string GetDescription()
        {
            if (CardNumber < 10)
            {
                return " " + CardNumber.ToString();
            }
            else
            {
                return CardNumber.ToString();
            }
        }
    }

The Constructor Method

Remember the constructor method of a class is a public method called exactly the same name as the class. So in this case:

        public Card()
        {
            CardNumber = NextCardNumber;
            NextCardNumber += 1;
            Score = 0;
        }

Other Methods

The key other method is the 'Process' method. This will be used to perform any actions required when playing a card.

The remaining methods in the class appear to be public accessor methods. You can therefore access the protected 'Score', 'CardNumber' from outside of the parent or subclass. The 'GetDescription' method is a property which returns the card number as a string.

If you notice, every other method can be overridden in a subclass (ie virtual).