Dec-24-2021, 01:32 PM
I think you should make a Card class and a Hand class. The Card class knows how to do things like print its name and sort itself among a list of cards. The Hand class would know how to identify pairs and flushes. Mine is incomplete but should give some idea of how this could be done. The Player would be delt a hand, place bets and acquire or pay out chips.
import collections import random class Card(): """A playing card that has a rank and suit""" # These are class variables. They are associated with the class, not instances of the class suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"] rank_names = ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"] short_rank_names = ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] rank_range = range(2, 15) def __init__(self, rank, suit): # These are instance variables. Each instance has their own rank an suit self.rank = rank self.suit = suit def __lt__(self, other): """For sorting and comparing by rank""" return self.rank < other.rank # self.rank is my rank. other.rank is the rank of another card def __gt__(self, other): """For sorting and comparing by rank""" return self.rank > other.rank def __eq__(self, other): """For sorting and comparing by rank""" return self.rank == other.rank def name(self): """Get long name for card""" return f"{self.rank_names[self.rank]} {self.suit}" def __repr__(self): """Get short name for card""" return f"{self.short_rank_names[self.rank]}{self.suit[0]}" class Hand(): """A list of cards""" def __init__(self, cards=None): self.cards = [] if cards is None else cards def ranks(self): """Get list of card ranks and their count. Use to identify pairs, 3 of a kind etc""" def sort_key(counter): """Sorting key function for (rank, count) tuples""" return (counter[1], counter[0]) items = list(collections.Counter([card.rank for card in self.cards]).items()) items.sort(key=sort_key, reverse=True) return items def suit(self, suit): """Get list of cards in specified suit""" ranks = [card.rank for card in self.cards if card.suit == suit] ranks.sort() return ranks def suits(self): """Get dictionary of cards grouped by suit. Use to identify flushes""" return {suit:self.suit(suit) for suit in Card.suit_names} def __repr__(self): """Print cards in hand""" return ", ".join([str(card) for card in self.cards]) class Deck(): """Deck of cards""" def __init__(self, shuffle=False): self.cards = [Card(rank, suit) for rank in Card.rank_range for suit in Card.suit_names] if shuffle: random.shuffle(self.cards) def __len__(self): """Return number of cards in deck""" return len(self.cards) def deal(self, count): """Deal count cards from top of deck""" cards = self.cards[:count] self.cards = self.cards[count:] return cards deck = Deck(True) hands = [Hand(deck.deal(5)) for _ in range(2)] for hand in hands: print("Hand", hand) print("Ranks", hand.ranks()) print("Suits", hand.suits())
Output:Hand 4C, 2D, 10D, 2S, 7D
Ranks [(2, 2), (10, 1), (7, 1), (4, 1)]
Suits {'Clubs': [4], 'Diamonds': [2, 7, 10], 'Hearts': [], 'Spades': [2]}
Hand QS, 8H, 5D, 6C, AS
Ranks [(14, 1), (12, 1), (8, 1), (6, 1), (5, 1)]
Suits {'Clubs': [6], 'Diamonds': [5], 'Hearts': [8], 'Spades': [12, 14]}