// function to create the data for the game, randomize the location of mines.
        private MineSpot[,] populateGameData(int selectedsize)
        {
            MineSpot[,] field = new MineSpot[selectedsize, selectedsize];
            Random rnd = new Random();

            for (int x = 0; x < selectedsize; x++)
            {
                for (int y = 0; y < selectedsize; y++)
                {
                    field[x, y] = new MineSpot(false, false);
                }
            }
            int i = 0;

            // initialize the spots for mines.
            while (i <= bombsLeft)
            {
                MineSpot mine = field[rnd.Next(selectedsize), rnd.Next(selectedsize)];
                if (!mine.getHasMine())
                {
                    mine.setMine(true);
                    i += 1;
                }
            }
            return(field);
        }
        private void ClickSpot(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;

            b.IsEnabled = false;
            int x = Grid.GetRow(b);
            int y = Grid.GetColumn(b);

            MineSpot spot = minefield[x, y];

            if (spot.getHasMine())
            {
                // game over, click all button and reveal the mines
                IEnumerable <Button> clickableSpots = GameBoard.Children.OfType <Button>();
                foreach (Button clickableSpot in clickableSpots)
                {
                    clickableSpot.IsEnabled = false;
                }

                IEnumerable <TextBlock> minespots = GameBoard.Children
                                                    .OfType <TextBlock>();
                foreach (TextBlock minespot in minespots)
                {
                    int row = Grid.GetRow(minespot);
                    int col = Grid.GetColumn(minespot);
                    if (minefield[row, col].getHasMine())
                    {
                        minespot.Text = "M";
                    }
                }

                status.Text = "You Lose!";
                return;
            }

            spot.setClicked(true);
            int total = CheckAdjacent(x, y);

            if (total > 0)
            {
                IEnumerable <TextBlock> celltxt = GameBoard.Children
                                                  .OfType <TextBlock>()
                                                  .Where(e => Grid.GetRow(e) == x && Grid.GetColumn(e) == y);

                foreach (TextBlock txt in celltxt)
                {
                    txt.Text = total.ToString();
                }
            }
            // if no adjacent cells contain mines, reveal all nearby cells.
            else
            {
                DepthClick(x - 1, y);
                DepthClick(x, y - 1);
                DepthClick(x + 1, y);
                DepthClick(x, y + 1);
            }
        }
 // returns 1 if spot has a mine, 0 otherwise
 private int CheckSpot(MineSpot spot)
 {
     if (spot != null)
     {
         if (spot.getHasMine())
         {
             return(1);
         }
         return(0);
     }
     return(0);
 }
        // flags a cell in the gameboard. if it's a mine, decrease bomb count by 1.
        // if all mines are found, end the game.
        // if the cell has a flag on it, remove the flag.
        public void MarkFlag(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;
            int    x = Grid.GetRow(b);
            int    y = Grid.GetColumn(b);

            IEnumerable <TextBlock> celltxt = GameBoard.Children
                                              .OfType <TextBlock>()
                                              .Where(e => Grid.GetRow(e) == x && Grid.GetColumn(e) == y);

            MineSpot spot = minefield[x, y];

            if (!spot.HasFlag)
            {
                if (spot.getHasMine())
                {
                    bombsLeft            -= 1;
                    bombsLeftDisplay.Text = bombsLeft.ToString();
                    if (bombsLeft == 0)
                    {
                        status.Text = "You Win!";
                        IEnumerable <Button> clickableSpots = GameBoard.Children.OfType <Button>();
                        foreach (Button clickableSpot in clickableSpots)
                        {
                            clickableSpot.IsEnabled = false;
                        }
                    }
                    ;
                }
                spot.setFlag(true);
                foreach (TextBlock txt in celltxt)
                {
                    txt.Text = "F";
                }
            }
            else
            {
                if (spot.getHasMine())
                {
                    bombsLeft            += 1;
                    bombsLeftDisplay.Text = bombsLeft.ToString();
                }
                spot.setFlag(false);
                foreach (TextBlock txt in celltxt)
                {
                    txt.Text = "";
                }
            }
        }
        // get all adjacent cells to the current position. if there's no cell in that position, it's replaced with null
        private MineSpot[] getAdjacentCells(int x, int y)
        {
            int maxX = minefield.GetLength(0);
            int maxY = minefield.GetLength(1);

            MineSpot[] adjacentCells = new MineSpot[8];

            // find the spots we have to check
            // cell order [left, up, right, down, upLeft, upRight, downLeft, downRight]
            adjacentCells[0] = x > 0 ? minefield[x - 1, y] : null;
            adjacentCells[1] = y > 0 ? minefield[x, y - 1] : null;
            adjacentCells[2] = x + 1 < maxX ? minefield[x + 1, y] : null;
            adjacentCells[3] = y + 1 < maxY ? minefield[x, y + 1] : null;
            adjacentCells[4] = x > 0 && y > 0 ? minefield[x - 1, y - 1] : null;
            adjacentCells[5] = x + 1 < maxX && y > 0 ? minefield[x + 1, y - 1] : null;
            adjacentCells[6] = x > 0 && y + 1 < maxY ? minefield[x - 1, y + 1] : null;
            adjacentCells[7] = x + 1 < maxX && y + 1 < maxY ? minefield[x + 1, y + 1] : null;

            return(adjacentCells);
        }