For Loop giving from Index Number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TPLJ
    New Member
    • Mar 2013
    • 1

    For Loop giving from Index Number

    I have a for loop that changes from 0 to 4, 5 to 9, 10 to 14, 15 to 19, then 20 to 24 each time i hit the play button. (both numbers in the range determined by a variable called CheckDynamicA and CheckDynamicB.

    Each set of 5 numbers is 5 check boxes representing a player in my game im designing.
    It works except for the last time I hit the "play button"

    Each time I hit the play button a variable called T is given T = T + 1.
    One the last click of the play button I have decision logic that turns T back to 0, and should bring the 20 to 24 back to 0 to 4.

    My problem is on the last click of the button though, as the debugger says that the value of the variable = 25, and not 24, which is out of range. No where in my code (ive checked dozens of times for errors) do I have it set to 25 in the "19 to 24", and no where does it add +1 to the max range.


    Code:
    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, CardCheckBox6, CardCheckBox7, CardCheckBox8, CardCheckBox9, _
                                                           CardCheckBox10, CardCheckBox11, CardCheckBox12, CardCheckBox13, CardCheckBox14, CardCheckBox15, CardCheckBox16, CardCheckBox17, _
                                                           CardCheckBox18, CardCheckBox19, CardCheckBox20, CardCheckBox21, CardCheckBox22, CardCheckBox23, CardCheckBox24, CardCheckBox25}
            Dim HitPoints() As Label = {Nothing, HitPoints1, HitPoints2, HitPoints3, HitPoints4, HitPoints5}
            Dim n As Integer
            Dim DisableCheckBoxInteger As Integer
    
    
            'fail safe to make sure checkboxes are checked
            If CardCheckBox1.Checked = False And CardCheckBox2.Checked = False And CardCheckBox3.Checked = False And CardCheckBox4.Checked = False And CardCheckBox5.Checked = False And CardCheckBox6.Checked = False And _
                CardCheckBox7.Checked = False And CardCheckBox8.Checked = False And CardCheckBox9.Checked = False And CardCheckBox10.Checked = False And CardCheckBox11.Checked = False And CardCheckBox12.Checked = False And _
                CardCheckBox13.Checked = False And CardCheckBox14.Checked = False And CardCheckBox15.Checked = False And CardCheckBox16.Checked = False And CardCheckBox17.Checked = False And CardCheckBox18.Checked = False And _
                CardCheckBox19.Checked = False And CardCheckBox20.Checked = False And CardCheckBox21.Checked = False And CardCheckBox22.Checked = False And CardCheckBox23.Checked = False And CardCheckBox24.Checked = False And _
                CardCheckBox25.Checked = False Then
    
                WarningLabel.Text = "Please select your card(s)"
            Else
                'Player 1
                For Me.Checks = ChecksDynamicA To ChecksDynamicB
                    If T = 0 And CardCheckBoxArray(Checks).Checked = True Then
                        For DisableCheckBoxInteger = 0 To 24
                            Select Case DisableCheckBoxInteger
                                Case 0 To 4
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
                                Case 5 To 9
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
                                Case 10 To 24
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
                            End Select
                        Next DisableCheckBoxInteger
                        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(Checks).Checked = True Then
                        For DisableCheckBoxInteger = 0 To 24
                            Select Case DisableCheckBoxInteger
                                Case 0 To 9
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
                                Case 10 To 14
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
                                Case 15 To 24
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
                            End Select
                        Next DisableCheckBoxInteger
                        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(Checks).Checked = True Then
                        For DisableCheckBoxInteger = 0 To 24
                            Select Case DisableCheckBoxInteger
                                Case 0 To 14
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
                                Case 15 To 19
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
                                Case 20 To 24
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
                            End Select
                        Next DisableCheckBoxInteger
                        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(Checks).Checked = True Then
                        For DisableCheckBoxInteger = 0 To 24
                            Select Case DisableCheckBoxInteger
                                Case 0 To 19
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
                                Case 20 To 24
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
                            End Select
                        Next DisableCheckBoxInteger
                        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(Checks).Checked = True Then
                        For DisableCheckBoxInteger = 0 To 24
                            Select Case DisableCheckBoxInteger
                                Case 0 To 4
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
                                Case 5 To 24
                                    CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
                            End Select
                        Next DisableCheckBoxInteger
                        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
                Next Checks
                '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
                    If Checks = Checks Then
                        NumberChecks = NumberArray(Checks)
                    End If
                    'Play Card(s)
                    If T = 0 Then
                        If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger > 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
                    ElseIf T = 1 Then
                        If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger2 > 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
                    ElseIf T = 2 Then
                        If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger3 > 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
                    ElseIf T = 3 Then
                        If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger4 > 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
                    ElseIf T = 4 Then
                        If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger5 > 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
                    End If
                    ChecksLabel.Text = Checks.ToString
                Next
                T += 1
                CardCheckBoxArray(Checks).Checked = False
                If T = 5 Then
                    T = 0
                End If
            End If
        End Sub
    Last edited by Rabbit; Mar 25 '13, 04:13 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    You have a lot of loops, it would help if you told us which loop you're talking about.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      I realise it's been a few months, and you've probably found an answer by now. However, for anyone who finds this in a search in future I wanted to add a comment.

      I haven't read the code in detail, but based on the question this sounds like a classic problem experienced in many programming languages, caused by the fact that so few people understand how a For loop actually works. Take the following example:
      Code:
      For I = 1 To 99
        ' Do something
      Next
      After executing this, you might reasonably expect the value of I to be 99. However, you'd be wrong.

      The value of I is initially set to 1. Each time execution reaches the Next statement, VB first increments I and then tests whether it has passed the limit (99). If so, it drops through to the code after the Next statement. Otherwise, it goes bacak and "does something" again.

      So in this example the value of I is incremented to 100, then VB spots that it's finished and stops looping. Hence, you come out with your variable holding a value higher than you might expect.

      Comment

      • !NoItAll
        Contributor
        • May 2006
        • 297

        #4
        You are changing the values of the upper and lower bounds of the loop inside the loop. That is really really bad design and you will likely never get what you expect.

        Des

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          So he is.

          I don't agree that it's "really really bad design" but you certainly do need to know what you're doing, and what the effects will be. Given the reported problem, that may not be the case here.

          Comment

          • !NoItAll
            Contributor
            • May 2006
            • 297

            #6
            I would argue that changing the value of either bound in a for loop creates difficult to support code - it's hard to follow the logic, especially in this example of nested for loops with changing boundaries. Especially since the internal code changes both boundaries. I gotta say, "ewww." I suppose there are valid reasons for doing something like this - but I would challenge anyone on my team to try a different design if this code came under review. Code that is hard to read and follow makes it difficult to find issues - even if not related to the design...

            Comment

            Working...