Advertisement
dmesticg

Untitled

Jun 17th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.68 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,message_text
  8. size(1000,650)
  9. v = getVariables("variable.txt")
  10. u
  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. message_text = v["message_text"]
  44. message_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. #this function displays the menu
  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. #this function displays everything on the poker table, it also records user inputs
  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,trueholder,hand1,amount_text,betamount,Game,Varun,Sina,message_text
  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,message_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. text("Current Pot is: $"+str(Game.currentPot),325,350)
  102.  
  103. if isClicked(gameboxes[9]):
  104.  
  105. indexes = [index for index in range(len(trueholder)) if trueholder[index] == True]
  106. theplayer.hand = thedeck.changeHand(theplayer.hand,indexes)
  107. trueholder = [False,False,False,False,False]
  108.  
  109. if isClicked(gameboxes[5]):
  110. Game.Check()
  111. betamount = 0
  112. trueholder = [False,False,False,False,False]
  113. elif isClicked(gameboxes[6]):
  114. Game.Fold(theplayer)
  115. trueholder = [False,False,False,False,False]
  116. elif isClicked(gameboxes[7]):
  117. Game.Call(theplayer)
  118. trueholder = [False,False,False,False,False]
  119. if isClicked(gameboxes[4]) :
  120. Game.Raise(theplayer,betamount)
  121. betamount = 0
  122. trueholder = [False,False,False,False,False]
  123.  
  124. #this function records the bet of the user
  125. def getBet(plusbox,minusbox,betamount,bankamount):
  126. if isClicked(minusbox) and betamount>1:
  127. betamount -=1
  128. elif isClicked(plusbox) and betamount<bankamount:
  129. betamount+=1
  130. return(betamount)
  131.  
  132. #this function draws all textboxes
  133. def drawTextboxes(textboxes):
  134. textSize(30)
  135. fill(255)
  136. for i in range(len(textboxes)):
  137. fill(textboxes[i][5])
  138. rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
  139. fill(255)
  140. text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
  141. #this functions draws rectangles
  142. def drawtextboxes(textboxes):
  143. textSize(30)
  144. fill(255)
  145. for i in range(len(textboxes)):
  146. fill(textboxes[i][5])
  147. rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
  148. fill(255)
  149. text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
  150. #this function checks if a rectangle has been clicked or not
  151. def isClicked(info):
  152. clicked = False
  153. if mousePressed and mouseButton == LEFT:
  154. if info[0] < mouseX < info[0] + info[2] and info[1] < mouseY < info[1] + info[3]:
  155. clicked = True
  156. return clicked
  157.  
  158. #this function draws cards
  159. def drawHand(hand,num,theimage,cardw,cardl):
  160. global backofcard,trueholder
  161.  
  162. for i in range(len(hand)):
  163. cardval = hand[i][0]
  164. suitval = hand[i][1]
  165.  
  166. textSize(30)
  167. if hand == [[0,0],[0,0],[0,0],[0,0],[0,0]]:
  168. copy(backofcard,0,0,1152,1600,110+130*i,150+(num*250),115,160)
  169.  
  170. else:
  171.  
  172. if isClicked([110+130*i,150+(num*250),115,160]) and sum(trueholder)<2 or trueholder[i]:
  173. trueholder[i] = True
  174. copy(backofcard,0,0,1152,1600,110+130*i,150+(num*250),115,160)
  175. else:
  176. copy(theimage, cardw*(cardval), cardl*suitval, cardw, cardl, 110+130*i, 150+(num*250), 115, 160)
  177.  
  178.  
  179.  
  180.  
  181. #this function gets the variables from the file
  182. def getVariables(fileName):
  183. try:
  184. variables = {}
  185. with open(fileName,"r") as file:
  186. for line in file:
  187. line = line.replace(" ","")
  188. name,value = line.split("=")
  189. variables[name] = string2type(value)
  190. return(variables)
  191. except Exception as E:
  192. print(E)
  193.  
  194. #This function turns strings from the file into boolean,string etc
  195. def string2type(value):
  196. try:#checks if each value is an integer(most common case), if it isnt itll convert it to its proper format
  197. return(int(value))
  198. except ValueError:
  199. if "[" in value and "]" in value:
  200. value = value.replace("[","")
  201. value = value.replace("]","")
  202. value = value.split(",")
  203.  
  204. for i in range(len(value)):
  205. value[i] = string2type(value[i])
  206. elif value == "True":
  207. value = True
  208. elif value == "False":
  209. value = False
  210. else:
  211. value = value.replace('"',"")
  212. value = value.strip()
  213. return(value)
  214.  
  215.  
  216.  
  217.  
  218. class Deck: #!
  219. def __init__(self,numsuit,numcard):
  220. self.numsuit = numsuit
  221. self.numcard = numcard
  222. self.deck = [[True] * numsuit for i in range (numcard)]
  223. #This function draws cards from the deck that have previously not drawn
  224. def createCard(self): #!
  225. if self.deck.count:
  226. suitval = random.randint(0,self.numsuit-1)
  227. cardval = random.randint(0,self.numcard-1)
  228. while self.deck[cardval][suitval] == False:
  229. suitval = random.randint(0,self.numsuit-1)
  230. cardval = random.randint(0,self.numcard-1)
  231. self.deck[cardval][suitval] = False
  232. return((cardval,suitval))
  233. #This function creates the user's hand
  234. def createHand(self,handSize):
  235. cards = []
  236. for i in range(handSize):
  237. card = Deck.createCard(self)
  238. cards.append(card)
  239. return(cards)
  240. #this function changes the user's hand
  241. def changeHand(self,hand,indexList):
  242. for i in range(len(indexList)):
  243. hand[indexList[i]] = Deck.createCard(self)
  244. return(hand)
  245. #this function checks for pairs,triples, four of a kind, and two pairs
  246. def Occur(cards):
  247. scorelist = [2,4,8]
  248. counter = collections.Counter(cards)
  249. largest = [0,0]
  250. largest2 = [0,0]
  251. for key in counter:
  252. occur = counter[key]
  253. if occur > largest2[1]:
  254. if occur > largest[1]:
  255. largest2 = largest
  256. largest = [key,occur]
  257. else:
  258. largest2 = [key,occur]
  259. num = max([largest[0],largest2[0]])
  260. if largest[1] == 3 and largest2[1] == 2:
  261. return(7,largest[0])
  262. elif largest[1] == largest2[1]:
  263. return(3,num)
  264. elif largest[1] in [2,3,4]:
  265. return(scorelist[[2,3,4].index(largest[1])],num )
  266. return(1,num)
  267. #this function checks Poker specific hands
  268. def bubbleSort(arr):
  269. isSorted = False
  270. while not(isSorted):
  271. for i in range(len(arr) - 1):
  272. for j in range(len(arr) - 1 - i):
  273. if arr[j] > arr[j+1]:
  274. arr[j], arr[j+1] = arr[j+1],arr[j]
  275. else:
  276. isSorted = True
  277. return(arr)
  278. #this function checks Poker specific hands
  279. def Flush(cards):
  280. counter = collections.Counter(cards)
  281. if len(counter) == 1:
  282. return(True,max(cards))
  283. return(False,max(cards))
  284. #this function checks Poker specific hands
  285. def Straight(cards):
  286. if cards[4] - cards[0] == 4:
  287. return(True,max(cards))
  288. return(False,max(cards))
  289. #this function checks Poker specific hands
  290. def Royal(cards):
  291. royal = [0,9,10,11,12]
  292. if cards == royal:
  293. return(True,13)
  294. return(False,13)
  295.  
  296. #This function checks gives the user a score based off of the rarity of the his/her hand
  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. #This completes the user's action of betting
  320. def bet(self,betAmount):
  321. if betAmount <= self.balance:
  322. self.balance -= betAmount
  323. self.game.currentPot += betAmount
  324. else:
  325. displayMessage("Not enough money")
  326.  
  327.  
  328.  
  329. # This class covers all of the rules, and actions that are specific to Poker
  330. class Poker:
  331. def __init__(self,Deck):
  332. self.checkCounter = 0
  333. self.turn = 0
  334. self.currentPot = 0
  335. self.previousBet = 0
  336. self.raised = False
  337. self.Deck = Deck
  338. #This function adds each player to the game
  339. def addPlayers(self,playerList):
  340. self.playerList = playerList
  341. #This completes the user's action of "checking"
  342. def Check(self):
  343. if self.raised == False:
  344. if self.checkCounter < 1:
  345. self.checkCounter += 1
  346. self.turn += 1
  347. else:
  348. self.checkCounter = 0
  349. Poker.checkWinner(self)
  350. else:
  351. displayMessage("You have to call/raise buddy")
  352. #This folds the player's hand which ends the round
  353. def Fold(self,player):
  354. Poker.newRound(self,self.playerList[(self.turn + 1) % 2])
  355. #This function completes the user's action of "raising"
  356. def Raise(self,player,betAmount):
  357. if betAmount > self.previousBet and betAmount <= self.playerList[(self.turn + 1) % 2]:
  358. self.raised = True
  359. self.previousBet = betAmount
  360. player.bet(betAmount)
  361. self.turn += 1
  362. else:
  363. displayMessage("You have to raise more than "+str(self.previousBet)+" or you have bet too much!")
  364.  
  365. #This functions completes the user's action of "calling"
  366. def Call(self,player):
  367. if self.raised:
  368. self.raised = False
  369. player.bet(self.previousBet)
  370. self.turn += 1
  371. else:
  372. displayMessage("There is nothing to call")
  373.  
  374. #This function checks which winner won by comparing the score of their hand and the highest card value
  375. def checkWinner(self):
  376. p1score = getScore(self.playerList[0].hand)
  377. p2score = getScore(self.playerList[1].hand)
  378. if p1score[0] > p2score[0]:
  379. winner = self.playerList[0]
  380. elif p1score[0] < p2score[0]:
  381. winner = self.playerList[1]
  382. else:
  383. if p1score[1] > p2score[1]:
  384. winner = self.playerList[0]
  385. elif p1score[1] < p2score[1]:
  386. winner = self.playerList[1]
  387. else:
  388. winner = 0
  389. Poker.newRound(self,winner)
  390.  
  391. #This function sets up a new round
  392. def newRound(self,winner):
  393. self.playerList[0].hand = self.Deck.createHand(5)
  394. self.playerList[1].hand = self.Deck.createHand(5)
  395. if winner != 0:
  396.  
  397. winner.balance += self.currentPot
  398. displayMessage("The winner of the round is "+ str(winner.name))
  399. else:
  400. displayMessage("its a tie")
  401. self.playerList[0].balance += self.currentPot / 2
  402. self.playerList[1].balance += self.currentPot / 2
  403. self.Deck.deck = [[True] * self.Deck.numsuit for i in range (self.Deck.numcard)]
  404. Poker.__init__(self,self.Deck)
  405.  
  406.  
  407.  
  408. #This function displays the computer's output
  409. def displayMessage(thetext):
  410. global message_text
  411. message_text[4] =":"+thetext
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement