Антоан обнови решението на 15.11.2022 17:05 (преди над 2 години)
+import random
+
+
+class Card:
+
+ def __init__(self, suit, face):
+ self._suit = suit
+ self._face = face
+
+ def get_suit(self):
+ return self._suit
+
+ def get_face(self):
+ return self._face
+
+
+all_suits_collection = ('clubs', 'diamonds', 'hearts', 'spades')
+all_faces_collection = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')
+
+
+def generate_cards_based_on_face(face): # Generates single face all suit cards
Този и следващия метод бих сложил в класа Deck
, било то и като staticmethod
-и.
+ all_cards_of_face = []
+ for curr_suit in all_suits_collection:
+ all_cards_of_face.append(Card(curr_suit, face))
+ return all_cards_of_face
+
+
+def generate_cards_based_on_filter(face_filter): # Generates all faces given in filter to all suit cards
+ all_game_cards = []
+ for curr_face in face_filter:
+ all_game_cards.extend(generate_cards_based_on_face(curr_face))
+ return all_game_cards
+
+
+class Deck:
+ def __init__(self, face_filter=None):
+ self.face_filter = face_filter if face_filter is not None else all_faces_collection
+ self.cards = generate_cards_based_on_filter(self.face_filter)
+
+ def cut(self):
+ # Picks a random number of cards - One taken is the minimum and all but one is the max which can be shuffled
+ taken_cards_count = random.randint(1, len(self.cards) - 1) # Minimum of cards deck is 4
+ while taken_cards_count > 0:
Ако използваш for
, ще си спетиш ръчно инкрементиране на брояча.
+ self.cards.append(self.cards[0]) # Puts a card at the back and remove it from the front
+ self.cards.pop(0)
+ taken_cards_count -= 1
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def __getitem__(self, index):
+ return self.cards[index]
+
+ def remove_card(self):
+ self.cards.pop(0)
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+ def add_hand(self, hand):
+ self.cards.extend(hand)
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+ def remove_card(self):
+ self.cards.pop(0)
+
+ def clear_hand(self):
+ self.cards.clear()
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.number_of_players = number_of_players
+ self.players = [Player() for _i in range(number_of_players)]
Това i
явно е сложено погрешка.
+ self.deck = Deck() # Creates a valid deck ready for play
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+
+ def get_players(self):
+ return self.players
+
+ def add_card_to_player(self, player_index):
+ curr_card = self.deck[0] # Takes the first card of the deck
+ self.players[player_index].add_card(curr_card) # Adds it to the player
+ self.deck.remove_card()
+
+ def remove_cards_from_player(self, player_index):
+ player_hand = self.players[player_index].get_cards() # Takes the whole person card hand
+ self.deck.add_hand(player_hand) # Add hand must be before clear, otherwise there will be card loss
+ self.players[player_index].clear_hand() # Clears it out from the person
+
+ def collect_all_cards_from_players(self):
+ curr_player_index = 0
+ while curr_player_index < len(self.players):
+ self.remove_cards_from_player(curr_player_index)
+ curr_player_index += 1
+
+ def prepare_deck(self):
+ self.collect_all_cards_from_players()
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def complete_player_round_instruction(self, instruction, player_index):
+ given_cards = 0
+ while given_cards < instruction:
+ self.add_card_to_player(player_index)
+ given_cards += 1
+
+ def complete_whole_round_instruction(self, instruction, curr_player):
+ number_of_player_given_hand = 0
+ while number_of_player_given_hand < self.number_of_players:
+ self.complete_player_round_instruction(instruction, curr_player)
+ if self.dealing_direction == 'ltr':
+ curr_player += 1
+ if curr_player == self.number_of_players: # If we have reached the end, start at front
+ curr_player = 0
+ else: # dealing direction == 'rtl'
+ curr_player -= 1
+ if curr_player == -1: # If we have reached before the front, start at the end
+ curr_player = self.number_of_players - 1 # self.number_of_players is not a valid index
+ number_of_player_given_hand += 1
+
+ def deal(self, starting_player):
+ curr_player = self.players.index(starting_player)
+ for instruction in self.dealing_instructions:
+ self.complete_whole_round_instruction(instruction, curr_player)
+
+ 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']) # Updates the deck with this 'new' filter
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1)) # No deck filter needed as the whole patch of cards is played
Харесва ми колко модуларно си дефинирал функционалността си. Дори методи, които не изискват кой знае колко действия, си разделил на по-малки парчета. Поради тази причина получаваш бонус точка.
Този и следващия метод бих сложил в класа
Deck
, било то и катоstaticmethod
-и.Ако използваш
for
, ще си спетиш ръчно инкрементиране на брояча.Това
i
явно е сложено погрешка.