【问题标题】:Code to check all win combinations in Tic Tac Toe检查井字游戏中所有获胜组合的代码
【发布时间】:2016-01-28 17:20:39
【问题描述】:

我正在用 c# 编写一个井字游戏程序,为了检查玩家是否获胜,我目前有 16 个 if 语句(xo 都有 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")

【问题讨论】:

    标签: c# algorithm


    【解决方案1】:

    你可以这样做:

    if (button1.Text == button2.Text && button2.Text == button3.Text)
       MessageBox.Show("Congratulations! " +button1.Text +" wins!", "Winner!");                
    

    等等。

    【讨论】:

      【解决方案2】:

      或者您可以创建一个 3x3 按钮数组,然后您可以使用索引方法访问它们,即 button[row,column]

      【讨论】:

        猜你喜欢
        • 2015-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多