我的程序随机打破

时间:2013-03-26 17:11:57

标签: arrays vb.net random numbers

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

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

ChecksDynamic表示For循环的范围,因为这些数字表示对阵列中该玩家点有效的数字。

Atk Dialog表示你决定攻击哪个玩家。(因为你不能攻击自己,所以只能在2个玩家模式下攻击一个人)。

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

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

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

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

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

在GrabFromDeckandDiscard程序中,我再次使用T和Select Case来从正确的玩家项目库存中减去

然后它将相应的卡添加到丢弃堆

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

然后最后一个主要部分涉及将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

1 个答案:

答案 0 :(得分: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