ZeroAccend

AHK Practice Problem 1: Final Basketball Score

Jun 10th, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. ;============================== Start Auto-Execution Section ==============================
  2.  
  3. ; Always run as admin
  4. if not A_IsAdmin
  5. {
  6. Run *RunAs "%A_ScriptFullPath%" ; Requires v1.0.92.01+
  7. ExitApp
  8. }
  9.  
  10. ; Determines how fast a script will run (affects CPU utilization)
  11. ; The value -1 means the script will run at it's max speed possible
  12. SetBatchLines, -1
  13.  
  14. ; Avoids checking empty variables to see if they are environment variables
  15. ; Recommended for performance and compatibility with future AutoHotkey releases
  16. #NoEnv
  17.  
  18. ; Ensures that there is only a single instance of this script running
  19. #SingleInstance, Force
  20.  
  21. ; Makes a script unconditionally use its own folder as its working directory
  22. ; Ensures a consistent starting directory
  23. SetWorkingDir %A_ScriptDir%
  24.  
  25. ; sets title matching to search for "containing" instead of "exact"
  26. SetTitleMatchMode, 2
  27.  
  28. ; Sets the key send input type
  29. ; Use Event to make use of KeyDelay below
  30. SendMode, Input
  31.  
  32. ; return
  33.  
  34. ;============================== Main Script ==============================
  35.  
  36. ; Normally I'd do this with a GUI but I'm trying something different.
  37. ; This formats the MsgBox to look like how I want it.
  38. ; A SetTimer is used because the MsgBox must be up before it's controls
  39. ; can be changed.
  40. SetTimer, FormatMsgBox, 20
  41.  
  42. ; MsgBox I'll be using as a lazy GUI
  43. ; Yes assigns 1 to team. 2 assigns no.
  44. MsgBox, 515, Teams, How many teams are there?
  45. IfMsgBox, Yes
  46. team := 1
  47. IfMsgBox, No
  48. team := 2
  49. IfMsgBox, Cancel
  50. CancelCheck(1)
  51.  
  52. gosub, GetScores
  53.  
  54. gosub, CalcScore
  55.  
  56. GetScores:
  57. InputBox, threePt1, Team 1 - 3 Pointers, How many 3 pointers were made by team 1?
  58. CancelCheck(ErrorLevel)
  59. NumCheck(threePt1)
  60. InputBox, twoPt1, Team 1 - 2 Pointers, How many 2 pointers were made by team 1?
  61. CancelCheck(ErrorLevel)
  62. NumCheck(twoPt1)
  63. InputBox, freeTh1, Team 1 - Free Throws, How many free throws were made by team 1?
  64. CancelCheck(ErrorLevel)
  65. NumCheck(freeTh1)
  66. ; NumCheck(threePt1)
  67. ; NumCheck(twoPt1)
  68. ; NumCheck(freeTh1)
  69. if (team = 2){
  70. InputBox, threePt2, Team 2 - 3 Pointers, How many 3 pointers were made by team 2?
  71. CancelCheck(ErrorLevel)
  72. NumCheck(threePt2)
  73. InputBox, twoPt2, Team 2 - 2 Pointers, How many 2 pointers were made by team 2?
  74. CancelCheck(ErrorLevel)
  75. NumCheck(twoPt2)
  76. InputBox, freeTh2, Team 2 - Free Throws, How many free throws were made by team 2?
  77. CancelCheck(ErrorLevel)
  78. NumCheck(freeTh2)
  79. }
  80. return
  81.  
  82. CalcScore:
  83. team1Total := threePt1 * 3 + twoPt1 * 2 + freeTh1
  84. team2Total := threePt2 * 3 + twoPt2 * 2 + freeTh2
  85. if (team = 1){
  86. MsgBox % "The team's total score is: " . team1Total
  87. ExitApp
  88. }
  89. if (team1Total = team2Total)
  90. gosub, TieBreaker
  91.  
  92. winner := team1Total > team2Total ? "Team 1" : "Team 2"
  93. MsgBox % "The winner is " . winner . "`nTeam 1 Total: " . team1Total . "`nTeam 2 Total: " . team2Total
  94. ExitApp
  95.  
  96. TieBreaker:
  97. MsgBox % "It's a tie!`nTeam 1 Total: " . team1Total . "`nTeam 2 Total: " . team2Total . "`nPlease click OK to flip a coin and randomly choose a winner.`n`nHeads, team 1 wins." . A_Tab A_Tab A_Tab . "Tails, team 2 wins."
  98. Random, coinFlip, 1, 100
  99. result := (mod(coinFlip, 2)) = 1 ? "Tails" : "Heads"
  100. winner := result = 1 ? "Team 2" : "Team 1"
  101. MsgBox, % "Coin Flip.`n`nIt's " . result . "!`n`n" . winner . " wins!"
  102. ExitApp
  103.  
  104. ; Changes button names on msgbox to 1 Team and 2 Teams. Cancel is untouched.
  105. FormatMsgBox:
  106. ; SetTimer keeps running until Teams window is found
  107. IfWinNotExist, Teams
  108. return
  109. ; Once found, turn off SetTimer
  110. SetTimer, FormatMsgBox, Off
  111.  
  112. ; Activate window and change button text
  113. WinActivate
  114. ControlSetText, button1, 1 Team
  115. ControlSetText, button2, 2 Teams
  116. return
  117.  
  118. ; Checks if cancel is pushed when using an input box.
  119. ; If yes, tell user then exit script.
  120. CancelCheck(EL){
  121. if (EL = 1){
  122. MsgBox, Cancel was pressed.`nThe script will now exit.
  123. ExitApp
  124. }
  125. return
  126. }
  127.  
  128. NumCheck(temp){
  129. MsgBox numcheck start
  130. ; Check to ensure there are only number but there is at least one number
  131. if (RegExMatch(temp, "^[0-9]+$")){
  132. MsgBox This is a number.
  133. return
  134. }
  135.  
  136. ; Check to see if value is blank
  137. if (RegExMatch(temp, "^\s*$")){
  138. MsgBox, You cannot leave a number blank
  139. gosub, GetScores
  140. return
  141. }
  142. else{
  143. MsgBox % temp . " is not a number."
  144. gosub, GetScores
  145. }
  146. }
  147.  
  148. ;============================== GroggyOtter ==============================
  149. ;============================== End Script ==============================
Advertisement
Add Comment
Please, Sign In to add comment