Texo
Texo

Reputation: 47

How to keep all my button.backcolor different from the others

I dont want a Color to be shown twice in simple words: i dont want 2 or more Buttons with the same backcolors ( red,red, yellow, blue) OR (RED,RED,BLUE,BLUE) and so on.. They all have to be different like in here: (yellow,blue,red,purple) which basically means that each Color can be shown once.

Im using following Colors

Purple Red Orange Yellow Blue Green

enter image description here

            }
        }

        Random random = new Random();


        List<Color> possibleColors = new List<Color>()
{
    Color.Red,
    Color.Green,
    Color.Orange,
    Color.Blue,
    Color.Purple,
    Color.Yellow,

};



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

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

}

Upvotes: 1

Views: 80

Answers (6)

Texo
Texo

Reputation: 47

I dont want a Color to be shown twice in simple words: i dont want 2 same colours in the boxes for example ( red,red, yellow, blue) OR (RED,RED,BLUE,BLUE) and so on.. They all have to be different like in here: (yellow,blue,red,purple) which basically means that each Color can be shown once.

Im using following Colors

Purple Red Orange Yellow Blue Green

Upvotes: 0

Woef
Woef

Reputation: 1

So your question I understand it in two ways the first is that you want each button to have unique color the following solution will give you that result:

    private List<Color> selectedColors;

    private Color GetRandomColorOfLoist(Boolean newGame)
    {
        Color possibleColor;
        if (newGame)
        {
            selectedColors.Clear();
        }
        possibleColor = possibleColors[random.Next(0, possibleColors.Count)];
        while (selectedColors.Contains(possibleColor))
        {
            possibleColor = possibleColors[random.Next(0, possibleColors.Count)];

        }
        selectedColors.Add(possibleColor);
        return possibleColor;


    }

The second is you want a button not to have a previous selected color this solution will give you that result:

    private Color GetRandomColorOfLoist(int index)
    {
        Color possibleColor;
        if (selectedColors.Count < 4)
        {
            possibleColor = possibleColors[random.Next(0, possibleColors.Count)];
            selectedColors.Add(possibleColor);
            return possibleColor;
        }
        possibleColor = possibleColors[random.Next(0, possibleColors.Count)];
        while (possibleColor == selectedColors[index])
        {
            possibleColor = possibleColors[random.Next(0, possibleColors.Count)];
        }

        selectedColors[index] = possibleColor;

        return possibleColor;


    }

Upvotes: 0

Sinatr
Sinatr

Reputation: 21989

GetRandomColorOfLoist needs to know already taken colors. One possibility is to remove taken colors from the list (and you need a way to reset list)

List<Color> _default = new List<Color> { Color.Red, Color.Blue, ... };
List<Color> _colors;
Random _rnd = new Random();

void ResetRandomColor() => _colors = _default.ToList(); // copy

Color GetRandomColorOfLoist()
{
    if (_colors == null || _colors.Count == 0)
        throw new InvalidOperationException();
    var index = _rnd.Next(0, _colors.Count);
    try { return _colors[index]; }
    finally { _colors.RemoveAt(index); }
}

Usage:

ResetRandomColor();
new List<Button> { button1, button2, button3, button4 }.
    ForEach(o => o.BackColor = GetRandomColorOfLoist());

Upvotes: 0

Quentin Roger
Quentin Roger

Reputation: 6538

You can create an extension for IList<T>

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;  
    }  
}

To get your colors :

var colors= possibleColors.Shuffle().Take(4)

button1.BackColor =  colors[0];
button2.BackColor =  colors[1];
button3.BackColor =  colors[2];
button4.BackColor =  colors[3];

EDIT

Shuffle implementation comes from : https://stackoverflow.com/a/1262619/5703316

Upvotes: 1

NWessel
NWessel

Reputation: 75

Make an if statement, like

button1.BackColor = GetRandomColorOfLoist();
var temp = GetRandomColorOfLoist();
if(!button1.backColor == temp)
{
button2.BackColor = temp;
}

and some kindda of loop to repeat the process until it goes inside the loop

Upvotes: 0

amit dayama
amit dayama

Reputation: 3326

Still a few things aren't clear but you can do it following way:

Create an array of Color

When you execute button1.BackColor = GetRandomColorOfLoist();

Add the color to that array say ColorArray

and in your GetRandomColorOfLoist() function do something like:

Color TempColor;
do{
TempColor=possibleColors[random.Next(0, possibleColors.Count)];
}While(ColorArray.Contains(TempColor));
return TempColor;

Upvotes: 0

Related Questions