【问题标题】:Trouble with tic tac toe program井字游戏程序有问题
【发布时间】:2015-12-14 11:08:48
【问题描述】:

我正在为 Visual Basic 中的井字游戏制作程序。我特别难以检查获胜者。 O 可以正常工作,但程序说,当连续只有两个时(或者如果 x 放在中间),X 是赢家。

这是我的 CheckWin 子:

  Sub CheckWin()
    If ctr < 9 And board(0, 0) = 2 Or board(0, 1) = 2 Or board(1, 0) = 2 Or board(1, 1) = 2 Or board(1, 2) = 2 Or board(2, 1) = 2 Or board(2, 2) = 2 Then
        If board(0, 0) = 0 And board(1, 1) = 0 And board(2, 2) = 0 Then
            MsgBox("X is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 0) = 1 And board(1, 1) = 1 And board(2, 2) = 1 Then
            MsgBox("O is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 2) = 0 And board(1, 1) = 0 And board(2, 0) = 0 Then
            MsgBox("X is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 2) = 1 And board(1, 1) = 1 And board(2, 0) = 1 Then
            MsgBox("O is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 0) = 0 And board(1, 0) = 0 And board(2, 0) = 0 Then
            MsgBox("X is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 0) = 1 And board(1, 0) = 1 And board(2, 0) = 1 Then
            MsgBox("O is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 0) = 0 And board(0, 1) = 0 And board(0, 2) = 0 Then
            MsgBox("X is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 0) = 1 And board(0, 1) = 1 And board(0, 2) = 1 Then
            MsgBox("O is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 1) = 0 And board(1, 1) = 0 And board(2, 1) = 0 Then
            MsgBox("X is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 1) = 1 And board(1, 1) = 1 And board(2, 1) = 1 Then
            MsgBox("O is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 2) = 0 And board(1, 2) = 0 And board(2, 2) = 0 Then
            MsgBox("X is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(0, 2) = 1 And board(1, 2) = 1 And board(2, 2) = 1 Then
            MsgBox("O is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(1, 0) = 0 And board(1, 1) = 0 And board(1, 2) = 0 Then
            MsgBox("X is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(1, 0) = 1 And board(1, 1) = 1 And board(1, 2) = 1 Then
            MsgBox("O is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(2, 0) = 0 And board(2, 1) = 0 And board(2, 2) = 0 Then
            MsgBox("X is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        ElseIf board(2, 0) = 1 And board(2, 1) = 1 And board(2, 2) = 1 Then
            MsgBox("O is the winner.", MsgBoxStyle.Exclamation)
            Call disable()
        End If
    End If

        If ctr = 9 Then
            MsgBox("The game is a tie.", MsgBoxStyle.Exclamation)
            Call disable()
        End If


End Sub
Sub disable()
    Pic00.Enabled = False
    Pic01.Enabled = False
    Pic02.Enabled = False
    Pic10.Enabled = False
    Pic11.Enabled = False
    Pic12.Enabled = False
    Pic20.Enabled = False
    Pic21.Enabled = False
    Pic22.Enabled = False
End Sub

【问题讨论】:

    标签: vb.net tic-tac-toe


    【解决方案1】:

    你正在做的事情是不必要的复杂。我会完全重写而不是调试你所拥有的。让board(i,j) = 0 代表一个空方格,board(i,j) = -1 代表一个Xboard(i,j) = 1 代表和O。然后使用循环来计算行、列和对角线总和。如果任何和为 -3,X 获胜,如果任何和为 3,O 获胜。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多