Решение на От ливадите до Лас Вегас (и назад) от Георги Събев

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

Към профила на Георги Събев

Резултати

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

Код

import random
SUITS = [
'clubs',
'diamonds',
'hearts',
'spades'
]
DEFAULT_FACES = [
'2', '3', '4', '5',
'6', '7', '8', '9',
'10', 'J', 'Q', 'K', 'A'
]
BELOT_FACES = [
'7', '8', '9', '10',
'J', 'Q', 'K', 'A'
]
def shifted_list(elements, shifts):
return elements[shifts:] + elements[:shifts]
def reversed_direction(elements):
return elements[:1] + elements[1:][::-1]
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=DEFAULT_FACES):
self._cards = [
Card(suit, face) for suit in SUITS for face in face_filter
]
def cut(self):
if not self._cards:
return
cutting_point = random.randrange(1, len(self._cards))
self._cards = self._cards[cutting_point:] + self._cards[:cutting_point]
def shuffle(self):
random.shuffle(self._cards)
def collect_cards(self, cards_to_collect):
self._cards.extend(cards_to_collect)
def get_cards(self):
return self._cards
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
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 = [Player() for _ in range(number_of_players)]
self._deck = Deck()
def get_players(self):
return self._players
def get_deck(self):
return self._deck
def deal(self, starting_player):
starting_index = self._players.index(starting_player)
ordered_players = shifted_list(self._players, starting_index)
if self._dealing_direction != 'ltr':
ordered_players = reversed_direction(ordered_players)
for instruction in self._dealing_instructions:
for player in ordered_players:
dealt_cards = self._deck.get_cards()[:instruction]
player.get_cards().extend(dealt_cards)
for _ in range(instruction):
self._deck.get_cards().pop(0)
def prepare_deck(self):
for player in self._players:
cards = player.get_cards()
self._deck.collect_cards(cards)
player.get_cards().clear()
self._deck.cut()
self._deck.shuffle()
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self._deck = Deck(BELOT_FACES)
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.181s

OK

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

Георги обнови решението на 14.11.2022 22:55 (преди около 2 години)

+import random
+
+SUITS = [
+ 'clubs',
+ 'diamonds',
+ 'hearts',
+ 'spades'
+]
+
+DEFAULT_FACES = [
+ '2', '3', '4', '5',
+ '6', '7', '8', '9',
+ '10', 'J', 'Q', 'K', 'A'
+]
+
+BELOT_FACES = [
+ '7', '8', '9', '10',
+ 'J', 'Q', 'K', 'A'
+]
+
+
+def shifted_list(elements, shifts):
+ return elements[shifts:] + elements[:shifts]
+
+
+def reversed_list(elements):
+ return elements[:1] + elements[1:][::-1]
+
+
+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=DEFAULT_FACES):
+ self.cards = [
+ Card(suit, face) for suit in SUITS for face in face_filter
+ ]
+
+ def cut(self):
+ cutting_point = random.randrange(1, len(self.cards))
+ self.cards = self.cards[cutting_point:] + self.cards[:cutting_point]
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+
+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 = [Player() for _ in range(number_of_players)]
+ self.deck = Deck()
+
+ def get_players(self):
+ return self.players
+
+ def get_deck(self):
+ return self.deck
+
+ def deal(self, starting_player):
+ starting_index = self.players.index(starting_player)
+ ordered_players = shifted_list(self.get_players(), starting_index)
+
+ if self.dealing_direction != 'ltr':
+ ordered_players = reversed_list(ordered_players)
+
+ for instruction in self.dealing_instructions:
+ for player in ordered_players:
+ dealt_cards = self.deck.get_cards()[:instruction]
+
+ player.get_cards().extend(dealt_cards)
+
+ for _ in range(instruction):
+ self.deck.get_cards().pop(0)
+
+ def prepare_deck(self):
+ for player in self.players:
+ cards = player.cards
+
+ self.deck.get_cards().extend(cards)
+ player.cards.clear()
+
+ self.deck.cut()
+ self.deck.shuffle()
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+
+ self.deck = Deck(BELOT_FACES)
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Георги обнови решението на 15.11.2022 09:20 (преди около 2 години)

import random
SUITS = [
'clubs',
'diamonds',
'hearts',
'spades'
]
DEFAULT_FACES = [
'2', '3', '4', '5',
'6', '7', '8', '9',
'10', 'J', 'Q', 'K', 'A'
]
BELOT_FACES = [
'7', '8', '9', '10',
'J', 'Q', 'K', 'A'
]
def shifted_list(elements, shifts):
return elements[shifts:] + elements[:shifts]
def reversed_list(elements):

Разбирам защо имаш нужда от индекс 1, но ако ще го правиш така, името не е много логично, защото не просто обръщаш реда, а и запазваш първия елемент.

return elements[:1] + elements[1:][::-1]
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=DEFAULT_FACES):
self.cards = [
Card(suit, face) for suit in SUITS for face in face_filter
]
def cut(self):
+ if not self.cards:
+ return
+
cutting_point = random.randrange(1, len(self.cards))
self.cards = self.cards[cutting_point:] + self.cards[:cutting_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
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 = [Player() for _ in range(number_of_players)]
self.deck = Deck()
def get_players(self):
return self.players
def get_deck(self):
return self.deck
def deal(self, starting_player):
starting_index = self.players.index(starting_player)
ordered_players = shifted_list(self.get_players(), starting_index)
if self.dealing_direction != 'ltr':
ordered_players = reversed_list(ordered_players)
for instruction in self.dealing_instructions:
for player in ordered_players:
dealt_cards = self.deck.get_cards()[:instruction]
player.get_cards().extend(dealt_cards)
for _ in range(instruction):
self.deck.get_cards().pop(0)
def prepare_deck(self):
for player in self.players:
cards = player.cards
self.deck.get_cards().extend(cards)
player.cards.clear()
self.deck.cut()
self.deck.shuffle()
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self.deck = Deck(BELOT_FACES)
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Георги обнови решението на 15.11.2022 12:56 (преди около 2 години)

import random
SUITS = [
'clubs',
'diamonds',
'hearts',
'spades'
]
DEFAULT_FACES = [
'2', '3', '4', '5',
'6', '7', '8', '9',
'10', 'J', 'Q', 'K', 'A'
]
BELOT_FACES = [
'7', '8', '9', '10',
'J', 'Q', 'K', 'A'
]
def shifted_list(elements, shifts):
return elements[shifts:] + elements[:shifts]
-def reversed_list(elements):
+def reversed_direction(elements):
return elements[:1] + elements[1:][::-1]
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=DEFAULT_FACES):
- self.cards = [
+ self._cards = [
Card(suit, face) for suit in SUITS for face in face_filter
]
def cut(self):
- if not self.cards:
+ if not self._cards:
return
- cutting_point = random.randrange(1, len(self.cards))
- self.cards = self.cards[cutting_point:] + self.cards[:cutting_point]
+ cutting_point = random.randrange(1, len(self._cards))
+ self._cards = self._cards[cutting_point:] + self._cards[:cutting_point]
def shuffle(self):
- random.shuffle(self.cards)
+ random.shuffle(self._cards)
+ def collect_cards(self, cards_to_collect):
+ self._cards.extend(cards_to_collect)
+
def get_cards(self):
- return self.cards
+ return self._cards
class Player:
def __init__(self):
- self.cards = []
+ self._cards = []
def get_cards(self):
- return self.cards
+ return self._cards
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._number_of_players = number_of_players
+ self._dealing_direction = dealing_direction
+ self._dealing_instructions = dealing_instructions
- self.players = [Player() for _ in range(number_of_players)]
- self.deck = Deck()
+ self._players = [Player() for _ in range(number_of_players)]
+ self._deck = Deck()
def get_players(self):
- return self.players
+ return self._players
def get_deck(self):
- return self.deck
+ return self._deck
def deal(self, starting_player):
- starting_index = self.players.index(starting_player)
- ordered_players = shifted_list(self.get_players(), starting_index)
+ starting_index = self._players.index(starting_player)
+ ordered_players = shifted_list(self._players, starting_index)
- if self.dealing_direction != 'ltr':
- ordered_players = reversed_list(ordered_players)
+ if self._dealing_direction != 'ltr':
+ ordered_players = reversed_direction(ordered_players)
- for instruction in self.dealing_instructions:
+ for instruction in self._dealing_instructions:
for player in ordered_players:
- dealt_cards = self.deck.get_cards()[:instruction]
+ dealt_cards = self._deck.get_cards()[:instruction]
player.get_cards().extend(dealt_cards)
for _ in range(instruction):
- self.deck.get_cards().pop(0)
+ self._deck.get_cards().pop(0)
def prepare_deck(self):
- for player in self.players:
- cards = player.cards
+ for player in self._players:
+ cards = player.get_cards()
- self.deck.get_cards().extend(cards)
- player.cards.clear()
+ self._deck.collect_cards(cards)
+ player.get_cards().clear()
- self.deck.cut()
- self.deck.shuffle()
+ self._deck.cut()
+ self._deck.shuffle()
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
- self.deck = Deck(BELOT_FACES)
+ self._deck = Deck(BELOT_FACES)
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Преименуван метод за обръщане на реда на раздаването със запазване на първия елемент (reversed_list(elements) -> reversed_direction(elements));

Оправени имена на атрибути за вътрешно ползване в класовете (protected).

Добавен метод collect_cards(cards) в class Deck.

Премахната ненужна употреба на get_players() в метода deal(). (90. ред)