Advertisement
dmesticg

with v

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