Section C 2023

From TRCCompSci - AQA Computer Science
Revision as of 13:17, 10 February 2023 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This section is to document the skeleton program. Section C will be questions relating to the actual code within the program and could relate to specific methods, variables, programming skills, or theory topics.

Execution

The 'static void Main' in the class of program will always be the first block of code executed.

The line:

    class Program
    {
        static void Main(string[] args)
        {
            Dastan ThisGame = new Dastan(6, 6, 4);
            ThisGame.PlayGame();
            Console.WriteLine("Goodbye!");
            Console.ReadLine();
        }
    }

Declares and instantiates an object of 'Dastan', this will cause the constructor method of the 'Dastan' class to be run. The code for the constructor is below:

        public Dastan(int R, int C, int NoOfPieces)
        {
            Players.Add(new Player("Player One", 1));
            Players.Add(new Player("Player Two", -1));
            CreateMoveOptions();
            NoOfRows = R;
            NoOfColumns = C;
            MoveOptionOfferPosition = 0;
            CreateMoveOptionOffer();
            CreateBoard();
            CreatePieces(NoOfPieces);
            CurrentPlayer = Players[0];
        }

The 'Dastan' constructor will create 2 new player objects. This will cause the constructor method of the 'Player' class to run. The constructor method for the 'Player' class is:

        public Player(string N, int D)
        {
            Score = 100;
            Name = N;
            Direction = D;
        }

Once the 2 players are created the 'Dastan' constructor calls the 'CreateMoveOptions' method of the 'Dastan' class. The 'CreateMoveOptions' method will execute and is used to create the different move options for each player. The 'CreateMoveOptions' method is:

        private void CreateMoveOptions()
        {
            Players[0].AddToMoveOptionQueue(CreateMoveOption("ryott", 1));
            Players[0].AddToMoveOptionQueue(CreateMoveOption("chowkidar", 1));
            Players[0].AddToMoveOptionQueue(CreateMoveOption("cuirassier", 1));
            Players[0].AddToMoveOptionQueue(CreateMoveOption("faujdar", 1));
            Players[0].AddToMoveOptionQueue(CreateMoveOption("jazair", 1));
            Players[1].AddToMoveOptionQueue(CreateMoveOption("ryott", -1));
            Players[1].AddToMoveOptionQueue(CreateMoveOption("chowkidar", -1));
            Players[1].AddToMoveOptionQueue(CreateMoveOption("jazair", -1));
            Players[1].AddToMoveOptionQueue(CreateMoveOption("faujdar", -1));
            Players[1].AddToMoveOptionQueue(CreateMoveOption("cuirassier", -1));
        }

The 'Dastan' constructor continues to set the 'NoOfRows', 'NoOfColumns', and the 'MoveOptionOfferPosition'. It then continues to run the methods 'CreateMoveOptionOffer', 'CreateBoard', and 'CreatePieces'. Finally the 'Dastan' constructor will set the 'CurrentPlayer'.

So back to the 'static void Main' and this line:

ThisGame.PlayGame();

This calls the 'PlayGame' method of the 'Dastan' object.

Classes

The skeleton program has the following classes:

Dastan

Piece

Square

Kolta

MoveOption

Move

MoveOptionQueue

Player

Question Help

Private vs Protected

Remember a variable or method can be declared Public, Private, Protected. The default for C# is private, so any method or variable which doesn't specifically declare Public or Protected is automatically Private. But what does this really mean, well a Public variable or method is accessible from outside of the object. So in terms of the skeleton program, if you create a Card object called 'temp' then you will be able to access 'temp.GetCardNumber()'. However the actual variable CardNumber is protected so you can't access it from outside and you can't therefore access 'temp.CardNumber'.

Private means it is only available from within an object or the class itself. Protected is relatively the same as private, except it will also be available in any subclass.

Inheritance & Polymorphism

Remember a class can be used as the basis for subclass, when this happens all of the data values and methods of the base class will automatically be available in the subclass. This is called 'Inheritance'. So in the skeleton program the Card class as been used to create the subclasses of DifficultyCard & ToolCard. Both DifficultyCard & ToolCard will inherit everything not declared as 'Private', you therefore only need to declare what is new within a subclass.

Polymorphism is the idea that what a subclass inherits can be replaced with the subclasses own version. A subclass can therefore override a method which it inherits from its parent or base class.

Virtual & Override

These terms are related to Polymorphism above.

Any method declared as 'virtual' can be overridden in a subclass. So some methods within the skeleton program are already set to be 'virtual', for example the Card class as 4 methods declared as 'virtual'. Then in ToolCard 'GetDescription' is overridden and in DifficultyCard both 'GetDescription' & 'Process' are overridden.

Use of 'this'

In C# the term 'this' refers to the object. So for example if you have a class of Card and you create an object of it called 'tempCard'. The use of 'this' will mean the object 'tempCard'.

Use of 'base'

The term 'base' will always refer to the parent or base class. In the skeleton program the term is used in constructor methods within a subclass, the term 'base' will also run the constructor method of the parent or base class.

This could also be used to run a method from the parent or base class. For example the DifficultyCard class overrides the Process method. What could happen is the Process method within the DifficultyCard subclass could run 'base.Process()' as part of its own implementation.

Constructors

A Constructor is a method which is run when an object is created. You can tell it is a Constructor because it will always be called exactly the same name as the class.

So in the skeleton program the first class is called 'Breakthrough' and it contains a method declared as 'public Breakthrough()'.

Static

A static method belongs to the class rather than any object of a class. So they can be accessed without having to create a new object.

So in the skeleton program, look at the Card class and you will see that 'NextCardNumber' is static. This will mean that 'NextCardNumber' only exists within the class itself and not within the objects themselves. This will allow the NextCardNumber to increment every time a card is created.

Lists

A List is a data structure similar to an array, but without the need to manipulate it by using element numbers. A List will have a 'Add' method along with a 'Remove' method. You can still use the element numbers if your want.

When you declare a List the data type must be specified, this is done in the angle brackets <....>.