コード例 #1
0
        public void GameOver()
        {
            for (var i = 0; i < this.ColumnCount; i++)
            {
                for (var j = 0; j < this.RowCount; j++)
                {
                    this.ButtonArray[i, j].ToggleRevealed();
                    this.ButtonArray[i, j].IsEnabled = false;
                    if (this.ButtonArray[i, j].IsFlagged)
                    {
                        if (this.ButtonArray[i, j].IsMine)
                        {
                            StackPanel goodFlagStackPanel = new StackPanel();
                            Image      goodFlagImage      = new Image();
                            goodFlagStackPanel.Orientation = Orientation.Horizontal;
                            goodFlagImage.Source           = ImageWorker.GenerateImage(@"..\..\Assets\goodflag.png");
                            goodFlagStackPanel.Children.Add(goodFlagImage);

                            this.ButtonArray[i, j].Content = goodFlagStackPanel;
                        }
                        else
                        {
                            StackPanel badFlagStackPanel = new StackPanel();
                            badFlagStackPanel.Orientation = Orientation.Horizontal;

                            Image badFlagImage = new Image();
                            badFlagImage.Source = ImageWorker.GenerateImage(@"..\..\Assets\badflag.png");
                            badFlagStackPanel.Children.Add(badFlagImage);

                            this.ButtonArray[i, j].Content = badFlagStackPanel;
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void ToggleRevealed()
        {
            if (!this.IsFlagged)
            {
                this.IsRevealed = true;

                if (this.IsMine)
                {
                    StackPanel stackPanel = new StackPanel();
                    stackPanel.Orientation = Orientation.Horizontal;
                    Image mineImage = new Image();
                    mineImage.Source = ImageWorker.GenerateImage(@"..\..\Assets\mine.gif");

                    stackPanel.Children.Add(mineImage);

                    this.Content = stackPanel;
                }
                else
                {
                    this.Content = (this.SurroundingMineCount > 0) ? (this.SurroundingMineCount.ToString()) : "";
                }

                this.Background = this.IsMine ? Brushes.IndianRed : Brushes.DarkGray;
            }
        }
コード例 #3
0
        private void BoardButton_Click(object sender, RoutedEventArgs e)
        {
            MinesweeperButton toggledButton = sender as MinesweeperButton;

            StackPanel stackPanel = new StackPanel();

            stackPanel.Orientation = Orientation.Horizontal;
            Image flagImage = new Image();

            flagImage.Source = ImageWorker.GenerateImage(@"..\..\Assets\flag.gif");

            stackPanel.Children.Add(flagImage);

            if (!toggledButton.IsFlagged && (enableFlagButton.IsChecked ?? false))
            {
                // If the target button is not flagged and the EnableFlagButton is turned ON
                if (game.FlagCount > 0)
                {
                    remainingMinesTextBlock.Text = game.DecrementFlagCounter().ToString();
                    toggledButton.ToggleFlagOnButton();
                    toggledButton.Content = stackPanel;
                    buttonReset.Content   = FindResource("neutral_emoji");

                    if (game.FlagCount == 0)
                    {
                        remainingMinesTextBlock.Foreground = Brushes.IndianRed;
                        remainingMinesImage.Source         = ImageWorker.GenerateImage(@"..\..\Assets\badflag.png");
                    }
                }
                else
                {
                    MessageBox.Show("You have no more flags left.");
                    buttonReset.Content = FindResource("neutral_emoji");
                }
            }
            else if (!toggledButton.IsFlagged)
            {
                // If the target button is not flagged and the EnableFlagButton is turned OFF
                toggledButton.ToggleRevealed();

                if (toggledButton.SurroundingMineCount == 0)
                {
                    game.RevealAllBlanks(toggledButton);
                }

                if (toggledButton.IsMine)
                {
                    buttonReset.Content = FindResource("mineclicked_emoji");
                    gameTimer.Stop();
                    game.GameOver();
                    StartAnimation();
                }
                else
                {
                    buttonReset.Content = FindResource("neutral_emoji");

                    if (GameWon())
                    {
                        buttonReset.Content = FindResource("gameover_emoji");
                        gameTimer.Stop();
                        game.GameOver();

                        leaderboardObject = new Leaderboard(playerName, mineCount.ToString(), timerTextBlock.Text);
                        leaderboardObject.WriteDataToFile();

                        StartAnimation();
                    }
                }
            }
            else if (toggledButton.IsFlagged && (enableFlagButton.IsChecked ?? false))
            {
                // If the target button IS FLAGGED and the EnableFlagButton is turned ON
                toggledButton.ToggleFlagOnButton();
                remainingMinesTextBlock.Text       = game.IncrementFlagCounter().ToString();
                remainingMinesTextBlock.Foreground = Brushes.DarkGreen;
                toggledButton.Content = "";
                buttonReset.Content   = FindResource("neutral_emoji");

                remainingMinesImage.Source = ImageWorker.GenerateImage(@"..\..\Assets\goodflag.png");
            }
        }