I'm working on a mastermind game and have come to a point where I wonder how to implement the part of the game where the user only gets to fill in the colors for one row at a time.
The solution I have for the moment is posted below and you will instantly realize why it is not acceptable.

currentButton comes as sender.
"b1",
"b2" etc points to the row which was assigned to the name property at the creation of the buttons.
CODE
if (currentButton.Name.StartsWith("b0")
{
currentButton.BackColor = panel2.BackColor;
}
if (currentButton.Name.StartsWith("b1") && currentButtonSet[0].BackColor != Color.Black && currentButtonSet[1].BackColor != Color.Black && currentButtonSet[2].BackColor != Color.Black && currentButtonSet[3].BackColor != Color.Black)
{
currentButton.BackColor = panel2.BackColor;
}
if (currentButton.Name.StartsWith("b2") && currentButtonSet[4].BackColor != Color.Black && currentButtonSet[5].BackColor != Color.Black && currentButtonSet[6].BackColor != Color.Black && currentButtonSet[7].BackColor != Color.Black)
{
currentButton.BackColor = panel2.BackColor;
}
if (currentButton.Name.StartsWith("b3") && currentButtonSet[8].BackColor != Color.Black && currentButtonSet[9].BackColor != Color.Black && currentButtonSet[10].BackColor != Color.Black && currentButtonSet[11].BackColor != Color.Black)
{
currentButton.BackColor = panel2.BackColor;
}
if (currentButton.Name.StartsWith("b4") && currentButtonSet[12].BackColor != Color.Black && currentButtonSet[13].BackColor != Color.Black && currentButtonSet[14].BackColor != Color.Black && currentButtonSet[15].BackColor != Color.Black)
{
currentButton.BackColor = panel2.BackColor;
}
if (currentButton.Name.StartsWith("b5") && currentButtonSet[16].BackColor != Color.Black && currentButtonSet[17].BackColor != Color.Black && currentButtonSet[18].BackColor != Color.Black && currentButtonSet[19].BackColor != Color.Black)
{
currentButton.BackColor = panel2.BackColor;
}
if (currentButton.Name.StartsWith("b6") && currentButtonSet[20].BackColor != Color.Black && currentButtonSet[21].BackColor != Color.Black && currentButtonSet[22].BackColor != Color.Black && currentButtonSet[23].BackColor != Color.Black)
{
currentButton.BackColor = panel2.BackColor;
}
if (currentButton.Name.StartsWith("b7") && currentButtonSet[24].BackColor != Color.Black && currentButtonSet[25].BackColor != Color.Black && currentButtonSet[26]BackColor != Color.Black && currentButtonSet[27].BackColor != Color.Black)
{
currentButton.BackColor = panel2.BackColor;
}
Any suggestions on how I could implement the "row for row" part of the game in a more code effiecient way? Should I use another type than an array for instance, like ArrayList or Hashtable to make it easier?
Thanks.
This post has been edited by Zeddicus: 9 Oct, 2008 - 03:39 AM