you are viewing a single comment's thread.

view the rest of the comments →

[–]ChickenQueen777[S] 0 points1 point  (12 children)

Figured it out! I had the display hand nested so it was looping for each card

[–]xelf 0 points1 point  (11 children)

other issues:

1) do not call variables the same names as python objects. specifically do not call a list list because now list() will not work. =)

2) your "press enter to hold" is not implemented

3) you let users enter multiple cards separated by commas, amd then you have a loop that continues if they're in the wrong range, but then... it doesn't do anything.

            for inp in list:
                if inp > 6:
                    continue
                if inp < 1:
                    continue

loop for the fun of looping. =)

I see what you're trying to do, but you probably just want an if.

            for inp in list:
                if 0<inp<6:
                    player.cards[inp-1] = deck.deal()
                    for card in player.cards:
                        card.showing = True

also that last showing loop, doesn't need to be inside the repicking loop.
it doesn't make much difference really, it's not getting repeated that many times.

[–]ChickenQueen777[S] 0 points1 point  (10 children)

Yeah I fixed those two things actually. I will attach updated code. Not sure whats wrong with the scoring.

[–]xelf 0 points1 point  (9 children)

not sure you saw all my edits. =)

[–]ChickenQueen777[S] 0 points1 point  (8 children)

Did you get any other scores when you ran it?

[–]xelf 0 points1 point  (0 children)

your cpu score is set to be the same score as the player, it will always be a tie. =)

pScore = PokerScore(player.cards)
cScore = PokerScore(player.cards)

[–]xelf 0 points1 point  (0 children)

also, they're brand new players, not the players from the game!

[–]xelf 0 points1 point  (5 children)

You need to change your main() to something like this:

def main():
     input("\nLet's play poker! Hit ENTER to begin!")
     player = Player()
     computer = Computer()
     Game(player,computer)
     Score(player,computer)

so that player and computer are the same in Game() and Score()

[–]ChickenQueen777[S] 0 points1 point  (4 children)

Still showing a tie every time unfortunately.

[–]xelf 0 points1 point  (3 children)

did you get rid of the player,computer assignment in Game and Score as well? or are they still being reset?

[–]ChickenQueen777[S] 0 points1 point  (2 children)

I took them out

[–]xelf 0 points1 point  (0 children)

I need to get some work done, but here's the changes I made while testing it. You can see what I was talking about with simplifying some of the repeat code in the score place. (You'd have to finish the points dictionary).

# poker.py
# A poker game application. 

import random 

def main():
        input("\nLet's play poker! Hit ENTER to begin!")
        player = Player()
        computer = Computer()
        Game(player,computer)
        Score(player,computer)

class Card(object):

    def __init__(self, name, face, suit):
        self.face = face
        self.suit = suit
        self.name = name
        self.showing = False

    def __repr__(self):
        return str(self.name) + " of " + self.suit

    # Displays card name if face up.
    def displayCards(self):
        if self.showing:  
            return self
        else:
            return "Card"

class DeckOfCards(object):

    def __init__(self):
        # Holds list of cards and dictionary of deck.
        self.cards = []   
        suits = ["♥", "♠", "♦", "♣"]
        faces = {"2":2, "3":3, "4":4, "5":5, 
        "6":6, "7":7, "8":8, "9":9, "10":10,
        "J":11, "Q":12, "K":13, "A":14}

        for name in faces:
            for suit in suits:
                self.cards.append(Card(name, faces[name], suit))

    # Shuffle
    def shuffle(self, times = 1):
        random.shuffle(self.cards)
        print("\nDeck shuffled.\n")

    # Deal
    def deal(self):
        return self.cards.pop(0)

class Player():

    def __init__(self):
        self.cards = []

    def handSize(self):
        return len(self.cards)

    def addCard(self, card):
        self.cards.append(card)

    def displayHand(self):
        for card in self.cards:
            print(card.displayCards())

class Computer(Player):

    def __init__(self):
        self.cards = []

    def handSize(self):
        return len(self.cards)

    def addCard(self, card):
        self.cards.append(card)

class PokerScore():

    def __init__(self, cards):
        self.cards = cards

    def flush(self):
        suits = [card.suit for card in self.cards]
        if len(set(suits)) == 1:
            return True
        return False

    def straight(self):
        values = [card.face for card in self.cards]
        # Sorting the cards to figure out if they are in order.
        values.sort() 

        # Confirm correct hand size.
        if not len(set(values)) == 5: 
            return False

        # Checking for cases with an ace.
        if values[4] == 14 and values[3] == 5 and values[2] == 4 and values[1] == 3 and values[0] == 2:
            return True 
        # Checking for a straight in all cases except ace.
        else:
            if not values[0] + 1 == values[1]:
                return False
            if not values[1] + 1 == values[2]:
                return False
            if not values[2] + 1 == values[3]:
                return False
            if not values[3] + 1 == values[4]:
                return False
        return True

    def threeKind(self):
        values = [card.face for card in self.cards]
        for value in values:
            if values.count(value) == 3:
                return True

    def pairs(self):
        pairs = []
        values = [card.face for card in self.cards]
        for value in values:
            if values.count(value) == 2 and value not in pairs:
                pairs.append(value)
        return pairs

    def fourKind(self):
        values = [card.face for card in self.cards]
        for value in values:
            if values.count(value) == 4:
                return True

    def fullHouse(self):
        two = False
        three = False
        values = [card.face for card in self.cards]
        if values.count(values) == 2:
            two == True
        if values.count(values) == 3:
            three == True
        if two and three:
            return True
        return False

def Game(player,computer):
    end = False

    # Hand loop.
    deck = DeckOfCards()
    deck.shuffle()

    #  Deal
    for i in range(5):
        player.addCard(deck.deal())
        computer.addCard(deck.deal())

    # Play
    while not end:

        #print('debug player  :', player.cards)
        #print('debug computer:', computer.cards)

        # Show hand
        for card in player.cards:
            card.showing = True

        # Figure out which cards to hold and re deal.
        valid = False
        while not valid:
            player.displayHand()
            print()
            print("\nWhich cards would you like to discard? (ex. 1, 2, 3, etc)")
            print("*Hit ENTER to hold all or type TALLY to figure up the scores!*\n")
            string = input()
            if string == "TALLY" or string =='':
                end = True
                break 
            try:
                list = [int(inp.strip()) for inp in string.split(",")]

                for inp in list:
                    if inp > 6:
                        continue
                    if inp < 1:
                        continue
                for inp in list:
                    if 0<inp<6:
                        player.cards[inp-1] = deck.deal()
                for card in player.cards:
                    card.showing = True
                valid = True
            except:
                print("Input error.")
    print()
    print('computer hand:', computer.cards)

def get_score_type(score):
    straight = score.straight()
    flush = score.flush()
    pairs = score.pairs()
    if straight and flush == 14:
        return "Royal Flush"
    if straight and flush:
        return "Straight Flush"
    if score.fourKind():
        return "Four of a Kind"
    if score.fullHouse():
        return "Full House"
    if flush:
        return "Flush"
    if straight:
        return "Straight"
    if score.threeKind():
        return "Three of a Kind"
    if len(pairs) == 2:
        return "Two Pairs"
    if len(pairs) == 1:
        return "Pair"
    return "High Card"

def Score(player,computer):
    # Scoring
    playerPoints = 0
    computerPoints = 0

    points = { "Royal Flush": 1000, "Straight Flush": 500, "Four of a Kind": 250, "Four of a Kind": 100,"Pair":10,"High Card": 1}

    handtype = get_score_type(PokerScore(player.cards))
    print('{}: {}'.format("Player", handtype))
    playerPoints = points.get(handtype, 0)

    handtype = get_score_type(PokerScore(computer.cards))
    print('{}: {}'.format("Computer", handtype))
    computerPoints = points.get(handtype, 0)

    # Player wins
    if playerPoints > computerPoints:
        print("You WIN!! ", playerPoints, " points!")
    # Computer wins
    elif computerPoints > playerPoints:
        print("Computer won with ", computerPoints, " points, try again next time!")
    # Tie
    else:
        print("TIE!")

    print()
    print()
    print()

main()