Advertisement
dmesticg

v3

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