0

我在 VB 2010 中编写了一个井字游戏应用程序,它运行良好,但我希望能够跟踪 X 和 O 和/或玩家 1 和玩家 2 的获胜次数。我试过了添加 do while 循环,但我对如何正确实现它们一无所知,我不确定这是否是跟踪胜利的正确方法。任何帮助将不胜感激!这是我的代码:

公开课形式1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub


Private Sub btn11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn11.Click
    'clos the form
    Me.Close()

End Sub

Private Sub btn10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn10.Click
    btn1.Enabled = True
    btn2.Enabled = True
    btn3.Enabled = True
    btn4.Enabled = True
    btn5.Enabled = True
    btn6.Enabled = True
    btn7.Enabled = True
    btn8.Enabled = True
    btn9.Enabled = True
    btn1.Text = ""
    btn2.Text = ""
    btn3.Text = ""
    btn4.Text = ""
    btn5.Text = ""
    btn6.Text = ""
    btn7.Text = ""
    btn8.Text = ""
    btn9.Text = ""
End Sub

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    If lbl2.Text = "X" Then
        btn1.Text = "X"
        lbl2.Text = "O"
    Else
        btn1.Text = "O"
        lbl2.Text = "X"
    End If
    btn1.Enabled = False
End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
    If lbl2.Text = "X" Then
        btn2.Text = "X"
        lbl2.Text = "O"
    Else
        btn2.Text = "O"
        lbl2.Text = "X"
    End If
    Call win()
    btn2.Enabled = False
End Sub

Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
    If lbl2.Text = "X" Then
        btn3.Text = "X"
        lbl2.Text = "O"
    Else
        btn3.Text = "O"
        lbl2.Text = "X"
    End If
    Call win()
    btn3.Enabled = False
End Sub

Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
    If lbl2.Text = "X" Then
        btn4.Text = "X"
        lbl2.Text = "O"
    Else
        btn4.Text = "O"
        lbl2.Text = "X"
    End If
    Call win()
    btn4.Enabled = False
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
    If lbl2.Text = "X" Then
        btn5.Text = "X"
        lbl2.Text = "O"
    Else
        btn5.Text = "O"
        lbl2.Text = "X"
    End If
    Call win()
    btn5.Enabled = False
End Sub

Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
    If lbl2.Text = "X" Then
        btn6.Text = "X"
        lbl2.Text = "O"
    Else
        btn6.Text = "O"
        lbl2.Text = "X"
    End If
    Call win()
    btn6.Enabled = False
End Sub

Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
    If lbl2.Text = "X" Then
        btn7.Text = "X"
        lbl2.Text = "O"
    Else
        btn7.Text = "O"
        lbl2.Text = "X"
    End If
    Call win()
    btn7.Enabled = False
End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
    If lbl2.Text = "X" Then
        btn8.Text = "X"
        lbl2.Text = "O"
    Else
        btn8.Text = "O"
        lbl2.Text = "X"
    End If
    Call win()
    btn8.Enabled = False
End Sub

Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
    If lbl2.Text = "X" Then
        btn9.Text = "X"
        lbl2.Text = "O"
    Else
        btn9.Text = "O"
        lbl2.Text = "X"
    End If
    Call win()
    btn9.Enabled = False
End Sub

Private Sub win()
    If btn1.Text = "X" And btn2.Text = "X" And btn3.Text = "X" Then
        lbl2.Text = "X wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn4.Text = "X" And btn5.Text = "X" And btn6.Text = "X" Then
        lbl2.Text = "X wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn7.Text = "X" And btn8.Text = "X" And btn9.Text = "X" Then
        lbl2.Text = "X wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn1.Text = "X" And btn4.Text = "X" And btn7.Text = "X" Then
        lbl2.Text = "X wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn2.Text = "X" And btn5.Text = "X" And btn8.Text = "X" Then
        lbl2.Text = "X wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn3.Text = "X" And btn6.Text = "X" And btn9.Text = "X" Then
        lbl2.Text = "X wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn1.Text = "X" And btn5.Text = "X" And btn9.Text = "X" Then
        lbl2.Text = "X wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn3.Text = "X" And btn5.Text = "X" And btn7.Text = "X" Then
        lbl2.Text = "X wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn1.Text = "O" And btn2.Text = "O" And btn3.Text = "O" Then
        lbl2.Text = "O wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn4.Text = "O" And btn5.Text = "O" And btn6.Text = "O" Then
        lbl2.Text = "O wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn7.Text = "O" And btn8.Text = "O" And btn9.Text = "O" Then
        lbl2.Text = "O wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn1.Text = "O" And btn4.Text = "O" And btn7.Text = "O" Then
        lbl2.Text = "O wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn2.Text = "O" And btn5.Text = "O" And btn8.Text = "O" Then
        lbl2.Text = "O wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn3.Text = "O" And btn6.Text = "O" And btn9.Text = "O" Then
        lbl2.Text = "O wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn1.Text = "O" And btn5.Text = "O" And btn9.Text = "O" Then
        lbl2.Text = "O wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
    If btn3.Text = "O" And btn5.Text = "O" And btn7.Text = "O" Then
        lbl2.Text = "O wins"
        btn1.Enabled = False
        btn2.Enabled = False
        btn3.Enabled = False
        btn4.Enabled = False
        btn5.Enabled = False
        btn6.Enabled = False
        btn7.Enabled = False
        btn8.Enabled = False
        btn9.Enabled = False
    End If
End Sub

结束类

4

1 回答 1

1

在您的类中声明变量并使用这些变量跟踪胜利。通过将它们保留在类中,而不是您的 Win() 方法,下次有人单击调用 Win() 的按钮时,它们不会被重置为 0。

Private m_intPlayerOneWins As Integer = 0
Private m_intPlayerTwoWins As Integer = 0

然后在你的 Win() 方法中:

'If X Wins
m_intPlayerOneWins += 1

或者

'If O Wins
m_intPlayerTwoWins += 1
于 2013-06-20T21:04:58.470 回答