Решение на От ливадите до Лас Вегас (и назад) от Димитър Аврамов

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

Към профила на Димитър Аврамов

Резултати

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

Код

import random
suits = ('clubs', 'diamonds', 'hearts', 'spades')
faces = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'K', 'Q', 'A')
class Card:
def __init__(self, suit, face):
self.__set_suit(suit)
self.__set_face(face)
def __set_suit(self, value):
self.__suit = value
def __set_face(self, value):
self.__face = value
def get_suit(self):
return self.__suit
def get_face(self):
return self.__face
class Deck:
def __init__(self, face_filter=None):
self.__set_cards(face_filter)
def __set_cards(self, value):
self.__cards = []
if value is not None:
for suit in suits:
for face in value:
self.__cards.append(Card(suit, face))
else:
for suit in suits:
for face in faces:
self.__cards.append(Card(suit, face))
def shuffle(self):
random.shuffle(self.__cards)
def get_cards(self):
return self.__cards
def remove_cards(self, count):
result = []
for i in range(0, count):
result.append(self.__cards.pop())
return result
def add_cards(self, new_cards):
self.__cards.extend(new_cards)
def cut(self):
start = random.randint(1, len(self.__cards) - 1)
first_half = self.__cards[start:]
del self.__cards[start:]
self.__cards[0:0] = first_half
class Player:
def __init__(self):
self.__cards = []
def remove_all_cards(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_cards(self, new_cards):
self.__cards.extend(new_cards)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.__players_count = number_of_players
self.__set_players(number_of_players)
self.__direction = dealing_direction
self.__instructions = dealing_instructions
self._set_deck()
def _set_deck(self):
self._deck = Deck()
def get_deck(self):
return self._deck
def __set_players(self, value):
self.__players = []
for i in range(value):
self.__players.append(Player())
def get_players(self):
return self.__players
def deal(self, starting_player):
starting_index = self.__players.index(starting_player)
first_part = []
second_part = []
if self.__direction == 'ltr':
first_part = self.__players[starting_index:]
second_part = self.__players[0:starting_index]
elif self.__direction == 'rtl':
first_part = self.__players[starting_index::-1]
second_part = self.__players[len(self.__players):starting_index:-1]

Насилих се да измисля по-кратък начин за това, само и само да цитирам Стоичков - "first part good second part I'm not like":

self.__players[starting_index+1:][::-1]

Шегата настрана. Твоето решение е ок. Просто не се сдържах.

for instruction in self.__instructions:
for player in first_part:
player.add_cards(self._deck.remove_cards(instruction))
for player in second_part:
player.add_cards(self._deck.remove_cards(instruction))
def prepare_deck(self):
for player in self.__players:
self._deck.add_cards(player.get_cards())
player.remove_all_cards()
self._deck.shuffle()
self._deck.cut()
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
def _set_deck(self):
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.156s

OK

История (1 версия и 9 коментара)

Димитър обнови решението на 14.11.2022 18:11 (преди около 2 години)

+import random
+
+suits = ('clubs', 'diamonds', 'hearts', 'spades')
+faces = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'K', 'Q', 'A')
+
+
+class Card:
+ def __init__(self, suit, face):
+ self.__set_suit(suit)
+ self.__set_face(face)
+
+ def __set_suit(self, value):
+ self.__suit = value
+
+ def __set_face(self, value):
+ self.__face = value
+
+ def get_suit(self):
+ return self.__suit
+
+ def get_face(self):
+ return self.__face
+
+
+class Deck:
+ def __init__(self, face_filter=None):
+ self.__set_cards(face_filter)
+
+ def __set_cards(self, value):
+ self.__cards = []
+
+ if value is not None:
+ for suit in suits:
+ for face in value:
+ self.__cards.append(Card(suit, face))
+ else:
+ for suit in suits:
+ for face in faces:
+ self.__cards.append(Card(suit, face))
+
+ def shuffle(self):
+ random.shuffle(self.__cards)
+
+ def get_cards(self):
+ return self.__cards
+
+ def remove_cards(self, count):
+ result = []
+ for i in range(0, count):
+ result.append(self.__cards.pop())
+
+ return result
+
+ def add_cards(self, new_cards):
+ self.__cards.extend(new_cards)
+
+ def cut(self):
+ start = random.randint(1, len(self.__cards) - 1)
+ first_half = self.__cards[start:]
+ del self.__cards[start:]
+ self.__cards[0:0] = first_half
+
+
+class Player:
+ def __init__(self):
+ self.__cards = []
+
+ def remove_all_cards(self):
+ self.__cards = []
+
+ def get_cards(self):
+ return self.__cards
+
+ def add_cards(self, new_cards):
+ self.__cards.extend(new_cards)
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.__players_count = number_of_players
+ self.__set_players(number_of_players)
+ self.__direction = dealing_direction
+ self.__instructions = dealing_instructions
+ self._set_deck()
+
+ def _set_deck(self):
+ self._deck = Deck()
+
+ def get_deck(self):
+ return self._deck
+
+ def __set_players(self, value):
+ self.__players = []
+
+ for i in range(value):
+ self.__players.append(Player())
+
+ def get_players(self):
+ return self.__players
+
+ def deal(self, starting_player):
+ starting_index = self.__players.index(starting_player)
+ first_part = []
+ second_part = []
+
+ if self.__direction == 'ltr':
+ first_part = self.__players[starting_index:]
+ second_part = self.__players[0:starting_index]
+ elif self.__direction == 'rtl':
+ first_part = self.__players[starting_index::-1]
+ second_part = self.__players[len(self.__players):starting_index:-1]

Насилих се да измисля по-кратък начин за това, само и само да цитирам Стоичков - "first part good second part I'm not like":

self.__players[starting_index+1:][::-1]

Шегата настрана. Твоето решение е ок. Просто не се сдържах.

+
+ for instruction in self.__instructions:
+
+ for player in first_part:
+ player.add_cards(self._deck.remove_cards(instruction))
+
+ for player in second_part:
+ player.add_cards(self._deck.remove_cards(instruction))
+
+ def prepare_deck(self):
+ for player in self.__players:
+ self._deck.add_cards(player.get_cards())
+ player.remove_all_cards()
+
+ self._deck.shuffle()
+ self._deck.cut()
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+
+ def _set_deck(self):
+ 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))