0

所以我一直在尝试为编程课做一个井字游戏,老师没用。: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
4

2 回答 2

2

我看到的三个问题:

num = Random.Next(0, 10)

Random.Next 是包含下限和不包含上限的。这意味着计算机可以在此处滚动 0。这不会导致循环,但有点浪费,因为您从不接受条件中的 0。

ElseIf num = 9 And Grid_9.Text = "" Then
    Grid_9.Text = compChar

在这里,您的模式不包括将控制权返回给 Player。我希望计算机在这种情况下会转两圈。

ElseIf num = 8 And Grid_7.Text = "" Then
    Grid_8.Text = compChar : turnIs = "Player"

现在这是可能的循环问题。请注意,当计算机滚动 8 时,您正在检查网格 7 的内容。因此,如果网格 8 是最后一个空间,您将始终陷入另一个 ChooseBox() 调用。循环永远不会解决。

于 2012-08-31T15:17:39.253 回答
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 = ""检查的需求。

注意:为了可读性,忘记:字符允许您在一行中有多行代码也是一个好主意。除非该行是单数指令或条件 then 指令,否则最终只会使代码变得庞大。

于 2012-08-31T15:18:24.057 回答