Йоан обнови решението на 13.11.2022 16:49 (преди над 2 години)
+import random
+
+suits = ['clubs', 'diamonds', 'hearts', 'spades']
+faces = ['2', '3', '4', '5', '6', '7',
+ '8', '9', '10', 'J', 'Q', 'K', 'A']
+
+
+class Card:
+
+ def __init__(self, suit, face):
+ self._suit = suit
+ self._face = face
+
+ def __repr__(self):
+ return f'{self._face} of {self._suit}'
+
+ def get_suit(self):
+ return self._suit
+
+ def get_face(self):
+ return self._face
+
+
+class Deck:
+
+ def __init__(self, face_filter=faces):
+ self._face_filter = face_filter
+ self.cards = [Card(suit, face) for suit in suits for face in face_filter]
+
+ def cut(self):
+ random_section = random.randint(1, 51)
+ self.cards = self.cards[random_section:] + self.cards[:random_section]
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+
+class Player:
+
+ def __init__(self):
+ self.hand_dealt = []
+
+ def get_cards(self):
+ hand = self.hand_dealt.copy()
+ self.hand_dealt.clear()
+ return hand
+
+
+class Game:
+
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self._number_of_players = number_of_players
+ self._dealing_direction = dealing_direction
+ self._dealing_instructions = dealing_instructions
+ self._players = [Player() for _ in range(self._number_of_players)]
+ self._deck = Deck()
+
+ def get_players(self):
+ return self._players
+
+ def prepare_deck(self):
+ for player in self._players:
+ self._deck.cards.extend(player.get_cards())
+
+ self._deck.shuffle()
+ self._deck.cut()
+
+ def deal(self, player):
+ index = self._players.index(player)
+ players_count = len(self._players)
+
+ for times in self._dealing_instructions:
+ for _ in range(times):
+ player.hand_dealt.append(self._deck.cards.pop())
+
+ if self._dealing_direction == "rtl":
+ for i in range(index - 1, index - players_count, -1):
+ for times in self._dealing_instructions:
+ for _ in range(times):
+ self._players[i].hand_dealt.append(self._deck.cards.pop())
+
+ elif self._dealing_direction == "ltr":
+ for i in range(index + 1, index + players_count):
+ for times in self._dealing_instructions:
+ for _ in range(times):
+ self._players[i % players_count].hand_dealt.append(self._deck.cards.pop())
+ return
+
+ def get_deck(self):
+ return self._deck
+
+
+class Belot(Game):
+
+ def __init__(self):
+ super().__init__(4, "ltr", (2, 3, 3))
+ self._deck = Deck(faces[5:])
+
+
+class Poker(Game):
+
+ def __init__(self):
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))
Това бих опитал в вместя в логиката по-долу, защото и самата тя би станала по-проста, а и ще спестиш дублиране на код.