-1

I've been trying to create the game "Mastermind" and im trying that when I click the "Show" button, it will generate 3 colours from ROYGBIV.(Already done)

However now I need to do a statement where it will compare the random colours generated. This is part of the code

public partial class Form1 : Form
    {
        Color[] RandomColor = new Color[7] { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet };
        Random r = new Random();

        public Form1()
        {
            InitializeComponent();

        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            btnShow.Text = "Hide";

            for (int i = 0; i < RandomColor.Length; i++)
            {
                int RandomColorNum = r.Next(0, RandomColor.Length);

                switch (i)
                {
                    case 1: pnlNPC1.BackColor = RandomColor[RandomColorNum];
                        break;
                    case 2: pnlNPC2.BackColor = RandomColor[RandomColorNum];
                        break;
                    case 3: pnlNPC3.BackColor = RandomColor[RandomColorNum];
                        break;
                }

            } 

            pnlNPC1.Visible = true;
            pnlNPC2.Visible = true;
            pnlNPC3.Visible = true;



         }

Thanks guys, and all the best!

HurpaDerpa
  • 130
  • 1
  • 13
  • why not compare the `ToARGB` values? – Ňɏssa Pøngjǣrdenlarp Dec 26 '14 at 16:47
  • If you want to avoid duplicate colors, you can set colors to a list and then remove the color that is used. I recommend a separate function for that. BTW, you are iterating over the array of colors, not over the NPC panels I assume you wanted. – Joonas Koski Dec 26 '14 at 16:51
  • fyi- I think you need to also do `r.Next(0, RandomColor.Length -1)` or you will get an index out of bounds – Nyra Dec 26 '14 at 16:51
  • 2
    Random.Next is lower bound inclusive and upper bound exclusive. He's safe with the `r.Next` call as-is. http://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx If that change is made, the last element will never be chosen. – dodexahedron Dec 26 '14 at 16:54
  • Thanks for that info @dodexahedron :) (leaving other comment up for context) – Nyra Dec 26 '14 at 16:57
  • 3
    HurpaDerpa, Just [suffle](http://stackoverflow.com/questions/9557883/random-plot-algorithm) your array and take first 3 colors. – L.B Dec 26 '14 at 17:08

1 Answers1

0

Here is a function you can use:

int[] PickIntegers(int amountToPick, int maxSize)
{
    List<int> numbers = new List<int>(amountToPick);

    for (int x = 0; x < amountToPick; x++)
    {
        int n = 0;
        while (true)
        {
            n = r.Next(0, maxSize);
            if (!numbers.Contains(n))
            {
                numbers.Add(n);
                break;
            }
        }
    }

    return numbers.ToArray();
}

Then you can call like this:

private void btnShow_Click(object sender, EventArgs e)
{
    btnShow.Text = "Hide";

    int[] numbers = PickIntegers(3, RandomColor.Length);

    pnlNPC1.BackColor = RandomColor[numbers[0]];
    pnlNPC2.BackColor = RandomColor[numbers[1]];
    pnlNPC3.BackColor = RandomColor[numbers[2]];


    pnlNPC1.Visible = true;
    pnlNPC2.Visible = true;
    pnlNPC3.Visible = true;
 }

Note: This code has not be tested. It might contain typos.

Black Frog
  • 11,595
  • 1
  • 35
  • 66