对于我的部分代码,有一个部分一遍又一遍地使用相同的 if 语句,唯一的区别是其中一个变量。有没有更好的方法来构建我的代码?
if (buttonArray[m, j].BackColor == levelTwo && buttonArray[m, a].BackColor == levelTwo
&& buttonArray[i, j].BackColor == levelTwo)
{
buttonArray[i, j].BackColor = levelThree;
buttonArray[m, j].BackColor = Color.Transparent;
buttonArray[m, a].BackColor = Color.Transparent;
}
if (buttonArray[m, j].BackColor == levelThree && buttonArray[m, a].BackColor == levelThree
&& buttonArray[i, j].BackColor == levelThree)
{
buttonArray[i, j].BackColor = levelFour;
buttonArray[m, j].BackColor = Color.Transparent;
buttonArray[m, a].BackColor = Color.Transparent;
}
if (buttonArray[m, j].BackColor == levelFour && buttonArray[m, a].BackColor == levelFour
&& buttonArray[i, j].BackColor == levelFour)
{
buttonArray[i, j].BackColor = levelFive;
buttonArray[m, j].BackColor = Color.Transparent;
buttonArray[m, a].BackColor = Color.Transparent;
}
if (buttonArray[m, j].BackColor == levelFive && buttonArray[m, a].BackColor == levelFive
&& buttonArray[i, j].BackColor == levelFive)
{
buttonArray[i, j].BackColor = levelSix;
buttonArray[m, j].BackColor = Color.Transparent;
buttonArray[m, a].BackColor = Color.Transparent;
}
if (buttonArray[m, j].BackColor == levelSix && buttonArray[m, a].BackColor == levelSix
&& buttonArray[i, j].BackColor == levelSix)
{
buttonArray[i, j].BackColor = levelSeven;
buttonArray[m, j].BackColor = Color.Transparent;
buttonArray[m, a].BackColor = Color.Transparent;
}
if (buttonArray[m, j].BackColor == levelSeven && buttonArray[m, a].BackColor == levelSeven && buttonArray[i, j].BackColor == levelSeven)
{
buttonArray[i, j].BackColor = levelEight;
buttonArray[m, j].BackColor = Color.Transparent;
buttonArray[m, a].BackColor = Color.Transparent;
}