0

Something's wrong with how the winner is determined in my poker game.

I've tried checking that the player score value is reset from the loop and that all other logic related variables are too

TableCards = []
for Card in Flop:
    TableCards.append(Card)

TableCards.append(Turn)
TableCards.append(River)

for Card in Player.Hand:
    TableCards.append(Card)

TableCardValues = []
TableCardSuits = []

for Card in TableCards:
    TableCardValues.append(Card.Value)
    TableCardSuits.append(Card.Suit)

TableCardValues.sort()
TableCardSuits.sort()
ArrayLengthI = len(TableCardValues)
Straight = False
Flush = False
Pairs = []
ThreeOfAKinds = []
FourOfAKinds = []
StraightCount = 0
PlayerHandValues = []
DealerHandValues = []

for Card in Player.Hand:
    PlayerHandValues.append(Card.Value)

for Card in Dealer.Hand:
    DealerHandValues.append(Card.Value)

for X in range(ArrayLengthI - 1):
    if TableCardValues[X + 1] == TableCardValues[X] + 1:
        StraightCount += 1

if StraightCount >= 5:
    Straight == True

for Suit in TableCardSuits:
    if TableCardSuits.count(Suit) >= 5:
        Flush = True

for Value in set(TableCardValues):
    if TableCardValues.count(Value) == 2:
        Pairs.append(Value)

    elif TableCardValues.count(Value) == 3:
        ThreeOfAKinds.append(Value)

    elif TableCardValues.count(Value) == 4:
        FourOfAKinds.append(Value)

if Straight == True or Flush == True:
    if Straight == True and Flush == True:
        Player.HandScore = PokerHands.StraightFlush * max(PlayerHandValues)

    elif Straight == True:
        Player.HandScore = PokerHands.Straight * max(PlayerHandValues)

    elif Flush == True:
        Player.HandScore = PokerHands.Flush * max(PlayerHandValues)

elif FourOfAKinds != []:
    Player.HandScore = PokerHands.FourOfAKind * max(PlayerHandValues)

elif ThreeOfAKinds != []:
    if len(Pairs) >= 1:
        Player.HandScore = PokerHands.FullHouse * max(PlayerHandValues)

    else:
        Player.HandScore = PokerHands.ThreeOfAKind * max(PlayerHandValues)

elif len(Pairs) == 2:
    Player.HandScore = PokerHands.TwoPair * max(PlayerHandValues)

elif len(Pairs) == 1:
    Player.HandScore = PokerHands.OnePair * max(PlayerHandValues)

else:
    Player.HandScore = PokerHands.HighCard * max(PlayerHandValues)

TableCardsDealer = []
TableCardValuesDealer = []
TableCardSuitsDealer = []
for Card in Flop:
    TableCardsDealer.append(Card)

TableCardsDealer.append(Turn)
TableCardsDealer.append(River)
DealerStraight = False
DealerFlush = False
DealerStraightCount = 0
DealerPairs = []
DealerThreeOfAKinds = []
DealerFourOfAKinds = []

for Card in Dealer.Hand:
    TableCardsDealer.append(Card)

for Card in TableCards:
    TableCardValuesDealer.append(Card.Value)
    TableCardSuitsDealer.append(Card.Suit)
TableCardSuitsDealer.sort()
TableCardValuesDealer.sort()

TableCardsDealerLength = len(TableCardSuitsDealer)

for X in range(TableCardsDealerLength - 1):
    if TableCardValuesDealer[X + 1] == TableCardValuesDealer[X] + 1:
        DealerStraightCount += 1

if DealerStraightCount >= 5:
    DealerStraight == True

for Suit in TableCardSuitsDealer:
    if TableCardSuitsDealer.count(Suit) >= 5:
        DealerFlush == True

for Value in set(TableCardValuesDealer):
    if TableCardValuesDealer.count(Value) == 2:
        DealerPairs.append(Value)

    elif TableCardValuesDealer.count(Value) == 3:
        DealerThreeOfAKinds.append(Value)

    elif TableCardValuesDealer.count(Value) == 4:
        DealerFourOfAKinds.append(Value)

if DealerStraight == True or DealerFlush == True:
    if DealerStraight == True and DealerFlush == True:
        Dealer.HandScore = PokerHands.StraightFlush * max(DealerHandValues)

    elif DealerStraight == True:
        Dealer.HandScore = PokerHands.Straight * max(DealerHandValues)

    elif DealerFlush == True:
        Dealer.HandScore = PokerHands.Flush * max(DealerHandValues)

elif DealerFourOfAKinds != []:
    Dealer.HandScore = PokerHands.FourOfAKind * max(DealerHandValues)

elif DealerThreeOfAKinds != []:
    if len(DealerPairs) >= 1:
        Dealer.HandScore = PokerHands.FullHouse * max(DealerHandValues)

    else:
        Dealer.HandScore = PokerHands.ThreeOfAKind * max(DealerHandValues)

elif len(DealerPairs) == 2:
    Dealer.HandScore = PokerHands.TwoPair * max(DealerHandValues)

elif len(DealerPairs) == 1:
    Dealer.HandScore = PokerHands.OnePair * max(DealerHandValues)

else:
    Dealer.HandScore = PokerHands.HighCard * max(DealerHandValues)

if Player.HandScore > Dealer.HandScore:
    print("Well Done, sir. You've won.")
    Player.Money += Pot
    Pot = 0

elif Player.HandScore < Dealer.HandScore:
    print("I Apologise, but you've lost.")
    Dealer.Money += Pot
    Pot = 0

elif Player.HandScore == Dealer.HandScore:
    print("You've tied")
    Pot = 0

Junk = input()
Player.HandScore = 0
Dealer.HandScore = 0
system("cls")

There are no error messages, but it seems to determine the winner wrong, and I can't exactly pinpoint the reason why.

Tim
  • 2,510
  • 1
  • 22
  • 26
  • 3
    What is going wrong? Eg what are you expecting vs what you are getting. Also, this is not a complete example as many of the variables are undefined. If you don't want to provide all your code, at least provide an initial state of the variables. – Tim Sep 23 '19 at 01:15
  • In Python, we usually name variables and functions like `table_card_values` instead of `TableCardValues`. That style is reserved for [classes](https://docs.python.org/3/tutorial/classes.html) – Boris Verkhovskiy Sep 23 '19 at 03:01
  • The first obvious error is that after expanding TableCards into two arrays for value and suit, you then sort those two arrays independently, so they no longer correspond. You should sort the TableCards array by value first, then copy it into the two arrays (or even better, not bother with the new arrays at all and just do your calculations with TableCards). – Lee Daniel Crocker Sep 23 '19 at 18:27
  • Second obvious error is StraightCount. The seven cards 2-3-4-6-7-8-9 will be seen as a straight in your code. – Lee Daniel Crocker Sep 23 '19 at 18:29
  • And anohter...you need to evaluate the possibilities from highest to lowest: First check for straight flush (which you can't actually do because of the first error), then quads, then full house, flush, etc., down to no pair. And finally, your "elif len(Pairs) == 2" should be >=, because with 7 cards you might have three pairs, even though only two count. – Lee Daniel Crocker Sep 23 '19 at 18:34
  • And finally, you make no attempt to differentiate hands of the same category other than by high card, which is not how poker hands are ranked. – Lee Daniel Crocker Sep 23 '19 at 18:35

0 Answers0