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:

  1. Press "Spin!" Button
  2. 3x3 Grid Of Buttons Randomize Their Colours (Either Red, Green, Blue or Gold)
  3. If Middle 3 Match In Colour, Award Points


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.




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


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)];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.