Search code examples
c#dialogmessageboxapplication-restart

C# using a message box(yes/no) to restart my game


I am creating a minefield style game as a task and I am wondering if there is a quick easy way to restart my game. When the character walks on to a bomb, I have a message box pop up saying 'game over, try again?' with a Yes and No option. I want the No option to close the game and I want the Yes option to restart the game to its original state that it is opened in. Any ideas? This is the message box code when it executes:

 private void checkBomb( int X, int Y)
{
    if (bombs[X, Y])
    {
        this.BackColor = Color.Red;
        downBtn.Enabled = false;
        upBtn.Enabled = false;
        leftBtn.Enabled = false;
        rightBtn.Enabled = false;
        showBombs();
        // Dialog box with two buttons: yes and no.
        //
        DialogResult result1 = MessageBox.Show("Game Over! Try again?",
        "Game Over",
        MessageBoxButtons.YesNo);
    }
    else
    {
        countBombs(X, Y);
    }
}

Solution

  • I think you would be able to do the following:

    MessageBoxResult result = MessageBox.Show("Game Over! Try again?",
        "Game Over",
        MessageBoxButtons.YesNo);
    if (result == MessageBoxResult.No)
    {
        System.Windows.Application.Current.Shutdown();
    }
    if (result == MessageBoxResult.Yes)
    {
        //Restart your game
    }