Hi I am making the simon game from. I am having a problem with one part it seems as if somehow my Arrtemp array ubound is changing from 0 to 1 for no reason at all. If someone could tell me why it would be great thanks.




VB Code:
  1. Dim lastcolor As String
  2. Dim memoryStack() As String
  3. Dim gameMemoryStack() As String   'make a connection with the memorycounters thats i
  4. Dim firstColor As String
  5. Dim memoryCounter As Integer
  6. Dim roundNumber As Integer
  7. Dim arrTemp() As String
  8. Private Function pop() As String
  9.     Dim arrTemp() As String
  10.     ReDim arrTemp(UBound(memoryStack))
  11.     arrTemp() = reverseOrder(memoryStack)
  12.    
  13.     pop = arrTemp(UBound(arrTemp))
  14.     If UBound(memoryStack) = 0 Then
  15.         Call nextRound   'gets the next round
  16.     Else
  17.         ReDim Preserve arrTemp(UBound(arrTemp) - 1)
  18.     End If
  19.         Debug.Print UBound(arrTemp)     '******************
  20.         memoryStack = arrTemp             '*****printing "0"
  21.         Debug.Print UBound(arrTemp)   '*******************
  22. End Function
  23.  
  24. Private Sub Form_Load()
  25. 'sets default values to the two arrays used to hold values of the letters pushed
  26.     Dim i As Integer
  27.     Dim randomNumber As Integer
  28.     Dim gameColor As String
  29.     ReDim memoryStack(0)
  30.     ReDim gameMemoryStack(24)
  31.     roundNumber = 1
  32.    
  33.     Randomize
  34.     'decides what the colors are going to be for the whole game
  35.     'fills an array with random colors of red,yellow,blue, and green
  36.     For i = 0 To 24
  37.         randomNumber = Int(4 * Rnd)
  38.         Select Case randomNumber
  39.             Case 0
  40.                 gameColor = "Yellow"
  41.             Case 1
  42.                 gameColor = "Red"
  43.             Case 2
  44.                 gameColor = "Blue"
  45.             Case 3
  46.                 gameColor = "Green"
  47.         End Select
  48.         gameMemoryStack(i) = gameColor
  49.     Next i
  50.     firstColor = gameMemoryStack(0)
  51.     Call nextRound
  52.    
  53.  
  54. End Sub
  55. Private Sub lblYellowbutton_click()
  56.     Dim begColor As String
  57.     Dim colorPush As String
  58.     colorPush = "Yellow"
  59.  
  60.     If (colorPush = memoryStack(memoryCounter)) Then
  61.        lastcolor = pop
  62.        memoryCounter = memoryCounter + 1
  63.        Debug.Print UBound(arrTemp)
  64.        If UBound(arrTemp) = 1 Then
  65.           roundNumber = roundNumber + 1
  66.           Call nextRound
  67.        End If
  68.         'we want to add a new color to the old memory stack
  69.     Else
  70.         End
  71.     End If
  72. End Sub
  73. Private Sub lblBluebutton_click()
  74.     Dim begColor As String
  75.     Dim colorPush As String
  76.     colorPush = "Blue"
  77.      
  78.     If (colorPush = memoryStack(memoryCounter)) Then
  79.        lastcolor = pop                       '  *******************
  80.                                                        ' ********* prints "1"
  81.        Debug.Print UBound(arrTemp)  '   **********************
  82.        memoryCounter = memoryCounter + 1
  83.        Debug.Print UBound(arrTemp)
  84.        If UBound(arrTemp) = 1 Then    'changes memory stack to arrtemp
  85.           roundNumber = roundNumber + 1
  86.           Call nextRound
  87.        End If
  88.     Else
  89.        End
  90.     End If
  91. End Sub
  92. Private Sub lblGreenbutton_click()
  93.     Dim begColor As String
  94.     Dim colorPush As String
  95.     colorPush = "Green"
  96.    
  97.     If (colorPush = memoryStack(memoryCounter)) Then
  98.        lastcolor = pop
  99.        memoryCounter = memoryCounter + 1
  100.        Debug.Print UBound(arrTemp)
  101.        If UBound(arrTemp) = 1 Then
  102.           roundNumber = roundNumber + 1
  103.           Call nextRound
  104.        End If
  105.     Else
  106.        End
  107.     End If
  108. End Sub
  109. Private Sub lblRedbutton_click()
  110.     Dim begColor As String
  111.     Dim colorPush As String
  112.     colorPush = "Red"
  113.  
  114.      If (colorPush = memoryStack(memoryCounter)) Then
  115.        lastcolor = pop
  116.        memoryCounter = memoryCounter + 1
  117.        Debug.Print UBound(arrTemp)
  118.        If UBound(arrTemp) = 1 Then       '***********************************
  119.           roundNumber = roundNumber + 1 'this may be a problem ??????????????
  120.           Call nextRound        '***********************************
  121.        End If
  122.     Else
  123.        End
  124.     End If
  125. End Sub
  126.  
  127.  
  128. Private Sub TMRBLINK_Timer()
  129.      'this procedure makes the buttons blink
  130.      Dim i As Integer, j As Integer
  131.      Static blinkNumber As Integer
  132.      
  133.         If blinkNumber < UBound(memoryStack) Then
  134.             firstColor = memoryStack(blinkNumber)
  135.             Call colorSelect
  136.             tmrblink.Enabled = False
  137.             tmrColorBlinked.Enabled = True
  138.             blinkNumber = blinkNumber + 1
  139.         Else
  140.             blinkNumber = 0
  141.             tmrblink.Enabled = False
  142.         End If
  143.        
  144.      
  145.    
  146. End Sub
  147.  
  148.  
  149. Private Function reverseOrder(arrName() As String) As String()
  150.     'this function reverses the order of the array so that we can
  151.     'pull from the bottom
  152.     Dim i As Integer
  153.     Dim tempArr() As String
  154.     ReDim tempArr(UBound(arrName))
  155.     For i = 0 To UBound(arrName)
  156.         tempArr(i) = arrName(UBound(arrName) - 1)
  157.     Next i
  158.     reverseOrder = tempArr
  159.  
  160. End Function
  161.  
  162. Private Sub tmrColorBlinked_Timer()
  163.     'sets the colors back to the original backcolor
  164.     lblYellowButton.BackColor = &HC0C0&
  165.     lblRedButton.BackColor = &HC0&
  166.     lblGreenButton.BackColor = &H8000&
  167.     lblBlueButton.BackColor = &H800000
  168.     tmrColorBlinked.Enabled = False
  169.     tmrblink.Enabled = True
  170. End Sub
  171.  
  172. Private Sub nextRound()
  173.     'this sub procedure starts a new round of the game
  174.     'it pulls a new number out of the gameMemoryStack array
  175.     'and adds it to the memorystack
  176.     Dim i As Integer
  177.     memoryCounter = 0   'intitializes the memory counter
  178.     ReDim memoryStack(roundNumber)
  179.     MsgBox "Round " & roundNumber
  180.     For i = 0 To roundNumber - 1
  181.         memoryStack(i) = gameMemoryStack(i)            'fills the array to be tested
  182.     Next i
  183.     ReDim arrTemp(UBound(memoryStack))
  184.     arrTemp() = reverseOrder(memoryStack)
  185.     tmrblink.Enabled = True                            'tells the color button to flash
  186. End Sub
  187. Private Sub MessageBox()
  188.     'Just a simple message box
  189.     Dim mbox As Integer
  190.     Dim title As String
  191.     Dim message As String
  192.     title = "Game Over"
  193.     message = "You got " & roundNumber & " correct"
  194.     mbox = MsgBox(message, , title)
  195. End Sub
  196.  
  197.  
  198. Private Sub colorSelect()
  199.     Select Case firstColor
  200.         Case "Yellow"
  201.             lblYellowButton.BackColor = vbYellow
  202.         Case "Red"
  203.             lblRedButton.BackColor = vbRed
  204.         Case "Blue"
  205.             lblBlueButton.BackColor = vbBlue
  206.         Case "Green"
  207.             lblGreenButton.BackColor = vbGreen
  208.     End Select
  209. End Sub