[C#] Random radioButton

Started by
7 comments, last by ApochPiQ 17 years ago
I'm creating a simple math game and i need to make addition, multiplication and subtraction go random. Right now addition is default. How could i do it? Here's the full code:
 public partial class Form1 : Form
    {
        Random randomNo = new Random();
        int no1;
        int no2;
        string problemType = "+";

        public Form1()
        {
            InitializeComponent();
            Answer.KeyDown += new KeyEventHandler(Answer_KeyDown);
            Answer.KeyDown += new KeyEventHandler(NextQuestion_KeyDown);
        }

        private void StartGame_Click(object sender, EventArgs e)
        {
            Clock = new Timer();
            Clock.Interval = 1000;
            Clock.Start();
            Clock.Tick += new EventHandler(Timer_Tick);
            StartGame.Visible = false;
            CheckAnswer.Visible = true;
            Question.Visible = true;
            Answer.Visible = true;
            NextQuestion.Visible = true;
            Feedback.Text = "";
            Feedback.Visible = true;
            label1.Visible = true;
            Gen_Question();
        }

        private void Gen_Question()
        {
            no1 = randomNo.Next(100);
            no2 = randomNo.Next(100);

            if (radioButton1.Checked)
            {
                problemType = "+";
            }
            else if (radioButton2.Checked)
            {
                problemType = "-";
            }
            else if (radioButton3.Checked)
            {
                problemType = "*";
            }

            Question.Text = no1 + problemType + no2;
        }
        private void CheckAnswer_Click(object sender, EventArgs e)
        {
            int result = 0;
            if (problemType == "+")
            {
                result = no1 + no2;
            }
            else if (problemType == "-")
            {
                result = no1 - no2;
            }
            else if (problemType == "*")
            {
                result = no1 * no2;
            }

            if (Answer.Text == result.ToString())
            {
                Feedback.Text = "Correct";
            }
            else
            {
                Feedback.Text = "Incorrect";
            }

        }

        private void NextQuestion_Click(object sender, EventArgs e)
        {
            x = 30;
            Clock.Start();
            this.label1.Text = "" + x;
            Gen_Question();
            Answer.Text = "";
            Feedback.Text = "";
        }
        int x = 30;

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (x == 1) { Clock.Stop(); }
            x = x - 1;
            this.label1.Text = "" + x;
            if (x == 0) { stop(); }
        }
        private void stop()
        {
            this.Question.Text = "Time's Up";

        }

        private void Answer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int result = 0;
                if (problemType == "+")
                {
                    result = no1 + no2;
                }
                else if (problemType == "-")
                {
                    result = no1 - no2;
                }
                else if (problemType == "*")
                {
                    result = no1 * no2;
                }

                if (Answer.Text == result.ToString())
                {
                    Feedback.Text = "Correct";
                }
                else
                {
                    Feedback.Text = "Incorrect";
                }
            }
        }

        private void NextQuestion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                x = 30;
                Clock.Start();
                this.label1.Text = "" + x;
                Gen_Question();
                Answer.Text = "";
                Feedback.Text = "";
            }
        }
    }
}
Advertisement
Do you actually want the radio button to be selected, or do you just want a random selection of operations?

Either way, the simplest thing to do would be to call Random.Next(3). This will give you either 0, 1, or 2. You can then use if()/else if() (or, better, switch) to check the number. Let 0 correspond to addition, 1 to multiplication, and 2 to subtraction, etc. Check the appropriate radio button with button.Checked = true;, and you're all set.

I'd recommend putting all of that procedure into a function like PickRandomOperation, and then call that from Gen_Question or wherever it makes sense.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

private void PickRandomOperation(object sender, EventArgs e)            {               Random radioButton = new Random.Next(3);                radioButton1.Next(0);                radioButton2.Next(1);                radioButton3.Next(2);            }            }    }


I did this but there's some errors like: System.Random.Next() is a 'method' but is used like a 'type' and System.Windows.Forms.RadioButton does not contain a definition for 'Next'. Also i didn't understood the if/else part of your explanation.
Quote:Original post by NotSlyPee
private void PickRandomOperation(object sender, EventArgs e)            {               Random radioButton = new Random.Next(3);                radioButton1.Next(0);                radioButton2.Next(1);                radioButton3.Next(2);            }            }    }


I did this but there's some errors like: System.Random.Next() is a 'method' but is used like a 'type' and System.Windows.Forms.RadioButton does not contain a definition for 'Next'. Also i didn't understood the if/else part of your explanation.

I believe ApochPiQ was talking along these lines...
private void PickRandomOperation(object sender, EventArgs e){   Random randomNumber = new Random();   int tempNumber = randomNumber.Next(3);   switch(tempNumber)   {    case 1:       radioButton1.Checked = true;       break;    case 2:       radioButton2.Checked = true;       break;    case 3:       radioButton3.Checked = true;       break;    default:   }}    
public partial class Form1 : Form    {        Random randomNo = new Random();        int no1;        int no2;        string problemType = "+";        public Form1()        {            InitializeComponent();            Answer.KeyDown += new KeyEventHandler(Answer_KeyDown);            Answer.KeyDown += new KeyEventHandler(NextQuestion_KeyDown);        }        private void PickRandomOperation(object sender, EventArgs e)        {            Random randomNumber = new Random();            int tempNumber = randomNumber.Next(3);            switch (tempNumber)            {                case 1:                    radioButton1.Checked = true;                    break;                case 2:                    radioButton2.Checked = true;                    break;                case 3:                    radioButton3.Checked = true;                    break;            }        }        private void StartGame_Click(object sender, EventArgs e)        {            Clock = new Timer();            Clock.Interval = 1000;            Clock.Start();            Clock.Tick += new EventHandler(Timer_Tick);            StartGame.Visible = false;            CheckAnswer.Visible = true;            Question.Visible = true;            Answer.Visible = true;            NextQuestion.Visible = true;            Feedback.Text = "";            Feedback.Visible = true;            label1.Visible = true;            Gen_Question();        }        private void Gen_Question()        {            no1 = randomNo.Next(100);            no2 = randomNo.Next(100);            if (radioButton1.Checked)            {                problemType = "+";            }            else if (radioButton2.Checked)            {                problemType = "-";            }            else if (radioButton3.Checked)            {                problemType = "*";            }            Question.Text = no1 + problemType + no2;        }        private void CheckAnswer_Click(object sender, EventArgs e)        {            int result = 0;            if (problemType == "+")            {                result = no1 + no2;            }            else if (problemType == "-")            {                result = no1 - no2;            }            else if (problemType == "*")            {                result = no1 * no2;            }            if (Answer.Text == result.ToString())            {                Feedback.Text = "Correct";            }            else            {                Feedback.Text = "Incorrect";            }        }        private void NextQuestion_Click(object sender, EventArgs e)        {            x = 30;            Clock.Start();            this.label1.Text = "" + x;            Gen_Question();            Answer.Text = "";            Feedback.Text = "";        }        int x = 30;        private void Timer_Tick(object sender, EventArgs e)        {            if (x == 1) { Clock.Stop(); }            x = x - 1;            this.label1.Text = "" + x;            if (x == 0) { stop(); }        }        private void stop()        {            this.Question.Text = "Time's Up";        }        private void Answer_KeyDown(object sender, KeyEventArgs e)        {            if (e.KeyCode == Keys.Enter)            {                int result = 0;                if (problemType == "+")                {                    result = no1 + no2;                }                else if (problemType == "-")                {                    result = no1 - no2;                }                else if (problemType == "*")                {                    result = no1 * no2;                }                if (Answer.Text == result.ToString())                {                    Feedback.Text = "Correct";                }                else                {                    Feedback.Text = "Incorrect";                }            }        }        private void NextQuestion_KeyDown(object sender, KeyEventArgs e)        {            if (e.KeyCode == Keys.Space)            {                x = 30;                Clock.Start();                this.label1.Text = "" + x;                Gen_Question();                Answer.Text = "";                Feedback.Text = "";            }        }    }}


Now it does look like that but random radioButton idea still doesn't work.
There's two problems with the code:

First, you have to add this code to the start of PickRandomOperation:

radioButton1.Checked = false;

Otherwise, you can have two radiobuttons that are checked at the same time.

Second, you have to call PickRandomOperation somewhere. Where do you want to choose a random operation? At the start of each question? Then call it at the start of Gen_Question. (You can use null for both of its arguments.)
I have redone this part of the code:

public partial class Form1 : Form    {        Random randomNo = new Random();        int no1;        int no2;        string problemType = "+";        Random randomNumber = new Random();        public Form1()        {            InitializeComponent();            Answer.KeyDown += new KeyEventHandler(Answer_KeyDown);            Answer.KeyDown += new KeyEventHandler(NextQuestion_KeyDown);        }        private void PickRandomOperation(object sender, EventArgs e)        {        darkart:            int tempNumber = randomNumber.Next(4);            if (tempNumber == 0) { goto darkart; }            if (tempNumber == 1)            {                radioButton1.Checked = true;                radioButton2.Checked = false;                radioButton3.Checked = false;            }            if (tempNumber == 2)            {                radioButton1.Checked = false;                radioButton2.Checked = true;                radioButton3.Checked = false;            }            if (tempNumber == 3)            {                radioButton1.Checked = false;                radioButton2.Checked = false;                radioButton3.Checked = true;            }        }
class Form1 { ... // UI controls RadioButton[] options; public Form1(){  ... // other stuff  options = new RadioButton[]{radioButton1, radioButton2, radioButton3}; }  void PickRandomChoice(){  for(int i = 0; i < options.Length; i++) options.Checked = false;  options[Random.Next(options.Length)].Checked = true; }}


Edit: wtf happened with the tags there?
A few quick comments on your code (NotSlyPee's, not Bob Janova's).

  • You have two instances of Random - randomNo, randomNumber. You only need one. Random numbers are random, so it really doesn't matter if you use the same Random generator for many different things.

  • PickRandomOperation isn't used as an event handler, so you don't need the wasted sender and e parameters. I'd suggest removing them.

  • Your method for "skipping" 0 in PickRandomOperation is unnecessary. Instead, call Next(3), and check for 0, 1, or 2. It's wasteful to keep generating random numbers that way, and doing it with a goto isn't the nicest method either. If you really need to do that, use a while loop instead.

  • Each of your if() statements in PickRandomOperation is mutually exclusive. That is, if tempNumber is 1, it can't be 2, or 3. Instead of having three separate if() statements, you should use if() and else if():

    if(tempNumber == 0){ // do stuff}else if(tempNumber == 1){ // do stuff}else if(tempNumber == 2){ // do stuff}

    Suppose tempNumber is 0. With separate if() statements, your program will go "hey, it's 0... do the stuff for 0". Then, it will check again at the next if(): "nope, it's not 1, it's 0". Then it'll check again at the next if(): "nope, it's not 2, it's 0". When you use else if(), the program will say "hey, it's 0... do the stuff for 0... and all these other 'else' things I can ignore because I already took care of it".

    This is a good habit to get into, because it helps you remember that only one of those things can happen at a time, and it can sometimes make your program more efficient (if you're doing complex things in the if() check).

    Even better still, use a switch like Pseudonym illustrated above.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement