Пример #1
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 = "";
                }
            }
        }
Пример #2
0
        // 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);
        }
Пример #3
0
        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);
            }
        }
Пример #4
0
 // 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);
 }