【发布时间】:2016-01-28 17:20:39
【问题描述】:
我正在用 c# 编写一个井字游戏程序,为了检查玩家是否获胜,我目前有 16 个 if 语句(x 和 o 都有 8 个选项)。 (按钮的编号类似于电话拨号盘)。
简短示例:
if (button1.Text == "X" && button2.Text == "X" && button3.Text=="X")//horizontal top X
{
MessageBox.Show("Congratulations! X wins!", "Winner!");
}
if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O")//horizontal top O
{
MessageBox.Show("Congratulations! O wins!", "Winner!");
}
if (button4.Text == "X" && button5.Text == "X" && button6.Text == "X")//horizontal middle X
{
MessagBox.Show("Congratulations! X wins!");
}
if (button4.Text == "O" && button5.Text == "O" && button6.Text == "O")//horizontal middle O
{
MesageBox.Show("Congratulations! O wins!", "Winner!");
}
有没有办法说“下一个按钮”/按钮索引?,因为那样我就可以做到:
if (button.Text==NextButton.Text==NextButton.Text)
{
MessageBox.Show("Congratulations! X wins!", "Winner!");
}
或:
if (button[i].Text=="X" && button[i++].Text=="X" && button[i+=2].Text=="X")
【问题讨论】: