Results 1 to 5 of 5

Thread: infinite loop?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    14

    infinite loop?

    i'm programing a blackjack game, it works fine except the sub for preventing a vard from reapearing. if i run it vb says stackoverflow, and that there might me an infinite loop. here is the cood of the control does anyone see if it is an infinite loop or not?

    Code:
    Public Class card
    
      Private getal As Random = New Random
    
      Private card As String
      Private Valuecard As Integer
      Private cardgetal As Integer
      Protected cards_used As ArrayList = New ArrayList
      Private card_array() As String = {"h1", "k1", "r1", "s1", _
                                        "h2", "k2", "r2", "s2", _
                                        "h3", "k3", "r3", "s3", _
                                        "h4", "k4", "r4", "s4", _
                                        "h5", "k5", "r5", "s5", _
                                        "h6", "k6", "r6", "s6", _
                                        "h7", "k7", "r7", "s7", _
                                        "h8", "k8", "r8", "s8", _
                                        "h9", "k9", "r9", "s9", _
                                        "h10", "k10", "r10", "s10", _
                                        "hv", "kv", "rv", "sv", _
                                        "hq", "kq", "rq", "sq", _
                                        "hh", "kh", "rh", "sh"}
    
      Protected Function genereerKaart() As String
    
        cardgetal = getal.Next(0, 51)
        card = card_array(cardgetal)
        controle_card(card)
        Return card
    
      End Function
      Private Sub controle_card(ByVal card As String)
        Dim i = 0
    
        If cards_used.Count = 0 Then
          cards_used.Add(card)
        Else
    
          Do Until i = (cards_used.Count)
            If card IsNot cards_used(i) Then
              i = i + 1
            Else
              genereerKaart()
              Stop
            End If
    
          Loop
    
          cards_used.Add(card)
        End If
      End Sub
    end class

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: infinite loop?

    In the controle_card sub you call genereerKaart functions which in tunr calls controle_card
    Code:
        controle_card 
        -> genereerKaart
           -> controle_card
              -> genereerKaart
                 -> controle_card
    ....
    stack overflow as a result

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    14

    Re: infinite loop?

    any ideas how to fix it?

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: infinite loop?

    Try this:
    Code:
    Protected Function genereerKaart() As String
        Do 
            cardgetal = getal.Next(0, 51)
            card = card_array(cardgetal)
        Loop Until controle_card(card)
        Return card
    End Function
    
    
    Private Function controle_card(ByVal card As String) As Boolean
        
        If Not cards_used.Contains(card) OrElse cards_used.Count = 0 Then
            cards_used.Add(card)
            Return True
        Else
            Return False
        End If
        
    End Function

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    14

    Re: infinite loop?

    jeah its working knwo thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width