コード例 #1
0
        //Fill the target cell with the value "true" and expand to the surrounding area, base on the value of indexBoard
        /// <param name="x"> The coordinate x of the input </param>
        /// <param name="y"> The coordinate y of the input </param>
        /// <param name="indexBoard"> The given indexBoard, the values of indexBoard is needed for the expansion to perform correctly</param>
        // The function currently is in development
        public void revealCell(int x, int y, char[,] indexBoard)
        {
            //Variables
            //This boolean 2D array represent the cells that has been opened during the a call of this function.
            //One array is generated per call and used only in that call
            bool[,] ifOpened = new bool[height, width];

            //This is the diameter of the cell expansion.
            int expandingRadius = 0;

            //A variable to check if the expansion can further occur
            bool canExpand = false;

            //Reveal the target cell
            stateBoard[x, y] = true;
            if (indexBoard[x, y] == ' ') //If the target cell is empty
            {
                RevealSquare(x, y);

                ifOpened[x, y] = true;  //Set the origin of the expansion to true, allowing the expansion to occur
                canExpand      = true;
            }

            //Revealing the surrounding cells
            while (canExpand)      // stop expanding when the radius reach half of the height or width
            {
                expandingRadius++; //Expand the radius
                canExpand = false;

                //Looping through the area within radius
                for (int i = x - expandingRadius; i <= x + expandingRadius; i++)
                {
                    for (int j = y - expandingRadius; j <= y + expandingRadius; j++)
                    {
                        if ((i < height) && (j < width) && (i >= 0) && (j >= 0))
                        {
                            //Check the 8 surrounding cell to know if there were any cells opened (satisfied if there was)
                            //Check if this cells is empty or not (satisfied if empty)
                            //Check if this cells is opened or not (satisfied if not opened yet)
                            //If all the conditions are satisfied, reveal it and its 8 surrounding cells
                            if (MathFunc.CheckSurrounding(ifOpened, height, width, i, j) && indexBoard[i, j] == ' ' &&
                                ifOpened[i, j] == false)
                            {
                                RevealSquare(i, j);
                                ifOpened[i, j] = true;  //Mark the cell as opened
                                //If there is at least 1 cell opened in this loop, the expansion can continue
                                //Otherwise, it cannot
                                canExpand = true;
                            }
                        }
                    }
                }
            }
        }