Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Private mathQuestions() As Integer
- Private mathOperators() As String
- Private userAnswers() As Integer
- Private opType As Integer
- Private numQuestions As Integer
- Private curDate As Date
- Private gameRunning As Boolean
- Private Sub optAdd_Click()
- Range("Operator").Value = "+"
- opType = 1
- End Sub
- Private Sub optSubtract_Click()
- Range("Opertator").Value = "-"
- opType = 2
- End Sub
- Private Sub optMultiply_Click()
- Range("Operator").Value = "x"
- opType = 3
- End Sub
- Private Sub optDivide_Click()
- Range("Operator").Value = "/"
- opType = 4
- End Sub
- Private Sub optAny_Click()
- Range("Operator").Value = ""
- End Sub
- Private Sub cmdBegin_Click()
- EnableControls False
- numQuestions = 0
- gameRunning = True
- Range("A2:C" & UsedRange.Rows.Count).ClearContents
- Range("Answer").Select
- Application.MoveAfterReturn = False
- GetOperatorType
- GetOperands
- curDate = Now
- MathGame
- End Sub
- Private Sub EnableControls(ctrlsEnabled As Boolean)
- cmdBegin.Enabled = ctrlsEnabled
- optAdd.Enabled = ctrlsEnabled
- optSubtract.Enabled = ctrlsEnabled
- optMultiply.Enabled = ctrlsEnabled
- optDivide.Enabled = ctrlsEnabled
- optAny.Enabled = ctrlsEnabled
- End Sub
- Private Sub GetOperatorType()
- If optAdd.Value = True Then opType = 1
- If optSubtract.Value = True Then opType = 2
- If optMultiply.Value = True Then opType = 3
- If optDivide.Value = True Then opType = 4
- If optAny.Value = True Then GetRandomOperator
- End Sub
- Private Sub GetRandomOperator()
- Randomize
- opType = Int(4 * Rnd) + 1
- Select Case opType
- Case Is = 1
- Range("Operator").Value = "+"
- Case Is = 2
- Range("Operator").Value = "-"
- Case Is = 3
- Range("Operator").Value = "x"
- Case Is = 4
- Range("Operator").Value = "/"
- Case Else
- Range("Operator").Value = "+"
- End Select
- End Sub
- Private Sub GetOperands()
- Dim rightOperand As Integer
- rightOperand = GetRandomNumber(1)
- Range("RightOperand").Value = rightOperand
- Range("LeftOperand").Value = GetRandomNumber(rightOperand)
- End Sub
- Private Function GetRandomNumber(divisibleBy As Integer) As Integer
- Dim ranNum As Integer
- Const upperLimit = 10
- Randomize
- Do
- ranNum = Int(upperLimit * Rnd) + 1
- Loop Until ((opType <> 4) Or (ranNum Mod divisibleBy = 0))
- GetRandomNumber = ranNum
- End Function
- Private Sub MathGame()
- Dim numSeconds As Integer
- Dim nextTime As Date
- Const TIMEALLOWED = 60
- numSeconds = 1
- nextTime = Now + TimeValue("00:00:10")
- Application.OnTime EarliestTime:=nextTime, _
- Procedure:="MathGameSheet.MathGame", Schedule:=True
- Do
- If (nextTime) Then
- Range("Clock").Value = Range("Clock").Value - numSeconds
- Else
- Range("Clock").Value = TIMEALLOWED
- End If
- Loop Until Range("Clock").Value = 0
- If (Range("Clock").Value = 0) Then
- gameRunning = False
- Range("Clock").Value = TIMEALLOWED
- Application.OnTime EarliestTime:=nextTime, _
- Procedure:="MathGameSheet.MathGame", Schedule:=True
- EnableControls True
- ClearBoard
- ScoreAnswers
- Application.MoveAfterReturn = True
- End If
- End Sub
- Private Sub Worksheet_Change(ByVal Target As Range)
- If (Target.Address = "$L$8") And _
- (Range("Answer").Value <> "") And gameRunning Then
- numQuestions = numQuestions + 1
- StoreQuestions
- If optAny.Value = True Then
- GetRandomOperator
- End If
- GetOperands
- Range("Answer").Select
- Selection.Value = ""
- End If
- End Sub
- Private Sub StoreQuestions()
- ReDim Preserve mathQuestions(1, numQuestions) As Integer
- ReDim Preserve mathOperators(numQuestions) As String
- ReDim Preserve userAnswers(numQuestions) As Integer
- mathQuestions(0, numQuestions - 1) = Range("LeftOperand").Value
- mathQuestions(1, numQuestions - 1) = Range("RightOperand").Value
- mathOperators(numQuestions - 1) = Range("Operator").Value
- userAnswers(numQuestions - 1) = Val(Range("Answer").Value)
- End Sub
- Private Sub ClearBoard()
- Range("LeftOperand").Value = ""
- Range("RightOperand").Value = ""
- Range("Answer").Value = ""
- End Sub
- Private Sub ScoreAnswers()
- Dim I As Integer
- Dim numWrong As Integer
- For I = 0 To numQuestions - 1
- Cells(I + 2, "A").Value = mathQuestions(0, I) & _
- mathOperators(I) & mathQuestions(1, I)
- Cells(I + 2, "B").Value = userAnswers(I)
- If mathOperators(I) = "x" Then
- Cells(I + 2, "C").Formula = "=" & _
- mathQuestions(0, I) & "*" & mathQuestions(1, I)
- Cells(I + 2, "B").Font.Color = RGB(0, 0, 0)
- Else
- Cells(I + 2, "C").Formula = "=" & _
- mathQuestions(0, I) & mathOperators(I) & mathQuestions(1, I)
- Cells(I + 2, "B").Font.Color = RGB(0, 0, 0)
- End If
- If Cells(I + 2, "B").Value <> Cells(I + 2, "C").Value Then
- Cells(I + 2, "B").Font.Color = RGB(255, 0, 0)
- numWrong = numWrong + 1
- End If
- Next I
- Cells(I + 2, "A").Value = "Score (%)"
- Cells(I + 2, "B").Font.Color = RGB(0, 0, 0)
- Cells(I + 2, "B").Formula = "=" & _
- (numQuestions - numWrong) / numQuestions & "*100"
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement