0

对于我的部分代码,有一个部分一遍又一遍地使用相同的 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;
}
4

3 回答 3

4

你可以创建一个方法(虽然我不知道 Level 的类型......)

private void CheckLevel(int levelIndex)
{
    if (buttonArray[m, j].BackColor == levelArray[levelIndex] && buttonArray[m, a].BackColor == levelArray[levelIndex] 
        && buttonArray[i, j].BackColor == levelArray[levelIndex])
    {
        buttonArray[i, j].BackColor = levelArray[levelIndex + 1];
        buttonArray[m, j].BackColor = Color.Transparent;
        buttonArray[m, a].BackColor = Color.Transparent;
    }

}

并在您的代码中:

CheckLevel(LevelTwoIndex);
CheckLevel(LevelThreeIndex);
CheckLevel(LevelFourIndex);
于 2013-02-06T19:01:36.610 回答
2

看起来您正在尝试做的是每次发生某些事情时循环所有这些按钮的颜色。

这是对该概念的更重要的重写:

首先从创建一个开始,private Queue<Color> colors = new Queue<Color>(); 这将包含您循环使用的所有颜色,按照它们的使用顺序。

我们可以在首次创建表单时填充它:

colors.Enqueue(Color.Red);
colors.Enqueue(Color.Yellow);
colors.Enqueue(Color.Violet);
//Add other colors

现在我们只需要一个方法将“下一个”颜色应用于任意数量的控件:

public void ApplyNextColor(params Control[] controls)
{
    Color nextColor = colors.Dequeue();
    colors.Enqueue(nextColor);//add to end so that we cycle; 
    //you can optionally remove and do nothing if there are not items.

    foreach (Control control in controls)
        control.BackColor = nextColor;
}

然后我们可以从例如按钮单击事件或计时器滴答事件中调用它:

ApplyNextColor(buttonArray[m, j], buttonArray[m, a]);
于 2013-02-06T19:09:31.070 回答
0
if (buttonArray[m, j].BackColor == buttonArray[m, a].BackColor 
        && buttonArray[m, a].BackColor == buttonArray[i, j].BackColor) {
    if (buttonArray[m, j].BackColor == levelThree) { // or a switch - case
        ....
    }
}
于 2013-02-06T19:05:08.770 回答