Ejemplo n.º 1
0
        /*
         * SetCellneighbours is used to set the neighbours for each Cell and then pass them in as a array of cells.
         * Loops through the CellGrid checking which postion the Cell is at eg. in a corner or against the side.
         * If in a corner or agasint a side then it takes the neighbour from the opposite side so as to make the grid wrap around.
         * e.g. making the top and bottom neightbours as well as the left and right.
         * Code is not as nice as how i would like it. I was unable to figure out how to use a Enum to refrence the number in the array instead.
         */
        public void SetCellNeighbours()
        {
            Cell[] cellArrayNeighbours;
            for (int row = 0; row < Height; row++)
            {
                for (int col = 0; col < Width; col++)
                {
                    cellArrayNeighbours = new Cell[8];

                    if (row == 0 && col == 0)                                     //TOPLeft
                    {
                        cellArrayNeighbours[0] = CellGrid[Height - 1, Width - 1]; //uniq

                        cellArrayNeighbours[1] = CellGrid[row, Width - 1];        //Left from col
                        cellArrayNeighbours[2] = CellGrid[row + 1, Width - 1];    //Bottomleft from col
                        cellArrayNeighbours[3] = CellGrid[row + 1, col];          //Bottom
                        cellArrayNeighbours[4] = CellGrid[row + 1, col + 1];      //BottomRight
                        cellArrayNeighbours[5] = CellGrid[row, col + 1];          //Right
                        cellArrayNeighbours[6] = CellGrid[Height - 1, col + 1];   //TopRight from row
                        cellArrayNeighbours[7] = CellGrid[Height - 1, col];       //Top  from row
                    }
                    else if (row == Height - 1 && col == 0)                       //BottomLeft
                    {
                        cellArrayNeighbours[0] = CellGrid[row - 1, Width - 1];    //Topleft from col
                        cellArrayNeighbours[1] = CellGrid[row, Width - 1];        //Left from col

                        cellArrayNeighbours[2] = CellGrid[0, Width - 1];          //uniq

                        cellArrayNeighbours[3] = CellGrid[0, col];                //Bottom from row
                        cellArrayNeighbours[4] = CellGrid[0, col + 1];            //BottomRight from row
                        cellArrayNeighbours[5] = CellGrid[row, col + 1];          //Right
                        cellArrayNeighbours[6] = CellGrid[row - 1, col + 1];      //TopRight
                        cellArrayNeighbours[7] = CellGrid[row - 1, col];          //Top
                    }
                    else if (row == Height - 1 && col == Width - 1)               //BottomRight
                    {
                        cellArrayNeighbours[0] = CellGrid[row - 1, col - 1];      //Topleft
                        cellArrayNeighbours[1] = CellGrid[row, col - 1];          //Left
                        cellArrayNeighbours[2] = CellGrid[0, col - 1];            //BottomLeft from row == height
                        cellArrayNeighbours[3] = CellGrid[0, col];                //Bottom from row == height

                        cellArrayNeighbours[4] = CellGrid[0, 0];

                        cellArrayNeighbours[5] = CellGrid[row, 0];              //Right from col == width
                        cellArrayNeighbours[6] = CellGrid[row - 1, 0];          //TopRight from col == width
                        cellArrayNeighbours[7] = CellGrid[row - 1, col];        //Top
                    }
                    else if (row == 0 && col == Width - 1)                      //TopRight
                    {
                        cellArrayNeighbours[0] = CellGrid[Height - 1, col - 1]; //TopLeft from row ==0
                        cellArrayNeighbours[1] = CellGrid[row, col - 1];        //Left
                        cellArrayNeighbours[2] = CellGrid[row + 1, col - 1];    //Bottomleft
                        cellArrayNeighbours[3] = CellGrid[row + 1, col];        //Bottom
                        cellArrayNeighbours[4] = CellGrid[row + 1, 0];          //BottomRight from col == width
                        cellArrayNeighbours[5] = CellGrid[row, 0];              //Right from col == width

                        cellArrayNeighbours[6] = CellGrid[Height - 1, 0];

                        cellArrayNeighbours[7] = CellGrid[Height - 1, col];     //Top from row ==0
                    }
                    else if (row == 0)                                          //Top
                    {
                        cellArrayNeighbours[0] = CellGrid[Height - 1, col - 1]; //TopLeft
                        cellArrayNeighbours[6] = CellGrid[Height - 1, col + 1]; //TopRight
                        cellArrayNeighbours[7] = CellGrid[Height - 1, col];     //Top

                        cellArrayNeighbours[1] = CellGrid[row, col - 1];        //Left
                        cellArrayNeighbours[2] = CellGrid[row + 1, col - 1];    //Bottomleft
                        cellArrayNeighbours[3] = CellGrid[row + 1, col];        //Bottom
                        cellArrayNeighbours[4] = CellGrid[row + 1, col + 1];    //BottomRight
                        cellArrayNeighbours[5] = CellGrid[row, col + 1];        //Right
                    }
                    else if (row == Height - 1)                                 //Bottom
                    {
                        cellArrayNeighbours[2] = CellGrid[0, col - 1];          //BottomLeft
                        cellArrayNeighbours[3] = CellGrid[0, col];              //Bottom
                        cellArrayNeighbours[4] = CellGrid[0, col + 1];          //BottomRight

                        cellArrayNeighbours[0] = CellGrid[row - 1, col - 1];    //Topleft
                        cellArrayNeighbours[1] = CellGrid[row, col - 1];        //Left
                        cellArrayNeighbours[5] = CellGrid[row, col + 1];        //Right
                        cellArrayNeighbours[6] = CellGrid[row - 1, col + 1];    //TopRight
                        cellArrayNeighbours[7] = CellGrid[row - 1, col];        //Top
                    }
                    else if (col == 0)                                          //Left
                    {
                        cellArrayNeighbours[0] = CellGrid[row - 1, Width - 1];  //Topleft
                        cellArrayNeighbours[1] = CellGrid[row, Width - 1];      //Left
                        cellArrayNeighbours[2] = CellGrid[row + 1, Width - 1];  //Bottomleft

                        cellArrayNeighbours[3] = CellGrid[row + 1, col];        //Bottom
                        cellArrayNeighbours[4] = CellGrid[row + 1, col + 1];    //BottomRight
                        cellArrayNeighbours[5] = CellGrid[row, col + 1];        //Right
                        cellArrayNeighbours[6] = CellGrid[row - 1, col + 1];    //TopRight
                        cellArrayNeighbours[7] = CellGrid[row - 1, col];        //Top
                    }
                    else if (col == Width - 1)                                  //Right
                    {
                        cellArrayNeighbours[4] = CellGrid[row + 1, 0];          //BottomRight
                        cellArrayNeighbours[5] = CellGrid[row, 0];              //Right
                        cellArrayNeighbours[6] = CellGrid[row - 1, 0];          //TopRight

                        cellArrayNeighbours[0] = CellGrid[row - 1, col - 1];    //Topleft
                        cellArrayNeighbours[1] = CellGrid[row, col - 1];        //Left
                        cellArrayNeighbours[2] = CellGrid[row + 1, col - 1];    //Bottomleft
                        cellArrayNeighbours[3] = CellGrid[row + 1, col];        //Bottom
                        cellArrayNeighbours[7] = CellGrid[row - 1, col];        //Top
                    }
                    else
                    {
                        cellArrayNeighbours[0] = CellGrid[row - 1, col - 1];    //Topleft
                        cellArrayNeighbours[1] = CellGrid[row, col - 1];        //Left
                        cellArrayNeighbours[2] = CellGrid[row + 1, col - 1];    //Bottomleft
                        cellArrayNeighbours[3] = CellGrid[row + 1, col];        //Bottom
                        cellArrayNeighbours[4] = CellGrid[row + 1, col + 1];    //BottomRight
                        cellArrayNeighbours[5] = CellGrid[row, col + 1];        //Right
                        cellArrayNeighbours[6] = CellGrid[row - 1, col + 1];    //TopRight
                        cellArrayNeighbours[7] = CellGrid[row - 1, col];        //Top
                    }

                    CellGrid[row, col].SetCellneighbours(cellArrayNeighbours);      //Sets the current Cells Neighbours by passing it the array cellArrayNeighbours.
                }
            }
        }