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

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

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

Резултати

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

Код

import random
import itertools
card_faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
card_types = ['clubs', 'diamonds', 'hearts', 'spades']
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=card_faces):
self._cards = [Card(card_type, card) for (card, card_type)
in itertools.product(face_filter, card_types)]
def cut(self):
pos = random.randint(1, len(self._cards) - 1)
self._cards = self._cards[:pos] + self._cards[pos:]
def shuffle(self):
random.shuffle(self._cards)
def add_cards(self, cards_to_add):
self._cards.extend(cards_to_add)
def get_cards(self, count=None):
count = count or len(self._cards)
return self._cards[:count]
def remove_cards(self, count):
self._cards = self._cards[count:]
def __getitem__(self, key):
return self._cards[key]
class Player:
def __init__(self):
self.cards = []
def add_cards(self, cards):
self.cards.extend(cards)
def collect_cards(self):
cards_to_collect = self.cards
self.cards = []
return cards_to_collect
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_instruction = dealing_instructions
self.players = [Player() for i in range(number_of_players)]
self.game_deck= Deck()
def get_players(self):
random_position = random.randint(1, self.number_of_players)
return self.players[random_position:]+self.players[:random_position]
def prepare_deck(self):
collected_cards = [self.players[i].collect_cards()
for i in range(len(self.players))]
collected_cards = [
hand for non_empty_hand in collected_cards for hand in non_empty_hand if non_empty_hand != []]
self.game_deck.add_cards(collected_cards)
self.game_deck.shuffle()
self.game_deck.cut()
def deal(self, starting_player):
player_position = self.players.index(starting_player)
if self.dealing_direction == 'ltr':
self.players = self.players[player_position:] + self.players[:player_position]
else:
self.players = self.players[player_position::-1] + self.players[:player_position:-1]
for i in self.dealing_instruction:
for player in self.players:
player.add_cards(self.game_deck.get_cards(i))
self.game_deck.remove_cards(i)
def get_deck(self):
return self.game_deck
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
self.game_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))

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

....F..........
======================================================================
FAIL: test_cutting_deck (test.TestDeck)
Test cutting a deck.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: [<solution.Card object at 0xffff9c086230>, <solution.Card object at 0xffff9c086290>, <solution.Card object at 0xffff9c0862f0>, <solution.Card object at 0xffff9c086350>, <solution.Card object at 0xffff9c0863b0>, <solution.Card object at 0xffff9c086410>, <solution.Card object at 0xffff9c086470>, <solution.Card object at 0xffff9c0864d0>, <solution.Card object at 0xffff9c086530>, <solution.Card object at 0xffff9c086590>, <solution.Card object at 0xffff9c0865f0>, <solution.Card object at 0xffff9c086650>, <solution.Card object at 0xffff9c0866b0>, <solution.Card object at 0xffff9c086710>, <solution.Card object at 0xffff9c086770>, <solution.Card object at 0xffff9c0867d0>, <solution.Card object at 0xffff9c086830>, <solution.Card object at 0xffff9c086890>, <solution.Card object at 0xffff9c0868f0>, <solution.Card object at 0xffff9c086950>] == [<solution.Card object at 0xffff9c086230>, <solution.Card object at 0xffff9c086290>, <solution.Card object at 0xffff9c0862f0>, <solution.Card object at 0xffff9c086350>, <solution.Card object at 0xffff9c0863b0>, <solution.Card object at 0xffff9c086410>, <solution.Card object at 0xffff9c086470>, <solution.Card object at 0xffff9c0864d0>, <solution.Card object at 0xffff9c086530>, <solution.Card object at 0xffff9c086590>, <solution.Card object at 0xffff9c0865f0>, <solution.Card object at 0xffff9c086650>, <solution.Card object at 0xffff9c0866b0>, <solution.Card object at 0xffff9c086710>, <solution.Card object at 0xffff9c086770>, <solution.Card object at 0xffff9c0867d0>, <solution.Card object at 0xffff9c086830>, <solution.Card object at 0xffff9c086890>, <solution.Card object at 0xffff9c0868f0>, <solution.Card object at 0xffff9c086950>]

----------------------------------------------------------------------
Ran 15 tests in 0.170s

FAILED (failures=1)

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

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

+import random
+import itertools
+
+card_faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+card_types = ['clubs', 'diamonds', 'hearts', 'spades']
+
+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=card_faces):
+ self._cards = [Card(card, type) for (card, type)
+ in itertools.product(face_filter, card_types)]
+
+ def cut(self):
+ pos = random.randint(1, len(self._cards))
+ self._cards = self._cards[:pos]+self._cards[pos:]
+
+ def shuffle(self):
+ random.shuffle(self._cards)
+
+ def add_cards(self, cards_to_add):
+ self._cards.extend(cards_to_add)
+
+ def get_cards(self,count = None):
+ count = count or len(self._cards)
+ return self._cards[:count]
+
+ def remove_cards(self,count):
+ self._cards = self._cards[count:]
+
+ def __getitem__(self, key):
+ return self._cards[key]
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def add_cards(self, cards):
+ self.cards.extend(cards)
+
+ def collect_cards(self):
+ cards_to_collect = self.cards
+ self.cards = []
+ return cards_to_collect
+
+ def get_cards(self):
+ return self.cards
+
+
+class Game:
+ game_deck = Deck()
+
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instruction = dealing_instructions
+
+ self.players = [Player(i) for i in range(number_of_players)]
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ collected_cards = [self.players[i].collect_cards()
+ for i in range(len(self.players))]
+ collected_cards = [
+ hand for non_empty_hand in collected_cards for hand in non_empty_hand if non_empty_hand != []]
+ self.game_deck.add_cards(collected_cards)
+ self.game_deck.shuffle()
+ self.game_deck.cut()
+
+ def deal(self, starting_player):
+ player_position = self.players.index(starting_player)
+ if self.dealing_direction == 'ltr':
+ self.players = self.players[player_position:] + \
+ self.players[:player_position]
+ else:
+ self.players = self.players[player_position::-1] + \
+ self.players[:player_position:-1]
+ for i in self.dealing_instruction:
+ for player in self.players:
+ player.add_cards(self.game_deck.get_cards(i))
+ self.game_deck.remove_cards(i)
+
+ def get_deck(self):
+ return self.game_deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, "ltr", (2, 3, 3))
+ self.game_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 22:28 (преди около 2 години)

import random
import itertools
card_faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
card_types = ['clubs', 'diamonds', 'hearts', 'spades']
+
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=card_faces):
+ def __init__(self, face_filter = card_faces):
self._cards = [Card(card, type) for (card, type)
- in itertools.product(face_filter, card_types)]
+ in itertools.product(face_filter, card_types)]
def cut(self):
pos = random.randint(1, len(self._cards))
self._cards = self._cards[:pos]+self._cards[pos:]
def shuffle(self):
random.shuffle(self._cards)
def add_cards(self, cards_to_add):
self._cards.extend(cards_to_add)
- def get_cards(self,count = None):
+ def get_cards(self, count=None):
count = count or len(self._cards)
return self._cards[:count]
- def remove_cards(self,count):
+ def remove_cards(self, count):
self._cards = self._cards[count:]
def __getitem__(self, key):
return self._cards[key]
class Player:
def __init__(self):
self.cards = []
def add_cards(self, cards):
self.cards.extend(cards)
def collect_cards(self):
cards_to_collect = self.cards
self.cards = []
return cards_to_collect
def get_cards(self):
return self.cards
class Game:
game_deck = Deck()

Всяка инстанция на класа трябва да има собствено тесте. Сега, ако реша да инстанцирам класа ти няколко пъти, всички инстанции ще използват едно тесте и ще стане мармалад.

def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instruction = dealing_instructions
self.players = [Player(i) for i in range(number_of_players)]
def get_players(self):
return self.players
def prepare_deck(self):
collected_cards = [self.players[i].collect_cards()
for i in range(len(self.players))]
collected_cards = [
hand for non_empty_hand in collected_cards for hand in non_empty_hand if non_empty_hand != []]
self.game_deck.add_cards(collected_cards)
self.game_deck.shuffle()
self.game_deck.cut()
def deal(self, starting_player):
player_position = self.players.index(starting_player)
if self.dealing_direction == 'ltr':
self.players = self.players[player_position:] + \
self.players[:player_position]
else:
self.players = self.players[player_position::-1] + \

Като цяло не мисля, че е добра идея да презаписваш реда на вече създадените играчи. Все едно караш всички да станат от масата и да седнат на други места. Не мисля, че ще е проблем за тестовете, но е нелогично.

self.players[:player_position:-1]
for i in self.dealing_instruction:
for player in self.players:
player.add_cards(self.game_deck.get_cards(i))
self.game_deck.remove_cards(i)
def get_deck(self):
return self.game_deck
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
self.game_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))

Данаил обнови решението на 15.11.2022 15:48 (преди около 2 години)

import random
import itertools
card_faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
card_types = ['clubs', 'diamonds', 'hearts', 'spades']
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 = card_faces):
- self._cards = [Card(card, type) for (card, type)
+ def __init__(self, face_filter=card_faces):
+ self._cards = [Card(card_type, card) for (card, card_type)
in itertools.product(face_filter, card_types)]
def cut(self):
- pos = random.randint(1, len(self._cards))
- self._cards = self._cards[:pos]+self._cards[pos:]
+ pos = random.randint(1, len(self._cards) - 1)
+ self._cards = self._cards[:pos] + self._cards[pos:]
def shuffle(self):
random.shuffle(self._cards)
def add_cards(self, cards_to_add):
self._cards.extend(cards_to_add)
def get_cards(self, count=None):
count = count or len(self._cards)
return self._cards[:count]
def remove_cards(self, count):
self._cards = self._cards[count:]
def __getitem__(self, key):
return self._cards[key]
class Player:
def __init__(self):
self.cards = []
def add_cards(self, cards):
self.cards.extend(cards)
def collect_cards(self):
cards_to_collect = self.cards
self.cards = []
return cards_to_collect
def get_cards(self):
return self.cards
class Game:
- game_deck = Deck()
-
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instruction = dealing_instructions
- self.players = [Player(i) for i in range(number_of_players)]
+ self.players = [Player() for i in range(number_of_players)]
+ self.game_deck= Deck()
def get_players(self):
- return self.players
+ random_position = random.randint(1, self.number_of_players)
+ return self.players[random_position:]+self.players[:random_position]
def prepare_deck(self):
collected_cards = [self.players[i].collect_cards()
for i in range(len(self.players))]
collected_cards = [
hand for non_empty_hand in collected_cards for hand in non_empty_hand if non_empty_hand != []]
self.game_deck.add_cards(collected_cards)
self.game_deck.shuffle()
self.game_deck.cut()
def deal(self, starting_player):
player_position = self.players.index(starting_player)
if self.dealing_direction == 'ltr':
- self.players = self.players[player_position:] + \
- self.players[:player_position]
+ self.players = self.players[player_position:] + self.players[:player_position]
else:
- self.players = self.players[player_position::-1] + \
- self.players[:player_position:-1]
+ self.players = self.players[player_position::-1] + self.players[:player_position:-1]
for i in self.dealing_instruction:
for player in self.players:
player.add_cards(self.game_deck.get_cards(i))
self.game_deck.remove_cards(i)
def get_deck(self):
return self.game_deck
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
self.game_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))