Решение на От ливадите до Лас Вегас (и назад) от Стилиян Иванов

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

Към профила на Стилиян Иванов

Резултати

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

Код

import random
suits = ("clubs", "diamonds", "hearts", "spades")
faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
belot_faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
class Card:
"""Game card definition class"""
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:
"""Card deck definition class"""
def __init__(self, face_filter=faces):
self.cards = [Card(suit, face) for suit in suits for face in face_filter]
def cut(self):
"""Cuts a random number of cards"""
cut_cards = random.randint(1, len(self.cards) - 1)
self.cards = self.cards[cut_cards:] + self.cards[:cut_cards]
def shuffle(self):
"""Shuffles the cards in the deck"""
random.shuffle(self.cards)
def get_cards(self):
"""Returns the cards present in the deck"""
return self.cards
class Player:
"""Player in card game definition class"""
def __init__(self):
self.cards = []
def get_cards(self):
"""Returns the cards in possession of the player"""
return self.cards
class Game:
"""Card game definition class"""
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self._players = [Player() for _ in range(number_of_players)]
self._left_to_right = 1 if dealing_direction == "ltr" else -1
self._dealing_instructions = dealing_instructions
self._deck = Deck()
def get_players(self):
"""Returns a list of the players of the game"""
return self._players
def prepare_deck(self):
"""The cards of the players are returned to the deck and it is prepared for starting anew"""
for player in self._players:
self._deck.cards.extend(player.get_cards())
player.cards = []
self._deck.shuffle()
self._deck.cut()
def deal(self, starting_player):
"""Deals cards to the players according to the rules of the game"""
index = self._players.index(starting_player)
for instruction in self._dealing_instructions:
for player in self._players[index::self._left_to_right] + self._players[:index:self._left_to_right]:
player.cards.extend(self._deck.cards[:instruction])
self._deck.cards = self._deck.cards[instruction:]
def get_deck(self):
"""Returns the undealt yet cards"""
return self._deck
class Belot(Game):
"""Belot game definition class"""
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
self._deck = Deck(belot_faces)
class Poker(Game):
"""Poker game definition class"""
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.164s

OK

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

Стилиян обнови решението на 15.11.2022 13:38 (преди над 1 година)

+import random
+
+suits = ("clubs", "diamonds", "hearts", "spades")
+faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
+belot_faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
+
+
+class Card:
+ """Game card definition class"""
+
+ 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:
+ """Card deck definition class"""
+
+ def __init__(self, face_filter=faces):
+ self.cards = [Card(suit, face) for suit in suits for face in face_filter]
+
+ def cut(self):
+ """Cuts a random number of cards"""
+ cut_cards = random.randint(1,len(self.cards))

randint връща стойност в затворен интервал. Ако върне последния елемент от интервала, тестето ти няма да се промени. Шанс около 1:52 да ти фейлне теста.

+ self.cards = self.cards[cut_cards:] + self.cards[:cut_cards]
+
+ def shuffle(self):
+ """Shuffles the cards in the deck"""
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ """Returns the cards present in the deck"""
+ return self.cards
+
+
+class Player:
+ """Player in card game definition class"""
+
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ """Returns the cards in possession of the player"""
+ return self.cards
+
+
+class Game:
+ """Card game definition class"""
+
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self._players = [Player() for _ in range(number_of_players)]
+ self._left_to_right = 1 if dealing_direction == "ltr" else -1
+ self._dealing_instructions = dealing_instructions
+ self._deck = Deck()
+
+ def get_players(self):
+ """Returns a list of the players of the game"""
+ return self._players
+
+ def prepare_deck(self):
+ """The cards of the players are returned to the deck and it is prepared for starting anew"""
+ for player in self._players:
+ self._deck.cards.extend(player.get_cards())
+ player.cards = []
+ self._deck.shuffle()
+ self._deck.cut()
+
+ def deal(self, starting_player):
+ """Deals cards to the players according to the rules of the game"""
+ index = self._players.index(starting_player)
+ for instruction in self._dealing_instructions:
+ for player in self._players[index::self._left_to_right] + self._players[:index:self._left_to_right]:
+ player.cards.extend(self._deck.cards[:instruction])
+ self._deck.cards = self._deck.cards[instruction:]
+
+ def get_deck(self):
+ """Returns the undealt yet cards"""
+ return self._deck
+
+
+class Belot(Game):
+ """Belot game definition class"""
+
+ def __init__(self):
+ super().__init__(4, "ltr", (2, 3, 3))
+ self._deck = Deck(belot_faces)
+
+
+class Poker(Game):
+ """Poker game definition class"""
+
+ def __init__(self):
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))

Стилиян обнови решението на 15.11.2022 14:06 (преди над 1 година)

import random
suits = ("clubs", "diamonds", "hearts", "spades")
faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
belot_faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
class Card:
"""Game card definition class"""
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:
"""Card deck definition class"""
def __init__(self, face_filter=faces):
self.cards = [Card(suit, face) for suit in suits for face in face_filter]
def cut(self):
"""Cuts a random number of cards"""
- cut_cards = random.randint(1,len(self.cards))
+ cut_cards = random.randint(1, len(self.cards) - 1)
self.cards = self.cards[cut_cards:] + self.cards[:cut_cards]
def shuffle(self):
"""Shuffles the cards in the deck"""
random.shuffle(self.cards)
def get_cards(self):
"""Returns the cards present in the deck"""
return self.cards
class Player:
"""Player in card game definition class"""
def __init__(self):
self.cards = []
def get_cards(self):
"""Returns the cards in possession of the player"""
return self.cards
class Game:
"""Card game definition class"""
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self._players = [Player() for _ in range(number_of_players)]
self._left_to_right = 1 if dealing_direction == "ltr" else -1
self._dealing_instructions = dealing_instructions
self._deck = Deck()
def get_players(self):
"""Returns a list of the players of the game"""
return self._players
def prepare_deck(self):
"""The cards of the players are returned to the deck and it is prepared for starting anew"""
for player in self._players:
self._deck.cards.extend(player.get_cards())
player.cards = []
self._deck.shuffle()
self._deck.cut()
def deal(self, starting_player):
"""Deals cards to the players according to the rules of the game"""
index = self._players.index(starting_player)
for instruction in self._dealing_instructions:
for player in self._players[index::self._left_to_right] + self._players[:index:self._left_to_right]:
player.cards.extend(self._deck.cards[:instruction])
self._deck.cards = self._deck.cards[instruction:]
def get_deck(self):
"""Returns the undealt yet cards"""
return self._deck
class Belot(Game):
"""Belot game definition class"""
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
self._deck = Deck(belot_faces)
class Poker(Game):
"""Poker game definition class"""
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))