Ангел обнови решението на 15.11.2022 16:18 (преди около 2 години)
+import random
+
+
+class Card:
+
+ def __init__(self, suit, face):
+ self.suit = suit
Бих използвал protected имена на атрибути като тези, които не използваш извън класа - self._suit
+ self.face = face
+
+ def get_suit(self):
+ return self.suit
+
+ def get_face(self):
+ return self.face
+
+
+BELOT_PLAYERS = 4
+BELOT_DEAL = "ltr"
+POKER_PLAYERS = 9
+POKER_DEAL = "rtl"
+
+belot_suits = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+
+default_deck = [
+ Card('clubs', 'A'),
Това плаче са цикъл :(
+ Card('diamonds', 'A'),
+ Card('hearts', 'A'),
+ Card('spades', 'A'),
+
+ Card('clubs', '2'),
+ Card('diamonds', '2'),
+ Card('hearts', '2'),
+ Card('spades', '2'),
+
+ Card('clubs', '3'),
+ Card('diamonds', '3'),
+ Card('hearts', '3'),
+ Card('spades', '3'),
+
+ Card('clubs', '4'),
+ Card('diamonds', '4'),
+ Card('hearts', '4'),
+ Card('spades', '4'),
+
+ Card('clubs', '5'),
+ Card('diamonds', '5'),
+ Card('hearts', '5'),
+ Card('spades', '5'),
+
+ Card('clubs', '6'),
+ Card('diamonds', '6'),
+ Card('hearts', '6'),
+ Card('spades', '6'),
+
+ Card('clubs', '7'),
+ Card('diamonds', '7'),
+ Card('hearts', '7'),
+ Card('spades', '7'),
+
+ Card('clubs', '8'),
+ Card('diamonds', '8'),
+ Card('hearts', '8'),
+ Card('spades', '8'),
+
+ Card('clubs', '9'),
+ Card('diamonds', '9'),
+ Card('hearts', '9'),
+ Card('spades', '9'),
+
+ Card('clubs', '10'),
+ Card('diamonds', '10'),
+ Card('hearts', '10'),
+ Card('spades', '10'),
+
+ Card('clubs', 'J'),
+ Card('diamonds', 'J'),
+ Card('hearts', 'J'),
+ Card('spades', 'J'),
+
+ Card('clubs', 'Q'),
+ Card('diamonds', 'Q'),
+ Card('hearts', 'Q'),
+ Card('spades', 'Q'),
+
+ Card('clubs', 'K'),
+ Card('diamonds', 'K'),
+ Card('hearts', 'K'),
+ Card('spades', 'K'),
+]
+
+
+class Deck:
+ def __init__(self, face_filter=None):
+ if face_filter == None:
Когато сравняваш с None
винаги се използва is
: if face_filter is None
+ self.deck = default_deck
+ else:
+ self.deck = []
+ for suit in face_filter:
+ self.deck.append(Card(suit, 'clubs'))
+ self.deck.append(Card(suit, 'spades'))
+ self.deck.append(Card(suit, 'diamonds'))
+ self.deck.append(Card(suit, 'hearts'))
+
+ def cut(self):
+ cut_lenght = random.randint(1, len(self.deck))
+
+ cutted = self.deck[cut_lenght:len(self.deck):1]
Стъпката :1
в края е излишна.
+ del self.deck[-(len(self.deck) - cut_lenght):]
+
+ cutted.reverse()
+
+ for card in cutted:
+ self.deck.insert(0, card)
+
+ def shuffle(self):
+ random.shuffle(self.deck)
+
+ def get_cards(self):
+ return self.deck
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+
+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.deck = Deck()
+ self.players = [Player() for _ in range(number_of_players)]
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ cards = player.get_cards()
+
+ self.deck.get_cards().extend(cards)
+ player.cards.clear()
+
+ self.deck.cut()
+ self.deck.shuffle()
+
+ def deal(self, player):
+ start_idx = self.players.index(player)
+ first_half = self.get_players()[:start_idx:1]
+ second_half = self.get_players()[start_idx::1]
+ players = second_half + first_half
+
+ if self.dealing_direction == "rtl":
+ players = players[:1:1] + players[1::1][::-1]
+
+ for instruction in self.dealing_instructions:
+ for player in players:
+ player.get_cards().extend(self.deck.get_cards()[:instruction])
+
+ del self.deck.get_cards()[:instruction]
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(BELOT_PLAYERS, BELOT_DEAL, (2, 3, 3))
+
+ self.deck = Deck(belot_suits)
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(POKER_PLAYERS, POKER_DEAL, (1, 1, 1, 1, 1))
Бих използвал protected имена на атрибути като тези, които не използваш извън класа -
self._suit
Това плаче са цикъл :(
Когато сравняваш с
None
винаги се използваis
:if face_filter is None
Стъпката
:1
в края е излишна.