我正在尝试用VB创建一个Tic Tac Toe游戏,但我一直收到错误:
StackOverflowExpeption未处理
我已经检查了我的代码并观看了很多教程,仍然无法弄清楚出了什么问题,这里是代码:
Public Class TicTac
Private intSet As Integer = 0 'Movimentos que podes fazer
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
btn1.Text = ""
btn1.Enabled = True
btn2.Text = ""
btn2.Enabled = True
btn3.Text = ""
btn3.Enabled = True
btn4.Text = ""
btn4.Enabled = True
btn5.Text = ""
btn5.Enabled = True
btn6.Text = ""
btn6.Enabled = True
btn7.Text = ""
btn7.Enabled = True
btn8.Text = ""
btn8.Enabled = True
btn9.Text = ""
btn9.Enabled = True
End Sub
'formulário
Private Sub TicTac_Load(sender As Object, e As EventArgs) Handles MyBase.Load
btn1.Text = ""
btn1.Enabled = True
btn2.Text = ""
btn2.Enabled = True
btn3.Text = ""
btn3.Enabled = True
btn4.Text = ""
btn4.Enabled = True
btn5.Text = ""
btn5.Enabled = True
btn6.Text = ""
btn6.Enabled = True
btn7.Text = ""
btn7.Enabled = True
btn8.Text = ""
btn8.Enabled = True
btn9.Text = ""
btn9.Enabled = True
intSet = 0
End Sub
Sub ComputerPlays()
Dim objRandom As New Random
Dim intRandom As Integer
intRandom = objRandom.Next(1, 10)
If intRandom = 1 And btn1.Enabled = True Then
btn1.Text = "O"
btn1.Enabled = False
Exit Sub
End If
If intRandom = 2 And btn2.Enabled = True Then
btn2.Text = "O"
btn2.Enabled = False
Exit Sub
End If
If intRandom = 3 And btn3.Enabled = True Then
btn3.Text = "O"
btn3.Enabled = False
Exit Sub
End If
If intRandom = 4 And btn4.Enabled = True Then
btn4.Text = "O"
btn4.Enabled = False
Exit Sub
End If
If intRandom = 5 And btn5.Enabled = True Then
btn5.Text = "O"
btn5.Enabled = False
Exit Sub
End If
If intRandom = 6 And btn6.Enabled = True Then
btn6.Text = "O"
btn6.Enabled = False
Exit Sub
End If
If intRandom = 7 And btn7.Enabled = True Then
btn7.Text = "O"
btn7.Enabled = False
Exit Sub
End If
If intRandom = 8 And btn8.Enabled = True Then
btn8.Text = "O"
btn8.Enabled = False
Exit Sub
End If
If intRandom = 9 And btn9.Enabled = True Then
btn9.Text = "O"
btn9.Enabled = False
Exit Sub
End If
ComputerPlays()
End Sub
Private Sub MyPlays(sender As Object, e As EventArgs) Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click
'As jogadas do Utilizador
sender.text = "X"
sender.enabled = False
intSet += 1
CheckVencedor()
'AS Jogadas do PC
ComputerPlays()
intSet += 1
CheckVencedor()
End Sub
Sub CheckVencedor()
If intSet >= 9 Then
MessageBox.Show("Ninguém ganhou")
ElseIf btn1.Text = "O" And btn2.Text = "O" And btn3.Text = "O" Or
btn4.Text = "O" And btn5.Text = "O" And btn6.Text = "O" Or
btn7.Text = "O" And btn8.Text = "O" And btn9.Text = "O" Or
btn1.Text = "O" And btn4.Text = "O" And btn7.Text = "O" Or
btn2.Text = "O" And btn5.Text = "O" And btn8.Text = "O" Or
btn3.Text = "O" And btn6.Text = "O" And btn9.Text = "O" Or
btn1.Text = "O" And btn5.Text = "O" And btn9.Text = "O" Or
btn7.Text = "O" And btn5.Text = "O" And btn3.Text = "O" Then
MessageBox.Show("Computador ganhou, perdestes seu scrub!")
ElseIf btn1.Text = "X" And btn2.Text = "X" And btn3.Text = "O" Or
btn4.Text = "X" And btn5.Text = "X" And btn6.Text = "O" Or
btn7.Text = "X" And btn8.Text = "X" And btn9.Text = "O" Or
btn1.Text = "X" And btn4.Text = "X" And btn7.Text = "O" Or
btn2.Text = "X" And btn5.Text = "X" And btn8.Text = "O" Or
btn3.Text = "X" And btn6.Text = "X" And btn9.Text = "O" Or
btn1.Text = "X" And btn5.Text = "X" And btn9.Text = "O" Or
btn7.Text = "X" And btn5.Text = "X" And btn3.Text = "O" Then
MessageBox.Show("Wow, ganhastes seu noob!")
End If
End Sub
End Class
有时,游戏可以运作,但它会不断崩溃。