Advertisement
Guest User

Non-working VBA dice game

a guest
Mar 23rd, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.64 KB | None | 0 0
  1. Option Explicit
  2.  
  3.  
  4. Private Sub imgDice1_Click()
  5. ckBox1.Value = Not ckBox1.Value
  6. End Sub
  7.  
  8. Private Sub imgDice2_Click()
  9. ckBox2.Value = Not ckBox2.Value
  10. End Sub
  11. Private Sub imgDice3_Click()
  12. ckBox3.Value = Not ckBox3.Value
  13. End Sub
  14. Private Sub imgDice4_Click()
  15. ckBox4.Value = Not ckBox4.Value
  16. End Sub
  17. Private Sub imgDice5_Click()
  18. ckBox5.Value = Not ckBox5.Value
  19. End Sub
  20.  
  21. Private Sub ToggleControls(toggle As Boolean)
  22. ckBox1.Enabled = toggle
  23. ckBox2.Enabled = toggle
  24. ckBox3.Enabled = toggle
  25. ckBox4.Enabled = toggle
  26. ckBox5.Enabled = toggle
  27. imgDice1.Enabled = toggle
  28. imgDice2.Enabled = toggle
  29. imgDice3.Enabled = toggle
  30. imgDice4.Enabled = toggle
  31. imgDice5.Enabled = toggle
  32. End Sub
  33. Private Sub cmdNewGame_Click()
  34. ckBox1.Value = False
  35. ckBox2.Value = False
  36. ckBox3.Value = False
  37. ckBox4.Value = False
  38. ckBox5.Value = False
  39.  
  40. Range("C12").Value = ""
  41. cmdRollDice.Enabled = True
  42. cmdNewGame.Enabled = False
  43. imgDice1.Picture = LoadPicture(Workbooks("PokerDice.xlsm").Path & "\" & "empty.bmp")
  44. imgDice2.Picture = LoadPicture(Workbooks("PokerDice.xlsm").Path & "\" & "empty.bmp")
  45. imgDice3.Picture = LoadPicture(Workbooks("PokerDice.xlsm").Path & "\" & "empty.bmp")
  46. imgDice4.Picture = LoadPicture(Workbooks("PokerDice.xlsm").Path & "\" & "empty.bmp")
  47. imgDice5.Picture = LoadPicture(Workbooks("PokerDice.xlsm").Path & "\" & "empty.bmp")
  48. End Sub
  49. Private Sub cmdRollDice_Click()
  50. Static numRolls As Integer
  51. Dim imageFile As String
  52. Dim imagePath As String
  53.  
  54. imagePath = Workbooks("PokeDice.xlsm").Path & "\"
  55. numRolls = numRolls + 1
  56. Randomize
  57.  
  58. If ckBox1.Value = False Then
  59. Range("B2").Value = Int(Rnd * 6) + 1
  60. imageFile = imagePath & Trim(Str(Range("B2").Value)) & ".bmp"
  61. imgDice1.Picture = LoadPicture(imageFile)
  62. End If
  63.  
  64. If ckBox2.Value = False Then
  65. Range("C2").Value = Int(Rnd * 6) + 1
  66. imageFile = imagePath & Trim(Str(Range("C2").Value)) & ".bmp"
  67. imgDice2.Picture = LoadPicture(imageFile)
  68. End If
  69.  
  70. If ckBox3.Value = False Then
  71. Range("D2").Value = Int(Rnd * 6) + 1
  72. imageFile = imagePath & Trim(Str(Range("D2").Value)) & ".bmp"
  73. imgDice3.Picture = LoadPicture(imageFile)
  74. End If
  75.  
  76. If ckBox4.Value = False Then
  77. Range("E2").Value = Int(Rnd * 6) + 1
  78. imageFile = imagePath & Trim(Str(Range("E2").Value)) & ".bmp"
  79. imgDice4.Picture = LoadPicture(imageFile)
  80. End If
  81.  
  82. If ckBox5.Value = False Then
  83. Range("F2").Value = Int(Rnd * 6) + 1
  84. imageFile = imagePath & Trim(Str(Range("F2").Value)) & ".bmp"
  85. imgDice5.Picture = LoadPicture(imageFile)
  86. End If
  87.  
  88. If numRolls = 2 Then
  89. cmdRollDice.Enabled = False
  90. cmdNewGame.Enabled = True
  91. numRolls = 0
  92. Else
  93. ToggleControls True
  94. End If
  95.  
  96. DisplayResult
  97.  
  98. End Sub
  99. Private Sub DisplayResult()
  100. Dim numOnes As Integer
  101. Dim numTwos As Integer
  102. Dim numThrees As Integer
  103. Dim numFours As Integer
  104. Dim numFives As Integer
  105. Dim numSixes As Integer
  106. Dim result As String
  107.  
  108. numOnes = GetNumOnes
  109. numTwos = GetNumTwos
  110. numThrees = GetNumThrees
  111. numFours = GetNumFours
  112. numFives = GetNumFives
  113. numSixes = GetNumSixes
  114.  
  115. result = IsNothingOrStraight(numOnes, numTwos, numThrees, numFours, numFives, numSixes, result)
  116. result = IsOnePair(numOnes, numTwos, numThrees, numFours, numFives, numSixes, result)
  117. result = IsTwoPair(numOnes, numTwos, numThrees, numFours, numFives, numSixes, result)
  118. result = IsThreeOfAKind(numOnes, numTwos, numThrees, numFours, numFives, numSixes, result)
  119. result = IsFourOfAKind(numOnes, numTwos, numThrees, numFours, numFives, numSixes, result)
  120. result = IsFiveOfAKind(numOnes, numTwos, numThrees, numFours, numFives, numSixes, result)
  121. result = IsFullHouse(numOnes, numTwos, numThrees, numFours, numFives, numSixes, result)
  122.  
  123. Range("C12").Value = result
  124. End Sub
  125. Private Function GetNumOnes() As Integer
  126. Dim numOnes As Integer
  127.  
  128. If Range("B2").Value = 1 Then numOnes = numOnes + 1
  129. If Range("C2").Value = 1 Then numOnes = numOnes + 1
  130. If Range("D2").Value = 1 Then numOnes = numOnes + 1
  131. If Range("E2").Value = 1 Then numOnes = numOnes + 1
  132. If Range("F2").Value = 1 Then numOnes = numOnes + 1
  133. GetNumOnes = numOnes
  134. End Function
  135. Private Function GetNumTwos() As Integer
  136. Dim numTwos As Integer
  137.  
  138. If Range("B2").Value = 2 Then numTwos = numTwos + 1
  139. If Range("C2").Value = 2 Then numTwos = numTwos + 1
  140. If Range("D2").Value = 2 Then numTwos = numTwos + 1
  141. If Range("E2").Value = 2 Then numTwos = numTwos + 1
  142. If Range("F2").Value = 2 Then numTwos = numTwos + 1
  143. GetNumTwos = numTwos
  144. End Function
  145. Private Function GetNumThrees() As Integer
  146. Dim numThrees As Integer
  147.  
  148. If Range("B2").Value = 3 Then numThrees = numThrees + 1
  149. If Range("D2").Value = 3 Then numThrees = numThrees + 1
  150. If Range("E2").Value = 3 Then numThrees = numThrees + 1
  151. If Range("F2").Value = 3 Then numThrees = numThrees + 1
  152. GetNumThrees = numThrees
  153. End Function
  154. Private Function GetNumFours() As Integer
  155. Dim numFours As Integer
  156.  
  157. If Range("B2").Value = 4 Then numFours = numFours + 1
  158. If Range("C2").Value = 4 Then numFours = numFours + 1
  159. If Range("D2").Value = 4 Then numFours = numFours + 1
  160. If Range("E2").Value = 4 Then numFours = numFours + 1
  161. If Range("F2").Value = 4 Then numFours = numFours + 1
  162. GetNumFours = numFours
  163. End Function
  164. Private Function GetNumFives() As Integer
  165. Dim numFives As Integer
  166.  
  167. If Range("B2").Value = 5 Then numFives = numFives + 1
  168. If Range("C2").Value = 5 Then numFives = numFives + 1
  169. If Range("D2").Value = 5 Then numFives = numFives + 1
  170. If Range("E2").Value = 5 Then numFives = numFives + 1
  171. If Range("F2").Value = 5 Then numFives = numFives + 1
  172. GetNumFives = numFives
  173. End Function
  174. Private Function GetNumSixes() As Integer
  175. Dim numSixes As Integer
  176.  
  177. If Range("B2").Value = 6 Then numSixes = numSixes + 1
  178. If Range("C2").Value = 6 Then numSixes = numSixes + 1
  179. If Range("D2").Value = 6 Then numSixes = numSixes + 1
  180. If Range("E2").Value = 6 Then numSixes = numSixes + 1
  181. If Range("F2").Value = 6 Then numSixes = numSixes + 1
  182. GetNumSixes = numSixes
  183. End Function
  184.  
  185. Private Function IsNothingOrStraight(numOnes As Integer, numTwos As Integer, numThrees As Integer, numFours As Integer, numFives As Integer, numSixes As Integer, result As String) As String
  186.  
  187. If (numOnes <= 1) And (numTwos <= 1) And (numThrees <= 1) And (numFours <= 1) And (numFives <= 1) And (numSixes <= 1) Then
  188. If (numSixes = 1) And (numOnes = 0) Then
  189. IsNothingOrStraight = "6 High Straight"
  190. ElseIf (numSixes = 1) And (numOnes = 1) Then
  191. IsNothingOrStraight = "6 High"
  192. Else
  193. IsNothingOrStraight = "5 High Straight"
  194. End If
  195. Else
  196. IsNothingOrStraight = result
  197. End If
  198. End Function
  199. Private Function IsOnePair(numOnes As Integer, numTwos As Integer, numThrees As Integer, numFours As Integer, numFives As Integer, numSixes As Integer, result As String) As String
  200. If (numOnes = 2) And (numTwos <= 1) And (numThrees <= 1) And (numFours <= 1) And (numFives <= 1) And (numSixes <= 1) Then
  201. IsOnePair = "Pair of Ones"
  202. ElseIf (numOnes <= 1) And (numTwos = 2) And (numThrees <= 1) And (numFours <= 1) And (numFives <= 1) And (numSixes <= 1) Then
  203. IsOnePair = "Pair of Twos"
  204. ElseIf (numOnes <= 1) And (numTwos <= 1) And (numThrees = 2) And (numFours <= 1) And (numFives <= 1) And (numSixes <= 1) Then
  205. IsOnePair = "Pair of Threes"
  206. ElseIf (numOnes <= 1) And (numTwos <= 1) And (numThrees <= 1) And (numFours = 2) And (numFives <= 1) And (numSixes <= 1) Then
  207. IsOnePair = "Pair of Fours"
  208. ElseIf (numOnes <= 1) And (numTwos <= 1) And (numThrees <= 1) And (numFours <= 1) And (numFives = 2) And (numSixes <= 1) Then
  209. IsOnePair = "Pair of Fives"
  210. ElseIf (numOnes <= 1) And (numTwos <= 1) And (numThrees <= 1) And (numFours <= 1) And (numFives <= 1) And (numSixes = 2) Then
  211. IsOnePair = "Pair of Sixes"
  212. Else
  213. IsOnePair = result
  214. End If
  215. End Function
  216. Private Function IsTwoPair(numOnes As Integer, numTwos As Integer, numThrees As Integer, numFours As Integer, numFives As Integer, numSixes As Integer, result As String) As String
  217. If (numOnes = 2 And numTwos = 2) Or (numOnes = 2 And numThrees = 2) Or (numOnes = 2 And numThrees = 2) Or (numOnes = 2 And numFours = 2) Or (numOnes = 2 And numFives = 2) Or (numOnes = 2 And numSixes = 2) Or (numTwos = 2 And numThrees = 2) Or (numTwos = 2 And numFours = 2) Or (numTwos = 2 And numFives = 2) Or (numTwos = 2 And numSixes = 2) Or (numThrees = 2 And numFours = 2) Or (numThrees = 2 And numFives = 2) Or (numThrees = 2 And numSixes = 2) Or (numFours = 2 And numFives = 2) Or (numFours = 2 And numSixes = 2) Or (numFives = 2 And numSixes = 2) Then
  218. IsTwoPair = "Two Pair"
  219. Else
  220. IsTwoPair = result
  221. End If
  222. End Function
  223. Private Function IsThreeOfAKind(numOnes As Integer, numTwos As Integer, numThrees As Integer, numFours As Integer, numFives As Integer, numSixes As Integer, result As String) As String
  224. If (numOnes = 3 And numTwos < 2 And numThrees < 2 And numFours < 2 And numFives < 2 And numSixes < 2) Then
  225. IsThreeOfAKind = "Three Ones"
  226. ElseIf (numOnes < 2 And numTwos = 3 And numThrees < 2 And numFours < 2 And numFives < 2 And numSixes < 2) Then
  227. IsThreeOfAKind = "Three Twos"
  228. ElseIf (numOnes < 2 And numTwos < 2 And numThrees = 3 And numFours < 2 And numFives < 2 And numSixes < 2) Then
  229. IsThreeOfAKind = "Three Threes"
  230. ElseIf (numOnes < 2 And numTwos < 2 And numThrees < 2 And numFours = 3 And numFives < 2 And numSixes < 2) Then
  231. IsThreeOfAKind = "Three Fours"
  232. ElseIf (numOnes < 2 And numTwos < 2 And numThrees < 2 And numFours < 2 And numFives = 3 And numSixes < 2) Then
  233. IsThreeOfAKind = "Three Fives"
  234. ElseIf (numOnes < 2 And numTwos < 2 And numThrees < 2 And numFours < 2 And numFives < 2 And numSixes = 3) Then
  235. IsThreeOfAKind = "Three Sixes"
  236. Else
  237. IsThreeOfAKind = result
  238. End If
  239. End Function
  240. Private Function IsFourOfAKind(numOnes As Integer, numTwos As Integer, numThrees As Integer, numFours As Integer, numFives As Integer, numSixes As Integer, result As String) As String
  241. If numOnes = 4 Then
  242. IsFourOfAKind = "Four Ones"
  243. ElseIf numTwos = 4 Then
  244. IsFourOfAKind = "Four Twos"
  245. ElseIf numThrees = 4 Then
  246. IsFourOfAKind = "Four Threes"
  247. ElseIf numFours = 4 Then
  248. IsFourOfAKind = "Four Fours"
  249. ElseIf numFives = 4 Then
  250. IsFourOfAKind = "Four Fives"
  251. ElseIf numSixes = 4 Then
  252. IsFourOfAKind = "Four Sixes"
  253. Else
  254. IsFourOfAKind = result
  255. End If
  256. End Function
  257. Private Function IsFiveOfAKind(numOnes As Integer, numTwos As Integer, numThrees As Integer, numFours As Integer, numFives As Integer, numSixes As Integer, result As String) As String
  258. If numOnes = 5 Then
  259. IsFiveOfAKind = "Five Ones"
  260. ElseIf numTwos = 5 Then
  261. IsFiveOfAKind = "Five Twos"
  262. ElseIf numThrees = 5 Then
  263. IsFiveOfAKind = "Five Threes"
  264. ElseIf numFours = 5 Then
  265. IsFiveOfAKind = "Five Fours"
  266. ElseIf numFives = 5 Then
  267. IsFiveOfAKind = "Five Fives"
  268. ElseIf numSixes = 5 Then
  269. IsFiveOfAKind = "Five Sixes"
  270. Else
  271. IsFiveOfAKind = result
  272. End If
  273. End Function
  274. Private Function IsFullHouse(numOnes As Integer, numTwos As Integer, numThrees As Integer, numFours As Integer, numFives As Integer, numSixes As Integer, result As String) As String
  275. If (numOnes = 3 And numTwos = 2) Or (numOnes = 3 And numThrees = 2) Or (numOnes = 3 And numFours = 2) Or (numOnes = 3 And numFives = 2) Or (numOnes = 3 And numSixes = 2) Or (numTwos = 3 And numOnes = 2) Or (numTwos = 3 And numThrees = 2) Or (numTwos = 3 And numFours = 2) Or (numTwos = 3 And numFives = 2) Or (numTwos = 3 And numSixes = 2) Or (numThrees = 3 And numOnes = 2) Or (numThrees = 3 And numTwos = 2) Or (numThrees = 3 And numFours = 2) Or (numThrees = 3 And numFives = 2) Or (numThrees = 3 And numSixes = 2) Or (numFours = 3 And numOnes = 2) Or (numFours = 3 And numTwos = 2) Or (numFours = 3 And numThrees = 2) Or (numFours = 3 And numFives = 2) Or (numFours = 3 And numSixes = 2) Or (numFives = 3 And numOnes = 2) Or (numFives = 3 And numTwos = 2) Or (numFives = 3 And numThrees = 2) Or (numFives = 3 And numFours = 2) Or (numFives = 3 And numSixes = 2) Or (numSixes = 3 And numOnes = 2) Or (numSixes = 3 And numTwos = 2) Or (numSixes = 3 And numThrees = 2) Or (numSixes = 3 And numFours = 2) Or ( _
  276. numSixes = 3 And numFives = 2) Then
  277. IsFullHouse = "Full House"
  278. Else
  279. IsFullHouse = result
  280. End If
  281. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement