Advertisement
dmesticg

Untitled

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