Difference between revisions of "2021 - Piece"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "<syntaxhighlight lang=c#> class Piece { protected bool destroyed, belongsToPlayer1; protected int fuelCostOfMove, VPValue, connectionsToDestroy; pr...")
 
(Things to note)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
=Actual Code=
 
<syntaxhighlight lang=c#>
 
<syntaxhighlight lang=c#>
 
class Piece
 
class Piece
Line 70: Line 71:
 
     }
 
     }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
=Things to note=
 +
Protected means they are available in the parent and any sub-classess.
 +
 +
Virtual used so that methods can be overriden in the sub-classes.
 +
 +
Constructor

Latest revision as of 11:49, 3 September 2020

Actual Code

class Piece
    {
        protected bool destroyed, belongsToPlayer1;
        protected int fuelCostOfMove, VPValue, connectionsToDestroy;
        protected string pieceType;

        public Piece(bool player1)
        {
            fuelCostOfMove = 1;
            belongsToPlayer1 = player1;
            destroyed = false;
            pieceType = "S";
            VPValue = 1;
            connectionsToDestroy = 2;
        }

        public virtual int GetVPs()
        {
            return VPValue;
        }

        public virtual bool GetBelongsToPlayer1()
        {
            return belongsToPlayer1;
        }

        public virtual int CheckMoveIsValid(int distanceBetweenTiles, string startTerrain, string endTerrain)
        {
            if (distanceBetweenTiles == 1)
            {
                if (startTerrain == "~" || endTerrain == "~")
                {
                    return fuelCostOfMove * 2;
                }
                else
                {
                    return fuelCostOfMove;
                }
            }
            return -1;
        }

        public virtual bool HasMethod(string methodName)
        {
            return this.GetType().GetMethod(methodName) != null;
        }

        public virtual int GetConnectionsNeededToDestroy()
        {
            return connectionsToDestroy;
        }

        public virtual string GetPieceType()
        {
            if (belongsToPlayer1)
            {
                return pieceType;
            }
            else
            {
                return pieceType.ToLower();
            }
        }

        public virtual void DestroyPiece()
        {
            destroyed = true;
        }
    }

Things to note

Protected means they are available in the parent and any sub-classess.

Virtual used so that methods can be overriden in the sub-classes.

Constructor