Advertisement
dmesticg

Untitled

Mar 18th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. import random
  2. import collections
  3.  
  4. def bubbleSort(arr):
  5. isSorted = False
  6. while not(isSorted):
  7. for i in range(len(arr) - 1):
  8. for j in range(len(arr) - 1 - i):
  9. if arr[j] > arr[j+1]:
  10. arr[j], arr[j+1] = arr[j+1],arr[j]
  11. else:
  12. isSorted = True
  13. return (arr)
  14.  
  15. def initialize_deck (deck, numsuit, numcard):
  16. deck = [[True] * numsuit for i in range (numcard)]
  17. return (deck)
  18.  
  19. def getcard(deck,numsuit,numcard):
  20. card = ""
  21. suitval = random.randint(0,numsuit-1)
  22. cardval = random.randint(0,numcard-1)
  23. while deck[cardval][suitval] == False:
  24. suitval = random.randint(0,numsuit-1)
  25. cardval = random.randint(0,numcard-1)
  26. deck[cardval][suitval] = False
  27. return(deck,(cardval,suitval))
  28.  
  29. def getcards(howmany,deck,numsuit,numcard):
  30. cards = []
  31. for i in range(howmany):
  32. deck,card = getcard(deck,numsuit,numcard)
  33. cards.append(card)
  34. return(deck,cards)
  35.  
  36.  
  37. def check_straight(cards):
  38. for i in range(len(cards)):
  39. if i+1 != len(cards) and cards[i] + 1 != cards[i+1]:
  40. return(False)
  41. return(True)
  42.  
  43. def check_flush(hand):
  44. suits = [i[1] for i in hand]
  45. if len(set(suits)) == 1:
  46. return(True)
  47. return(False)
  48.  
  49. def check_straightflush(hand):
  50. if check_flush(hand) and check_straight(hand):
  51. return(True)
  52. return(False)
  53.  
  54. def check_royal(cards):
  55. royal = [1,10,11,12,13]
  56. if cards == royal:
  57. return(True)
  58. return(False)
  59.  
  60. def check_twopair(cards):
  61. counter = list(collections.Counter(cards).items())
  62. pairs = 0
  63. for i in range(len(cards)):
  64. try:
  65. if counter[i][1] == 2:
  66. pairs = pairs + 1
  67. if pairs == 2:
  68. return(True)
  69. except:
  70. return(False)
  71.  
  72. def check_kinds(cards):
  73. counter = collections.Counter(cards)
  74. occurrences = counter.most_common(1)[0][1]
  75. cards_occur = [x[0] for x in counter.most_common() if x[1] == occurrences]
  76. card = max(cards_occur)
  77. return(card,occurrences)
  78.  
  79. def check_fullhouse(cards):
  80. card,occur = check_kinds(cards)
  81. cards2 = []
  82. if occur == 3:
  83. for i in range(len(cards)):
  84. if card != cards[i]:
  85. cards2.append(cards[i])
  86. if check_kinds(cards2)[1] == 2:
  87. return(True)
  88. return(False)
  89.  
  90. def evaluate_hand(hand):
  91. flush = False
  92. straight = False
  93. score = 0
  94. suits = [i[1] for i in hand]
  95. cards = bubbleSort([i[0] for i in hand])
  96. highcard = cards[len(cards)-1]
  97. highkind,kind = check_kinds(cards)
  98. if kind == 2:
  99. score = 1
  100. if check_twopair(cards):
  101. score = 2
  102. if kind == 3:
  103. score = 3
  104. if check_straight(cards):
  105. score = 4
  106. straight = True
  107. if check_flush(hand):
  108. score = 5
  109. flush = True
  110. if check_fullhouse(cards):
  111. score = 6
  112. if kind == 4:
  113. score = 7
  114. if flush and straight:
  115. score = 8
  116. if flush and check_royal(cards):
  117. score = 9
  118. name = hands[score]
  119. return(score,highcard,highkind,name)
  120.  
  121. def determine_winner(hand1,hand2):
  122. score1,high1,kind1,name1 = evaluate_hand(hand1)
  123. score2,high2,kind2,name2 = evaluate_hand(hand2)
  124. highcardscores = [0,4,8]
  125. if score1 > score2:
  126. wintext = "Hand 1 Wins"
  127. elif score2 > score1:
  128. wintext = "Hand 2 Wins"
  129. else:
  130. if score1 in highcardscores:
  131. if high1 > high2:
  132. wintext = "Hand 1 Wins"
  133. elif high2 > high1:
  134. wintext = "Hand 2 Wins"
  135. else:
  136. wintext = "Tie Game!"
  137. else:
  138. if kind1 > kind2:
  139. wintext = "Hand 1 Wins"
  140. elif kind2 > kind1:
  141. wintext = "Hand 2 Wins"
  142. else:
  143. wintext = "Tie Game!"
  144. textSize(75)
  145. text(wintext,500,600)
  146. drawtext(name1,0)
  147. drawtext(name2,1)
  148.  
  149. def drawtext(whattext,num):
  150. textSize(30)
  151. text(whattext,810,220-(num*220)+100)
  152.  
  153.  
  154. def drawhand(hand,num):
  155. global deckimage,cardw,cardl
  156. for i in range(len(hand)):
  157. cardval = hand[i][0]
  158. suitval = hand[i][1]
  159. textSize(30)
  160. text("Hand "+str(num+1),100,220-(num*220)+100)
  161. copy(deckimage, cardw*cardval, cardl*suitval, cardw, cardl, 300+100*i, 220-(num*220), 100, 160)
  162.  
  163. def clickbox(x,y,w,h):
  164. global clicked
  165. fill(100)
  166. rect(x,y,w,h)
  167. textSize(50)
  168. fill(255)
  169. text("Redraw",x+10,y+100)
  170.  
  171. def setup():
  172. global hands,deck,keepGoing,deckimage,cardl,cardw
  173. size(1090,750)
  174. deckimage = loadImage("deck.png")
  175. cardl = 110
  176. cardw = 80
  177. keepGoing = True
  178. deck = []
  179. hands = ["High Card","Pair","Two Pair","Three of a Kind","Straight","Flush","Full House","Four of a Kind","Straight Flush","Royal Flush"]
  180. deck = initialize_deck(deck, 4, 13)
  181.  
  182.  
  183. def draw():
  184. global hands, deck, keepGoing, deckimage,clicked
  185. clickbox(100,600,200,200)
  186. if keyPressed and key == "p":
  187. keepGoing = True
  188. if keepGoing:
  189. background(0)
  190. deck,hand1 = getcards(5,deck,4,13)
  191. deck,hand2 = getcards(5,deck,4,13)
  192. drawhand(hand1,0)
  193. drawhand(hand2,1)
  194. determine_winner(hand1,hand2)
  195. keepGoing = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement