我有一个 if else 语句来确定用户是否赢得了我的井字游戏,但这是我的任务!:) 为了使代码更紧凑、更易于阅读,我有很多重复的代码块并减少了我拥有的代码量。
这是我的一些代码:
if (button1.Text == "X" & button2.Text == "X" & button3.Text == "X")
{
foreach (Button button in addButton())
{
button1.BackColor = Color.Green;
button2.BackColor = Color.Green;
button3.BackColor = Color.Green;
button.Enabled = false;
}
}
else if (button1.Text == "X" & button4.Text == "X" & button7.Text == "X")
{
foreach (Button button in addButton())
{
button1.BackColor = Color.Green;
button4.BackColor = Color.Green;
button7.BackColor = Color.Green;
button.Enabled = false;
}
}
else if (button1.Text == "X" & button5.Text == "X" & button9.Text == "X")
{
foreach (Button button in addButton())
{
button1.BackColor = Color.Green;
button4.BackColor = Color.Green;
button7.BackColor = Color.Green;
button.Enabled = false;
}
}
我在想也许我可以把它放在一个私有方法中?但不确定你是否真的可以而且我不知道如何调用该方法,甚至是一个类?