Visual Basic Tic Tac Toe错误

时间:2014-12-30 19:42:48

标签: visual-studio-2010

嘿大家我试图创造ttt并且它有效但是当我进行试运行并且我让玩家获胜时,最后一个角色并没有显示出来。如果它应该是3 X或O的对齐,计算机将识别获胜的动作并宣布获胜者但btnclicked.text仍然=没有。这里是我的代码,我不知道如何修复它,因此,if语句将识别绘制并生成输出而不进行评估。

Private Sub btnMoveMade_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ btn00.Click, btn01.Click, btn02.Click, btn10.Click, btn11.Click, btn12.Click, btn20.Click, btn21.Click, btn22.Click Dim btnSquareClicked As Button = sender Dim player1 As String = Me.txtName1.Text Dim player2 As String = Me.txtname2.Text Static chrTTT(2, 2) As Char Static player As String = "X" If btnSquareClicked.Text <> Nothing Then MessageBox.Show("Invalid Move.") Else Dim index As String index = btnSquareClicked.Tag Dim x As Integer = Val(index.Chars(0)) Dim y As Integer = Val(index.Chars(1)) Call StoreMove(x, y, player, chrTTT) If IsWinner(chrTTT) Then player = player1 MessageBox.Show(player & "!, Congratulations, You Won!") btnNewGame.Visible = True ElseIf IsWinner2(chrTTT) Then player = player2 MessageBox.Show(player & "!, Congratulations, You won!") btnNewGame.Visible = True ElseIf btn00.Text <> Nothing And btn01.Text <> Nothing And btn02.Text <> Nothing And btn10.Text <> Nothing And btn11.Text <> Nothing _ And btn12.Text <> Nothing And btn20.Text <> Nothing And btn21.Text <> Nothing _ And btn22.Text <> Nothing And IsWinner(chrTTT) = False And IsWinner2(chrTTT) = False Then MessageBox.Show("Aww, it's a draw") Else If player = "X" Then player = "O" btnSquareClicked.Text = "X" Else player = "X" btnSquareClicked.Text = "O" End If End If End If End Sub Sub StoreMove(ByVal x As Integer, ByVal y As Integer, ByVal player As Char, ByRef TTT(,) As Char) TTT(x, y) = player End Sub Function IsWinner(ByRef TTT(,) As Char) As Boolean For row As Integer = 0 To 2 If TTT(row, 0) = TTT(row, 1) And TTT(row, 1) = TTT(row, 2) And TTT(row, 0) = "X" Then Return True End If Next row For col As Integer = 0 To 2 If TTT(0, col) = TTT(1, col) And TTT(1, col) = TTT(2, col) And TTT(0, col) = "X" Then Return True End If Next col If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 2) And TTT(0, 0) = "X" Then Return True End If If TTT(0, 2) = TTT(1, 1) And TTT(1, 1) = TTT(2, 0) And TTT(0, 2) = "X" Then Return True End If Dim movesLeft As Boolean = False For row As Integer = 0 To 2 For col As Integer = 0 To 2 If TTT(row, col) = Nothing Then movesLeft = True End If Next col Next row If Not movesLeft Then Return True End If Return False End Function Function IsWinner2(ByRef TTT(,) As Char) As Boolean For row As Integer = 0 To 2 If TTT(row, 0) = TTT(row, 1) And TTT(row, 1) = TTT(row, 2) And TTT(row, 0) = "O" Then Return True End If Next row For col As Integer = 0 To 2 If TTT(0, col) = TTT(1, col) And TTT(1, col) = TTT(2, col) And TTT(0, col) = "O" Then Return True End If Next col If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 2) And TTT(0, 0) = "O" Then Return True End If If TTT(0, 2) = TTT(1, 1) And TTT(1, 1) = TTT(2, 0) And TTT(0, 2) = "O" Then Return True End If Dim movesLeft As Boolean = False For row As Integer = 0 To 2 For col As Integer = 0 To 2 If TTT(row, col) = Nothing Then movesLeft = True End If Next col Next row If Not movesLeft Then Return True End If Return False End Function End Class

2 个答案:

答案 0 :(得分:0)

在之前设置按钮文字

    If btnSquareClicked.Text <> Nothing Then
        MessageBox.Show("Invalid Move.")
    Else
        btnSquareClicked.Text = player

        ' ... rest of the code ...

    End If

答案 1 :(得分:0)

非常感谢,我只是尝试了它并且它有效,但是消息框假设显示什么时候它不会出现,而不是它仍然告诉我其中一个球员赢了

ElseIf btn00.Text <> Nothing And btn01.Text <> Nothing And btn02.Text <> Nothing And btn10.Text <> Nothing And btn11.Text <> Nothing _ And btn12.Text <> Nothing And btn20.Text <> Nothing And btn21.Text <> Nothing _ And btn22.Text <> Nothing And IsWinner(chrTTT) = False And IsWinner2(chrTTT) = False Then MessageBox.Show("Aww, it's a draw")

这是专门用于检查平局的部分。