0

I am doing a game in which I need the computer to select two random cards, for some reason I always get a "Computer POINT" match and the randomCard never changes

Dim flipped As Boolean
Dim counter As Integer = 0
Dim rnd = New Random()
randomCard = cardList(rnd.Next(0, cardList.Count))
While counter < 2 
'counter is 2 because the computer has to make two moves
    flipped = randomCard.IsFlipped
    If counter = 0 And flipped <> True Then
    'First move
        firstCard = randomCard
        Dim nLabel As Integer = randomCard.PairType
        counter += 1

     ElseIf counter = 1 And flipped <> True Then
     'Second move
          secCard = randomCard


          If firstCard.PairType = secCard.PairType And firstCard IsNot secCard Then
          'If the two random cards are from the same type
          MsgBox("Computer match. ONE POINT!")
          cardList.Remove(firstCard)
          cardList.Remove(secCard)
          counter += 1

          Else
              MsgBox("Computer NO match!")
              counter += 1              
          End If
   End If
'Next random to keep loop going
rnd = New Random()
randomCard = cardList(rnd.Next(0, cardList.Count))
End While
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
madeluccar
  • 157
  • 1
  • 1
  • 8
  • For one thing, stop creating new `Random` objects. You should always create just one and reuse it. Declare a member variable and assign your one and only `Random` object to that variable and then call `Next` on that same object repeatedly. – jmcilhinney Nov 23 '15 at 06:27
  • I'm guessing that your randomCard.flipped code is always returning true. Without your randomCard code it will be impossible to tell why. Cab you edit your post to include all the randomCard code please? Also same problem with your firstCard code...By the way- For next time, the learning curve for a new user isn't steep as such, but finding out how to use stack isn't always obvious. Have a look here [ask] .. cheers. – David Wilson Nov 23 '15 at 21:00

1 Answers1

1

I couldn't test your code because I don't have your list of cards. However try taking the second "rnd = New Random()" out. It may be using the same seed a second time, resulting in a repeat. Instead, just call rnd.Next to get the next random number. See Random number generator only generating one random number.

DenverJT
  • 125
  • 7