0

我花了几个小时制作井字游戏,主要是使用 if else 语句,我刚刚复制和粘贴的一些代码......

我如何只创建一个代码实例,然后再参考它?- 当我需要该代码时,而不是每次都复制和粘贴大量代码行。在这段代码中,我得到一个介于 1 到 10 之间的随机数,该数字将存储在 RI 中……然后轮到 Ai 时,计算机将在按钮中随机输入一个 X,然后使用此代码检查确定计算机是否获胜。我希望这是有道理的=)

do
{
    storeRI = rc.Next(0, 10); //storing random number into storeRI so it can be used later on in life. 
    if (storeRI == 1 && btn1.Text == "")
    {
        btn1.Text = "X";
        Turn = 1;
        if (btn1.Text == "X" & btn2.Text == "X" & btn3.Text == "X")
        {
            btn1.BackColor = Color.Green;
            btn2.BackColor = Color.Green;
            btn3.BackColor = Color.Green;

            XScore += 1;
            lblPScoreX.Text = XScore.ToString();

            foreach (Button btn in buttonList)
                 btn.Enabled = false;
        }
        else if (btn1.Text == "X" & btn4.Text == "X" & btn7.Text == "X")
        {
             btn1.BackColor = Color.Green;
             btn4.BackColor = Color.Green;
             btn7.BackColor = Color.Green;

             XScore += 1;
             lblPScoreX.Text = XScore.ToString();

             foreach (Button btn in buttonList)
                btn.Enabled = false;
        }
    }
}

例如,这段代码是一些我至少复制了 10 次的代码,它只是让我的代码看起来很难看,而且很难阅读。

4

5 回答 5

8

例如这段代码,是一些代码,我至少复制了 10 次

任何时候你复制了代码,你都应该尝试将它移动到一个方法中。然后,您可以直接调用该方法。在您的情况下,一种方法可以轻松地接受三个按钮来影响并直接处理它们。

于 2013-01-23T19:55:46.673 回答
2

您可能想要研究的重构技术是Consolidate Duplicate Conditional Fragments

if (btn1.Text == "X" & btn2.Text == "X" & btn3.Text == "X")
{
    btn2.BackColor = Color.Green;
    btn3.BackColor = Color.Green;
}
else if (btn1.Text == "X" & btn4.Text == "X" & btn7.Text == "X")
{
    btn4.BackColor = Color.Green;
    btn7.BackColor = Color.Green;
}
else
{
    return/break/continue; // hard to tell which you what as you have a do without a corresponding while
}

btn1.BackColor = Color.Green;

XScore += 1;
lblPScoreX.Text = XScore.ToString();
foreach (Button btn in buttonList)
{
    btn.Enabled = false;
}
于 2013-01-23T19:57:21.300 回答
2
   bool TestWin(Button btnA, Button btnB, button btnC)
   {
           if (btnA.Text == "X" & btnB.Text == "X" & btnC.Text == "X")
           {
                    btnA.BackColor = Color.Green;
                    btnB.BackColor = Color.Green;
                    btnC.BackColor = Color.Green;

                    XScore += 1;
                    lblPScoreX.Text = XScore.ToString();

                    foreach (Button btn in buttonList)
                    {
                         btn.Enabled = false;
                    }
                    return true;
           }
           return false;
     }


     if (!TestWin(btn1, btn2, btn3))
           TestWin(btn1, btn4, btn7);
于 2013-01-23T20:02:05.033 回答
0
if (btn1.Text == "X")
{
    btn1.BackColor = Color.Green;
    if(btn2.Text == "X" & btn3.Text == "X")
        btn2.BackColor = Color.Green;
        btn3.BackColor = Color.Green;
    }
    else if (btn4.Text == "X" & btn7.Text == "X")
    {
        btn4.BackColor = Color.Green;
        btn7.BackColor = Color.Green;
    }
    XScore += 1;
    lblPScoreX.Text = XScore.ToString();
    foreach (Button btn in buttonList)
    {
        btn.Enabled = false;
    }
}
于 2013-01-23T20:00:23.070 回答
0

长长的 if else 语句没有错;拥有可阅读的长代码比不可管理的短代码要好。

我可以看到可能有帮助的一件事是添加一个方法(进行一些更改以匹配您的类型):

public void ChangeButtonColor(Color thisColor, params Button[] buttons)
{
    foreach (Button thisButton in buttons)
    {
        thisButton.BackColor = thisColor
    }
}

然后你可以添加到你的代码中:

ChangeButtonColor(Color.Green, btn1, btn2, btn3);

这将为您节省一些重复的代码。

于 2013-01-23T20:06:27.227 回答