954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Producing Random Colours For A Group Of Buttons

Hi there,

I have an assignment for University that requires me to use a grid of buttons in any way I want to create some sort of game.

I've chosen to make mine like a Bandit machine:
Press "Spin!" Button
3x3 Grid Of Buttons Randomize Their Colours (Either Red, Green, Blue or Gold)
If Middle 3 Match In Colour, Award Points
Screenshot

So as you can see from the list and screenshot above, I have a 3x3 grid of buttons all with a grey background to begin with. I then want them to randomly pick a colour from Red, Green, Blue or Gold when the "Spin!" button is pressed. Then if the middle 3 match up in colour then I award the player with points.

The assignment MUST use buttons for this, so please don't leave any suggestions for using alternate elements.

Any help you can give me would be much appreciated.

Cheers,

Andy.

Abysinian
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

UPDATE:

(I couldn't find where to edit my original post)

I've managed to make some progress. I've got it so the entire grid randomizes, but the problem is they all randomize to the SAME colour.

This is the code I'm using:

private void spin_Click(object sender, EventArgs e)
        {
            topleft.BackColor = randomColourArray[RandomNumber()];
            topmiddle.BackColor = randomColourArray[RandomNumber()];
            topright.BackColor = randomColourArray[RandomNumber()];

            middleleft.BackColor = randomColourArray[RandomNumber()];
            middlemiddle.BackColor = randomColourArray[RandomNumber()];
            middleright.BackColor = randomColourArray[RandomNumber()];

            bottomleft.BackColor = randomColourArray[RandomNumber()];
            bottommiddle.BackColor = randomColourArray[RandomNumber()];
            bottomright.BackColor = randomColourArray[RandomNumber()];
        }

        private int RandomNumber()
        {
            Random random = new Random();
            int num = random.Next(0,4);
            return num;
        }
Abysinian
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

SOLVED!

Amended my code to this:

private void randomizeButtons()
        {
            Random random = new Random();

            topleft.BackColor = randomColourArray[random.Next(4)];
            topmiddle.BackColor = randomColourArray[random.Next(4)];
            topright.BackColor = randomColourArray[random.Next(4)];

            middleleft.BackColor = randomColourArray[random.Next(4)];
            middlemiddle.BackColor = randomColourArray[random.Next(4)];
            middleright.BackColor = randomColourArray[random.Next(4)];

            bottomleft.BackColor = randomColourArray[random.Next(4)];
            bottommiddle.BackColor = randomColourArray[random.Next(4)];
            bottomright.BackColor = randomColourArray[random.Next(4)];

            checkWin();
        }
Abysinian
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You