在整个猜测之前,我的刽子手游戏结束了

时间:2016-11-08 20:08:18

标签: vb.net

我正在用Visual Basic编写一个刽子手游戏。这是我的代码。

我认为问题位于private sub labelcheck()下,但我试图删除AndAlso等,但似乎没有任何效果。我是visual basic的新手,所以我对如何调试这些问题的理解非常有限。谢谢。This is picture of the problem

Imports System.IO
Public Class Form1
    Public n As Integer
    Public a As String

    Private Sub Button30_Click(sender As System.Object, e As EventArgs) Handles Button30.Click
        For Each button As Button In GroupBox1.Controls
            button.Enabled = True
        Next
        If File.Exists("C:\dictionary.txt") Then
            Dim words() As String = File.ReadAllLines("C:\dictionary.txt")
            Dim r As New Random(System.DateTime.Now.Millisecond)
            n = r.Next(0, 80368)
            Dim amount As Integer = words(n).Count()
            Label9.Text = "Letters:" & " " & amount
            Dim letters() As Char = words(n)
            Dim idxnum As Integer = 0

            For Each labelitm As Label In Me.GroupBox2.Controls
                If idxnum = amount - 1 Then
                    labelitm.Text = letters(idxnum)
                Else
                    labelitm.Text = ""
                End If
                idxnum = idxnum + 1
            Next

            For Each labelitm As Label In GroupBox2.Controls
                If labelitm.Text = "" Then
                Else
                    labelitm.Visible = False
                End If
            Next
        Else

        End If
    End Sub

    Private Sub labelcheck()
        For Each labelitm As Label In GroupBox2.Controls
            If a = labelitm.Text Then
                labelitm.Visible = True
                If Label1.Visible AndAlso Label2.Visible AndAlso Label3.Visible AndAlso Label4.Visible AndAlso Label5.Visible AndAlso Label6.Visible AndAlso Label7.Visible AndAlso Label8.Visible = True Then
                    MessageBox.Show("You won!")
                    Button30.PerformClick()
                End If
            Else

            End If
        Next
    End Sub

    Private Sub ButtonClick(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, Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button14.Click, Button15.Click, Button16.Click, Button17.Click, Button18.Click, Button19.Click, Button20.Click, Button21.Click, Button22.Click, Button23.Click, Button24.Click, Button25.Click, Button26.Click, Button27.Click, Button28.Click, Button29.Click

        Select Case DirectCast(sender, Button).Name
            Case "Button1"
                Button1.Enabled = False
                a = "a"
                labelcheck()
            Case "Button2"
                Button2.Enabled = False
                a = "b"
                labelcheck()
            Case "Button3"
                Button3.Enabled = False
                a = "c"
                labelcheck()
            Case "Button4"
                Button4.Enabled = False
                a = "d"
                labelcheck()
            Case "Button5"
                Button5.Enabled = False
                a = "e"
                labelcheck()
            Case "Button6"
                Button6.Enabled = False
                a = "f"
                labelcheck()
            Case "Button7"
                Button7.Enabled = False
                a = "g"
                labelcheck()
            Case "Button8"
                Button8.Enabled = False
                a = "h"
                labelcheck()
            Case "Button9"
                Button9.Enabled = False
                a = "i"
                labelcheck()
            Case "Button10"
                Button10.Enabled = False
                a = "j"
                labelcheck()
            Case "Button11"
                Button11.Enabled = False
                a = "k"
                labelcheck()
            Case "Button12"
                Button12.Enabled = False
                a = "l"
                labelcheck()
            Case "Button13"
                Button13.Enabled = False
                a = "m"
                labelcheck()
            Case "Button14"
                Button14.Enabled = False
                a = "n"
                labelcheck()
            Case "Button15"
                Button15.Enabled = False
                a = "o"
                labelcheck()
            Case "Button16"
                Button16.Enabled = False
                a = "p"
                labelcheck()
            Case "Button17"
                Button17.Enabled = False
                a = "q"
                labelcheck()
            Case "Button18"
                Button18.Enabled = False
                a = "r"
                labelcheck()
            Case "Button19"
                Button19.Enabled = False
                a = "s"
                labelcheck()
            Case "Button20"
                Button20.Enabled = False
                a = "t"
                labelcheck()
            Case "Button21"
                Button21.Enabled = False
                a = "u"
                labelcheck()
            Case "Button22"
                Button22.Enabled = False
                a = "v"
                labelcheck()
            Case "Button23"
                Button23.Enabled = False
                a = "w"
                labelcheck()
            Case "Button24"
                Button24.Enabled = False
                a = "x"
                labelcheck()
            Case "Button25"
                Button25.Enabled = False
                a = "y"
                labelcheck()
            Case "Button26"
                Button26.Enabled = False
                a = "z"
                labelcheck()
            Case "Button27"
                Button27.Enabled = False
                a = "ø"
                labelcheck()
            Case "Button28"
                Button28.Enabled = False
                a = "ø"
                labelcheck()
            Case "Button29"
                Button29.Enabled = False
                a = "å  "
                labelcheck()
        End Select
    End Sub
End Class

enter image description here

1 个答案:

答案 0 :(得分:0)

labelcheck()内,您使用Control对象迭代Label。您应该使用Controls.OfType(Of Label)仅返回Label s

For Each labelitm As Label In GroupBox2.Controls.OfType(Of Label)()
    If a = labelitm.Text Then
        labelitm.Enabled = True
        If Label1.Enabled AndAlso ... Then
            MessageBox.Show("You won!")
            Button30.PerformClick()
        End If
    End If
Next

我还会简化您的大量按钮点击处理程序

Private Sub ButtonClick(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, Button10.Click, Button11.Click,
    Button12.Click, Button13.Click, Button14.Click, Button15.Click,
    Button16.Click, Button17.Click, Button18.Click, Button19.Click,
    Button20.Click, Button21.Click, Button22.Click, Button23.Click,
    Button24.Click, Button25.Click, Button26.Click, Button27.Click,
    Button28.Click, Button29.Click

    Dim myButton As Button = DirectCast(sender, Button)
    Dim buttonIndex As Integer = Integer.Parse(myButton.Name.Replace("Button", ""))

    Select Case buttonIndex
        Case 1 To 26
            myButton.Enabled = False
            ' See http://www.asciitable.com/
            a = Chr(buttonIndex + 96).ToString()
            labelCheck()
        Case 27, 28
            myButton.Enabled = False
            a = "ø"
            labelCheck()
        Case 29
            myButton.Enabled = False
            a = "å  "
            labelCheck()
    End Select

End Sub