麻烦的tic tac toe程序

时间:2015-12-14 02:49:17

标签: vb.net tic-tac-toe

我在Visual Basic中制作了一个tic tac toe的程序。我特别难以检查获胜者。 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

1 个答案:

答案 0 :(得分:3)

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