所以我一直在尝试为编程课程制作TicTacToe游戏,老师也没用。 :升
当我运行程序时,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
答案 0 :(得分:2)
我看到的三个问题:
num = Random.Next(0, 10)
Random.Next是下限包含和上限独占。这意味着计算机可以在这里滚动0。这不会导致循环,但是因为你的条件句中你从不接受0,所以它有点浪费。
ElseIf num = 9 And Grid_9.Text = "" Then
Grid_9.Text = compChar
此处,您的模式不包括将控制权返回给玩家。我希望在这种情况下计算机可以转两圈。
ElseIf num = 8 And Grid_7.Text = "" Then
Grid_8.Text = compChar : turnIs = "Player"
现在这可能是循环问题。请注意,当计算机滚动8时,您正在检查网格7中的内容。因此,如果网格8是最后一个空格,您将始终进入另一个ChooseBox()调用。循环永远不会解决。
答案 1 :(得分:0)
你已经为自己的无限选择做好了准备。最好使用包含每个单元格编号的列表:
Dim cells As New List(Of String)()
cells.Add("1")
cells.Add("2")
...
cells.Add("9")
然后在用完特定单元格时,将其从列表中删除。
ElseIf num = 5 And Grid_5.Text = "" Then
Grid_5.Text = compChar
turnIs = "Player"
cells.RemoveAt(num - 1) ' Remove the cell at the index in the List
ElseIf num = 6 And Grid_6.Text = "" Then
...
这将允许您随意选择随机:
Dim Random As New Random
num = Random.Next(0, cells.Count - 1) ' This will only allow choices within range
这样,无论何时调用ChooseBox()
方法,您都可以确定随机选择的索引是否可用。这也可能减少对Text = ""
检查的需求。
注意:出于可读性的考虑,忘记:
字符允许您在一行上有多行代码也是一个好主意。除非该行是单指令或条件指令,否则它最终会使代码变得笨重。