Advertisement
dmesticg

V2

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