Георги обнови решението на 03.11.2022 15:51 (преди над 2 години)
+import random
+
+
+class Card:
+ """Playing card."""
+
+ def __init__(self, suit, face):
+ self._suit = suit
+ self._face = face
+
+ def get_suit(self):
+ """Get card's suit value."""
+ return self._suit
+
+ def get_face(self):
+ """Get card's face value."""
+ return self._face
+
+
+class Deck:
+ """Deck of playing cards."""
+
+ SUITS = ('clubs', 'diamonds', 'hearts', 'spades')
+ FACES = list(map(str, range(2, 11))) + ['J', 'Q', 'K', 'A']
+
+ def __init__(self, face_filter=None):
+ """Initializator."""
+ self.cards = []
+ faces = [x for x in self.FACES if not face_filter or x in face_filter]
+ for suit in self.SUITS:
+ for face in faces:
+ self.cards.append(Card(suit, face))
+
+ def cut(self):
+ '''Cut the deck.'''
+ # A valid cut contains at least one moved card, hence the ones below
+ index = random.randrange(1, len(self.cards) - 1)
+ self.cards = self.cards[index:] + self.cards[:index]
+
+ def shuffle(self):
+ '''Shuffle the deck.'''
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ '''Return the list of cards.'''
+ return self.cards
+
+
+class Player:
+ """Card player."""
+
+ def __init__(self):
+ """Initializator."""
+ self.cards = []
+
+ def get_cards(self):
+ """Return the list of cards that the player holds."""
+ return self.cards
+
+
+class Game:
+ """A game played with cards."""
+
+ def __init__(self, number_of_players, dealing_direction,
+ dealing_instructions):
+ """Initializator."""
+ self._number_of_players = number_of_players
+ self._dealing_direction = dealing_direction
+ self._dealing_instructions = dealing_instructions
+ self._deck = Deck()
+ self._players = []
+ for player in range(number_of_players):
+ self._players.append(Player())
+
+ def _get_players_dealing_flow(self, player):
+ """Get players in order ready for dealing, starting with player."""
+ player_order_step = 1 if self._dealing_direction == 'ltr' else -1
+ ordered_players = self._players[::player_order_step]
+ index = ordered_players.index(player)
+ return ordered_players[index:] + ordered_players[:index]
+
+ def _collect_cards_from_players(self):
+ """Collect the cards from all players."""
+ for player in self._players:
+ self._deck.cards.extend(player.cards)
+ player.cards = []
+
+ def get_players(self):
+ """Return the list of players, playing the game."""
+ return self._players
+
+ def prepare_deck(self):
+ """Prepare the deck for dealing."""
+ self._collect_cards_from_players()
+ self._deck.shuffle()
+ self._deck.cut()
+
+ def deal(self, player):
+ """Deal the cards to the players."""
+ player_flow = self._get_players_dealing_flow(player)
+ for card_count in self._dealing_instructions:
+ for player in player_flow:
+ player.cards.extend(self._deck.cards[:card_count])
+ self._deck.cards = self._deck.cards[card_count:]
+
+ def get_deck(self):
+ """Return the Deck used for the game."""
+ return self._deck
+
+
+class Belot(Game):
+ """Belot card game."""
+
+ def __init__(self):
+ """Initializator."""
+ super().__init__(4, 'ltr', (2, 3, 3))
+ self._deck = Deck(face_filter=('7', '8', '9', '10',
+ 'J', 'Q', 'K', 'A'))
+
+
+class Poker(Game):
+ """Poker card game."""
+
+ def __init__(self):
+ """Initializator."""
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))