Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import collections
- def setup():
- global restart_text,pause_text,throw_text,score_text,menuboxes,gameboxes,pokertable,pokertitle,start_text,whichmenu
- global bankamount,betamount,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
- global handsize,numberofplayers,backofcard,drawtwocards_text,deckimage,hand1,cardl,cardw,hand2,turn,turn_text,trueholder,trueholder1,thedeck,hand1,hand2
- global v,bankamount,Game,Sina,Varun,message_text
- size(1000,650)
- v = getVariables("variable.txt")
- thedeck = Deck(4,13)
- cardw = v["cardw"]
- cardl = v["cardl"]
- #This is the amount of money each user wants to bet
- betamount = v["betamount"]
- #Amount of money in bank
- pokertable = loadImage("poker.jpg")
- pokertitle = loadImage("PokerTitle.png")
- backofcard = loadImage("backofcard.jpg")
- deckimage = loadImage("deck.png")
- start_text = v["start_text"]
- start_text.append(color(255,99,71))
- bet_text = v["bet_text"]
- bet_text.append(color(0,0,0))
- negatory_text = v["negatory_text"]
- negatory_text.append(color(50,50,50))
- pository_text = v["pository_text"]
- pository_text.append(color(50,50,50))
- amount_text = v["amount_text"]
- amount_text.append(color(0,200,0))
- check_text = v["check_text"]
- check_text.append(color(139, 0, 255))
- fold_text = v["fold_text"]
- fold_text.append(color(255, 0, 0))
- call_text = v["call_text"]
- call_text.append(color(127,255,212))
- drawtwocards_text = v["drawtwocards_text"]
- drawtwocards_text.append(color(100,149,237))
- balance_text = v["balance_text"]
- balance_text.append(color(100,149,237))
- message_text = v["message_text"]
- message_text.append(color(0,0,0))
- whichmenu = v["whichmenu"]
- menuboxes = [start_text]
- handsize = v["handsize"]
- numberofplayers = v["numberofplayers"]
- trueholder = [False,False,False,False,False]
- Game = Poker(thedeck)
- Sina = Player(1000,"Sina",Game)
- Varun = Player(1000,"Varun",Game)
- Game.addPlayers([Sina,Varun])
- def draw():
- global whichmenu,trueholder,turn
- global betamount,bankamount,bet_text,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
- global backofcard,handsize,numberofplayers,drawtwocards_text,deckimage,hand1,hand2, thedeck,turn,trueholder,trueholder1,v
- image(pokertable,-100,-50,1100,750)
- if whichmenu == 0:
- menu()
- elif whichmenu == 1:
- pokerTable()
- #this displays back of card
- #This displays the hand
- #this function displays the menu
- def menu():
- global whichmenu
- background(0)
- image(pokertable,0,0,1000,650)
- image(pokertitle,315,190,400,100)
- drawTextboxes(menuboxes)
- if isClicked(textboxes[0]):
- whichmenu = 1
- #this function displays everything on the poker table, it also records user inputs
- def pokerTable():
- global betamount,bankamount,bet_text,negatory_text,pository_text,amount_text,bet_text,check_text,fold_text,call_text,raise_text,balance_text
- global backofcard,handsize,numberofplayers,drawtwocards_text,deckimage,hand1,hand2, thedeck,turn,trueholder,hand1,amount_text,betamount,Game,Varun,Sina,message_text
- Game.turn = Game.turn%2
- gameboxes = [bet_text,negatory_text,pository_text,amount_text,bet_text, check_text,fold_text,call_text,balance_text,drawtwocards_text,message_text]
- drawTextboxes(gameboxes)
- theplayer = Game.playerList[Game.turn]
- betamount = getBet(gameboxes[2],gameboxes[1],betamount,theplayer.balance)
- amount_text = [820,150,110,50,"$"+str(betamount),color(0,200,0)]
- text(theplayer.balance,240,105)
- drawHand([[0,0],[0,0],[0,0],[0,0],[0,0]],1,backofcard,1152,1600)
- drawHand([[0,0],[0,0],[0,0],[0,0],[0,0]],0,backofcard,1152,1600)
- drawHand(theplayer.hand,Game.turn,deckimage,80,110)
- text("Current Pot is: $"+str(Game.currentPot),325,350)
- if isClicked(gameboxes[9]):
- if not(theplayer.changed):
- indexes = [index for index in range(len(trueholder)) if trueholder[index] == True]
- theplayer.hand = thedeck.changeHand(theplayer.hand,indexes)
- theplayer.changed = True
- trueholder = [False,False,False,False,False]
- else:
- displayMessage("you have already changed cards")
- if isClicked(gameboxes[5]):
- Game.Check()
- betamount = 0
- trueholder = [False,False,False,False,False]
- elif isClicked(gameboxes[6]):
- Game.Fold(theplayer)
- trueholder = [False,False,False,False,False]
- elif isClicked(gameboxes[7]):
- Game.Call(theplayer)
- trueholder = [False,False,False,False,False]
- if isClicked(gameboxes[4]) :
- Game.Raise(theplayer,betamount)
- betamount = 0
- trueholder = [False,False,False,False,False]
- #this function records the bet of the user
- def getBet(plusbox,minusbox,betamount,bankamount):
- if isClicked(minusbox) and betamount>1:
- betamount -=1
- elif isClicked(plusbox) and betamount<bankamount:
- betamount+=1
- return(betamount)
- #this function draws all textboxes
- def drawTextboxes(textboxes):
- textSize(30)
- fill(255)
- for i in range(len(textboxes)):
- fill(textboxes[i][5])
- rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
- fill(255)
- text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
- #this functions draws rectangles
- def drawtextboxes(textboxes):
- textSize(30)
- fill(255)
- for i in range(len(textboxes)):
- fill(textboxes[i][5])
- rect(textboxes[i][0],textboxes[i][1],textboxes[i][2],textboxes[i][3])
- fill(255)
- text(textboxes[i][4],textboxes[i][0],textboxes[i][1]+35)
- #this function checks if a rectangle has been clicked or not
- def isClicked(info):
- clicked = False
- if mousePressed and mouseButton == LEFT:
- if info[0] < mouseX < info[0] + info[2] and info[1] < mouseY < info[1] + info[3]:
- clicked = True
- return clicked
- #this function draws cards
- def drawHand(hand,num,theimage,cardw,cardl):
- global backofcard,trueholder
- for i in range(len(hand)):
- cardval = hand[i][0]
- suitval = hand[i][1]
- textSize(30)
- if hand == [[0,0],[0,0],[0,0],[0,0],[0,0]]:
- copy(backofcard,0,0,1152,1600,110+130*i,150+(num*250),115,160)
- else:
- if isClicked([110+130*i,150+(num*250),115,160]) and sum(trueholder)<2 or trueholder[i]:
- trueholder[i] = True
- copy(backofcard,0,0,1152,1600,110+130*i,150+(num*250),115,160)
- else:
- copy(theimage, cardw*(cardval), cardl*suitval, cardw, cardl, 110+130*i, 150+(num*250), 115, 160)
- #this function gets the variables from the file
- def getVariables(fileName):
- try:
- variables = {}
- with open(fileName,"r") as file:
- for line in file:
- line = line.replace(" ","")
- name,value = line.split("=")
- variables[name] = string2type(value)
- return(variables)
- except Exception as E:
- print(E)
- #This function turns strings from the file into boolean,string etc
- def string2type(value):
- try:#checks if each value is an integer(most common case), if it isnt itll convert it to its proper format
- return(int(value))
- except ValueError:
- if "[" in value and "]" in value:
- value = value.replace("[","")
- value = value.replace("]","")
- value = value.split(",")
- for i in range(len(value)):
- value[i] = string2type(value[i])
- elif value == "True":
- value = True
- elif value == "False":
- value = False
- else:
- value = value.replace('"',"")
- value = value.strip()
- return(value)
- class Deck: #!
- def __init__(self,numsuit,numcard):
- self.numsuit = numsuit
- self.numcard = numcard
- self.deck = [[True] * numsuit for i in range (numcard)]
- #This function draws cards from the deck that have previously not drawn
- def createCard(self): #!
- if self.deck.count:
- suitval = random.randint(0,self.numsuit-1)
- cardval = random.randint(0,self.numcard-1)
- while self.deck[cardval][suitval] == False:
- suitval = random.randint(0,self.numsuit-1)
- cardval = random.randint(0,self.numcard-1)
- self.deck[cardval][suitval] = False
- return((cardval,suitval))
- #This function creates the user's hand
- def createHand(self,handSize):
- cards = []
- for i in range(handSize):
- card = Deck.createCard(self)
- cards.append(card)
- return(cards)
- #this function changes the user's hand
- def changeHand(self,hand,indexList):
- for i in range(len(indexList)):
- hand[indexList[i]] = Deck.createCard(self)
- return(hand)
- #this function checks for pairs,triples, four of a kind, and two pairs
- def Occur(cards):
- scorelist = [2,4,8]
- counter = collections.Counter(cards)
- largest = [0,0]
- largest2 = [0,0]
- for key in counter:
- occur = counter[key]
- if occur > largest2[1]:
- if occur > largest[1]:
- largest2 = largest
- largest = [key,occur]
- else:
- largest2 = [key,occur]
- num = max([largest[0],largest2[0]])
- if largest[1] == 3 and largest2[1] == 2:
- return(7,largest[0])
- elif largest[1] == largest2[1]:
- return(3,num)
- elif largest[1] in [2,3,4]:
- return(scorelist[[2,3,4].index(largest[1])],num )
- return(1,num)
- #this function checks Poker specific hands
- def bubbleSort(arr):
- isSorted = False
- while not(isSorted):
- for i in range(len(arr) - 1):
- for j in range(len(arr) - 1 - i):
- if arr[j] > arr[j+1]:
- arr[j], arr[j+1] = arr[j+1],arr[j]
- else:
- isSorted = True
- return(arr)
- #this function checks Poker specific hands
- def Flush(cards):
- counter = collections.Counter(cards)
- if len(counter) == 1:
- return(True,max(cards))
- return(False,max(cards))
- #this function checks Poker specific hands
- def Straight(cards):
- if cards[4] - cards[0] == 4:
- return(True,max(cards))
- return(False,max(cards))
- #this function checks Poker specific hands
- def Royal(cards):
- royal = [0,9,10,11,12]
- if cards == royal:
- return(True,13)
- return(False,13)
- #This function checks gives the user a score based off of the rarity of the his/her hand
- def getScore(hand):
- suit = bubbleSort([i[1] for i in hand])
- value = bubbleSort([i[0] for i in hand])
- occurances = Occur(value)
- if occurances == 2:
- if Flush(suit) == 5:
- if Straight(value):
- return(9)
- if Royal(value):
- return(10)
- return(6)
- if Straight(value):
- return(5)
- return(1)
- return(occurances)
- class Player:
- def __init__(self,startBalance,name,game):
- self.balance = startBalance
- self.name = name
- self.game = game
- self.changed = False
- self.hand = game.Deck.createHand(5)
- #This completes the user's action of betting
- def bet(self,betAmount):
- if betAmount <= self.balance:
- self.balance -= betAmount
- self.game.currentPot += betAmount
- else:
- displayMessage("Not enough money")
- # This class covers all of the rules, and actions that are specific to Poker
- class Poker:
- def __init__(self,Deck):
- self.checkCounter = 0
- self.turn = 0
- self.currentPot = 0
- self.previousBet = 0
- self.raised = False
- self.Deck = Deck
- #This function adds each player to the game
- def addPlayers(self,playerList):
- self.playerList = playerList
- #This completes the user's action of "checking"
- def Check(self):
- if self.raised == False:
- if self.checkCounter < 1:
- self.checkCounter += 1
- self.turn += 1
- else:
- self.checkCounter = 0
- Poker.checkWinner(self)
- else:
- displayMessage("You have to call/raise buddy")
- #This folds the player's hand which ends the round
- def Fold(self,player):
- Poker.newRound(self,self.playerList[(self.turn + 1) % 2])
- #This function completes the user's action of "raising"
- def Raise(self,player,betAmount):
- if betAmount > self.previousBet and betAmount <= self.playerList[(self.turn + 1) % 2]:
- self.raised = True
- self.previousBet = betAmount
- player.bet(betAmount)
- self.turn += 1
- else:
- displayMessage("You have to raise more than "+str(self.previousBet)+" or you have bet too much!")
- #This functions completes the user's action of "calling"
- def Call(self,player):
- if self.raised:
- self.raised = False
- player.bet(self.previousBet)
- self.turn += 1
- else:
- displayMessage("There is nothing to call")
- #This function checks which winner won by comparing the score of their hand and the highest card value
- def checkWinner(self):
- p1score = getScore(self.playerList[0].hand)
- p2score = getScore(self.playerList[1].hand)
- if p1score[0] > p2score[0]:
- winner = self.playerList[0]
- elif p1score[0] < p2score[0]:
- winner = self.playerList[1]
- else:
- if p1score[1] > p2score[1]:
- winner = self.playerList[0]
- elif p1score[1] < p2score[1]:
- winner = self.playerList[1]
- else:
- winner = 0
- Poker.newRound(self,winner)
- #This function sets up a new round
- def newRound(self,winner):
- self.playerList[0].hand = self.Deck.createHand(5)
- self.playerList[1].hand = self.Deck.createHand(5)
- self.playerList[0].changed = False
- self.playerList[1].changed = False
- if winner != 0:
- winner.balance += self.currentPot
- displayMessage("The winner of the round is "+ str(winner.name))
- else:
- displayMessage("its a tie")
- self.playerList[0].balance += self.currentPot / 2
- self.playerList[1].balance += self.currentPot / 2
- self.Deck.deck = [[True] * self.Deck.numsuit for i in range (self.Deck.numcard)]
- Poker.__init__(self,self.Deck)
- #This function displays the computer's output
- def displayMessage(thetext):
- global message_text
- message_text[4] =":"+thetext
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement