Advertisement
dmesticg

Untitled

Jun 10th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 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.  
  81.  
  82.  
  83. def draw():
  84. global whichmenu
  85. if whichmenu == 0:
  86. menu()
  87. elif whichmenu == 1:
  88. pokerTable()
  89.  
  90.  
  91.  
  92. def menu():
  93. global whichmenu
  94. background(0)
  95.  
  96. image(pokertable,0,0,1000,650)
  97. image(pokertitle,315,190,400,100)
  98. print(mouseX,mouseY)
  99. drawTextboxes(menuboxes)
  100. if isClicked(textboxes[0]):
  101. whichmenu = 1
  102.  
  103. def pokerTable():
  104. global betamount,bankamount,bet_text,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
  105. global backofcard,handsize,numberofplayers,drawtwocards_text,deckimage,hand1,hand2,thedeck
  106.  
  107. image(pokertable,-100,-50,1100,750)
  108. gameboxes = [bet_text,negatory_text,pository_text,amount_text,bet_text, check_text,fold_text,call_text,raise_text,balance_text,drawtwocards_text]
  109. drawTextboxes(gameboxes)
  110. betamount = getBet(gameboxes[2],gameboxes[1],betamount,bankamount)
  111. amount_text = [820,150,110,50,"$"+str(betamount),color(0,200,0)]
  112. #this displays back of card
  113. hand1 = thedeck.createHand(5)
  114. hand2 = thedeck.createHand(5)
  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. if hand1score > hand2score:
  123. print("Hand 1 win with score of "+str(hand1score))
  124. elif hand1score < hand2score:
  125. print("Hand 2 win with score of "+str(hand2score))
  126. else:
  127. print("Tie with score of "+str(hand1score))
  128.  
  129.  
  130. if isClicked(gameboxes[5]):
  131. check()
  132. elif isClicked(gameboxes[6]):
  133. fold()
  134. elif isClicked(gameboxes[7]):
  135. call()
  136. elif isClicked(gameboxes[8]):
  137. #Raise() is uppercase cuz raise() is a in-built function
  138. Raise()
  139.  
  140.  
  141. def copypokerCards(handsize,numberofplayers,cardimage,ogcordx,ogcordy,ogwidth,ogheight,newx,newy,newwidth,newheight):
  142. for i in range(handsize):
  143. for l in range(numberofplayers):
  144. copy(cardimage,ogcordx,ogcordy,ogwidth,ogheight,newx+130*i,newy+250*l,newwidth,newheight)
  145. def Raise():
  146. pass
  147. def call():
  148. pass
  149.  
  150. def check():
  151. pass
  152.  
  153.  
  154. def fold():
  155. pass
  156.  
  157.  
  158.  
  159. def getBet(plusbox,minusbox,betamount,bankamount):
  160. if isClicked(minusbox) and betamount>1:
  161. betamount -=1
  162. elif isClicked(plusbox) and betamount<bankamount:
  163. betamount+=1
  164. return(betamount)
  165.  
  166.  
  167. def drawTextboxes(textboxes):
  168. textSize(30)
  169. fill(255)
  170. for i in range(len(textboxes)):
  171. fill(textboxes[i][5])
  172. rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
  173. fill(255)
  174. text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
  175.  
  176. def drawtextboxes(textboxes):
  177. textSize(30)
  178. fill(255)
  179. for i in range(len(textboxes)):
  180. fill(textboxes[i][5])
  181. rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
  182. fill(255)
  183. text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
  184.  
  185. def isClicked(info):
  186. clicked = False
  187. if mousePressed and mouseButton == LEFT:
  188. if info[0] < mouseX < info[0] + info[2] and info[1] < mouseY < info[1] + info[3]:
  189. clicked = True
  190. return clicked
  191.  
  192. def drawHand(hand,num,theimage,cardw,cardl):
  193. print(hand)
  194. for i in range(len(hand)):
  195. cardval = hand[i][0]
  196. suitval = hand[i][1]
  197. textSize(30)
  198. copy(theimage, cardw*(cardval ), cardl*suitval, cardw, cardl, 110+130*i, 150+(num*250), 115, 160)
  199.  
  200. def Occur(cards):
  201. scorelist = [2,4,8]
  202. counter = collections.Counter(cards)
  203. largest = [0,0]
  204. largest2 = [0,0]
  205. for key in counter:
  206. occur = counter[key]
  207. if occur > largest2[1]:
  208. if occur > largest[1]:
  209. largest2 = largest
  210. largest = [key,occur]
  211. else:
  212. largest2 = [key,occur]
  213. num = max([largest[0],largest2[0]])
  214. if largest[1] == 3 and largest2[1] == 2:
  215. return(7,largest[0])
  216. elif largest[1] == largest2[1]:
  217. return(3,num)
  218. elif largest[1] in [2,3,4]:
  219. return(scorelist[[2,3,4].index(largest[1])],num )
  220. return(1,num)
  221.  
  222.  
  223. def Flush(cards):
  224. counter = collections.Counter(cards)
  225. if len(counter) == 1:
  226. return(True,max(cards))
  227. return(False,max(cards))
  228.  
  229. def Straight(cards):
  230. if cards[4] - cards[0] == 4:
  231. return(True,max(cards))
  232. return(False,max(cards))
  233.  
  234. def Royal(cards):
  235. royal = [0,9,10,11,12]
  236. if cards == royal:
  237. return(True,13)
  238. return(False,13)
  239.  
  240. def getScore(hand):
  241. suit = bubbleSort([i[1] for i in hand])
  242. value = bubbleSort([i[0] for i in hand])
  243. occurances = Occur(value)
  244. if occurances == 2:
  245. if Flush(suit) == 5:
  246. if Straight(value):
  247. return(9)
  248. if Royal(value):
  249. return(10)
  250. return(6)
  251. if Straight(value):
  252. return(5)
  253. return(1)
  254. return(occurances)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement