Advertisement
ifigazsi

Poker Game

Oct 24th, 2023 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.04 KB | Help | 0 0
  1. from random import shuffle
  2.  
  3. class Card:
  4.  
  5.     numbers = {11: "J", 12: "Q", 13: "K", 14: "A"}
  6.     suits = {"Spades": "♠", "Diamonds": "♦", "Hearts": "♥", "Clubs": "♣"}
  7.  
  8.     def __init__(self, number, suit):
  9.         self.number = number
  10.         self.suit = suit
  11.  
  12.     def __repr__(self):
  13.         number = Card.numbers.get(self.number, self.number)
  14.         suit = Card.suits.get(self.suit, self.suit)
  15.         return f"{number}{suit}"
  16.  
  17.     def __gt__(self, other):
  18.         return self.number > other.number
  19.  
  20.     def __len__(self):
  21.         return 1
  22.  
  23. class Deck:
  24.  
  25.     def __init__(self):
  26.         self.deck = []
  27.         self.create()
  28.         self.shuffle()
  29.  
  30.     def create(self):
  31.         suits = ["Spades", "Diamonds", "Clubs", "Hearts"]
  32.         for suit in suits:
  33.             for number in range(2, 15):
  34.                 self.deck.append(Card(number, suit))
  35.  
  36.     def shuffle(self):
  37.         shuffle(self.deck)
  38.  
  39.     def deal(self, no_of_cards):
  40.         cards = []
  41.         for i in range(no_of_cards):
  42.             card = self.deck.pop()
  43.             cards.append(card)
  44.         return cards
  45.  
  46. class Hand:
  47.  
  48.     def __init__(self):
  49.         self.cards = []
  50.         self.suits = None
  51.         self._cards_dict = None
  52.         self._cards_dict_same_suit = None
  53.  
  54.     def show_cards(self):
  55.         text = ""
  56.         for c in self.cards:
  57.             text += f"{c}, "
  58.         return text[:-2]
  59.  
  60.     def _count_suits(self):
  61.         if not self.suits:
  62.             self.suits = {}
  63.             for c in self.cards:
  64.                 self.suits[c.suit] = self.suits.get(c.suit, 0) + 1
  65.  
  66.     @property
  67.     def max_type_of_same_suit(self):
  68.         self._count_suits()
  69.         y = max([(v, k) for k, v in self.suits.items()])
  70.         return y[1]
  71.  
  72.     @property
  73.     def cards_dict(self):
  74.         if self._cards_dict is None:
  75.             self._cards_dict = {}
  76.             for i in range(2, 15):
  77.                 self._cards_dict[i] = []
  78.             for c in self.cards:
  79.                 self._cards_dict[c.number].append(c)
  80.         return self._cards_dict
  81.  
  82.     @property
  83.     def cards_dict_same_suit(self):
  84.         if self._cards_dict_same_suit is None:
  85.             self._cards_dict_same_suit = {}
  86.             for i in range(2, 15):
  87.                 self._cards_dict_same_suit[i] = []
  88.             for k, v in self._cards_dict.items():
  89.                 for c in v:
  90.                     if c.suit == self.max_type_of_same_suit:
  91.                         self._cards_dict_same_suit[k] = c
  92.         return self._cards_dict_same_suit
  93.  
  94.  
  95. class Rule:
  96.  
  97.     def __init__(self, cards, same_suit_cards):
  98.         self.cards = cards
  99.         self.same_suit_cards = same_suit_cards
  100.  
  101.     def check_one_pair(self):
  102.         count = 0
  103.         for i in range(2, 15):
  104.             if len(self.cards[i]) == 2:
  105.                 count += 1
  106.         return count == 1
  107.  
  108.     def check_two_pairs(self):
  109.         count = 0
  110.         for i in range(2, 15):
  111.             if len(self.cards[i]) == 2:
  112.                 count += 1
  113.         return count >= 2
  114.  
  115.     def check_three_of_a_kind(self):
  116.         count = 0
  117.         for i in range(2, 15):
  118.             if len(self.cards[i]) == 3:
  119.                 count += 1
  120.         return count >= 1
  121.  
  122.     def check_straight(self):
  123.         count = 0
  124.         for i in range(2, 15):
  125.             count += 1
  126.             if len(self.cards[i]) == 0:
  127.                 count = 0
  128.             if count == 5:
  129.                 return True
  130.         return False
  131.  
  132.     def check_flush(self):
  133.         return (sum([len(x) for x in self.same_suit_cards.values()])) >= 5
  134.  
  135.     def check_full_house(self):
  136.         return (self.check_three_of_a_kind()) and (self.check_one_pair() or self.check_two_pairs())
  137.  
  138.     def check_four_of_a_kind(self):
  139.         count = 0
  140.         for i in range(2, 15):
  141.             if len(self.cards[i]) == 4:
  142.                 count += 1
  143.         return count == 1
  144.  
  145.     def check_straight_flush(self):
  146.         count = 0
  147.         for i in range(2, 15):
  148.             count += 1
  149.             if len(self.same_suit_cards[i]) == 0:
  150.                 count = 0
  151.             if count == 5:
  152.                 return True
  153.         return False
  154.  
  155.     def check_royal_flush(self):
  156.         for i in range(10, 15):
  157.             if len(self.same_suit_cards[i]) != 1:
  158.                 return False
  159.         return True
  160.  
  161. hand = Hand()
  162. deck = Deck()
  163. #hand.cards = deck.deal(7)
  164. x = []
  165. x.append(Card(7, "Diamonds"))
  166. x.append(Card(9, "Diamonds"))
  167. x.append(Card(10, "Diamonds"))
  168. x.append(Card(5, "Diamonds"))
  169. x.append(Card(3, "Clubs"))
  170. x.append(Card(11, "Spades"))
  171. x.append(Card(11, "Diamonds"))
  172. hand.cards = x
  173. print(f"Kartyak: {hand.show_cards()}")
  174. rules = Rule(hand.cards_dict, hand.cards_dict_same_suit)
  175. print("cards_dict:")
  176. print(hand.cards_dict)
  177. print("cards_dict_same_suit:")
  178. print(hand.cards_dict_same_suit)
  179. print(f"Van-e pár: {rules.check_one_pair()}")
  180. print(f"Van-e két pár: {rules.check_two_pairs()}")
  181. print(f"Van-e drill: {rules.check_three_of_a_kind()}")
  182. print(f"Van-e full: {rules.check_full_house()}")
  183. print(f"Van-e royalf: {rules.check_royal_flush()}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement