Решение на От ливадите до Лас Вегас (и назад) от Николай Натов

Обратно към всички решения

Към профила на Николай Натов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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 = 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):
cut_count = random.randint(1, len(self._cards) - 1)
for _ in range(cut_count):
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):
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 _ in range(self._number_of_players):
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)
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))

Лог от изпълнението

...............
----------------------------------------------------------------------
Ran 15 tests in 0.159s

OK

История (3 версии и 4 коментара)

Николай обнови решението на 13.11.2022 21:59 (преди около 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 = 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):
+ 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))

Николай обнови решението на 14.11.2022 13:17 (преди около 2 години)

import random
class Card:
def __init__(self, suit, face):
- self.__suit = suit
- self.__face = face
+ self._suit = suit
+ self._face = face
def get_suit(self):
- return self.__suit
+ return self._suit
def get_face(self):
- return self.__face
+ 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
+ cut_count = random.randint(1, len(self.__cards) - 1)
+ for _ in range(cut_count):
+ 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):
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):
+ for _ in range(self._number_of_players):
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))

Николай обнови решението на 14.11.2022 13:19 (преди около 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 = None):
card_faces = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
card_suits = ["clubs", "diamonds", "hearts", "spades"]
- self.__cards = []
+ 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))
+ self._cards.append(Card(suit, card_face))
def cut(self):
- cut_count = random.randint(1, len(self.__cards) - 1)
+ cut_count = random.randint(1, len(self._cards) - 1)
for _ in range(cut_count):
- self.__cards.insert(0, self.__cards.pop())
+ self._cards.insert(0, self._cards.pop())
def shuffle(self):
- random.shuffle(self.__cards)
+ random.shuffle(self._cards)
def get_cards(self):
- return self.__cards
+ return self._cards
def add_cards(self, cards):
for card in cards:
- if self.__cards.count(card) == 0:
- self.__cards.append(card)
+ 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:]
+ drawn_cards = self._cards[-card_count:]
+ del self._cards[-card_count:]
return drawn_cards
def is_empty(self):
- return len(self.__cards) == 0
+ return len(self._cards) == 0
class Player:
def __init__(self):
- self.__cards_in_hand = []
+ 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)
+ if self._cards_in_hand.count(card) == 0:
+ self._cards_in_hand.append(card)
def clear_cards(self):
- self.__cards_in_hand.clear()
+ self._cards_in_hand.clear()
def get_cards(self):
- return self.__cards_in_hand
+ 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 _ in range(self._number_of_players):
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)
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))