Need help with coloring the background of multiple Picture Boxes in C#

For a school project we are making a connect four game (using visual studio). This proves to be more difficult than we originally imagined. I simply cannot figure out how to color the picture boxes accordingly (red for player, yellow for AI) without making an if thingy for every picturebox. Since there are 42, I do not want to do this.

I have the data for the picture boxes in an array (data: the color they are supposed to be) like so:

public int[][] data = new int[7][]{ //0 = none, 1 = red 2 = yellow new int[6]{0,0,0,0,0,0}, //0 new int[6]{0,0,0,0,0,0}, //1 new int[6]{0,0,0,0,0,0}, //etc. new int[6]{0,0,0,0,0,0}, new int[6]{0,0,0,0,0,0}, new int[6]{0,0,0,0,0,0}, new int[6]{0,0,0,0,0,0} }; 

This is how the GUI looks, the squares are the picture boxes I want to color, the buttons is where the player would select a row to drop his "piece" in.

  1. This is how I would want the program to work: *
  2. Player pushes a button, which selects a row. *
  3. The program looks for the bottom most empty sqaure and changes the int in the array to 1, indicating its a red square now.

  4. The AI decides where it wants to place its piece, and changes the data to 2

  5. The program then draws the playing field as indicated in the array, listed above.

What I wanted to do for step 5 is run down an array wit hall the picture boxes in it, and compare its color to the saved value in the data array, and change the picture box if necessary.

I tried making a object array in a separate class, but I didn't find the picture boxes and I have no idea how it would see them. I don't want to write all my code in the same thing as the event handlers are, that would be a mess.

Here is a download link to the project, if anyone wants to check it out. Its via mediafre, if anyone wants, here is a link to it in my google drive.

I'm fairly new to c# and visual studio, and what i know i taught myself, so if anyone can help whatsoever, that would be greatly appreciated.

Thanks for taking the time to read!

by Riock via /r/csharp

Leave a Reply