1. Option Explicit
  2.  
  3. Dim UserBet As Long
  4. Dim UserHorse As Integer
  5. Dim UserWallet As Long
  6.  
  7. Dim Horses As Variant
  8. Dim HorsesFinished As Integer
  9. Dim OrderOfBrilliance As String
  10. Dim RealOrder(0 To 3) As Integer
  11.  
  12. Dim StartCount As Integer
  13. Dim ElapsedTime As Integer
  14. Dim HorseTimes(0 To 3) As Integer
  15. Dim HorseMovement(0 To 3) As Integer
  16. Dim HorseOdds(0 To 3) As Integer
  17.  
  18. Dim KeyCount1 As String
  19. Dim LastKey As Integer
  20.  
  21. Private Sub Form_KeyPress(KeyAscii As Integer)
  22. If KeyAscii = 27 Then
  23. RestartTimer.Enabled = True
  24. UserWallet = 500
  25. lblWallet.Caption = "£" & UserWallet
  26. ElseIf KeyAscii = 77 Then
  27. KeyCount1 = "M"
  28. LastKey = 77
  29. ElseIf KeyAscii = 79 And LastKey = 77 Then
  30. KeyCount1 = "MO"
  31. LastKey = 79
  32. ElseIf KeyAscii = 78 And LastKey = 79 Then
  33. KeyCount1 = "MON"
  34. LastKey = 78
  35. ElseIf KeyAscii = 69 And LastKey = 78 Then
  36. KeyCount1 = "MONE"
  37. LastKey = 69
  38. ElseIf KeyAscii = 89 And LastKey = 69 Then
  39. KeyCount1 = "MONEY"
  40.  
  41. If KeyCount1 = "MONEY" Then
  42. UserWallet = "30000"
  43. lblWallet.Caption = "£" & UserWallet
  44. End If
  45. End If
  46. End Sub
  47.  
  48. Private Sub Form_Load()
  49. Debug.Print "---App Start"
  50.  
  51. ' Setup Horse env variables
  52. Horses = Array("Ace", "Aftershock", "Maestro", "Sabre")
  53.  
  54. Call SetHorse
  55.  
  56. Dim j As Integer
  57. For j = LBound(Horses) To UBound(Horses)
  58. ' Show visible information about horses
  59. cmbHorses.AddItem Horses(j)
  60. lblHorse(j).Caption = Horses(j) & " on track " & j + 1 & " at " & HorseOdds(j) & ":1"
  61. Next
  62.  
  63. ' Store and show the user's pot
  64. UserWallet = 500
  65. lblWallet.Caption = "£" & UserWallet
  66.  
  67. ' Starting countdown timer
  68. StartCount = 5
  69.  
  70. ' Elapsed time count
  71. ElapsedTime = 1
  72. End Sub
  73.  
  74. Private Sub SetHorse()
  75. Debug.Print "---HORSE VALUES---"
  76.  
  77. Dim i As Integer
  78. For i = 0 To 3
  79. ' Calculate horse transport times
  80. Randomize
  81. HorseTimes(i) = CInt((5 - 1 + 1) * Rnd + 1)
  82.  
  83. HorseMovement(i) = CInt(8020 * (HorseTimes(i) / 1000))
  84. HorseTimer(i).Interval = HorseTimes(i)
  85. Debug.Print "Horse " & i + 1 & " interval: " & HorseMovement(i)
  86. Debug.Print "Horse " & i + 1 & " time: " & HorseTimer(i).Interval
  87. Next
  88.  
  89. Dim j As Integer
  90. For j = 0 To 3
  91. ' Calculate horse odds
  92. Randomize
  93. HorseOdds(j) = CInt((5 - 1 + 1) * Rnd + 1)
  94.  
  95. Debug.Print "Horse " & j + 1 & " has odds: " & HorseOdds(j) & ":1"
  96. Next
  97. End Sub
  98.  
  99. Private Sub cmbHorses_Click()
  100. ' When a horse is selected, let the user place a bet
  101. lblChoice.Visible = True
  102. lblBet.Visible = True
  103. lblBet2.Visible = True
  104. txtBet.Visible = True
  105. End Sub
  106.  
  107. Private Sub txtBet_Change()
  108. On Error GoTo TypeMismatchHandler
  109.  
  110. ' Some error checking to prevent users entering spurious data,
  111. ' i.e. text instead of integers
  112. If Not txtBet.Text = "" Then
  113. ' Preventing buffer overflows..
  114. If CLng(txtBet.Text) >= 5 And Len(txtBet.Text) < 5 Then
  115. cmdBet.Visible = True
  116. End If
  117. Else
  118. cmdBet.Visible = False
  119. End If
  120.  
  121. Exit Sub
  122.  
  123. TypeMismatchHandler:
  124. txtBet.Text = ""
  125. End Sub
  126.  
  127. Private Sub cmdBet_Click()
  128. On Error GoTo TypeMismatchHandler
  129.  
  130. ' Similar error checks as before, but repeated for safety
  131. If Not txtBet.Text = "" And Len(txtBet.Text) < 5 Then
  132. If CLng(txtBet.Text) >= 5 And CLng(txtBet.Text) <= 1000 Then
  133. ' Make sure that the user isn't betting more than they can pull
  134. If UserWallet - CLng(txtBet.Text) < 0 Then
  135. lblBetDone.Caption = "Sorry, you do not have enough money to make that bet."
  136. Else
  137. Debug.Print "---BET MADE---"
  138.  
  139. ' Update the user's pot
  140. UserWallet = UserWallet - CLng(txtBet.Text)
  141. lblWallet.Caption = "£" & UserWallet
  142.  
  143. Debug.Print "User Money: " & UserWallet
  144.  
  145. ' Store betting information
  146. ' Horse and amount bet
  147. UserHorse = cmbHorses.ListIndex
  148. UserBet = CLng(txtBet.Text)
  149.  
  150. Debug.Print "User Bet: " & UserBet
  151.  
  152. ' Prevent the user from making any further changes to bet
  153. cmdBet.Enabled = False
  154. txtBet.Enabled = False
  155. cmbHorses.Enabled = False
  156.  
  157. lblBetDone.Caption = "Great! Your bet has been placed. Now get to the track, the race is about to begin!"
  158.  
  159. ' Enable race timer so that the race can begin
  160. RaceStartTimer.Enabled = True
  161. End If
  162. Else
  163. lblBetDone.Caption = "Sorry, you can not make a bet of such an amount."
  164. End If
  165. Else
  166. lblBetDone.Caption = "Sorry, that is not a valid amount to bet."
  167. End If
  168.  
  169. Exit Sub
  170.  
  171. TypeMismatchHandler:
  172. lblBetDone.Caption = "There was a problem with your request."
  173. End Sub
  174.  
  175. Private Sub RaceStartTimer_Timer()
  176. If Not StartCount <= 0 Then
  177. lblTimer.Caption = StartCount
  178. Else
  179. Debug.Print "---RACE STARTED---"
  180. lblTimer.Visible = False
  181. RaceStartTimer.Enabled = False
  182.  
  183. ' Show the race track
  184. lblBegun.Visible = True
  185. lblTrackStart.Visible = True
  186. lblTrackStart2.Visible = True
  187. lblTrackEnd.Visible = True
  188. lblTrackEnd2.Visible = True
  189. lblTrack1.Visible = True
  190. lblTrack2.Visible = True
  191. lblTrack3.Visible = True
  192. lblTrack4.Visible = True
  193. shpTrack1.Visible = True
  194. shpTrack2.Visible = True
  195. shpTrack3.Visible = True
  196. shpTrack4.Visible = True
  197. shpEndLine.Visible = True
  198. shpStartLine.Visible = True
  199.  
  200. NRaceHorse(0).Visible = True
  201. NRaceHorse(1).Visible = True
  202. NRaceHorse(2).Visible = True
  203. NRaceHorse(3).Visible = True
  204.  
  205. lblTime.Visible = True
  206. lblTheTimer.Visible = True
  207. HorseTimer(0).Enabled = True
  208. HorseTimer(1).Enabled = True
  209. HorseTimer(2).Enabled = True
  210. HorseTimer(3).Enabled = True
  211. Timer.Enabled = True
  212. End If
  213.  
  214. StartCount = StartCount - 1
  215. End Sub
  216.  
  217. Private Sub Timer_Timer()
  218. lblTheTimer.Caption = "0:" & ElapsedTime
  219.  
  220. ElapsedTime = ElapsedTime + 1
  221. End Sub
  222.  
  223. Private Sub HorseTimer_Timer(Index As Integer)
  224. NRaceHorse(Index).Left = NRaceHorse(Index).Left + HorseMovement(Index)
  225.  
  226. 'Debug.Print "Horse " & Index & " is at Left val " & NRaceHorse(Index).Left
  227. If NRaceHorse(Index).Left >= 13300 Then
  228. Debug.Print "Horse " & Index & " finished at Left val " & NRaceHorse(Index).Left
  229. HorseTimer(Index).Enabled = False
  230. HorsesFinished = HorsesFinished + 1
  231.  
  232. If OrderOfBrilliance = "r" Then
  233. OrderOfBrilliance = CStr(Index)
  234. Else
  235. OrderOfBrilliance = OrderOfBrilliance & CStr(Index)
  236. End If
  237. End If
  238.  
  239. If HorsesFinished = 4 Then
  240. Debug.Print "Horse " & Index + 1 & " has finished the race last."
  241. Timer.Enabled = False
  242. End If
  243.  
  244. If Len(OrderOfBrilliance) = 4 Then
  245. Debug.Print "End of race proc called"
  246. Call HorsesEnd
  247. End If
  248. End Sub
  249.  
  250.  
  251. Private Sub HorsesEnd()
  252. Debug.Print "End of race proc started"
  253.  
  254. Dim i As Integer
  255. For i = 0 To 3
  256. Dim SplitString(0 To 3) As String
  257. RealOrder(i) = Mid(OrderOfBrilliance, i + 1, 1)
  258. Next
  259.  
  260. TableTimer.Enabled = True
  261. End Sub
  262.  
  263. Private Sub TableTimer_Timer()
  264. Debug.Print "---TRACK HIDDEN---"
  265.  
  266. lblTrackStart.Visible = False
  267. lblTrackStart2.Visible = False
  268. lblTrackEnd.Visible = False
  269. lblTrackEnd2.Visible = False
  270. lblTrack1.Visible = False
  271. lblTrack2.Visible = False
  272. lblTrack3.Visible = False
  273. lblTrack4.Visible = False
  274. shpTrack1.Visible = False
  275. shpTrack2.Visible = False
  276. shpTrack3.Visible = False
  277. shpTrack4.Visible = False
  278. shpEndLine.Visible = False
  279. shpStartLine.Visible = False
  280.  
  281. NRaceHorse(0).Visible = False
  282. NRaceHorse(1).Visible = False
  283. NRaceHorse(2).Visible = False
  284. NRaceHorse(3).Visible = False
  285.  
  286. lblTime.Visible = False
  287. lblTheTimer.Visible = False
  288.  
  289. '''-------------------------
  290.  
  291. lblBegun.Caption = "The Winners Are:"
  292.  
  293. lblPos1.Visible = True
  294. lblPos2.Visible = True
  295. lblPos3.Visible = True
  296. lblPos4.Visible = True
  297.  
  298. lblStatDesc.Visible = True
  299. lblWinLoss.Visible = True
  300.  
  301. lblComfort.Visible = True
  302.  
  303. Dim i As Integer
  304. For i = 0 To 3
  305. lblPos(i).Visible = True
  306. lblPos(i).Caption = Horses(RealOrder(i))
  307. Next
  308.  
  309. If RealOrder(0) = UserHorse Then
  310. lblWinLoss.Caption = ", at the odds " & HorseOdds(UserHorse) & ":1, won £" & UserBet * HorseOdds(UserHorse) & "!"
  311. UserWallet = UserWallet + (UserBet * HorseOdds(UserHorse))
  312. lblComfort.Caption = "Well done! Now let's see if you can double that sum."
  313. Else
  314. lblWinLoss.Caption = "lost £" & UserBet & "!"
  315. lblComfort.Caption = "Better luck next time! Play again! Let's see if you can regain your loss!"
  316. End If
  317.  
  318. If UserWallet > 0 Then
  319. lblWallet.Caption = "£" & UserWallet
  320. End If
  321.  
  322. lblRestart.Visible = True
  323.  
  324. TableTimer.Enabled = False
  325. RestartTimer.Enabled = True
  326. End Sub
  327.  
  328. Private Sub RestartTimer_Timer()
  329. Debug.Print "-----RACE RESTARTED-----"
  330.  
  331. lblTrackStart.Visible = False
  332. lblTrackStart2.Visible = False
  333. lblTrackEnd.Visible = False
  334. lblTrackEnd2.Visible = False
  335. lblTrack1.Visible = False
  336. lblTrack2.Visible = False
  337. lblTrack3.Visible = False
  338. lblTrack4.Visible = False
  339. shpTrack1.Visible = False
  340. shpTrack2.Visible = False
  341. shpTrack3.Visible = False
  342. shpTrack4.Visible = False
  343. shpEndLine.Visible = False
  344. shpStartLine.Visible = False
  345.  
  346. NRaceHorse(0).Visible = False
  347. NRaceHorse(1).Visible = False
  348. NRaceHorse(2).Visible = False
  349. NRaceHorse(3).Visible = False
  350.  
  351. lblTime.Visible = False
  352. lblTheTimer.Visible = False
  353.  
  354. lblBegun.Caption = "And they're off!"
  355. lblBegun.Visible = False
  356.  
  357. lblPos1.Visible = False
  358. lblPos2.Visible = False
  359. lblPos3.Visible = False
  360. lblPos4.Visible = False
  361.  
  362. lblStatDesc.Visible = False
  363. lblWinLoss.Visible = False
  364. lblComfort.Visible = False
  365.  
  366. lblTimer.Caption = ""
  367. lblTimer.Visible = True
  368.  
  369. OrderOfBrilliance = "r"
  370.  
  371. RealOrder(0) = 0
  372. RealOrder(1) = 0
  373. RealOrder(2) = 0
  374. RealOrder(3) = 0
  375.  
  376. HorseOdds(0) = 0
  377. HorseOdds(1) = 0
  378. HorseOdds(2) = 0
  379. HorseOdds(3) = 0
  380.  
  381. UserHorse = -1
  382.  
  383. HorsesFinished = 0
  384.  
  385. UserBet = 0
  386.  
  387. Dim i As Integer
  388. For i = 0 To 3
  389. lblPos(i).Visible = False
  390. Next
  391.  
  392. Dim j As Integer
  393. For j = 0 To 3
  394. NRaceHorse(j).Left = 5280
  395. Next
  396.  
  397. lblTheTimer.Caption = "0:00"
  398.  
  399. cmdBet.Enabled = True
  400. cmdBet.Visible = False
  401. txtBet.Enabled = True
  402. txtBet.Visible = False
  403. txtBet.Text = ""
  404. cmbHorses.Enabled = True
  405.  
  406. lblChoice.Visible = False
  407. lblBet.Visible = False
  408. lblBet2.Visible = False
  409. lblBetDone.Caption = ""
  410.  
  411. Call SetHorse
  412.  
  413. Dim k As Integer
  414. For k = LBound(Horses) To UBound(Horses)
  415. ' Show visible information about horses
  416. lblHorse(k).Caption = Horses(k) & " on track " & k + 1 & " at " & HorseOdds(k) & ":1"
  417. Next
  418.  
  419. StartCount = 5
  420.  
  421. ElapsedTime = 1
  422.  
  423. lblRestart.Visible = False
  424.  
  425. TableTimer.Enabled = False
  426.  
  427. If UserWallet <= 0 Then
  428. UserWallet = 100
  429. lblWallet.Caption = "£" & UserWallet
  430. MsgBox ("Looks like you've run out of money. But no worries, you're in luck, a friendly banker has sent some cash your way. Let's see if you can regain your losses.")
  431. End If
  432.  
  433. RestartTimer.Enabled = False
  434. End Sub
  435.  
  436.