Advertisement
Guest User

Untitled

a guest
Jun 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.83 KB | None | 0 0
  1. import random
  2. import collections
  3. def setup():
  4. global restart_text,pause_text,throw_text,score_text,menuboxes,gameboxes,pokertable,pokertitle,start_text,whichmenu
  5. global bankamount,betamount,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
  6. global handsize,numberofplayers,backofcard,drawtwocards_text,deckimage,hand1,cardl,cardw,hand2,turn,turn_text,trueholder,trueholder1,thedeck,hand1,hand2
  7. global v,bankamount,Game,Sina,Varun
  8. size(1000,650)
  9. v = getVariables("variable.txt")
  10.  
  11. thedeck = Deck(4,13)
  12.  
  13. cardw = v["cardw"]
  14. cardl = v["cardl"]
  15. #This is the amount of money each user wants to bet
  16. betamount = v["betamount"]
  17. #Amount of money in bank
  18.  
  19. pokertable = loadImage("poker.jpg")
  20. pokertitle = loadImage("PokerTitle.png")
  21. backofcard = loadImage("backofcard.jpg")
  22. deckimage = loadImage("deck.png")
  23. start_text = v["start_text"]
  24. start_text.append(color(255,99,71))
  25. bet_text = v["bet_text"]
  26. bet_text.append(color(0,0,0))
  27. negatory_text = v["negatory_text"]
  28. negatory_text.append(color(50,50,50))
  29. pository_text = v["pository_text"]
  30. pository_text.append(color(50,50,50))
  31. amount_text = v["amount_text"]
  32. amount_text.append(color(0,200,0))
  33. check_text = v["check_text"]
  34. check_text.append(color(139, 0, 255))
  35. fold_text = v["fold_text"]
  36. fold_text.append(color(255, 0, 0))
  37. call_text = v["call_text"]
  38. call_text.append(color(127,255,212))
  39. drawtwocards_text = v["drawtwocards_text"]
  40. drawtwocards_text.append(color(100,149,237))
  41. balance_text = v["balance_text"]
  42. balance_text.append(color(100,149,237))
  43. turn_text = v["turn_text"]
  44. turn_text.append(color(0,0,0))
  45. whichmenu = v["whichmenu"]
  46. menuboxes = [start_text]
  47. handsize = v["handsize"]
  48. numberofplayers = v["numberofplayers"]
  49. trueholder = [False,False,False,False,False]
  50. Game = Poker(thedeck)
  51. Sina = Player(1000,"Sina",Game)
  52. Varun = Player(1000,"Varun",Game)
  53. Game.addPlayers([Sina,Varun])
  54.  
  55.  
  56. def draw():
  57. global whichmenu,trueholder,turn
  58. global betamount,bankamount,bet_text,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
  59. global backofcard,handsize,numberofplayers,drawtwocards_text,deckimage,hand1,hand2, thedeck,turn,trueholder,trueholder1,v
  60.  
  61.  
  62. image(pokertable,-100,-50,1100,750)
  63. if whichmenu == 0:
  64. menu()
  65. elif whichmenu == 1:
  66. pokerTable()
  67. #this displays back of card
  68. #This displays the hand
  69.  
  70.  
  71.  
  72.  
  73.  
  74. def menu():
  75. global whichmenu
  76. background(0)
  77.  
  78. image(pokertable,0,0,1000,650)
  79. image(pokertitle,315,190,400,100)
  80.  
  81. drawTextboxes(menuboxes)
  82. if isClicked(textboxes[0]):
  83. whichmenu = 1
  84.  
  85.  
  86.  
  87.  
  88. def pokerTable():
  89. global betamount,bankamount,bet_text,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
  90. global backofcard,handsize,numberofplayers,drawtwocards_text,deckimage,hand1,hand2, thedeck,turn,turn_text,trueholder,hand1,amount_text,betamount,Game,Varun,Sina
  91. Game.turn = Game.turn%2
  92. gameboxes = [bet_text,negatory_text,pository_text,amount_text,bet_text, check_text,fold_text,call_text,balance_text,drawtwocards_text,turn_text]
  93. drawTextboxes(gameboxes)
  94. theplayer = Game.playerList[Game.turn]
  95. betamount = getBet(gameboxes[2],gameboxes[1],betamount,theplayer.balance)
  96. amount_text = [820,150,110,50,"$"+str(betamount),color(0,200,0)]
  97. text(theplayer.balance,240,105)
  98. drawHand([[0,0],[0,0],[0,0],[0,0],[0,0]],1,backofcard,1152,1600)
  99. drawHand([[0,0],[0,0],[0,0],[0,0],[0,0]],0,backofcard,1152,1600)
  100. drawHand(theplayer.hand,Game.turn,deckimage,80,110)
  101.  
  102.  
  103. if isClicked(gameboxes[10]):
  104. trueholder = [False,False,False,False,False]
  105. Game.turn+=1
  106. if isClicked(gameboxes[9]):
  107.  
  108. indexes = [index for index in range(len(trueholder)) if trueholder[index] == True]
  109. theplayer.hand = thedeck.changeHand(theplayer.hand,indexes)
  110. trueholder = [False,False,False,False,False]
  111.  
  112. if isClicked(gameboxes[5]):
  113. Game.Check()
  114. betamount = 0
  115.  
  116. elif isClicked(gameboxes[6]):
  117. Game.Fold(theplayer)
  118.  
  119. elif isClicked(gameboxes[7]):
  120. Game.Call(theplayer)
  121.  
  122. if isClicked(gameboxes[4]) and Game.turn ==1:
  123. Game.Raise(theplayer,betamount)
  124. betamount = 0
  125.  
  126.  
  127.  
  128. def getBet(plusbox,minusbox,betamount,bankamount):
  129. if isClicked(minusbox) and betamount>1:
  130. betamount -=1
  131. elif isClicked(plusbox) and betamount<bankamount:
  132. betamount+=1
  133. return(betamount)
  134.  
  135.  
  136. def drawTextboxes(textboxes):
  137. textSize(30)
  138. fill(255)
  139. for i in range(len(textboxes)):
  140. fill(textboxes[i][5])
  141. rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
  142. fill(255)
  143. text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
  144.  
  145. def drawtextboxes(textboxes):
  146. textSize(30)
  147. fill(255)
  148. for i in range(len(textboxes)):
  149. fill(textboxes[i][5])
  150. rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
  151. fill(255)
  152. text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
  153.  
  154. def isClicked(info):
  155. clicked = False
  156. if mousePressed and mouseButton == LEFT:
  157. if info[0] < mouseX < info[0] + info[2] and info[1] < mouseY < info[1] + info[3]:
  158. clicked = True
  159. return clicked
  160.  
  161. def drawHand(hand,num,theimage,cardw,cardl):
  162. global backofcard,trueholder
  163.  
  164. for i in range(len(hand)):
  165. cardval = hand[i][0]
  166. suitval = hand[i][1]
  167.  
  168. textSize(30)
  169. if hand == [[0,0],[0,0],[0,0],[0,0],[0,0]]:
  170. copy(backofcard,0,0,1152,1600,110+130*i,150+(num*250),115,160)
  171.  
  172. else:
  173.  
  174. if isClicked([110+130*i,150+(num*250),115,160]) and sum(trueholder)<2 or trueholder[i]:
  175. trueholder[i] = True
  176. copy(backofcard,0,0,1152,1600,110+130*i,150+(num*250),115,160)
  177. else:
  178. copy(theimage, cardw*(cardval), cardl*suitval, cardw, cardl, 110+130*i, 150+(num*250), 115, 160)
  179.  
  180.  
  181.  
  182.  
  183.  
  184. def getVariables(fileName):
  185. try:
  186. variables = {}
  187. with open(fileName,"r") as file:
  188. for line in file:
  189. line = line.replace(" ","")
  190. name,value = line.split("=")
  191. variables[name] = string2type(value)
  192. return(variables)
  193. except Exception as E:
  194. print(E)
  195.  
  196. def string2type(value):
  197. try:
  198. return(int(value))
  199. except ValueError:
  200. if "[" in value and "]" in value:
  201. value = value.replace("[","")
  202. value = value.replace("]","")
  203. value = value.split(",")
  204.  
  205. for i in range(len(value)):
  206. value[i] = string2type(value[i])
  207. elif value == "True":
  208. value = True
  209. elif value == "False":
  210. value = False
  211. else:
  212. value = value.replace('"',"")
  213. value = value.strip()
  214. return(value)
  215.  
  216.  
  217.  
  218.  
  219. class Deck: #!
  220. def __init__(self,numsuit,numcard):
  221. self.numsuit = numsuit
  222. self.numcard = numcard
  223. self.deck = [[True] * numsuit for i in range (numcard)]
  224.  
  225. def createCard(self): #!
  226. if self.deck.count:
  227. suitval = random.randint(0,self.numsuit-1)
  228. cardval = random.randint(0,self.numcard-1)
  229. while self.deck[cardval][suitval] == False:
  230. suitval = random.randint(0,self.numsuit-1)
  231. cardval = random.randint(0,self.numcard-1)
  232. self.deck[cardval][suitval] = False
  233. return((cardval,suitval))
  234.  
  235. def createHand(self,handSize):
  236. cards = []
  237. for i in range(handSize):
  238. card = Deck.createCard(self)
  239. cards.append(card)
  240. return(cards)
  241.  
  242. def changeHand(self,hand,indexList):
  243. for i in range(len(indexList)):
  244. hand[indexList[i]] = Deck.createCard(self)
  245. return(hand)
  246.  
  247. def Occur(cards):
  248. scorelist = [2,4,8]
  249. counter = collections.Counter(cards)
  250. largest = [0,0]
  251. largest2 = [0,0]
  252. for key in counter:
  253. occur = counter[key]
  254. if occur > largest2[1]:
  255. if occur > largest[1]:
  256. largest2 = largest
  257. largest = [key,occur]
  258. else:
  259. largest2 = [key,occur]
  260. num = max([largest[0],largest2[0]])
  261. if largest[1] == 3 and largest2[1] == 2:
  262. return(7,largest[0])
  263. elif largest[1] == largest2[1]:
  264. return(3,num)
  265. elif largest[1] in [2,3,4]:
  266. return(scorelist[[2,3,4].index(largest[1])],num )
  267. return(1,num)
  268.  
  269. def bubbleSort(arr):
  270. isSorted = False
  271. while not(isSorted):
  272. for i in range(len(arr) - 1):
  273. for j in range(len(arr) - 1 - i):
  274. if arr[j] > arr[j+1]:
  275. arr[j], arr[j+1] = arr[j+1],arr[j]
  276. else:
  277. isSorted = True
  278. return(arr)
  279.  
  280. def Flush(cards):
  281. counter = collections.Counter(cards)
  282. if len(counter) == 1:
  283. return(True,max(cards))
  284. return(False,max(cards))
  285.  
  286. def Straight(cards):
  287. if cards[4] - cards[0] == 4:
  288. return(True,max(cards))
  289. return(False,max(cards))
  290.  
  291. def Royal(cards):
  292. royal = [0,9,10,11,12]
  293. if cards == royal:
  294. return(True,13)
  295. return(False,13)
  296.  
  297. def getScore(hand):
  298. suit = bubbleSort([i[1] for i in hand])
  299. value = bubbleSort([i[0] for i in hand])
  300. occurances = Occur(value)
  301. if occurances == 2:
  302. if Flush(suit) == 5:
  303. if Straight(value):
  304. return(9)
  305. if Royal(value):
  306. return(10)
  307. return(6)
  308. if Straight(value):
  309. return(5)
  310. return(1)
  311. return(occurances)
  312.  
  313. class Player:
  314. def __init__(self,startBalance,name,game):
  315. self.balance = startBalance
  316. self.name = name
  317. self.game = game
  318. self.hand = game.Deck.createHand(5)
  319. self.score = getScore(self.hand)
  320.  
  321. def bet(self,betAmount):
  322. if betAmount <= self.balance:
  323. self.balance -= betAmount
  324. self.game.currentPot += betAmount
  325. else:
  326. print("Not enough money")
  327.  
  328.  
  329.  
  330.  
  331. class Poker:
  332. def __init__(self,Deck):
  333. self.checkCounter = 0
  334. self.turn = 0
  335. self.currentPot = 0
  336. self.previousBet = 0
  337. self.raised = False
  338. self.Deck = Deck
  339. def addPlayers(self,playerList):
  340. self.playerList = playerList
  341. def Check(self):
  342. if self.raised == False:
  343. if self.checkCounter < 1:
  344. self.checkCounter += 1
  345. self.turn += 1
  346. else:
  347. self.checkCounter = 0
  348. Poker.checkWinner(self)
  349. else:
  350. print("You have to raise buddy")
  351. def Fold(self,player):
  352. Poker.newRound(self,self.playerList[(self.turn + 1) % 2])
  353. def Raise(self,player,betAmount):
  354. if betAmount > self.previousBet and betAmount <= self.playerList[(self.turn + 1) % 2]:
  355. self.raised = True
  356. self.previousBet = betAmount
  357. player.bet(betAmount)
  358. self.turn += 1
  359. else:
  360. print("You have to raise more than "+str(self.previousBet)+" or you have bet too much!")
  361. def Call(self,player):
  362. if self.raised:
  363. self.raised = False
  364. player.bet(self.previousBet)
  365. self.turn += 1
  366. else:
  367. print("There is nothing to call, retard")
  368. def checkWinner(self):
  369. if self.playerList[0].score[0] > self.playerList[1].score[0]:
  370. winner = self.playerList[0]
  371. elif self.playerList[0].score[0] < self.playerList[1].score[0]:
  372. winner = self.playerList[1]
  373. else:
  374. if self.playerList[0].score[1] > self.playerList[1].score[1]:
  375. winner = self.playerList[0]
  376. elif self.playerList[0].score[1] < self.playerList[1].score[1]:
  377. winner = self.playerList[1]
  378. else:
  379. winner = 0
  380. Poker.newRound(self,winner)
  381. def newRound(self,winner):
  382. self.playerList[0].hand = self.Deck.createHand(5)
  383. self.playerList[1].hand = self.Deck.createHand(5)
  384. if winner != 0:
  385.  
  386. winner.balance += self.currentPot
  387. print("The winner of the round is "+ winner.name)
  388. else:
  389. print("its a tie")
  390. self.playerList[0].balance += self.currentPot / 2
  391. self.playerList[1].balance += self.currentPot / 2
  392. self.Deck.deck = [[True] * self.Deck.numsuit for i in range (self.Deck.numcard)]
  393. Poker.__init__(self,self.Deck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement