Advertisement
Guest User

Non-working VBA math game

a guest
Apr 15th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. Option Explicit
  2. Private mathQuestions() As Integer
  3. Private mathOperators() As String
  4. Private userAnswers() As Integer
  5. Private opType As Integer
  6. Private numQuestions As Integer
  7. Private curDate As Date
  8. Private gameRunning As Boolean
  9. Private Sub optAdd_Click()
  10. Range("Operator").Value = "+"
  11. opType = 1
  12. End Sub
  13. Private Sub optSubtract_Click()
  14. Range("Opertator").Value = "-"
  15. opType = 2
  16. End Sub
  17. Private Sub optMultiply_Click()
  18. Range("Operator").Value = "x"
  19. opType = 3
  20. End Sub
  21. Private Sub optDivide_Click()
  22. Range("Operator").Value = "/"
  23. opType = 4
  24. End Sub
  25. Private Sub optAny_Click()
  26. Range("Operator").Value = ""
  27. End Sub
  28. Private Sub cmdBegin_Click()
  29.  
  30. EnableControls False
  31. numQuestions = 0
  32. gameRunning = True
  33. Range("A2:C" & UsedRange.Rows.Count).ClearContents
  34. Range("Answer").Select
  35. Application.MoveAfterReturn = False
  36.  
  37. GetOperatorType
  38. GetOperands
  39.  
  40. curDate = Now
  41. MathGame
  42. End Sub
  43. Private Sub EnableControls(ctrlsEnabled As Boolean)
  44. cmdBegin.Enabled = ctrlsEnabled
  45. optAdd.Enabled = ctrlsEnabled
  46. optSubtract.Enabled = ctrlsEnabled
  47. optMultiply.Enabled = ctrlsEnabled
  48. optDivide.Enabled = ctrlsEnabled
  49. optAny.Enabled = ctrlsEnabled
  50. End Sub
  51. Private Sub GetOperatorType()
  52. If optAdd.Value = True Then opType = 1
  53. If optSubtract.Value = True Then opType = 2
  54. If optMultiply.Value = True Then opType = 3
  55. If optDivide.Value = True Then opType = 4
  56. If optAny.Value = True Then GetRandomOperator
  57. End Sub
  58. Private Sub GetRandomOperator()
  59. Randomize
  60. opType = Int(4 * Rnd) + 1
  61. Select Case opType
  62. Case Is = 1
  63. Range("Operator").Value = "+"
  64. Case Is = 2
  65. Range("Operator").Value = "-"
  66. Case Is = 3
  67. Range("Operator").Value = "x"
  68. Case Is = 4
  69. Range("Operator").Value = "/"
  70. Case Else
  71. Range("Operator").Value = "+"
  72. End Select
  73. End Sub
  74. Private Sub GetOperands()
  75. Dim rightOperand As Integer
  76.  
  77. rightOperand = GetRandomNumber(1)
  78. Range("RightOperand").Value = rightOperand
  79. Range("LeftOperand").Value = GetRandomNumber(rightOperand)
  80. End Sub
  81. Private Function GetRandomNumber(divisibleBy As Integer) As Integer
  82. Dim ranNum As Integer
  83. Const upperLimit = 10
  84.  
  85. Randomize
  86.  
  87. Do
  88. ranNum = Int(upperLimit * Rnd) + 1
  89. Loop Until ((opType <> 4) Or (ranNum Mod divisibleBy = 0))
  90.  
  91. GetRandomNumber = ranNum
  92. End Function
  93. Private Sub MathGame()
  94.  
  95. Dim numSeconds As Integer
  96. Dim nextTime As Date
  97. Const TIMEALLOWED = 60
  98.  
  99. numSeconds = 1
  100.  
  101. nextTime = Now + TimeValue("00:00:10")
  102. Application.OnTime EarliestTime:=nextTime, _
  103. Procedure:="MathGameSheet.MathGame", Schedule:=True
  104.  
  105. Do
  106. If (nextTime) Then
  107. Range("Clock").Value = Range("Clock").Value - numSeconds
  108. Else
  109. Range("Clock").Value = TIMEALLOWED
  110. End If
  111. Loop Until Range("Clock").Value = 0
  112.  
  113. If (Range("Clock").Value = 0) Then
  114. gameRunning = False
  115. Range("Clock").Value = TIMEALLOWED
  116. Application.OnTime EarliestTime:=nextTime, _
  117. Procedure:="MathGameSheet.MathGame", Schedule:=True
  118. EnableControls True
  119. ClearBoard
  120. ScoreAnswers
  121. Application.MoveAfterReturn = True
  122. End If
  123. End Sub
  124. Private Sub Worksheet_Change(ByVal Target As Range)
  125.  
  126. If (Target.Address = "$L$8") And _
  127. (Range("Answer").Value <> "") And gameRunning Then
  128. numQuestions = numQuestions + 1
  129. StoreQuestions
  130. If optAny.Value = True Then
  131. GetRandomOperator
  132. End If
  133. GetOperands
  134. Range("Answer").Select
  135. Selection.Value = ""
  136. End If
  137. End Sub
  138. Private Sub StoreQuestions()
  139. ReDim Preserve mathQuestions(1, numQuestions) As Integer
  140. ReDim Preserve mathOperators(numQuestions) As String
  141. ReDim Preserve userAnswers(numQuestions) As Integer
  142.  
  143. mathQuestions(0, numQuestions - 1) = Range("LeftOperand").Value
  144. mathQuestions(1, numQuestions - 1) = Range("RightOperand").Value
  145. mathOperators(numQuestions - 1) = Range("Operator").Value
  146. userAnswers(numQuestions - 1) = Val(Range("Answer").Value)
  147. End Sub
  148. Private Sub ClearBoard()
  149. Range("LeftOperand").Value = ""
  150. Range("RightOperand").Value = ""
  151. Range("Answer").Value = ""
  152. End Sub
  153. Private Sub ScoreAnswers()
  154. Dim I As Integer
  155. Dim numWrong As Integer
  156.  
  157. For I = 0 To numQuestions - 1
  158. Cells(I + 2, "A").Value = mathQuestions(0, I) & _
  159. mathOperators(I) & mathQuestions(1, I)
  160. Cells(I + 2, "B").Value = userAnswers(I)
  161. If mathOperators(I) = "x" Then
  162. Cells(I + 2, "C").Formula = "=" & _
  163. mathQuestions(0, I) & "*" & mathQuestions(1, I)
  164. Cells(I + 2, "B").Font.Color = RGB(0, 0, 0)
  165. Else
  166. Cells(I + 2, "C").Formula = "=" & _
  167. mathQuestions(0, I) & mathOperators(I) & mathQuestions(1, I)
  168. Cells(I + 2, "B").Font.Color = RGB(0, 0, 0)
  169. End If
  170.  
  171. If Cells(I + 2, "B").Value <> Cells(I + 2, "C").Value Then
  172. Cells(I + 2, "B").Font.Color = RGB(255, 0, 0)
  173. numWrong = numWrong + 1
  174. End If
  175. Next I
  176.  
  177. Cells(I + 2, "A").Value = "Score (%)"
  178. Cells(I + 2, "B").Font.Color = RGB(0, 0, 0)
  179. Cells(I + 2, "B").Formula = "=" & _
  180. (numQuestions - numWrong) / numQuestions & "*100"
  181. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement