1

我正在尝试用 9 个按钮编写一个简单的井字游戏。当我单击任何按钮时,我会看到一个带有 WIN 对话框的消息框。尽管此消息框仅在出现 WIN CASE 时才显示。问题在于它没有正确比较 Button.Text 值。

最初 All 按钮的 TEXT 属性为空。单击时,我将他们的 TEXT 设置为“x”或“0”,视情况而定。这假设可以正常工作,但无法弄清楚为什么它没有将新分配的clicked按钮文本与其他按钮的文本进行比较。

这是我的代码

Public Class Form1
Dim count As Int32 = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


End Sub
Sub checkwin()
    If Button1.Text = Button2.Text And Button2.Text = Button3.Text Then

        MessageBox.Show(Button1.Text & "wins")
        Call reset()


    ElseIf Button4.Text = Button5.Text And Button5.Text = Button6.Text Then
        MessageBox.Show(Button4.Text & "wins")
        Call reset()

    ElseIf Button7.Text = Button8.Text And Button8.Text = Button9.Text Then
        MessageBox.Show(Button7.Text & "wins")
        Call reset()
    ElseIf Button1.Text = Button4.Text And Button4.Text = Button7.Text Then
        MessageBox.Show(Button1.Text & "wins")
        Call reset()

    ElseIf Button2.Text = Button5.Text And Button5.Text = Button8.Text Then
        MessageBox.Show(Button2.Text & "wins")
        Call reset()
    ElseIf Button3.Text = Button6.Text And Button6.Text = Button9.Text Then
        MessageBox.Show(Button3.Text & "wins")
        Call reset()
    ElseIf Button1.Text = Button5.Text And Button5.Text = Button9.Text Then
        MessageBox.Show(Button1.Text & "wins")
        Call reset()

    ElseIf Button3.Text = Button5.Text And Button5.Text = Button7.Text Then
        MessageBox.Show(Button3.Text & "wins")
        Call reset()
    End If

End Sub


Sub reset()
   For Each b as Button  in Controls

        b.Text = " "
        b.Enabled = True

    Next


End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
    If sender Is Button1 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button1.Text = "0"

        Else
            Button1.Text = "x"
        End If

        Button1.Enabled = False
        Call checkwin()
    End If

    If sender Is Button2 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button2.Text = "0"
        Else
            Button2.Text = "x"
        End If


        Button2.Enabled = False
        Call checkwin()
    End If

    If sender Is Button3 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button3.Text = "0"
        Else
            Button3.Text = "x"
        End If

        Button3.Enabled = False
        Call checkwin()


    End If
    If sender Is Button4 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button4.Text = "0"

        Else
            Button4.Text = "x"
        End If

        Button4.Enabled = False
        Call checkwin()
    End If
    If sender Is Button5 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button5.Text = "0"

        Else
            Button5.Text = "x"
        End If

        Button5.Enabled = False
        Call checkwin()
    End If

    If sender Is Button6 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button6.Text = "0"

        Else
            Button6.Text = "x"
        End If

        Button6.Enabled = False
        Call checkwin()
    End If

    If sender Is Button7 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button7.Text = "0"

        Else
            Button7.Text = "x"
        End If

        Button7.Enabled = False
        Call checkwin()
    End If

    If sender Is Button8 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button8.Text = "0"

        Else
            Button8.Text = "x"
        End If

        Button8.Enabled = False
        Call checkwin()
    End If

    If sender Is Button9 Then
        count = count + 1
        If count Mod 2 = 0 Then
            Button9.Text = "0"

        Else
            Button9.Text = "x"
        End If

        Button9.Enabled = False
        Call checkwin()
    End If



End Sub
End Class
4

1 回答 1

3

您没有消除checkWinIf语句中未使用的按钮。您还应该使用短路比较来避免不必要的处理。所以,第一个If语句看起来像这样:

If Not Button1.Enabled AndAlso Not Button2.Enabled AndAlso Not Button3.Enabled AndAlso _
            (Button1.Text = Button2.Text) AndAlso (Button2.Text = Button3.Text) Then
    ...
ElseIf Not Button4.Enabled ... 'etc.

哦,If您的按钮单击事件中的所有这些块都是不必要的。只需使用 sender 对象,如下所示:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
    Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click,
            Button6.Click, Button7.Click, Button8.Click, Button9.Click
    sender.Text = If(clickCount Mod 2 = 0, "X", "O")
    sender.Enabled = False
    clickCount += 1
    CheckWin()
End Sub

编辑(在评论中要求)
为了避免 CheckWin 子中的重复比较代码,您可以将工作外包给一个函数。这样比较代码只写一次,因此更不容易出错并且更容易阅读。我会做这样的事情:

Private Sub CheckWin()
    If LineWins(Button1, Button2, Button3) OrElse _
       LineWins(Button4, Button5, Button6) OrElse _
       LineWins(Button7, Button8, Button9) OrElse _
       LineWins(Button1, Button4, Button7) OrElse _
       LineWins(Button2, Button5, Button8) OrElse _
       LineWins(Button3, Button6, Button9) OrElse _
       LineWins(Button1, Button5, Button9) OrElse _
       LineWins(Button3, Button5, Button7) Then ResetGame()
End Sub

Private Function LineWins(b1 As Button, b2 As Button, b3 As Button) As Boolean
    LineWins = CBool(Not b1.Enabled AndAlso Not b2.Enabled AndAlso Not b3.Enabled _
                 AndAlso (b1.Text = b2.Text) AndAlso (b2.Text = b3.Text))
    If LineWins Then MessageBox.Show(b1.Text & " wins")
End Function

顺便说一句,您可能会注意到我已将“reset”重命名为“ResetGame”——我建议您这样做,因为Reset它已分配给 .NETFileSystem方法。

于 2012-07-25T17:41:52.357 回答