Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Name Type Purpose Restrictions
- # deckofcards Img Loads image
- # suitw, suitl Int Width/length of copy
- # keepgoing Bool. Runs copy function once
- # p1hand, p2hand List Holds the hand of bot and player
- # gamemode Int Loads specific game mode
- # Boxx, boxy Int Coord. of the button
- # boxyl, boxw Int Dimensions of the button
- # valid bool. Returns if button clicked
- # playtext, resettext Str String for the button
- # numofcards Int Used in for loops for # of cards
- # results (hand names) Int Returns a value based on what hand each player has
- # royalflushvalue Int Only possible sum when getting royal flush
- # cardranknames List List of the names of hand
- # availablecards List Contains the deck of cards, removes cards if used
- # winnertext List Contains the string for the result of game
- # winner Int Used to determine which player won or if it's a tie
- # maxvaluecard Int Used in for loops for going through the values of cards
- # counter Dict Finds the number of duplicate values and puts in a dictionary
- import random
- from collections import Counter
- def setup():
- global deckofcards, suitl, suitw, keepgoing, p1hand, p2hand, gamemode, boxx, boxy, boxw, boxl, valid, playtext, resettext, numofcards
- global royalflush, straightflush, fourkind, fullhouse, fulsh, straight, threekind, twopair, pair, highcard, cardranknames, result, royalflushvalue, availablecards
- global winnertext, winner, maxvaluecard
- background (0)
- size(1040,440)
- deckofcards = loadImage("cards.png")
- suitw = 80
- suitl = 110
- keepgoing = True
- p1hand = []
- p2hand = []
- gamemode = 0
- boxx = 80
- boxy = 10
- boxw = 150
- boxl = 50
- valid = False
- playtext = "Play"
- resettext = "Reset"
- numofcards = 5
- result = 9
- #ace + 10 + king + queen + jack
- royalflushvalue = (10+11+12+13+0)
- royalflush = 0
- straightflush = 1
- fourkind = 2
- fullhouse = 3
- fulsh = 4
- straight = 5
- threekind = 6
- twopair = 7
- pair = 8
- highcard = 9
- cardranknames = ["royalflush", "straightflush", "fourkind", "fullhouse", "flush", "straight", "threekind", "twopair", "pair", "highcard"]
- availablecards = []
- winnertext = ["player 1 wins", "bot wins", "tie"]
- for i in range(4):
- for j in range(13):
- availablecards.append((i,j))
- winner = 0
- maxvaluecard = 13
- def draw():
- global boxx, boxy, boxw, boxl
- fill(255,0,0)
- rect(boxx, boxy, boxw, boxl)
- whichgamemode()
- def whichgamemode():
- global deckofcards, suitx, suity, suitl, suitw, gamemode, boxx, boxy, boxw, boxl, valid, playtext, resettext
- global cardranknames, p1hand, p2hand, winnertext
- if valid:
- gamemode += 1
- valid = False
- if gamemode == 0:
- displaytext(playtext)
- elif gamemode == 1:
- displayhand()
- displaytext(resettext)
- p1hand = sorthand(p1hand)
- p2hand = sorthand(p2hand)
- textSize(40)
- text((winnertext[determinewinner()]),0,400)
- text((cardranknames[p1result]), 800,350)
- text((cardranknames[p2result]), 800, 100)
- else:
- setup()
- def determinewinner():
- global p1hand, p2hand, winner, p1result, p2result
- p1result = royalflushcheck(p1hand)
- p2result = royalflushcheck(p2hand)
- if p2result < p1result:
- winner = 1
- elif p1result < p2result:
- winner = 0
- else:
- if p1result == 9 and p2result == 9:
- if p1hand[4][1] > p2hand[4][1]:
- winner = 0
- elif p2hand[4][1] > p1hand[4][1]:
- winner = 1
- elif p1hand[4][1] == p2hand[4][1]:
- winner = 2
- else:
- winner = 2
- return (winner)
- def displaytext(whattext):
- fill(255)
- textSize(50)
- text(whattext, boxx, 50)
- def sorthand(hand):
- global numofcards
- #sorts the order of deck
- for i in range(numofcards-1):
- for j in range(numofcards-1):
- if (hand[i][1]) > (hand[i+1][1]):
- hand[i], hand[i+1] = hand[i+1], hand[i]
- return (hand)
- def royalflushcheck(hand):
- global numofcards, maxvaluecard
- global royalflush, straightflush, fourkind, fullhouse, fulsh, straight, threekind, twopair, pair, highcard, royalflushvalue
- totalvalue = 0
- numofpairs = 0
- result = 9
- # counts to see number of values that are the same
- counter = Counter(y for (x,y) in hand)
- if hand[0][0] == hand[1][0] == hand[2][0] == hand[3][0] == hand[4][0]:
- for i in range(numofcards-1):
- totalvalue += hand[i][1]
- ##Since royal flush must have ace, king, queen, jack, 10 I set a condition where it must equal to the sum of them
- if totalvalue == (royalflushvalue):
- result = royalflush
- ##sees if consecutive by seeing if total value is equal to the smallest value * numofcards + numofcards
- elif totalvalue == hand[0][1]*numofcards + numofcards:
- result = straightflush
- else:
- result = fulsh
- for i in range(maxvaluecard):
- # counts the numbers of values that are same and determines which hand it is
- if counter[i] == 2:
- numofpairs += 1
- if numofpairs == 1:
- # if there is one pair, then only one pair
- result = pair
- elif numofpairs == 2:
- # if there are two pairs, then only two pairs
- result = twopair
- if counter[i] == 3 and numofpairs == 1:
- # if there's one pair and three of one value it's a fullhouse
- result = fullhouse
- if counter[i] == 3 and numofpairs == 0:
- #if there's no pairs and three of one value, it's three of a kind
- result = threekind
- if counter[i] == 4:
- # if there's four of one value, it's four of a kind
- result = fourkind
- for i in range(numofcards-1):
- totalvalue += hand[i][1]
- if totalvalue == hand[0][1]*numofcards + numofcards:
- # checks to see if cards are consecutive, since it's in order of smallest to greatest it takes the smallest value, multiplies it by number of cards and adds that number. If that number is equal to the total value, it's consecutive.
- result = straight
- return (result)
- def displayhand():
- global deckofcards, suitl, suitw, keepgoing, p1hand, p2hand, numofcards
- while keepgoing:
- for i in range(numofcards):
- randomsuit1, randomvalue1 = generaterandom()
- p1hand.append((randomsuit1, randomvalue1))
- copy(deckofcards, suitw*randomvalue1, suitl*randomsuit1, suitw, suitl, 300+100*i, 220, 100, 160)
- for i in range (numofcards):
- randomsuit2, randomvalue2 = generaterandom()
- p2hand.append((randomsuit2, randomvalue2))
- copy(deckofcards, suitw*randomvalue2, suitl*randomsuit2, suitw, suitl, 300+100*i, 0, 100, 160)
- keepgoing = False
- def generaterandom():
- global availablecards
- (suit,value) = (random.choice(availablecards))
- availablecards.remove((suit,value))
- return(suit,value)
- def mouseClicked():
- global boxx, boxy, boxw, boxl, valid
- valid = boxx <= mouseX <= boxx + boxw and boxy <= mouseY <= boxy + boxl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement