-2

我的程序随机决定停止工作。没有错误消息,只是按下播放按钮没有任何反应,我检查了我的卡片摘要,发现对应玩家的 QuantityInteger 减去了一张卡片并添加到弃牌堆,但没有将新卡片添加到他们的手上。然而,这是完全随机的,因为它在随机间隔内工作或不工作。

T 是我代码的核心。目前我只为两个播放器模式实现了它。如果 T = 0 则执行玩家 1 的代码,如果 T = 1 则执行玩家 2 的代码。

ChecksDynamic 表示 For 循环上的范围,因为这些数字表示该玩家在数组中发现的有效数字。

攻击对话框代表您决定攻击的玩家。(只能在 2 人模式下攻击一个人,因为您无法攻击自己)。

T 还将不同玩家数量的整数放入一个用例中。用例的输出变量 = player 分配给 T 的特定值。

然后,我使用一个用例转换所有玩家卡片,以使我的代码动态地检查特定玩家的复选框。

If 语句然后查看复选框是否被选中,并且选择了正确播放器的数量整数(来自选择案例的 SelectPlayer)。

然后根据卡片伤害值减去生命值。

如果卡片是武器,玩家可以选择保留或丢弃卡片。如果不丢弃,除了生命值之外没有任何变化。

在 GrabFromDeckandDiscard 过程中,我再次使用 T 和 Select Case 从正确的玩家物品库存中减去

然后将对应的牌加入弃牌堆

然后我运行一个函数来随机选择一张牌组数量整数中可用的牌。我再次运行 Select 案例,然后将随机生成的数字中的需要卡添加到相应的玩家库存中。

然后最后一个主要部分涉及将 T 切换到下一个值。出于测试目的,我在我的表单上放了一个随机标签,并用它来查看 T 的值。T 似乎并没有失败,据我所知。

Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
        Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5}
        Dim HitPoints() As Label = {Nothing, HitPoints1, HitPoints2, HitPoints3, HitPoints4, HitPoints5}
        Dim n, SelectPlayer As Integer
        Label1.Text = T.ToString
        'Player 1
        If T = 0 And (CardCheckBoxArray(0).Checked = True Or CardCheckBoxArray(1).Checked = True Or CardCheckBoxArray(2).Checked = True Or CardCheckBoxArray(3).Checked = True Or CardCheckBoxArray(4).Checked = True) Then
            AtkPlayerDialog.Player1.Enabled = False
            AtkPlayerDialog.Player2.Enabled = True
            AtkPlayerDialog.Player3.Enabled = True
            AtkPlayerDialog.Player4.Enabled = True
            AtkPlayerDialog.Player5.Enabled = True
            ChecksDynamicA = 0
            ChecksDynamicB = 4
            'Player 2
        ElseIf T = 1 And (CardCheckBoxArray(0).Checked = True Or CardCheckBoxArray(1).Checked = True Or CardCheckBoxArray(2).Checked = True Or CardCheckBoxArray(3).Checked = True Or CardCheckBoxArray(4).Checked = True) Then
            AtkPlayerDialog.Player1.Enabled = True
            AtkPlayerDialog.Player2.Enabled = False
            AtkPlayerDialog.Player3.Enabled = True
            AtkPlayerDialog.Player4.Enabled = True
            AtkPlayerDialog.Player5.Enabled = True
            ChecksDynamicA = 5
            ChecksDynamicB = 9
            'Player 3
        ElseIf T = 2 And (CardCheckBoxArray(0).Checked = True Or CardCheckBoxArray(1).Checked = True Or CardCheckBoxArray(2).Checked = True Or CardCheckBoxArray(3).Checked = True Or CardCheckBoxArray(4).Checked = True) Then
            AtkPlayerDialog.Player1.Enabled = True
            AtkPlayerDialog.Player2.Enabled = True
            AtkPlayerDialog.Player3.Enabled = False
            AtkPlayerDialog.Player4.Enabled = True
            AtkPlayerDialog.Player5.Enabled = True
            ChecksDynamicA = 10
            ChecksDynamicB = 14
            'Player 4
        ElseIf T = 3 And (CardCheckBoxArray(0).Checked = True Or CardCheckBoxArray(1).Checked = True Or CardCheckBoxArray(2).Checked = True Or CardCheckBoxArray(3).Checked = True Or CardCheckBoxArray(4).Checked = True) Then
            AtkPlayerDialog.Player1.Enabled = True
            AtkPlayerDialog.Player2.Enabled = True
            AtkPlayerDialog.Player3.Enabled = True
            AtkPlayerDialog.Player4.Enabled = False
            AtkPlayerDialog.Player5.Enabled = True
            ChecksDynamicA = 15
            ChecksDynamicB = 19
            'Player 5
        ElseIf T = 4 And (CardCheckBoxArray(0).Checked = True Or CardCheckBoxArray(1).Checked = True Or CardCheckBoxArray(2).Checked = True Or CardCheckBoxArray(3).Checked = True Or CardCheckBoxArray(4).Checked = True) Then
            AtkPlayerDialog.Player1.Enabled = True
            AtkPlayerDialog.Player2.Enabled = True
            AtkPlayerDialog.Player3.Enabled = True
            AtkPlayerDialog.Player4.Enabled = True
            AtkPlayerDialog.Player5.Enabled = False
            ChecksDynamicA = 20
            ChecksDynamicB = 24
        End If
        'Code for choosing which player to attack
        AtkPlayerDialog.ShowDialog()
        If AtkPlayerDialog.DialogResult = 1 Then
            n = 1
        ElseIf AtkPlayerDialog.DialogResult = 2 Then
            n = 2
        ElseIf AtkPlayerDialog.DialogResult = 3 Then
            n = 3
        ElseIf AtkPlayerDialog.DialogResult = 4 Then
            n = 4
        ElseIf AtkPlayerDialog.DialogResult = 5 Then
            n = 5
        End If
        'CheckedLoop
        For Me.Checks = ChecksDynamicA To ChecksDynamicB
            'Supplement Numbers(1-5) variable in loop
                NumberChecks = NumberArray(Checks)
            'Select the Player
            Select Case T
                Case 0
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger
                Case 1
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger2
                Case 2
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger3
                Case 3
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger4
                Case 4
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger5
            End Select
            'Convert arrays to correct textboxes
            Select Case Checks
                Case 0, 5, 10, 15, 20
                    TextBoxInteger = 0
                Case 1, 6, 11, 16, 21
                    TextBoxInteger = 1
                Case 2, 7, 12, 17, 22
                    TextBoxInteger = 2
                Case 3, 8, 13, 18, 23
                    TextBoxInteger = 3
                Case 4, 9, 14, 19, 24
                    TextBoxInteger = 4
            End Select
            'Play Card(s)
            If CardCheckBoxArray(TextBoxInteger).Checked = True AndAlso SelectPlayer > 0 Then
                'Subtract Hitpoints when damage is delt
                Player1HandGroup(n).HitPoints -= Player1HandGroup(NumberChecks).DamageInteger
                HitPoints(n).Text = Player1HandGroup(n).HitPoints.ToString
                'When player plays hand, card quantity is removed from hand to the discard pile.
                If Player1HandGroup(NumberChecks).CardType = "Weapon" Then
                    DiscardDialog.ShowDialog()
                    'Choose if to Discard Weapon after usage
                    If DiscardDialog.DialogResult = Windows.Forms.DialogResult.OK Then
                        Call GrabFromDeckAndDiscard()
                    End If
                Else
                    Call GrabFromDeckAndDiscard()
                End If
            End If

            ChecksLabel.Text = Checks.ToString
        Next

        Dim CardCheckBoxInteger As Integer
        'Clear Check Boxes when turn is finished
        For CardCheckBoxInteger = 0 To 4
            CardCheckBoxArray(CardCheckBoxInteger).Checked = False
        Next

        'Turn off play button
        PlayButton.Enabled = False
    End Sub
    Private Sub GrabFromDeckAndDiscard()
        'ReDeclare CheckBox Array for Private sub
        Dim n As Integer
        Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5}
        'Discard
        Select Case T
            Case 0
                Player1HandGroup(NumberChecks).QuantityInteger -= 1
            Case 1
                Player1HandGroup(NumberChecks).QuantityInteger2 -= 1
            Case 2
                Player1HandGroup(NumberChecks).QuantityInteger3 -= 1
            Case 3
                Player1HandGroup(NumberChecks).QuantityInteger4 -= 1
            Case 4
                Player1HandGroup(NumberChecks).QuantityInteger5 -= 1
        End Select
        'Add Card to Discard Pile
        DiscardGroup(NumberChecks).QuantityInteger += 1
        'Shuffle Deck from Discard Pile if Deck is out of cards
        Call DiscardPile()
        'Reset Number Generator, unless weapon isn't discard
        Dim validDeckGroupsIndexes As New List(Of Integer)
        For ndx As Integer = 0 To (DeckGroup.Count - 1)
            If DeckGroup(ndx).QuantityInteger > 0 Then
                validDeckGroupsIndexes.Add(ndx)
            End If
        Next ndx
        Dim deckGroupNdx As Integer = Rnd.Next(0, validDeckGroupsIndexes.Count)
        Number = DeckGroup(deckGroupNdx).ID

        If DeckGroup(Number).QuantityInteger > 0 Then
            'Grab New Card From Deck
            DeckGroup(Number).QuantityInteger -= 1
            Select Case T
                Case 0
                    Player1HandGroup(NumberChecks).QuantityInteger += 1
                Case 1
                    Player1HandGroup(NumberChecks).QuantityInteger2 += 1
                Case 2
                    Player1HandGroup(NumberChecks).QuantityInteger3 += 1
                Case 3
                    Player1HandGroup(NumberChecks).QuantityInteger4 += 1
                Case 4
                    Player1HandGroup(NumberChecks).QuantityInteger5 += 1
            End Select
            ' assign card type to chosen card and assign "number" to corresponding cards number as well
            CardTypeArray(Checks) = Player1HandGroup(Number).CardType
            NumberArray(Checks) = Number
        End If
        'Switch to next player
        Select Case T
            Case 0
                For CardCheckBoxInteger = 0 To 4
                    Select Case CardCheckBoxInteger
                        Case 0
                            n = 5
                        Case 1
                            n = 6
                        Case 2
                            n = 7
                        Case 3
                            n = 8
                        Case 4
                            n = 9
                    End Select
                    CardCheckBoxArray(CardCheckBoxInteger).Text = Player1HandGroup(NumberArray(n)).CardNameString
                Next
                T += 1
            Case 1
                If GameSize = 2 Then
                    For CardCheckBoxInteger = 0 To 4
                        CardCheckBoxArray(CardCheckBoxInteger).Text = Player1HandGroup(NumberArray(CardCheckBoxInteger)).CardNameString
                    Next CardCheckBoxInteger
                    T -= 1
                End If
                If GameSize > 2 Then
                    T += 1
                End If
            Case 2
            Case 3
            Case 4

        End Select
        Label1.Text = T.ToString
    End Sub
4

1 回答 1

0

我认为如果你清理你的代码,解决方案会更容易找到。例如,这里至少有 20 行只是粗略一瞥就被删除了。

    Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
        Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5}
        Dim HitPoints() As Label = {Nothing, HitPoints1, HitPoints2, HitPoints3, HitPoints4, HitPoints5}
        Dim n, SelectPlayer As Integer
        Label1.Text = T.ToString
        'Player 1
        If CardCheckBoxArray(0).Checked Or CardCheckBoxArray(1).Checked Or CardCheckBoxArray(2).Checked Or CardCheckBoxArray(3).Checked Or CardCheckBoxArray(4).Checked Then
            AtkPlayerDialog.Player1.Enabled = Not (0=T)
            AtkPlayerDialog.Player2.Enabled = Not (1=T)
            AtkPlayerDialog.Player3.Enabled = Not (2=T)
            AtkPlayerDialog.Player4.Enabled = Not (3=T)
            AtkPlayerDialog.Player5.Enabled = Not (4=T)
            ChecksDynamicA = 5 * T
            ChecksDynamicB = 5 * T + 4
        End If
        'Code for choosing which player to attack
        AtkPlayerDialog.ShowDialog()
        n = AtkPlayerDialog.DialogResult
        'CheckedLoop
        For Me.Checks = ChecksDynamicA To ChecksDynamicB
            'Supplement Numbers(1-5) variable in loop
            NumberChecks = NumberArray(Checks)
            'Select the Player
            Select Case T
                Case 0
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger
                Case 1
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger2
                Case 2
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger3
                Case 3
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger4
                Case 4
                    SelectPlayer = Player1HandGroup(NumberChecks).QuantityInteger5
            End Select
            'Convert arrays to correct textboxes
            TextBoxInteger = Checks Mod 5
            'Play Card(s)
            If CardCheckBoxArray(TextBoxInteger).Checked AndAlso SelectPlayer > 0 Then
                'Subtract Hitpoints when damage is delt
                Player1HandGroup(n).HitPoints -= Player1HandGroup(NumberChecks).DamageInteger
                HitPoints(n).Text = Player1HandGroup(n).HitPoints.ToString
                'When player plays hand, card quantity is removed from hand to the discard pile.
                If Player1HandGroup(NumberChecks).CardType.Equals("Weapon") Then
                    DiscardDialog.ShowDialog()
                    'Choose if to Discard Weapon after usage
                    If DiscardDialog.DialogResult = Windows.Forms.DialogResult.OK Then
                        Call GrabFromDeckAndDiscard()
                    End If
                Else
                    Call GrabFromDeckAndDiscard()
                End If
            End If

            ChecksLabel.Text = Checks.ToString
        Next

        Dim CardCheckBoxInteger As Integer
        'Clear Check Boxes when turn is finished
        For CardCheckBoxInteger = 0 To 4
            CardCheckBoxArray(CardCheckBoxInteger).Checked = False
        Next

        'Turn off play button
        PlayButton.Enabled = False
    End Sub

和这个!

            For CardCheckBoxInteger = 0 To 4
                Select Case CardCheckBoxInteger
                    Case 0
                        n = 5
                    Case 1
                        n = 6
                    Case 2
                        n = 7
                    Case 3
                        n = 8
                    Case 4
                        n = 9
                End Select
                CardCheckBoxArray(CardCheckBoxInteger).Text = Player1HandGroup(NumberArray(n)).CardNameString
            Next

变成:

            For CardCheckBoxInteger = 0 To 4
                CardCheckBoxArray(CardCheckBoxInteger).Text = Player1HandGroup(NumberArray(CardCheckBoxInteger+5)).CardNameString
            Next
于 2013-03-26T17:24:44.087 回答