Advertisement
dmesticg

Untitled

Jun 17th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.43 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. if Game.turn==1:
  101. drawHand(Sina.hand,0,deckimage,80,110)
  102.  
  103. else:
  104. drawHand(Varun.hand,1,deckimage,80,110)
  105.  
  106.  
  107. if isClicked(gameboxes[10]):
  108. trueholder = [False,False,False,False,False]
  109. Game.turn+=1
  110. if isClicked(gameboxes[9]):
  111. if Game.turn == 1:
  112. indexes = [index for index in range(len(trueholder)) if trueholder[index] == True]
  113. hand1 = thedeck.changeHand(hand1,indexes)
  114. trueholder = [False,False,False,False,False]
  115. elif Game.turn ==0:
  116. indexes = [index for index in range(len(trueholder)) if trueholder[index] == True]
  117. hand2 = thedeck.changeHand(hand2,indexes)
  118. trueholder = [False,False,False,False,False]
  119.  
  120. if isClicked(gameboxes[5]):
  121. Game.Check()
  122. betamount = 0
  123.  
  124. elif isClicked(gameboxes[6]):
  125. Game.Fold(theplayer)
  126.  
  127. elif isClicked(gameboxes[7]):
  128. call()
  129. turn +=1
  130. elif isClicked(gameboxes[8]):
  131. Raise()
  132. turn +=1
  133.  
  134. if isClicked(gameboxes[4]) and Game.turn ==1:
  135. Game.Raise(Varun,betamount)
  136. betamount = 0
  137.  
  138. elif isClicked(gameboxes[4])and Game.turn ==0:
  139. Game.Raise(Sina,betamount)
  140. betamount = 0
  141.  
  142.  
  143.  
  144.  
  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. global backofcard,trueholder,turn
  194.  
  195. for i in range(len(hand)):
  196. cardval = hand[i][0]
  197. suitval = hand[i][1]
  198.  
  199. textSize(30)
  200.  
  201. if isClicked([110+130*i,150+(num*250),115,160]) and sum(trueholder)<2 or trueholder[i]:
  202.  
  203. trueholder[i] = True
  204. copy(backofcard,0,0,1152,1600,110+130*i,150+(num*250),115,160)
  205.  
  206. else:
  207. copy(theimage, cardw*(cardval), cardl*suitval, cardw, cardl, 110+130*i, 150+(num*250), 115, 160)
  208.  
  209.  
  210.  
  211. import random
  212.  
  213.  
  214. def getVariables(fileName):
  215. try:
  216. variables = {}
  217. with open(fileName,"r") as file:
  218. for line in file:
  219. line = line.replace(" ","")
  220. name,value = line.split("=")
  221. variables[name] = string2type(value)
  222. return(variables)
  223. except Exception as E:
  224. print(E)
  225.  
  226. def string2type(value):
  227. try:
  228. return(int(value))
  229. except ValueError:
  230. if "[" in value and "]" in value:
  231. value = value.replace("[","")
  232. value = value.replace("]","")
  233. value = value.split(",")
  234.  
  235. for i in range(len(value)):
  236. value[i] = string2type(value[i])
  237. elif value == "True":
  238. value = True
  239. elif value == "False":
  240. value = False
  241. else:
  242. value = value.replace('"',"")
  243. value = value.strip()
  244. return(value)
  245.  
  246.  
  247.  
  248.  
  249. class Deck: #!
  250. def __init__(self,numsuit,numcard):
  251. self.numsuit = numsuit
  252. self.numcard = numcard
  253. self.deck = [[True] * numsuit for i in range (numcard)]
  254.  
  255. def createCard(self): #!
  256. if self.deck.count:
  257. suitval = random.randint(0,self.numsuit-1)
  258. cardval = random.randint(0,self.numcard-1)
  259. while self.deck[cardval][suitval] == False:
  260. suitval = random.randint(0,self.numsuit-1)
  261. cardval = random.randint(0,self.numcard-1)
  262. self.deck[cardval][suitval] = False
  263. return((cardval,suitval))
  264.  
  265. def createHand(self,handSize):
  266. cards = []
  267. for i in range(handSize):
  268. card = Deck.createCard(self)
  269. cards.append(card)
  270. return(cards)
  271.  
  272. def changeHand(self,hand,indexList):
  273. for i in range(len(indexList)):
  274. hand[indexList[i]] = Deck.createCard(self)
  275. return(hand)
  276.  
  277. def Occur(cards):
  278. scorelist = [2,4,8]
  279. counter = collections.Counter(cards)
  280. largest = [0,0]
  281. largest2 = [0,0]
  282. for key in counter:
  283. occur = counter[key]
  284. if occur > largest2[1]:
  285. if occur > largest[1]:
  286. largest2 = largest
  287. largest = [key,occur]
  288. else:
  289. largest2 = [key,occur]
  290. num = max([largest[0],largest2[0]])
  291. if largest[1] == 3 and largest2[1] == 2:
  292. return(7,largest[0])
  293. elif largest[1] == largest2[1]:
  294. return(3,num)
  295. elif largest[1] in [2,3,4]:
  296. return(scorelist[[2,3,4].index(largest[1])],num )
  297. return(1,num)
  298.  
  299. def bubbleSort(arr):
  300. isSorted = False
  301. while not(isSorted):
  302. for i in range(len(arr) - 1):
  303. for j in range(len(arr) - 1 - i):
  304. if arr[j] > arr[j+1]:
  305. arr[j], arr[j+1] = arr[j+1],arr[j]
  306. else:
  307. isSorted = True
  308. return(arr)
  309.  
  310. def Flush(cards):
  311. counter = collections.Counter(cards)
  312. if len(counter) == 1:
  313. return(True,max(cards))
  314. return(False,max(cards))
  315.  
  316. def Straight(cards):
  317. if cards[4] - cards[0] == 4:
  318. return(True,max(cards))
  319. return(False,max(cards))
  320.  
  321. def Royal(cards):
  322. royal = [0,9,10,11,12]
  323. if cards == royal:
  324. return(True,13)
  325. return(False,13)
  326.  
  327. def getScore(hand):
  328. suit = bubbleSort([i[1] for i in hand])
  329. value = bubbleSort([i[0] for i in hand])
  330. occurances = Occur(value)
  331. if occurances == 2:
  332. if Flush(suit) == 5:
  333. if Straight(value):
  334. return(9)
  335. if Royal(value):
  336. return(10)
  337. return(6)
  338. if Straight(value):
  339. return(5)
  340. return(1)
  341. return(occurances)
  342.  
  343. class Player:
  344. def __init__(self,startBalance,name,game):
  345. self.balance = startBalance
  346. self.name = name
  347. self.game = game
  348. self.hand = game.Deck.createHand(5)
  349. self.score = getScore(self.hand)
  350.  
  351. def bet(self,betAmount):
  352. if betAmount <= self.balance:
  353. self.balance -= betAmount
  354. self.game.currentPot += betAmount
  355. else:
  356. print("Not enough money")
  357.  
  358.  
  359.  
  360.  
  361. class Poker:
  362. def __init__(self,Deck):
  363. self.checkCounter = 0
  364. self.turn = 0
  365. self.currentPot = 0
  366. self.previousBet = 0
  367. self.raised = False
  368. self.Deck = Deck
  369. def addPlayers(self,playerList):
  370. self.playerList = playerList
  371. def Check(self):
  372. if self.raised == False:
  373. if self.checkCounter < 1:
  374. self.checkCounter += 1
  375. self.turn += 1
  376. else:
  377. self.checkCounter = 0
  378. Poker.checkWinner(self)
  379. else:
  380. print("You have to raise buddy")
  381. def Fold(self,player):
  382. Poker.newRound(self,self.playerList[(self.turn + 1) % 2])
  383. def Raise(self,player,betAmount):
  384. if betAmount > self.previousBet and betAmount <= self.playerList[(self.turn + 1) % 2]:
  385. self.raised = True
  386. self.previousBet = betAmount
  387. player.bet(betAmount)
  388. self.turn += 1
  389. else:
  390. print("You have to raise more than "+str(self.previousBet)+" or you have bet too much!")
  391. def Call(self,player):
  392. if self.raised:
  393. self.raised = False
  394. player.bet(self.previousBet)
  395. self.turn += 1
  396. else:
  397. print("There is nothing to call, retard")
  398. def checkWinner(self):
  399. if self.playerList[0].score[0] > self.playerList[1].score[0]:
  400. winner = self.playerList[0]
  401. elif self.playerList[0].score[0] < self.playerList[1].score[0]:
  402. winner = self.playerList[1]
  403. else:
  404. if self.playerList[0].score[1] > self.playerList[1].score[1]:
  405. winner = self.playerList[0]
  406. elif self.playerList[0].score[1] < self.playerList[1].score[1]:
  407. winner = self.playerList[1]
  408. else:
  409. winner = 0
  410. Poker.newRound(self,winner)
  411. def newRound(self,winner):
  412. self.playerList[0].hand = self.Deck.createHand(5)
  413. self.playerList[1].hand = self.Deck.createHand(5)
  414. if winner != 0:
  415.  
  416. winner.balance += self.currentPot
  417. print("The winner of the round is "+ winner.name)
  418. else:
  419. print("its a tie")
  420. self.playerList[0].balance += self.currentPot / 2
  421. self.playerList[1].balance += self.currentPot / 2
  422. self.Deck.deck = [[True] * self.Deck.numsuit for i in range (self.Deck.numcard)]
  423. self.checkCounter = 0
  424. self.turn = 0
  425. self.currentPot = 0
  426. self.previousBet = 0
  427. self.raised = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement