Александра обнови решението на 15.11.2022 16:03 (преди около 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 get_suit(self):
+ return self.suit
+
+ def get_face(self):
+ return self.face
+
+ #def print_card(self):
+ # print("{},{}".format(self.suit,self.face))
+
+class Deck:
+ def __init__(self, _face_filter=faces):
+ self.face_filter = _face_filter
Не е нужно да слагаш долна черта. Едното е пропърти на иснтанцията на класа, а другото аргумент на инициализатора. Няма да има конфликт на имената.
+ self.cards = []
+ self.create_deck()
+
+ def create_deck(self):
+ for suit in suits:
+ for face in faces:
+ if face in self.face_filter:
+ self.cards.append(Card(suit,face))
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+ def get_face_filter(self):
+ return self.face_filter
+
+ def cut(self):
+ cut_card=self.cards[-1]
Липсват интервали около равното.
+ self.cards.pop()
+ self.cards.insert(0,cut_card)
Липсва интервал след запетаята.
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def draw_card(self):
+ self.cards.pop()
+
+ def print_deck(self):
+ for card in self.cards:
+ card.print_card()
+
+class Player:
+ def __init__(self):
+ self.owned_cards = []
+
+ def get_cards(self):
+ return self.owned_cards
+
+ def add_card_to_hand(self, card):
+ self.owned_cards.append(card)
+
+ def clear_hand(self):
+ self.owned_cards.clear()
+
+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 = []
+ self.add_players()
+
+ def add_players(self):
+ counter = 0
Ако напишеш for
няма да трябва ръчно да инкрементираш брояча.
+ while counter < self.number_of_players:
+ self.players.append(Player())
+ counter+=1
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ player_cards = player.get_cards()
+ for player_card in player_cards:
+ self.deck.add_card(player_card)
+ player.clear_hand()
+ self.deck.cut()
+ self.deck.shuffle()
+
+ def deal(self, player):
+ for instruction in self.dealing_instructions:
+ player_counter = 0
+ index = self.players.index(player)
+ while player_counter < len(self.players):
+ card_counter = 0
+ while instruction > card_counter:
+ self.players[index].add_card_to_hand(self.deck.get_cards()[-1])
+ self.deck.draw_card()
+ card_counter+=1
+ if self.dealing_direction == "ltr":
+ if index == len(self.players) - 1 :
+ index = -1
+ index+=1
+ else: index-=1
+ player_counter+=1
Дори при +=
и подобни, около +=
се слагат интервали.
+
+ 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))