Advertisement
dmesticg

Untitled

Jun 11th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. import collections
  2. import random
  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. class Deck: #!
  15. def __init__(self,numsuit,numcard):
  16. self.numsuit = numsuit
  17. self.numcard = numcard
  18. self.deck = [[True] * numsuit for i in range (numcard)]
  19.  
  20. def createCard(self): #!
  21. if self.deck.count:
  22. suitval = random.randint(0,self.numsuit-1)
  23. cardval = random.randint(0,self.numcard-1)
  24. while self.deck[cardval][suitval] == False:
  25. suitval = random.randint(0,self.numsuit-1)
  26. cardval = random.randint(0,self.numcard-1)
  27. self.deck[cardval][suitval] = False
  28. return((cardval,suitval))
  29.  
  30. def createHand(self,handSize):
  31. cards = []
  32. for i in range(handSize):
  33. card = Deck.createCard(self)
  34. cards.append(card)
  35. return(cards)
  36.  
  37. def changeHand(self,hand,indexList):
  38. for i in range(len(indexList)):
  39. hand[indexList[i]] = self.deck.createCard(self)
  40. return(hand)
  41.  
  42.  
  43. def setup():
  44. global restart_text,pause_text,throw_text,score_text,menuboxes,gameboxes,pokertable,pokertitle,start_text,whichmenu
  45. global bankamount,betamount,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
  46. global handsize,numberofplayers,backofcard,drawtwocards_text,deckimage,hand1,cardl,cardw,hand2,thedeck
  47. cardw = 80
  48. cardl = 110
  49. size(1000,650)
  50. #This is the amount of money each user wants to bet
  51. betamount = 0
  52. #Amount of money in bank
  53. bankamount = 100
  54. pokertable = loadImage("poker.jpg")
  55. pokertitle = loadImage("PokerTitle.png")
  56. backofcard = loadImage("backofcard.jpg")
  57. deckimage = loadImage("deck.png")
  58. start_text = [450,310,100,50,"START",color(255,99,71)]
  59.  
  60. bet_text = [780,100,200,50,"Bet",color(0,0,0)]
  61. negatory_text = [780,150,50,50,"-",color(50,50,50)]
  62. pository_text = [930,150,50,50,"+",color(50,50,50)]
  63. amount_text = [820,150,110,50,"$"+str(betamount),color(0,200,0)]
  64. check_text = [780,200,200,50,"check",color(139, 0, 255)]
  65. fold_text = [780,250,200,50,"fold",color(255, 0, 0)]
  66. call_text = [780,300,200,50,"call",color(255, 255, 0)]
  67. raise_text = [780,350,200,50,"raise",color( 255, 127, 0)]
  68. drawtwocards_text = [780,400,200,50,"change cards",color(0,0,0)]
  69. balance_text = [100,70,200,50,"balance:$"+str(bankamount),color(249,123,31)]
  70. whichmenu = 1
  71. menuboxes = [start_text]
  72.  
  73. handsize = 5
  74. numberofplayers =2
  75.  
  76. thedeck = Deck(4,13)
  77.  
  78. cardw = 80
  79. cardl = 110
  80. hand1 = thedeck.createHand(5)
  81. hand2 = thedeck.createHand(5)
  82.  
  83.  
  84.  
  85. def draw():
  86. global whichmenu
  87. if whichmenu == 0:
  88. menu()
  89. elif whichmenu == 1:
  90. pokerTable()
  91.  
  92.  
  93.  
  94. def menu():
  95. global whichmenu
  96. background(0)
  97.  
  98. image(pokertable,0,0,1000,650)
  99. image(pokertitle,315,190,400,100)
  100. print(mouseX,mouseY)
  101. drawTextboxes(menuboxes)
  102. if isClicked(textboxes[0]):
  103. whichmenu = 1
  104.  
  105. def pokerTable():
  106. global betamount,bankamount,bet_text,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
  107. global backofcard,handsize,numberofplayers,drawtwocards_text,deckimage,hand1,hand2,thedeck
  108.  
  109. image(pokertable,-100,-50,1100,750)
  110. gameboxes = [bet_text,negatory_text,pository_text,amount_text,bet_text, check_text,fold_text,call_text,raise_text,balance_text,drawtwocards_text]
  111. drawTextboxes(gameboxes)
  112. betamount = getBet(gameboxes[2],gameboxes[1],betamount,bankamount)
  113. amount_text = [820,150,110,50,"$"+str(betamount),color(0,200,0)]
  114. #this displays back of card
  115. drawHand([[0,0],[0,0],[0,0],[0,0],[0,0]],1,backofcard,1152,1600)
  116. drawHand([[0,0],[0,0],[0,0],[0,0],[0,0]],0,backofcard,1152,1600)
  117. #This displays the hand
  118. drawHand(hand1,0,deckimage,80,110)
  119. drawHand(hand2,1,deckimage,80,110)
  120. hand1score = getScore(hand1)
  121. hand2score = getScore(hand2)
  122. handList = ["High Card","Pair","Two Pair","Three of a Kind","Straight","Flush","Full House","Four of a Kind","Straight Flush","Royal Flush"]
  123. if hand1score[0] > hand2score[0]:
  124. print("Hand 1 wins with a "+handList[hand1score[0]-1])
  125. elif hand1score[0] < hand2score[0]:
  126. print("Hand 2 wins with a "+handList[hand2score[0]-1])
  127. else:
  128. if hand1score[1] > hand2score[1]:
  129. print("Hand 1 wins with a "+handList[hand1score[0]-1])
  130. elif hand1score[1] > hand2score[1]:
  131. print("Hand 2 wins with a "+handList[hand2score[0]-1])
  132. else:
  133. print("Tie Game NIBBA")
  134.  
  135. if isClicked(gameboxes[5]):
  136. check()
  137. elif isClicked(gameboxes[6]):
  138. fold()
  139. elif isClicked(gameboxes[7]):
  140. call()
  141. elif isClicked(gameboxes[8]):
  142. #Raise() is uppercase cuz raise() is a in-built function
  143. Raise()
  144.  
  145.  
  146. def copypokerCards(handsize,numberofplayers,cardimage,ogcordx,ogcordy,ogwidth,ogheight,newx,newy,newwidth,newheight):
  147. for i in range(handsize):
  148. for l in range(numberofplayers):
  149. copy(cardimage,ogcordx,ogcordy,ogwidth,ogheight,newx+130*i,newy+250*l,newwidth,newheight)
  150. def Raise():
  151. pass
  152. def call():
  153. pass
  154.  
  155. def check():
  156. pass
  157.  
  158.  
  159. def fold():
  160. pass
  161.  
  162.  
  163.  
  164. def getBet(plusbox,minusbox,betamount,bankamount):
  165. if isClicked(minusbox) and betamount>1:
  166. betamount -=1
  167. elif isClicked(plusbox) and betamount<bankamount:
  168. betamount+=1
  169. return(betamount)
  170.  
  171.  
  172. def drawTextboxes(textboxes):
  173. textSize(30)
  174. fill(255)
  175. for i in range(len(textboxes)):
  176. fill(textboxes[i][5])
  177. rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
  178. fill(255)
  179. text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
  180.  
  181. def drawtextboxes(textboxes):
  182. textSize(30)
  183. fill(255)
  184. for i in range(len(textboxes)):
  185. fill(textboxes[i][5])
  186. rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
  187. fill(255)
  188. text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
  189.  
  190. def isClicked(info):
  191. clicked = False
  192. if mousePressed and mouseButton == LEFT:
  193. if info[0] < mouseX < info[0] + info[2] and info[1] < mouseY < info[1] + info[3]:
  194. clicked = True
  195. return clicked
  196.  
  197. def drawHand(hand,num,theimage,cardw,cardl):
  198. for i in range(len(hand)):
  199. cardval = hand[i][0]
  200. suitval = hand[i][1]
  201. copy(theimage, cardw*(cardval ), cardl*suitval, cardw, cardl, 110+130*i, 150+(num*250), 115, 160)
  202.  
  203. def Occur(cards):
  204. scorelist = [2,4,8]
  205. counter = collections.Counter(cards)
  206. largest = [0,0]
  207. largest2 = [0,0]
  208. for key in counter:
  209. occur = counter[key]
  210. if occur > largest2[1]:
  211. if occur > largest[1]:
  212. largest2 = largest
  213. largest = [key,occur]
  214. else:
  215. largest2 = [key,occur]
  216. num = max([largest[0],largest2[0]])
  217. if largest[1] == 3 and largest2[1] == 2:
  218. return(7,largest[0])
  219. elif largest[1] == largest2[1] and largest[1] != 1:
  220. return(3,num)
  221. elif largest[1] in [2,3,4]:
  222. return(scorelist[[2,3,4].index(largest[1])],num )
  223. return(1,max(cards))
  224.  
  225.  
  226. def Flush(cards):
  227. counter = collections.Counter(cards)
  228. if len(counter) == 1:
  229. return(True,max(cards))
  230. return(False,max(cards))
  231.  
  232. def Straight(cards):
  233. if cards[4] - cards[0] == 4:
  234. return(True,max(cards))
  235. return(False,max(cards))
  236.  
  237. def Royal(cards):
  238. royal = [0,9,10,11,12]
  239. if cards == royal:
  240. return(True,13)
  241. return(False,13)
  242.  
  243. def getScore(hand):
  244. suit = bubbleSort([i[1] for i in hand])
  245. value = bubbleSort([i[0] for i in hand])
  246. occurances = Occur(value)
  247. if occurances == 2:
  248. if Flush(suit) == 5:
  249. if Straight(value):
  250. return(9)
  251. if Royal(value):
  252. return(10)
  253. return(6)
  254. if Straight(value):
  255. return(5)
  256. return(1)
  257. return(occurances)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement