-4

Im trying to make my Start button generate a Color in all of those 4 boxes marked on the top of the Picture. The Colors should not be the same. Its supposed to work like Lotto but with Colors instead of numbers. Can anyone please tell me whats wrong with my code?

Picture

And thats my code;

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            buttontest.BackColor = GetRandomColor();
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Color RandomColor = GetRandomColor();
            buttontest.BackColor = GetRandomColor();
            buttontest.Refresh();
        }

        private Random random;

        private void MainForm_Load(object sender, EventArgs e)
        {
            //Create a new instance of the random class
            random = new Random();
        }

        private Color GetRandomColor()
        {
            return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
        }

        private void buttontest_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            buttontest.BackColor = Color.Red;

        }


    }
}
Texo
  • 47
  • 11

2 Answers2

1

The answer to your question is something like that:

Random random = new Random();

private Color GetRandomColor()
{
    return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}

private void btnStart_Click(object sender, EventArgs e)
{
    button1.BackColor = GetRandomColor();
    button2.BackColor = GetRandomColor();
    button3.BackColor = GetRandomColor();
    button4.BackColor = GetRandomColor();
}

But as I know the game MasterMind you want to have a specific set of colors. Then the code should be something like that:

List<Color> possibleColors = new List<Color>()
{
    Color.Red,
    Color.Green,
    Color.Gold,
    Color.Blue
};

private Color GetRandomColorOfList()
{
    return possibleColors[random.Next(0, possibleColors.Count)];
}

private void button5_Click(object sender, EventArgs e)
{
    button1.BackColor = GetRandomColorOfList();
    button2.BackColor = GetRandomColorOfList();
    button3.BackColor = GetRandomColorOfList();
    button4.BackColor = GetRandomColorOfList();
}
0

You can use the list suggested by Michael Seidel above and shuffle it when the Start button is clicked using, for example, the Fisher-Yates algorithm implemented here.

private static Random rng = new Random();  

public static void Shuffle<T>(this IList<T> list)  
{  
    int n = list.Count;  
    while (n > 1) {  
        n--;  
        int k = rng.Next(n + 1);  
        T value = list[k];  
        list[k] = list[n];  
        list[n] = value;  
    }  
}
List<Color> possibleColors = new List<Color>()
{
    Color.Red,
    Color.Green,
    Color.Gold,
    Color.Blue
};

private void button5_Click(object sender, EventArgs e)
{
    possibleColors.Shuffle();
    button1.BackColor = possibleColors[0];
    button2.BackColor = possibleColors[1];
    button3.BackColor = possibleColors[2];
    button4.BackColor = possibleColors[3];
}
Community
  • 1
  • 1
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27