所以我一直在尝试为编程课做一个井字游戏,老师没用。:l
当我运行程序时,10 次中有 8 次;当我玩并让网格几乎充满时,它会冻结。这有点难以解释,我真的很累。我希望有人能帮帮忙。:秒
代码:
Private Sub ChooseBox()
Dim num As Integer
Dim Random As New Random
num = Random.Next(0, 10)
If Grid_1.Text <> "" And Grid_2.Text <> "" And Grid_3.Text <> "" And Grid_4.Text <> "" And Grid_5.Text <> "" And Grid_6.Text <> "" And Grid_7.Text <> "" And Grid_8.Text <> "" And Grid_9.Text <> "" Then
gStatus = "Tie"
Me.Text = "Tic Tac Toe - Nobody won!"
Else
If num = 1 And Grid_1.Text = "" Then
Grid_1.Text = compChar : turnIs = "Player"
ElseIf num = 2 And Grid_2.Text = "" Then
Grid_2.Text = compChar : turnIs = "Player"
ElseIf num = 3 And Grid_3.Text = "" Then
Grid_3.Text = compChar : turnIs = "Player"
ElseIf num = 4 And Grid_4.Text = "" Then
Grid_4.Text = compChar : turnIs = "Player"
ElseIf num = 5 And Grid_5.Text = "" Then
Grid_5.Text = compChar : turnIs = "Player"
ElseIf num = 6 And Grid_6.Text = "" Then
Grid_6.Text = compChar : turnIs = "Player"
ElseIf num = 7 And Grid_7.Text = "" Then
Grid_7.Text = compChar : turnIs = "Player"
ElseIf num = 8 And Grid_7.Text = "" Then
Grid_8.Text = compChar : turnIs = "Player"
ElseIf num = 9 And Grid_9.Text = "" Then
Grid_9.Text = compChar
Else : ChooseBox()
End If
End If
End Sub
Private Sub CheckResult()
'First Column is filled
If Grid_1.Text = compChar And Grid_2.Text = compChar And Grid_3.Text = compChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Computer Wins!"
ElseIf Grid_1.Text = playerChar And Grid_2.Text = playerChar And Grid_3.Text = playerChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Player Wins!"
End If
'Second Column is filled
If Grid_4.Text = compChar And Grid_5.Text = compChar And Grid_6.Text = compChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Computer Wins!"
ElseIf Grid_4.Text = playerChar And Grid_5.Text = playerChar And Grid_6.Text = playerChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Player Wins!"
End If
'Third Column is filled
If Grid_7.Text = compChar And Grid_8.Text = compChar And Grid_9.Text = compChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Computer Wins!"
ElseIf Grid_7.Text = playerChar And Grid_8.Text = playerChar And Grid_9.Text = playerChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Player Wins!"
End If
'First Row is filled
If Grid_1.Text = compChar And Grid_4.Text = compChar And Grid_7.Text = compChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Computer Wins!"
ElseIf Grid_1.Text = playerChar And Grid_4.Text = playerChar And Grid_7.Text = playerChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Player Wins!"
End If
'Second Row is filled
If Grid_2.Text = compChar And Grid_5.Text = compChar And Grid_8.Text = compChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Computer Wins!"
ElseIf Grid_2.Text = playerChar And Grid_5.Text = playerChar And Grid_8.Text = playerChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Player Wins!"
End If
'Third Row is filled
If Grid_3.Text = compChar And Grid_6.Text = compChar And Grid_9.Text = compChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Computer Wins!"
ElseIf Grid_3.Text = playerChar And Grid_6.Text = playerChar And Grid_9.Text = playerChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Player Wins!"
End If
'Diagonal Upwards is filled
If Grid_1.Text = compChar And Grid_5.Text = compChar And Grid_9.Text = compChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Computer Wins!"
ElseIf Grid_1.Text = playerChar And Grid_5.Text = playerChar And Grid_9.Text = playerChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Player Wins!"
End If
'Diagonal Downwards is filled
If Grid_3.Text = compChar And Grid_5.Text = compChar And Grid_7.Text = compChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Computer Wins!"
ElseIf Grid_3.Text = playerChar And Grid_5.Text = playerChar And Grid_7.Text = playerChar Then
gStatus = "Won"
Me.Text = "Tic Tac Toe - Player Wins!"
End If
End Sub