Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import shuffle
- class Card:
- numbers = {11: "J", 12: "Q", 13: "K", 14: "A"}
- suits = {"Spades": "♠", "Diamonds": "♦", "Hearts": "♥", "Clubs": "♣"}
- def __init__(self, number, suit):
- self.number = number
- self.suit = suit
- def __repr__(self):
- number = Card.numbers.get(self.number, self.number)
- suit = Card.suits.get(self.suit, self.suit)
- return f"{number}{suit}"
- def __gt__(self, other):
- return self.number > other.number
- def __len__(self):
- return 1
- class Deck:
- def __init__(self):
- self.deck = []
- self.create()
- self.shuffle()
- def create(self):
- suits = ["Spades", "Diamonds", "Clubs", "Hearts"]
- for suit in suits:
- for number in range(2, 15):
- self.deck.append(Card(number, suit))
- def shuffle(self):
- shuffle(self.deck)
- def deal(self, no_of_cards):
- cards = []
- for i in range(no_of_cards):
- card = self.deck.pop()
- cards.append(card)
- return cards
- class Hand:
- def __init__(self):
- self.cards = []
- self.suits = None
- self._cards_dict = None
- self._cards_dict_same_suit = None
- def show_cards(self):
- text = ""
- for c in self.cards:
- text += f"{c}, "
- return text[:-2]
- def _count_suits(self):
- if not self.suits:
- self.suits = {}
- for c in self.cards:
- self.suits[c.suit] = self.suits.get(c.suit, 0) + 1
- @property
- def max_type_of_same_suit(self):
- self._count_suits()
- y = max([(v, k) for k, v in self.suits.items()])
- return y[1]
- @property
- def cards_dict(self):
- if self._cards_dict is None:
- self._cards_dict = {}
- for i in range(2, 15):
- self._cards_dict[i] = []
- for c in self.cards:
- self._cards_dict[c.number].append(c)
- return self._cards_dict
- @property
- def cards_dict_same_suit(self):
- if self._cards_dict_same_suit is None:
- self._cards_dict_same_suit = {}
- for i in range(2, 15):
- self._cards_dict_same_suit[i] = []
- for k, v in self._cards_dict.items():
- for c in v:
- if c.suit == self.max_type_of_same_suit:
- self._cards_dict_same_suit[k] = c
- return self._cards_dict_same_suit
- class Rule:
- def __init__(self, cards, same_suit_cards):
- self.cards = cards
- self.same_suit_cards = same_suit_cards
- def check_one_pair(self):
- count = 0
- for i in range(2, 15):
- if len(self.cards[i]) == 2:
- count += 1
- return count == 1
- def check_two_pairs(self):
- count = 0
- for i in range(2, 15):
- if len(self.cards[i]) == 2:
- count += 1
- return count >= 2
- def check_three_of_a_kind(self):
- count = 0
- for i in range(2, 15):
- if len(self.cards[i]) == 3:
- count += 1
- return count >= 1
- def check_straight(self):
- count = 0
- for i in range(2, 15):
- count += 1
- if len(self.cards[i]) == 0:
- count = 0
- if count == 5:
- return True
- return False
- def check_flush(self):
- return (sum([len(x) for x in self.same_suit_cards.values()])) >= 5
- def check_full_house(self):
- return (self.check_three_of_a_kind()) and (self.check_one_pair() or self.check_two_pairs())
- def check_four_of_a_kind(self):
- count = 0
- for i in range(2, 15):
- if len(self.cards[i]) == 4:
- count += 1
- return count == 1
- def check_straight_flush(self):
- count = 0
- for i in range(2, 15):
- count += 1
- if len(self.same_suit_cards[i]) == 0:
- count = 0
- if count == 5:
- return True
- return False
- def check_royal_flush(self):
- for i in range(10, 15):
- if len(self.same_suit_cards[i]) != 1:
- return False
- return True
- hand = Hand()
- deck = Deck()
- #hand.cards = deck.deal(7)
- x = []
- x.append(Card(7, "Diamonds"))
- x.append(Card(9, "Diamonds"))
- x.append(Card(10, "Diamonds"))
- x.append(Card(5, "Diamonds"))
- x.append(Card(3, "Clubs"))
- x.append(Card(11, "Spades"))
- x.append(Card(11, "Diamonds"))
- hand.cards = x
- print(f"Kartyak: {hand.show_cards()}")
- rules = Rule(hand.cards_dict, hand.cards_dict_same_suit)
- print("cards_dict:")
- print(hand.cards_dict)
- print("cards_dict_same_suit:")
- print(hand.cards_dict_same_suit)
- print(f"Van-e pár: {rules.check_one_pair()}")
- print(f"Van-e két pár: {rules.check_two_pairs()}")
- print(f"Van-e drill: {rules.check_three_of_a_kind()}")
- print(f"Van-e full: {rules.check_full_house()}")
- print(f"Van-e royalf: {rules.check_royal_flush()}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement