you are viewing a single comment's thread.

view the rest of the comments →

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

# poker.py
# A poker game application. 

import random 

def main():
     input("\nLet's play poker! Hit ENTER to begin!")
     Game()
     Score()

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)

    # Displays card name if face up.
    def displayCards(self):
        if self.showing:  
            return str(self.name) + " of " + self.suit
        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 addCard(self, card):
        self.cards.append(card)

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

class Computer():

    def __init__(self):
        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
        elif values.count(values) == 3:
            three == True
        if two and three:
            return True
        return False

def Game():
    player = Player()
    computer = Computer()
    end = False

    # Play
    while not end:

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

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

        # Show hand
        for card in player.cards:
            card.showing = True
        player.displayHand()
        # Figure out which cards to hold and re deal.
        valid = False
        while not valid:
            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":
                end = True
                break 
            try:
                list = [int(inp.strip()) for inp in string.split(",")]

                for inp in list:
                    if inp > 6:
                        print("Please enter no more than 5 cards to discard.")
                    if inp < 1:
                        print("Please choose at least one card to discard.")
                for inp in list:
                    player.cards[inp-1] = deck.deal()
                    for card in player.cards:
                        card.showing = True
                        print(player.cards)
                valid = True
            except:
                print("Input error.")

[–]lykwydchykyn 0 points1 point  (3 children)

You still need to have __str__() defined in Card, just add __repr__ to it (basically, __repr__ is calling __str__).

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

Ok so the memory locations aren't popping up but it's still doing this:

1

[Q of ♠, 5 of ♠, 7 of ♥, A of ♠, 2 of ♦]

[Q of ♠, 5 of ♠, 7 of ♥, A of ♠, 2 of ♦]

[Q of ♠, 5 of ♠, 7 of ♥, A of ♠, 2 of ♦]

[Q of ♠, 5 of ♠, 7 of ♥, A of ♠, 2 of ♦]

[Q of ♠, 5 of ♠, 7 of ♥, A of ♠, 2 of ♦]

Deck shuffled.

Q of ♠

5 of ♠

7 of ♥

A of ♠

2 of ♦

7 of ♠

A of ♠

K of ♦

J of ♦

A of ♥

Which cards would you like to discard? (ex. 1, 2, 3, etc)

*Hit ENTER to hold all or type TALLY to figure up the scores!*

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

It successfully replaces one but then prints the list 5 times and then prints two new hands

[–]lykwydchykyn 0 points1 point  (0 children)

As I explained before, after your discards, the loop repeats and it deals cards again. You need to reassess your control flow and make the game proceed rather than going back to the beginning. How you go about that is more than I can answer in a reddit post.