Home > Mobile >  How to make a program wait until a button is clicked?
How to make a program wait until a button is clicked?

Time:12-16

I was wondering how I could make my program wait until a certain button is pressed.

To illustrate my question, I have made a dummy WPF game where the user can roll two dices as long as he doesn't get double. The goal of the game is having the highest roll count.

I have the following 'Dice' class :

class Dice
{
    TextBlock diceTextBlock;

    const int minNumber = 1;
    const int maxNumber = 6;

    int number;

    public int Number
    {
        get { return number; }
        set
        {
            number = value;
            diceTextBlock.Text = number.ToString();
        }
    }

    public Dice(TextBlock diceTextBlock)
    {
        this.diceTextBlock = diceTextBlock;
        Number = minNumber;
    }

    public void Roll()
    {
        Number = new Random().Next(minNumber, maxNumber   1);
    }
}

I have also the following 'GameWindow' class :

public partial class GameWindow : Window
{
    Dice dice1;
    Dice dice2;

    int rollCount;

    public GameWindow()
    {
        InitializeComponent();

        dice1 = new Dice(Dice1TextBlock);
        dice2 = new Dice(Dice2TextBlock);

        rollCount = 0;

        Play();
    }

    private void RollButton_Click(object sender, RoutedEventArgs e)
    {
        dice1.Roll();
        dice2.Roll();
        rollCount  ;
    }

    private void Play()
    {
        do
        {
            //  Wait for the user to press the 'RollButton'
        }
        while (dice1.Number != dice2.Number);
    }
}

How do I make my program wait for the user to press the 'RollButton' in the 'Play()' method?

I have tried to learn about events and asynchronous programming. However, as a beginner, I have some difficulties to understand these concepts. Also, I am not sure that these can help to solve my problem.

CodePudding user response:

to make it understandable to you, you need to remove the Play(); inside the GameWindow. Therefore you need to add Play(); to the end of RollButton_Click(...) inside the brackets. This should be easier than asynchronous programming especially if you just want to make a simple programme like this. Also the do while loop does nothing, just make a bool method which checks whether dice 1 and 2 have the same numbers. If they have the same number you can return true to end the game and false if the don't have matching numbers. Inside the method RollButton_Click(...) you Check whether the two numbers are matching or not. If they do you display the message that the two numbers matched and how many tries it took

CodePudding user response:

You could use a SemaphoreSlim to wait asynchronously:

public partial class GameWindow : Window, IDisposable
{
    Dice dice1;
    Dice dice2;

    int rollCount;

    SemaphoreSlim semaphore = new SemaphoreSlim(0, 1);

    public GameWindow()
    {
        InitializeComponent();

        dice1 = new Dice(Dice1TextBlock);
        dice2 = new Dice(Dice2TextBlock);

        rollCount = 0;

        Loaded  = async (s,e) => await PlayAsync();
    }

    private void RollButton_Click(object sender, RoutedEventArgs e)
    {
        dice1.Roll();
        dice2.Roll();
        rollCount  ;
        semaphore.Release();
    }

    private async Task PlayAsync()
    {
        //  Wait for the user to press the 'RollButton'
        do
        {
            await semaphore.WaitAsync();
        }
        while (dice1.Number != dice2.Number);

        MessageBox.Show("Yes!");
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        semaphore.Dispose();
    }

    public void Dispose()
    {
        semaphore.Dispose();
    }
}
  • Related