Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. # Name Type Purpose Restrictions
  2. # deckofcards Img Loads image
  3. # suitw, suitl Int Width/length of copy
  4. # keepgoing Bool. Runs copy function once
  5. # p1hand, p2hand List Holds the hand of bot and player
  6. # gamemode Int Loads specific game mode
  7. # Boxx, boxy Int Coord. of the button
  8. # boxyl, boxw Int Dimensions of the button
  9. # valid bool. Returns if button clicked
  10. # playtext, resettext Str String for the button
  11. # numofcards Int Used in for loops for # of cards
  12. # results (hand names) Int Returns a value based on what hand each player has
  13. # royalflushvalue Int Only possible sum when getting royal flush
  14. # cardranknames List List of the names of hand
  15. # availablecards List Contains the deck of cards, removes cards if used
  16. # winnertext List Contains the string for the result of game
  17. # winner Int Used to determine which player won or if it's a tie
  18. # maxvaluecard Int Used in for loops for going through the values of cards
  19. # counter Dict Finds the number of duplicate values and puts in a dictionary
  20.  
  21. import random
  22. from collections import Counter
  23. def setup():
  24. global deckofcards, suitl, suitw, keepgoing, p1hand, p2hand, gamemode, boxx, boxy, boxw, boxl, valid, playtext, resettext, numofcards
  25. global royalflush, straightflush, fourkind, fullhouse, fulsh, straight, threekind, twopair, pair, highcard, cardranknames, result, royalflushvalue, availablecards
  26. global winnertext, winner, maxvaluecard
  27. background (0)
  28. size(1040,440)
  29. deckofcards = loadImage("cards.png")
  30. suitw = 80
  31. suitl = 110
  32. keepgoing = True
  33. p1hand = []
  34. p2hand = []
  35. gamemode = 0
  36. boxx = 80
  37. boxy = 10
  38. boxw = 150
  39. boxl = 50
  40. valid = False
  41. playtext = "Play"
  42. resettext = "Reset"
  43. numofcards = 5
  44. result = 9
  45. #ace + 10 + king + queen + jack
  46. royalflushvalue = (10+11+12+13+0)
  47. royalflush = 0
  48. straightflush = 1
  49. fourkind = 2
  50. fullhouse = 3
  51. fulsh = 4
  52. straight = 5
  53. threekind = 6
  54. twopair = 7
  55. pair = 8
  56. highcard = 9
  57. cardranknames = ["royalflush", "straightflush", "fourkind", "fullhouse", "flush", "straight", "threekind", "twopair", "pair", "highcard"]
  58. availablecards = []
  59. winnertext = ["player 1 wins", "bot wins", "tie"]
  60. for i in range(4):
  61. for j in range(13):
  62. availablecards.append((i,j))
  63. winner = 0
  64. maxvaluecard = 13
  65.  
  66. def draw():
  67. global boxx, boxy, boxw, boxl
  68. fill(255,0,0)
  69. rect(boxx, boxy, boxw, boxl)
  70. whichgamemode()
  71.  
  72. def whichgamemode():
  73. global deckofcards, suitx, suity, suitl, suitw, gamemode, boxx, boxy, boxw, boxl, valid, playtext, resettext
  74. global cardranknames, p1hand, p2hand, winnertext
  75. if valid:
  76. gamemode += 1
  77. valid = False
  78.  
  79. if gamemode == 0:
  80. displaytext(playtext)
  81.  
  82. elif gamemode == 1:
  83. displayhand()
  84. displaytext(resettext)
  85. p1hand = sorthand(p1hand)
  86. p2hand = sorthand(p2hand)
  87. textSize(40)
  88. text((winnertext[determinewinner()]),0,400)
  89. text((cardranknames[p1result]), 800,350)
  90. text((cardranknames[p2result]), 800, 100)
  91. else:
  92. setup()
  93.  
  94. def determinewinner():
  95. global p1hand, p2hand, winner, p1result, p2result
  96. p1result = royalflushcheck(p1hand)
  97. p2result = royalflushcheck(p2hand)
  98. if p2result < p1result:
  99. winner = 1
  100. elif p1result < p2result:
  101. winner = 0
  102. else:
  103. if p1result == 9 and p2result == 9:
  104. if p1hand[4][1] > p2hand[4][1]:
  105. winner = 0
  106. elif p2hand[4][1] > p1hand[4][1]:
  107. winner = 1
  108. elif p1hand[4][1] == p2hand[4][1]:
  109. winner = 2
  110. else:
  111. winner = 2
  112. return (winner)
  113.  
  114. def displaytext(whattext):
  115. fill(255)
  116. textSize(50)
  117. text(whattext, boxx, 50)
  118.  
  119. def sorthand(hand):
  120. global numofcards
  121. #sorts the order of deck
  122. for i in range(numofcards-1):
  123. for j in range(numofcards-1):
  124. if (hand[i][1]) > (hand[i+1][1]):
  125. hand[i], hand[i+1] = hand[i+1], hand[i]
  126. return (hand)
  127.  
  128. def royalflushcheck(hand):
  129. global numofcards, maxvaluecard
  130. global royalflush, straightflush, fourkind, fullhouse, fulsh, straight, threekind, twopair, pair, highcard, royalflushvalue
  131. totalvalue = 0
  132. numofpairs = 0
  133. result = 9
  134. # counts to see number of values that are the same
  135. counter = Counter(y for (x,y) in hand)
  136. if hand[0][0] == hand[1][0] == hand[2][0] == hand[3][0] == hand[4][0]:
  137. for i in range(numofcards-1):
  138. totalvalue += hand[i][1]
  139. ##Since royal flush must have ace, king, queen, jack, 10 I set a condition where it must equal to the sum of them
  140. if totalvalue == (royalflushvalue):
  141. result = royalflush
  142. ##sees if consecutive by seeing if total value is equal to the smallest value * numofcards + numofcards
  143. elif totalvalue == hand[0][1]*numofcards + numofcards:
  144. result = straightflush
  145. else:
  146. result = fulsh
  147. for i in range(maxvaluecard):
  148. # counts the numbers of values that are same and determines which hand it is
  149. if counter[i] == 2:
  150. numofpairs += 1
  151. if numofpairs == 1:
  152. # if there is one pair, then only one pair
  153. result = pair
  154. elif numofpairs == 2:
  155. # if there are two pairs, then only two pairs
  156. result = twopair
  157.  
  158. if counter[i] == 3 and numofpairs == 1:
  159. # if there's one pair and three of one value it's a fullhouse
  160. result = fullhouse
  161.  
  162. if counter[i] == 3 and numofpairs == 0:
  163. #if there's no pairs and three of one value, it's three of a kind
  164. result = threekind
  165.  
  166. if counter[i] == 4:
  167. # if there's four of one value, it's four of a kind
  168. result = fourkind
  169.  
  170. for i in range(numofcards-1):
  171. totalvalue += hand[i][1]
  172. if totalvalue == hand[0][1]*numofcards + numofcards:
  173. # checks to see if cards are consecutive, since it's in order of smallest to greatest it takes the smallest value, multiplies it by number of cards and adds that number. If that number is equal to the total value, it's consecutive.
  174. result = straight
  175. return (result)
  176.  
  177. def displayhand():
  178. global deckofcards, suitl, suitw, keepgoing, p1hand, p2hand, numofcards
  179. while keepgoing:
  180. for i in range(numofcards):
  181. randomsuit1, randomvalue1 = generaterandom()
  182. p1hand.append((randomsuit1, randomvalue1))
  183. copy(deckofcards, suitw*randomvalue1, suitl*randomsuit1, suitw, suitl, 300+100*i, 220, 100, 160)
  184. for i in range (numofcards):
  185. randomsuit2, randomvalue2 = generaterandom()
  186. p2hand.append((randomsuit2, randomvalue2))
  187. copy(deckofcards, suitw*randomvalue2, suitl*randomsuit2, suitw, suitl, 300+100*i, 0, 100, 160)
  188. keepgoing = False
  189.  
  190. def generaterandom():
  191. global availablecards
  192. (suit,value) = (random.choice(availablecards))
  193. availablecards.remove((suit,value))
  194. return(suit,value)
  195.  
  196. def mouseClicked():
  197. global boxx, boxy, boxw, boxl, valid
  198. valid = boxx <= mouseX <= boxx + boxw and boxy <= mouseY <= boxy + boxl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement