2 Replies - 5192 Views - Last Post: 27 July 2010 - 03:28 AM Rate Topic: -----

#1 FreakShow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-July 10

Resistor Color Code Game

Posted 26 July 2010 - 04:31 PM

i have been working on a mini assignment given by my school using c# to do so i do hope people out there could figure out this for me.

The game is to match the colour code of the resistor with a randomly pick value,

something like this...

Attached Image

basically The colour bands of the resistor shown above are using “label”. One can select/choose the colour of the band through the “combo box”

The value display at the output label beside the “Random Resistor Value:” is a randomly generated value,

The other value display at the label beside the “Answer” button is the selected combo box value

Once the selected combo box value is confirmed, press the “Answer” button to check if the answer is correct,Once the answer is correct, a messagebox displaying the word “Correct” will be shown and if the answer is wrong, the word “Wrong” will be displayed.

this is wat i have done so far...
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int OrangeRes = 1000;
            int RedRes = 900;
            int YellowRes = 1100;
            int GreenRes = 1200;
            int BlueRes = 1300;
            int IndigoRes = 1400;
            int GrayRes = 1500;
            int WhiteRes = 1600;

            if (comboBox1.SelectedIndex == 0)
            {
                lbl1stband.BackColor = Color.Red;
                lblAnswer.Text = Convert.ToString(RedRes + "Ω +/- 10%");
                
            }

            else if (comboBox1.SelectedIndex == 1)
            {
                lbl1stband.BackColor = Color.Orange;
                lblAnswer.Text = Convert.ToString(OrangeRes + "Ω +/- 10%");
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                lbl1stband.BackColor = Color.Yellow;
                lblAnswer.Text = Convert.ToString(YellowRes+ "Ω +/- 10%");
            }
            else if (comboBox1.SelectedIndex == 3)
            {
                lbl1stband.BackColor = Color.Green;
                lblAnswer.Text = Convert.ToString(GreenRes + "Ω +/- 10%");
            }
            else if (comboBox1.SelectedIndex == 4)
            {
                lbl1stband.BackColor = Color.Blue;
                lblAnswer.Text = Convert.ToString(BlueRes + "Ω +/- 10%");
            }
            else if (comboBox1.SelectedIndex == 5)
            {
                lbl1stband.BackColor = Color.Indigo;
                lblAnswer.Text = Convert.ToString(IndigoRes + "Ω +/- 10%");
            }
            else if (comboBox1.SelectedIndex == 6)
            {
                lbl1stband.BackColor = Color.Gray;
                lblAnswer.Text = Convert.ToString(GrayRes + "Ω +/- 10%");
            }
            else if (comboBox1.SelectedIndex == 7)
            {
                lbl1stband.BackColor = Color.White;
                lblAnswer.Text = Convert.ToString(WhiteRes + "Ω +/- 10%");
            }


        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int OrangeRes = 1000;
            int RedRes = 900;
            int YellowRes = 1100;
            int GreenRes = 1200;
            int BlueRes = 1300;
            int IndigoRes = 1400;
            int GrayRes = 1500;
            int WhiteRes = 1600;

            if (comboBox2.SelectedIndex == 0)
            {
                lbl2ndband.BackColor = Color.Red;
                lblAnswer.Text = Convert.ToString(RedRes + "Ω +/- 10%");
            }

            else if (comboBox2.SelectedIndex == 1)
            {
                lbl2ndband.BackColor = Color.Orange;
                lblAnswer.Text = Convert.ToString(OrangeRes + "Ω +/- 10%");
            }
            else if (comboBox2.SelectedIndex == 2)
            {
                lbl2ndband.BackColor = Color.Yellow;
                lblAnswer.Text = Convert.ToString(YellowRes + "Ω +/- 10%");
            }
            else if (comboBox2.SelectedIndex == 3)
            {
                lbl2ndband.BackColor = Color.Green;
                lblAnswer.Text = Convert.ToString(GreenRes + "Ω +/- 10%");
            }
            else if (comboBox2.SelectedIndex == 4)
            {
                lbl2ndband.BackColor = Color.Blue;
                lblAnswer.Text = Convert.ToString(BlueRes + "Ω +/- 10%");
            }
            else if (comboBox2.SelectedIndex == 5)
            {
                lbl2ndband.BackColor = Color.Indigo;
                lblAnswer.Text = Convert.ToString(IndigoRes + "Ω +/- 10%");
            }
            else if (comboBox2.SelectedIndex == 6)
            {
                lbl2ndband.BackColor = Color.Gray;
                lblAnswer.Text = Convert.ToString(GrayRes + "Ω +/- 10%");
            }
            else if (comboBox2.SelectedIndex == 7)
            {
                lbl2ndband.BackColor = Color.White;
                lblAnswer.Text = Convert.ToString(WhiteRes + "Ω +/- 10%");
            }


        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int OrangeRes = 1000;
            int RedRes = 900;
            int YellowRes = 1100;
            int GreenRes = 1200;
            int BlueRes = 1300;
            int IndigoRes = 1400;
            int GrayRes = 1500;
            int WhiteRes = 1600;

            if (comboBox3.SelectedIndex == 0)
            {
                lbl3rdband.BackColor = Color.Red;
                lblAnswer.Text = Convert.ToString(RedRes + "Ω +/- 10%");
            }

            else if (comboBox3.SelectedIndex == 1)
            {
                lbl3rdband.BackColor = Color.Orange;
                lblAnswer.Text = Convert.ToString(OrangeRes + "Ω +/- 10%");
            }
            else if (comboBox3.SelectedIndex == 2)
            {
                lbl3rdband.BackColor = Color.Yellow;
                lblAnswer.Text = Convert.ToString(YellowRes + "Ω +/- 10%");
            }
            else if (comboBox3.SelectedIndex == 3)
            {
                lbl3rdband.BackColor = Color.Green;
                lblAnswer.Text = Convert.ToString(GreenRes + "Ω +/- 10%");
            }
            else if (comboBox3.SelectedIndex == 4)
            {
                lbl3rdband.BackColor = Color.Blue;
                lblAnswer.Text = Convert.ToString(BlueRes + "Ω +/- 10%");
            }
            else if (comboBox3.SelectedIndex == 5)
            {
                lbl3rdband.BackColor = Color.Indigo;
                lblAnswer.Text = Convert.ToString(IndigoRes + "Ω +/- 10%");
            }
            else if (comboBox3.SelectedIndex == 6)
            {
                lbl3rdband.BackColor = Color.Gray;
                lblAnswer.Text = Convert.ToString(GrayRes + "Ω +/- 10%");
            }
            else if (comboBox3.SelectedIndex == 7)
            {
                lbl3rdband.BackColor = Color.White;
                lblAnswer.Text = Convert.ToString(WhiteRes + "Ω +/- 10%");
            }
            
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            int OrangeRes = 1000;
            int RedRes = 900;
            int YellowRes = 1100;
            int GreenRes = 1200;
            int BlueRes = 1300;
            int IndigoRes = 1400;
            int GrayRes = 1500;
            int WhiteRes = 1600;

            if (comboBox4.SelectedIndex == 0)
            {
                lbl4thband.BackColor = Color.Red;
                lblAnswer.Text = Convert.ToString(RedRes + "Ω +/- 10%");
            }

            else if (comboBox4.SelectedIndex == 1)
            {
                lbl4thband.BackColor = Color.Orange;
                lblAnswer.Text = Convert.ToString(OrangeRes + "Ω +/- 10%");
            }
            else if (comboBox4.SelectedIndex == 2)
            {
                lbl4thband.BackColor = Color.Yellow;
                lblAnswer.Text = Convert.ToString(YellowRes + "Ω +/- 10%");
            }
            else if (comboBox4.SelectedIndex == 3)
            {
                lbl4thband.BackColor = Color.Green;
                lblAnswer.Text = Convert.ToString(GreenRes + "Ω +/- 10%");
            }
            else if (comboBox4.SelectedIndex == 4)
            {
                lbl4thband.BackColor = Color.Blue;
                lblAnswer.Text = Convert.ToString(BlueRes + "Ω +/- 10%");
            }
            else if (comboBox4.SelectedIndex == 5)
            {
                lbl4thband.BackColor = Color.Indigo;
                lblAnswer.Text = Convert.ToString(IndigoRes + "Ω +/- 10%");
            }
            else if (comboBox4.SelectedIndex == 6)
            {
                lbl4thband.BackColor = Color.Gray;
                lblAnswer.Text = Convert.ToString(GrayRes + "Ω +/- 10%");
            }
            else if (comboBox4.SelectedIndex == 7)
            {
                lbl4thband.BackColor = Color.White;
                lblAnswer.Text = Convert.ToString(WhiteRes + "Ω +/- 10%");
            }


Its in a big mess and i hope someone would check this out for me.
i can't figure out how to add the selected combobox's colour to another combobox's selected colour.

Edited by macosxnerd101: Welcome to DIC! :) Please remember to post your code using code tags, like so: :code:.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Resistor Color Code Game

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 995
  • View blog
  • Posts: 2,385
  • Joined: 04-October 09

Re: Resistor Color Code Game

Posted 26 July 2010 - 09:33 PM

You need to learn switch/case, for example, your first band could be:
            switch (comboBox1.SelectedIndex) {
                case 0: lbl1stband.BackColor = Color.Red;
                    lblAnswer.Text = Convert.ToString(RedRes + "Ω +/- 10%");
                    break;
                case 1: lbl1stband.BackColor = Color.Orange;
                    lblAnswer.Text = Convert.ToString(OrangeRes + "Ω +/- 10%");
                    break;
                case 2: lbl1stband.BackColor = Color.Yellow;
                    lblAnswer.Text = Convert.ToString(YellowRes + "Ω +/- 10%");
                    break;
                case 3: lbl1stband.BackColor = Color.Green;
                    lblAnswer.Text = Convert.ToString(GreenRes + "Ω +/- 10%");
                    break;
                case 4: lbl1stband.BackColor = Color.Blue;
                    lblAnswer.Text = Convert.ToString(BlueRes + "Ω +/- 10%");
                    break;
                case 5: lbl1stband.BackColor = Color.Indigo;
                    lblAnswer.Text = Convert.ToString(IndigoRes + "Ω +/- 10%");
                    break;
                case 6: lbl1stband.BackColor = Color.Gray;
                    lblAnswer.Text = Convert.ToString(GrayRes + "Ω +/- 10%");
                    break;
                case 7: lbl1stband.BackColor = Color.White;
                    lblAnswer.Text = Convert.ToString(WhiteRes + "Ω +/- 10%");
                    break;
                default: lblAnswer.Text = "Borked!"; // should never get here
                    break;
            }

Also your color definitions (WhiteRes, etc.) never change so you should move them out of the individual functions and make them constants.

This post has been edited by Momerath: 26 July 2010 - 09:34 PM

Was This Post Helpful? 1
  • +
  • -

#3 FreakShow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-July 10

Re: Resistor Color Code Game

Posted 27 July 2010 - 03:28 AM

i don't get what u trying to say "Also your color definitions (WhiteRes, etc.) never change so you should move them out of the individual functions and make them constants."

but its okay.i will learn.right now i need to add those colour's number to the other but i am stuck.i do not know where to start and keep getting errors.

but first i need to get the random resistor value to work,its like when i debug the program i will get a random value,
then i will click on the answer and the label beside the button will show the total resistor value the colours i selected.
i have to get the total value matching with the random value and i will end my assignment.
sorry but i am not good in c# so i do hope u can help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1