Роберт обнови решението на 13.11.2022 17:33 (преди около 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
+
+
+class Deck:
+ def __init__(self, face_filter=[]):
+ types = [str(x) for x in range(2, 11)]
+ types.extend(['J', 'Q', 'K', 'A'])
+ if face_filter:
+ self.cards = [Card(x, y) for x in face_filter for y in ['clubs', 'diamonds', 'hearts', 'spades']]
+ else:
+ self.cards = [Card(x, y) for x in types for y in ['clubs', 'spades', 'diamonds', 'hearts']]
+
+ def cut(self):
+ for a in range(random.randint(1, len(self.cards) - 1)):
+ self.cards.insert(0, self.cards.pop())
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def add_cards(self, cards):
+ self.cards.extend(cards)
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+ def draw_card(self):
+ if self.cards:
+ return self.cards.pop()
+
+
+class Player:
+ def __init__(self):
+ self.hand = []
+
+ def get_cards(self):
+ return self.hand
+
+ def clear_hand(self):
+ self.hand.clear()
+
+ def add_card_to_hand(self, card):
+ self.hand.append(card)
+
+ def add_cards_to_hand(self, cards):
+ self.hand.extend(cards)
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions, face_filter=[]):
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+ self.players = [Player() for i in range(self.number_of_players)]
+ self.deck = Deck(face_filter)
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for current_player in self.players:
+ self.deck.add_cards(current_player.get_cards())
+ current_player.clear_hand()
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, player_search):
+ given_player = -1
+ for current_player in range(self.number_of_players):
+ if self.players[current_player] is player_search:
+ given_player = current_player
+ break
+ if given_player == -1:
+ return
+ order = []
+ if self.dealing_direction == 'ltr':
+ order = [x for x in range(given_player, self.number_of_players)]
+ order.extend([x for x in range(0, given_player)])
+ elif self.dealing_direction == 'rtl':
+ order = [x for x in range(given_player, -1, -1)]
+ order.extend([x for x in range(len(self.players) - 1, given_player, -1)])
+ for instruction in self.dealing_instructions:
+ for current_player in order:
+ self.players[current_player].add_cards_to_hand([self.deck.draw_card() for card in range(instruction)])
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3), ['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
self.players.index(player_serach)
ще ти даде индекса, който търсиш.