2021-Get grid size

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

The grid size is currently stored within the HexGrid object, however it is currently private and can't be accessed. Find the HexGrid class in the skeleton program:

class HexGrid
    {
        protected List<Tile> tiles = new List<Tile>();
        protected List<Piece> pieces = new List<Piece>();
        int size;
        bool player1Turn;

        public HexGrid(int n)
        {
            size = n;
            SetUpTiles();
            SetUpNeighbours();
            player1Turn = true;
        }

This is just a fraction of the HexGrid class, but you can see size is declared as an int. Because the default for variables is 'private' this isn't available to save the game. We could change this by adding public before the int size;. Alternatively we can create a method to access it:

        public int GetSize()
        {
            return size;
        }

Alternatively we can make a property:

        public int GetSize
        {
            get { return size; }
        }