Николай обнови решението на 13.11.2022 21:59 (преди около 2 години)
+import random
+
+class Card:
+
+ def __init__(self, suit, face):
+ self.__suit = suit
Бих използвал само една долна черта. Моля виж становището ми тук: https://fmi.py-bg.net/tips/2
+ self.__face = face
+
+ def get_suit(self):
+ return self.__suit
+
+ def get_face(self):
+ return self.__face
+
+
+class Deck:
+
+ def __init__(self, face_filter = None):
+ card_faces = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
+ card_suits = ["clubs", "diamonds", "hearts", "spades"]
+ self.__cards = []
+ for suit in card_suits:
+ for card_face in card_faces:
+ if face_filter is not None and card_face not in face_filter:
+ continue
+ self.__cards.append(Card(suit, card_face))
+
+ def cut(self):
+ deck_card_count = len(self.__cards)
Доста завъртян начин да се напише, но работи, така че е ок. Когато се откючат останалите решения, бих те посъветвал да разгледаш и други имплементации.
+ cut_slice = slice(deck_card_count - random.randint(1, deck_card_count - 1), deck_card_count)
+ cards_to_cut = self.__cards[cut_slice]
+ del self.__cards[cut_slice]
+ self.__cards[0:0] = cards_to_cut
+
+ def shuffle(self):
+ random.shuffle(self.__cards)
+
+ def get_cards(self):
+ return self.__cards
+
+ def add_cards(self, cards):
+ for card in cards:
+ if self.__cards.count(card) == 0:
+ self.__cards.append(card)
+
+ def draw_cards(self, card_count):
+ drawn_cards = self.__cards[-card_count:]
+ del self.__cards[-card_count:]
+ return drawn_cards
+
+ def is_empty(self):
+ return len(self.__cards) == 0
+
+
+class Player:
+
+ def __init__(self):
+ self.__cards_in_hand = []
+
+ def add_cards(self, cards):
+ for card in cards:
+ if self.__cards_in_hand.count(card) == 0:
+ self.__cards_in_hand.append(card)
+
+ def clear_cards(self):
+ self.__cards_in_hand.clear()
+
+ def get_cards(self):
+ return self.__cards_in_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 = []
+ for i in range(0, self._number_of_players):
Ако няма да използваш дадена променлива, имнувай я _
, както е по конвенция.
Освен това range(0, x)
е същото като range(x)
.
+ self._players.append(Player())
+ self._deck = Deck()
+
+ def get_players(self):
+ return self._players
+
+ def prepare_deck(self):
+ for player in self._players:
+ self._deck.add_cards(player.get_cards())
+ player.clear_cards()
+ self._deck.shuffle()
+ self._deck.cut()
+
+ def deal(self, start_player):
+ start_player_idx = self._players.index(start_player)
+ for instruction in self._dealing_instructions:
+ for i in range(0, self._number_of_players):
+ cards_to_deal = self._deck.draw_cards(instruction)
+ player_idx = 0
Не мисля, че имаш нужда от този ред.
+ if self._dealing_direction == "rtl":
+ player_idx = (start_player_idx - i + self._number_of_players) % self._number_of_players
+ else:
+ player_idx = (start_player_idx + i) % self._number_of_players
+ self._players[player_idx].add_cards(cards_to_deal)
+
+ def get_deck(self):
+ return self._deck
+
+
+class Belot(Game):
+
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+ self._deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+
+
+class Poker(Game):
+
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))