dmesticg

Untitled

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