0

I'm trying to code a simple game of poker. My code can be found below. I was wondering how I could code the following combinations: Three of a kind, straight, full house, and four of a kind. I'm not using suits, and I only have a basic understanding of Python. Any advice is appreciated. Thank you.

I tried a few different combinations but I haven't even gotten close to coding it.

import random

print('The game ends if you fold')
print('11 = Jack')
print('12 = Queen')
print('13 = King')
print('14 = Ace')

#Choose a random card
cardOne = random.randint(1, 14)
cardTwo = random.randint(1, 14)

#Print the users hand
print('YOUR CARDS')
print(cardOne)
print(cardTwo)

oppCardOne = random.randint(1, 14)
oppCardTwo = random.randint(1, 14)

print("OPPONENT'S CARDS")
print(oppCardOne)
print(oppCardTwo)

def fold():
  print('Lol, ok u lose champ')
  exit()

swampOne = random.randint(1,14)
print('First Swamp Card:', swampOne)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print("Damn, you've got some buzungas")

if decision == 2:
  fold()

swampTwo = random.randint(1,14)
print('Second Swamp Card:', swampTwo)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print("Damn, you've got some buzungas")

if decision == 2:
  fold()

swampThree = random.randint(1,14)
print('Third Swamp Card:', swampThree)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print("Damn, you've got some buzungas")

if decision == 2:
  fold()

fourthStreet = random.randint(1, 14)
print('fourth Street:', fourthStreet)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print("Damn, you've got some buzungas")

if decision == 2:
  fold()


river = random.randint(1, 14)
print('River:', river)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print('Good Luck')

if decision == 2:
  fold()


#User combos



#Highest compile
if cardOne > oppCardOne or oppCardTwo:
  combo = 1
if cardTwo > oppCardOne or oppCardTwo:
  combo = 1
#Pair
if cardOne or cardTwo == swampOne or swampTwo or swampThree or fourthStreet or river:
  combo = 2
#Two pairs
if cardOne and cardTwo == swampOne or swampTwo or swampThree or fourthStreet or river:
  combo = 3
if cardOne == swampOne and swampTwo or swampOne and swampThree or swampOne and fourthStreet or swampOne and river:
  combo = 3
if cardOne == (swampTwo and swampOne) or (swampTwo and swampThree) or (swampTwo and fourthStreet) or (swampTwo and river):
  combo = 3
if cardOne == (swampThree and swampOne) or (swampThree and swampTwo) or (swampThree and fourthStreet) or (swampThree and river):
  combo = 3
if cardOne == (fourthStreet and swampOne) or (fourthStreet and swampTwo) or (fourthStreet and swampThree) or (fourthStreet and river):
  combo = 3
if cardOne == (river and swampOne) or (river and swampTwo) or (river and swampThree) or (river and fourthStreet):
  combo = 3
#Two pars card two
if cardTwo == swampOne and swampTwo or swampOne and swampThree or swampOne and fourthStreet or swampOne and river:
  combo = 3
if cardTwo == (swampTwo and swampOne) or (swampTwo and swampThree) or (swampTwo and fourthStreet) or (swampTwo and river):
  combo = 3
if cardTwo == (swampThree and swampOne) or (swampThree and swampTwo) or (swampThree and fourthStreet) or (swampThree and river):
  combo = 3
if cardTwo == (fourthStreet and swampOne) or (fourthStreet and swampTwo) or (fourthStreet and swampThree) or (fourthStreet and river):
  combo = 3
if cardTwo == (river and swampOne) or (river and swampTwo) or (river and swampThree) or (river and fourthStreet):
  combo = 3
#Hand pairs
if cardOne == cardTwo:
  combo = 3
#Three of a kind









#Opponent Combos
if oppCardOne > cardOne or cardTwo:
  oppCombo = 1
if oppCardTwo > cardOne or cardTwo:
  oppCombo = 1
if oppCardOne or oppCardTwo == swampOne or swampTwo or swampThree or fourthStreet or river:
  oppCombo = 2
if oppCardOne and oppCardTwo == swampOne or swampTwo or swampThree or fourthStreet or river:
  oppCombo = 3






#Determine who wins
if combo > oppCombo:
  print('YOU WIN YA SCHMUCK')
  exit()
elif oppCombo > combo:
  print('HA, YOU LOSE')
  exit()
else:
  print('TIE')
  exit()




print(combo)

I'm not receiving any error messages as I haven't really started to code these combinations.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • An OOP approach would be better. I understand that you are new but try learning what OOP is and it would make your life much easier. – Paolo Jun 06 '19 at 16:01
  • Welcome to Stack Overflow. There are numerous questions on Stack Overflow that address dealing with Poker hands. Just do a search for "poker hand". Per the guidelines, you should always do some searching to see if an existing question can help you out. – CryptoFool Jun 06 '19 at 16:05
  • 1
    Possible duplicate of [The simplest algorithm for poker hand evaluation](https://stackoverflow.com/questions/10363927/the-simplest-algorithm-for-poker-hand-evaluation) – CryptoFool Jun 06 '19 at 16:05

2 Answers2

0

You may find it useful to define the cards as python classes. That way you can organize the details of the card (suit, value) and make a 'hand' of five card objects. Then you can do simple if statements for each hand.

mneedham
  • 45
  • 4
0

I've written a bunch of Poker hand code, some of it for a programming class that I taught. One thing I've learned is that, no disrespect to @mneedham's answer intended, it is best to represent a card as just a number between 0 and 51. This has a lot of advantages in terms of how you process a 5 card hand looking for particular combinations.

If two cards represented as a number from 0 to 51 match in rank, then their number modded with 13 will be the same. So you can do something like this to evaluate hands that involve matching rank:

def evaluate_hand(cards):
    # build a list of lists for collecting cards of the same rank
    ranks = []
    for i in range(13):
        ranks.append([])

    # put each card in the appropriate rank list
    for i in cards:
        ranks[i % 13].append(i)

    # Sort our rank list descending by sublist length
    ranks = sorted(ranks, key=len, reverse=True)

    # Show what we end up with
    print(ranks)

    # Now show what we've got
    if len(ranks[0]) == 4:
        print("Four of a kind")
    elif len(ranks[0]) == 3 and len(ranks[1]) == 2:
        print("Full house")
    elif len(ranks[0]) == 3:
        print("Three of a kind")
    elif len(ranks[0]) == 2 and len(ranks[1]) == 2:
        print("Two pair")
    elif len(ranks[0]) == 2:
        print("Pair")
    else:
        print("Nada")

evaluate_hand([31, 4, 23, 17, 30])
evaluate_hand([36, 4, 23, 17, 30])
evaluate_hand([36, 4, 23, 19, 30])
evaluate_hand([4, 5, 6, 7, 8])

Result:

[[4, 17, 30], [31], [23], [], [], [], [], [], [], [], [], [], []]
Three of a kind
[[4, 17, 30], [36, 23], [], [], [], [], [], [], [], [], [], [], []]
Full house
[[4, 30], [36, 23], [19], [], [], [], [], [], [], [], [], [], []]
Two pair
[[4], [5], [6], [7], [8], [], [], [], [], [], [], [], []]
Nada

To recognize a flush, check if all 5 card values divided by 13 give the same number. To recognize a straight, create a list of each card value modded with 13 (create a list of ranks), sort it, and then check that the resulting numbers are sequential. If both these tests pass, you have a straight flush. I'll leave the coding of these checks to you.

Getting the suit for any number is easy:

def show_suit(card):
    print(['Diamond', 'Heart', 'Spade', 'Club'][int(card / 13)])

So is getting the named rank:

def show_rank(card):
    rank_names = { 0: 'Ace', 10: 'Jack', 11: 'Queen', 12: 'King'}
    rank = card % 13
    print(rank_names[rank] if rank in rank_names else rank)
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Yeah thank you this helps a lot. I'll probably change a little bit of your code to suit my program so it's not just copied and pasted but this definitely gives me a huge helping hand. Thanks again! – LankyGod Jun 06 '19 at 23:14
  • I can't really tell you that. It's your program, so it's your logic. I don't know what you want to accomplish. You need to understand your code and where you want to go well enough to answer that question yourself. I was as much explaining an idea as giving you useful code. I would suggest that you think through what you really want to do, and maybe start over with the concepts I've showed you in mind. – CryptoFool Jun 07 '19 at 15:38