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

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

Към профила на Емилиан Спасов

Резултати

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

Код

import random
FACES = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
SUITS = ["clubs", "diamonds", "hearts", "spades"]
LEFT_TO_RIGHT = "ltr"
RIGHT_TO_LEFT = "rtl"
class Card:
def __init__(self, suit: str, face: str) -> None:
self.suit = suit
self.face = face
def get_suit(self) -> str:
return self.suit
def get_face(self) -> str:
return self.face
def __str__(self) -> str:
return f'{self.get_suit()}-{self.get_face()}'
class Deck:
def __init__(self, face_filter: list[str] = None) -> None:
self.cards = self.__create_deck(face_filter)

Самото присвояване към self.cards можеш да направиш в self.__create_deck, а тук само да извикаш метода. не е проблем и така, разбира се, просто няма нужда да връщаш стойността и да я присвояваш извън метода.

Не съм съгласен, че е по-четимо. За тестването мога да си представя случаи, в които си прав. Реално, няма никакъв проблем с твоята имплементация. Въпрос на стил е.

def __generate_deck(self) -> list[Card]:
cards = []
for suit in SUITS:
for face in FACES:
cards.append(Card(suit, face))
return cards
def __create_deck(self, face_filter: list[str] = None) -> list[Card]:
deck = self.__generate_deck()
if face_filter is None:
return deck
return [card for card in deck if card.face in set(face_filter)]
def add_to_deck(self, cards: Card) -> None:
self.cards.extend(cards)
def remove_from_top(self, number_of_cards: int) -> list[Card]:
top_cards = self.cards[:number_of_cards]
self.cards = self.cards[number_of_cards:]
return top_cards
def cut(self) -> None:
cut_idx = random.randint(1, len(self.cards) - 1)
self.cards = self.cards[cut_idx:] + self.cards[0: cut_idx]
def shuffle(self) -> None:
random.shuffle(self.cards)
def get_cards(self) -> list[Card]:
return self.cards
class Player:
def __init__(self) -> None:
self.cards = []
def empty_cards(self) -> None:
self.cards = []
def receive_cards(self, new_cards: list[Card]) -> None:
self.cards.extend(new_cards)
def get_cards(self) -> list[Card]:
return self.cards
class Game:
def __init__(self,
number_of_players: int,
dealing_direction: str,
dealing_instructions: tuple[int, ...],
face_filter: list[str] = None) -> None:
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = self.__init_players(self.number_of_players)
self.deck = Deck(face_filter=face_filter)
def __init_players(self, number_of_players: int) -> list[Player]:
return [Player() for _ in range(number_of_players)]
def __get_player_dealing_indecies(self, start_idx: int) -> list[int]:
if self.dealing_direction == LEFT_TO_RIGHT:
return [x for x in range(start_idx, len(self.players))] + \
[x for x in range(0, start_idx)]
elif self.dealing_direction == RIGHT_TO_LEFT:
return [x for x in range(start_idx, -1, -1)] + \
[x for x in range(len(self.players) - 1, start_idx, -1)]
return []
def get_players(self) -> list[Player]:
return self.players
def prepare_deck(self) -> None:
for player in self.players:
cards = player.get_cards()
self.deck.add_to_deck(cards)
player.empty_cards()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player) -> None:
start_idx = self.players.index(player)
indecies = self.__get_player_dealing_indecies(start_idx)
for number_of_cards in self.dealing_instructions:
for idx in indecies:
top_cards = self.deck.remove_from_top(number_of_cards)
player = self.players[idx]
player.receive_cards(top_cards)
def get_deck(self) -> Deck:
return self.deck
class Belot(Game):
def __init__(self) -> None:
super().__init__(4, LEFT_TO_RIGHT, (2, 3, 3),
["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self) -> None:
super().__init__(9, RIGHT_TO_LEFT, (1, 1, 1, 1, 1))

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.155s

OK

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

Емилиан обнови решението на 05.11.2022 13:12 (преди около 2 години)

+import random
+
+FACES = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
+SUITS = ["clubs", "diamonds", "hearts", "spades"]
+LEFT_TO_RIGHT = "ltr"
+RIGHT_TO_LEFT = "rtl"
+
+
+class Card:
+ def __init__(self, suit: str, face: str):
+ self.suit = suit
+ self.face = face
+
+ def get_suit(self) -> str:
+ return self.suit.value

Това value ми се струва като нещо, което просто си забравил да махнеш? За останалото ще дадем допълнителни коментари, но това е нещо, което веднага мога да коментирам.

+
+ def get_face(self) -> str:
+ return self.face
+
+ def __str__(self) -> str:
+ return f'{self.suit}-{self.face}'
+
+
+class Deck:
+ def __init__(self, face_filter: list[str] = None):
+ self.deck = self.__create_deck(face_filter)
+
+ def __generate_deck(self) -> list[Card]:
+ deck = []
+ for suit in SUITS:
+ for face in FACES:
+ deck.append(Card(suit, face))
+ return deck
+
+ def __create_deck(self, face_filter: list[str] = None) -> list[Card]:
+ deck = self.__generate_deck()
+ if face_filter is None:
+ return deck
+
+ return [card for card in deck if card.face in set(face_filter)]
+
+ def add_to_deck(self, cards: Card) -> None:
+ self.deck.extend(cards)
+
+ def remove_from_top(self, number_of_cards: int) -> list[Card]:
+ top_cards = self.deck[:number_of_cards]
+ self.deck = self.deck[number_of_cards:]
+ return top_cards
+
+ def cut(self) -> None:
+ cut_idx = random.randint(1, len(self.deck) - 1)
+ self.deck = [*self.deck[cut_idx:], *self.deck[0: cut_idx]]
+
+ def shuffle(self) -> None:
+ random.shuffle(self.deck)
+
+ def get_cards(self) -> list[Card]:
+ return self.deck
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def empty_cards(self) -> None:
+ self.cards = []
+
+ def receive_cards(self, new_cards: list[Card]) -> None:
+ self.cards.extend(new_cards)
+
+ def get_cards(self) -> list[Card]:
+ return self.cards
+
+
+class Game:
+ def __init__(self,
+ number_of_players: int,
+ dealing_direction: str,
+ dealing_instructions: tuple[int, ...],
+ face_filter: list[str] = None):
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+ self.players = self.__init_players(self.number_of_players)
+ self.deck = Deck(face_filter=face_filter)
+
+ def __init_players(self, number_of_players: int) -> list[Player]:
+ return [Player() for _ in range(number_of_players)]
+
+ def __get_player_position(self, player: Player) -> int:
+ for idx, p in enumerate(self.players):
+ if p == player:
+ return idx
+ return -1
+
+ def __get_player_dealing_indecies(self, start_idx) -> list[int]:
+ if self.dealing_direction == LEFT_TO_RIGHT:
+ return [
+ *range(start_idx, len(self.players)), *range(0, start_idx)
+ ]
+ elif self.dealing_direction == RIGHT_TO_LEFT:
+ return [
+ *range(start_idx, -1, -1),
+ *range(len(self.players) - 1, start_idx, -1)
+ ]
+ return []
+
+ def get_players(self) -> list[Player]:
+ return self.players
+
+ def prepare_deck(self) -> None:
+ for player in self.players:
+ cards = player.get_cards()
+ self.deck.add_to_deck(cards)
+ player.empty_cards()
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, player: Player) -> None:
+ start_idx = self.__get_player_position(player)
+ indecies = self.__get_player_dealing_indecies(start_idx)
+
+ for number_of_cards in self.dealing_instructions:
+ for idx in indecies:
+ top_cards = self.deck.remove_from_top(number_of_cards)
+ player = self.players[idx]
+ player.receive_cards(top_cards)
+
+ def get_deck(self) -> Deck:
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super(Belot, self).__init__(
+ 4, LEFT_TO_RIGHT, (2, 3, 3),
+ ["7", "8", "9", "10", "J", "Q", "K", "A"])
+
+
+class Poker(Game):
+ def __init__(self):
+ super(Poker, self).__init__(
+ 9, RIGHT_TO_LEFT, (1, 1, 1, 1, 1))

Емилиан обнови решението на 06.11.2022 00:23 (преди около 2 години)

import random
FACES = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
SUITS = ["clubs", "diamonds", "hearts", "spades"]
LEFT_TO_RIGHT = "ltr"
RIGHT_TO_LEFT = "rtl"
class Card:
- def __init__(self, suit: str, face: str):
+ def __init__(self, suit: str, face: str) -> None:
self.suit = suit
self.face = face
def get_suit(self) -> str:
- return self.suit.value
+ return self.suit
def get_face(self) -> str:
return self.face
def __str__(self) -> str:
- return f'{self.suit}-{self.face}'
+ return f'{self.get_suit()}-{self.get_face()}'
class Deck:
- def __init__(self, face_filter: list[str] = None):
- self.deck = self.__create_deck(face_filter)
+ def __init__(self, face_filter: list[str] = None) -> None:
+ self.cards = self.__create_deck(face_filter)

Самото присвояване към self.cards можеш да направиш в self.__create_deck, а тук само да извикаш метода. не е проблем и така, разбира се, просто няма нужда да връщаш стойността и да я присвояваш извън метода.

def __generate_deck(self) -> list[Card]:
- deck = []
+ cards = []
for suit in SUITS:
for face in FACES:
- deck.append(Card(suit, face))
- return deck
+ cards.append(Card(suit, face))
+ return cards
def __create_deck(self, face_filter: list[str] = None) -> list[Card]:
deck = self.__generate_deck()
if face_filter is None:
return deck
return [card for card in deck if card.face in set(face_filter)]
def add_to_deck(self, cards: Card) -> None:
- self.deck.extend(cards)
+ self.cards.extend(cards)
def remove_from_top(self, number_of_cards: int) -> list[Card]:
- top_cards = self.deck[:number_of_cards]
- self.deck = self.deck[number_of_cards:]
+ top_cards = self.cards[:number_of_cards]
+ self.cards = self.cards[number_of_cards:]
return top_cards
def cut(self) -> None:
- cut_idx = random.randint(1, len(self.deck) - 1)
- self.deck = [*self.deck[cut_idx:], *self.deck[0: cut_idx]]
+ cut_idx = random.randint(1, len(self.cards) - 1)
+ self.cards = [*self.cards[cut_idx:], *self.cards[0: cut_idx]]
def shuffle(self) -> None:
- random.shuffle(self.deck)
+ random.shuffle(self.cards)
def get_cards(self) -> list[Card]:
- return self.deck
+ return self.cards
class Player:
- def __init__(self):
+ def __init__(self) -> None:
self.cards = []
def empty_cards(self) -> None:
self.cards = []
def receive_cards(self, new_cards: list[Card]) -> None:
self.cards.extend(new_cards)
def get_cards(self) -> list[Card]:
return self.cards
class Game:
def __init__(self,
number_of_players: int,
dealing_direction: str,
dealing_instructions: tuple[int, ...],
- face_filter: list[str] = None):
+ face_filter: list[str] = None) -> None:
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = self.__init_players(self.number_of_players)
self.deck = Deck(face_filter=face_filter)
def __init_players(self, number_of_players: int) -> list[Player]:
return [Player() for _ in range(number_of_players)]
def __get_player_position(self, player: Player) -> int:
for idx, p in enumerate(self.players):
if p == player:
return idx
return -1
- def __get_player_dealing_indecies(self, start_idx) -> list[int]:
+ def __get_player_dealing_indecies(self, start_idx: int) -> list[int]:
if self.dealing_direction == LEFT_TO_RIGHT:
return [
*range(start_idx, len(self.players)), *range(0, start_idx)

Тук * ми идва в повече. По-горе показах как можеш да го направиш по-четимо. Бих направил нещо подобно и тук. Твоето работи, просто не смятам, че има нужда да го правиш така.
Някои линтери дори се оплакват, ако използваш *, така че използването рядко наистина е нужно (въпреки, че има такива случаи).

]
elif self.dealing_direction == RIGHT_TO_LEFT:
return [
*range(start_idx, -1, -1),
*range(len(self.players) - 1, start_idx, -1)
]
return []
def get_players(self) -> list[Player]:
return self.players
def prepare_deck(self) -> None:
for player in self.players:
cards = player.get_cards()
self.deck.add_to_deck(cards)
player.empty_cards()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player) -> None:
start_idx = self.__get_player_position(player)
indecies = self.__get_player_dealing_indecies(start_idx)
for number_of_cards in self.dealing_instructions:
for idx in indecies:
top_cards = self.deck.remove_from_top(number_of_cards)
player = self.players[idx]
player.receive_cards(top_cards)
def get_deck(self) -> Deck:
return self.deck
class Belot(Game):
- def __init__(self):
+ def __init__(self) -> None:
super(Belot, self).__init__(

В Python 3 не е нужно да подаваш аргументи на super(). Това, което си написал, се изисвка само в Python 2.
Освен това, изглежда странно, когато всички аргументи са на нов ред. Сложи колкото се можеш повече на първия и приравни останалите на втория.

super().__init__(4, LEFT_TO_RGIHT, (2, 3, 3),
                 ["7", "8"...])
4, LEFT_TO_RIGHT, (2, 3, 3),
["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
- def __init__(self):
+ def __init__(self) -> None:
super(Poker, self).__init__(
9, RIGHT_TO_LEFT, (1, 1, 1, 1, 1))

Емилиан обнови решението на 08.11.2022 19:16 (преди около 2 години)

import random
FACES = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
SUITS = ["clubs", "diamonds", "hearts", "spades"]
LEFT_TO_RIGHT = "ltr"
RIGHT_TO_LEFT = "rtl"
class Card:
def __init__(self, suit: str, face: str) -> None:
self.suit = suit
self.face = face
def get_suit(self) -> str:
return self.suit
def get_face(self) -> str:
return self.face
def __str__(self) -> str:
return f'{self.get_suit()}-{self.get_face()}'
class Deck:
def __init__(self, face_filter: list[str] = None) -> None:
self.cards = self.__create_deck(face_filter)

Не съм съгласен, че е по-четимо. За тестването мога да си представя случаи, в които си прав. Реално, няма никакъв проблем с твоята имплементация. Въпрос на стил е.

def __generate_deck(self) -> list[Card]:
cards = []
for suit in SUITS:
for face in FACES:
cards.append(Card(suit, face))
return cards
def __create_deck(self, face_filter: list[str] = None) -> list[Card]:
deck = self.__generate_deck()
if face_filter is None:
return deck
return [card for card in deck if card.face in set(face_filter)]
def add_to_deck(self, cards: Card) -> None:
self.cards.extend(cards)
def remove_from_top(self, number_of_cards: int) -> list[Card]:
top_cards = self.cards[:number_of_cards]
self.cards = self.cards[number_of_cards:]
return top_cards
def cut(self) -> None:
cut_idx = random.randint(1, len(self.cards) - 1)
- self.cards = [*self.cards[cut_idx:], *self.cards[0: cut_idx]]
+ self.cards = self.cards[cut_idx:] + self.cards[0: cut_idx]
def shuffle(self) -> None:
random.shuffle(self.cards)
def get_cards(self) -> list[Card]:
return self.cards
class Player:
def __init__(self) -> None:
self.cards = []
def empty_cards(self) -> None:
self.cards = []
def receive_cards(self, new_cards: list[Card]) -> None:
self.cards.extend(new_cards)
def get_cards(self) -> list[Card]:
return self.cards
class Game:
def __init__(self,
number_of_players: int,
dealing_direction: str,
dealing_instructions: tuple[int, ...],
face_filter: list[str] = None) -> None:
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = self.__init_players(self.number_of_players)
self.deck = Deck(face_filter=face_filter)
def __init_players(self, number_of_players: int) -> list[Player]:
return [Player() for _ in range(number_of_players)]
- def __get_player_position(self, player: Player) -> int:
- for idx, p in enumerate(self.players):
- if p == player:
- return idx
- return -1
-
def __get_player_dealing_indecies(self, start_idx: int) -> list[int]:
if self.dealing_direction == LEFT_TO_RIGHT:
- return [
- *range(start_idx, len(self.players)), *range(0, start_idx)
- ]
+ return [x for x in range(start_idx, len(self.players))] + \
+ [x for x in range(0, start_idx)]
elif self.dealing_direction == RIGHT_TO_LEFT:
- return [
- *range(start_idx, -1, -1),
- *range(len(self.players) - 1, start_idx, -1)
- ]
+ return [x for x in range(start_idx, -1, -1)] + \
+ [x for x in range(len(self.players) - 1, start_idx, -1)]
return []
def get_players(self) -> list[Player]:
return self.players
def prepare_deck(self) -> None:
for player in self.players:
cards = player.get_cards()
self.deck.add_to_deck(cards)
player.empty_cards()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player) -> None:
- start_idx = self.__get_player_position(player)
+ start_idx = self.players.index(player)
indecies = self.__get_player_dealing_indecies(start_idx)
for number_of_cards in self.dealing_instructions:
for idx in indecies:
top_cards = self.deck.remove_from_top(number_of_cards)
player = self.players[idx]
player.receive_cards(top_cards)
def get_deck(self) -> Deck:
return self.deck
class Belot(Game):
def __init__(self) -> None:
- super(Belot, self).__init__(
- 4, LEFT_TO_RIGHT, (2, 3, 3),
- ["7", "8", "9", "10", "J", "Q", "K", "A"])
+ super().__init__(4, LEFT_TO_RIGHT, (2, 3, 3),
+ ["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self) -> None:
- super(Poker, self).__init__(
- 9, RIGHT_TO_LEFT, (1, 1, 1, 1, 1))
+ super().__init__(9, RIGHT_TO_LEFT, (1, 1, 1, 1, 1))

Емилиан обнови решението на 15.11.2022 17:38 (преди около 2 години)

import random
+
FACES = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
SUITS = ["clubs", "diamonds", "hearts", "spades"]
LEFT_TO_RIGHT = "ltr"
RIGHT_TO_LEFT = "rtl"
class Card:
def __init__(self, suit: str, face: str) -> None:
self.suit = suit
self.face = face
def get_suit(self) -> str:
return self.suit
def get_face(self) -> str:
return self.face
def __str__(self) -> str:
return f'{self.get_suit()}-{self.get_face()}'
class Deck:
def __init__(self, face_filter: list[str] = None) -> None:
self.cards = self.__create_deck(face_filter)
def __generate_deck(self) -> list[Card]:
cards = []
for suit in SUITS:
for face in FACES:
cards.append(Card(suit, face))
return cards
def __create_deck(self, face_filter: list[str] = None) -> list[Card]:
deck = self.__generate_deck()
if face_filter is None:
return deck
return [card for card in deck if card.face in set(face_filter)]
def add_to_deck(self, cards: Card) -> None:
self.cards.extend(cards)
def remove_from_top(self, number_of_cards: int) -> list[Card]:
top_cards = self.cards[:number_of_cards]
self.cards = self.cards[number_of_cards:]
return top_cards
def cut(self) -> None:
cut_idx = random.randint(1, len(self.cards) - 1)
self.cards = self.cards[cut_idx:] + self.cards[0: cut_idx]
def shuffle(self) -> None:
random.shuffle(self.cards)
def get_cards(self) -> list[Card]:
return self.cards
class Player:
def __init__(self) -> None:
self.cards = []
def empty_cards(self) -> None:
self.cards = []
def receive_cards(self, new_cards: list[Card]) -> None:
self.cards.extend(new_cards)
def get_cards(self) -> list[Card]:
return self.cards
class Game:
def __init__(self,
number_of_players: int,
dealing_direction: str,
dealing_instructions: tuple[int, ...],
face_filter: list[str] = None) -> None:
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = self.__init_players(self.number_of_players)
self.deck = Deck(face_filter=face_filter)
def __init_players(self, number_of_players: int) -> list[Player]:
return [Player() for _ in range(number_of_players)]
def __get_player_dealing_indecies(self, start_idx: int) -> list[int]:
if self.dealing_direction == LEFT_TO_RIGHT:
return [x for x in range(start_idx, len(self.players))] + \
[x for x in range(0, start_idx)]
elif self.dealing_direction == RIGHT_TO_LEFT:
return [x for x in range(start_idx, -1, -1)] + \
[x for x in range(len(self.players) - 1, start_idx, -1)]
return []
def get_players(self) -> list[Player]:
return self.players
def prepare_deck(self) -> None:
for player in self.players:
cards = player.get_cards()
self.deck.add_to_deck(cards)
player.empty_cards()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player) -> None:
start_idx = self.players.index(player)
indecies = self.__get_player_dealing_indecies(start_idx)
for number_of_cards in self.dealing_instructions:
for idx in indecies:
top_cards = self.deck.remove_from_top(number_of_cards)
player = self.players[idx]
player.receive_cards(top_cards)
def get_deck(self) -> Deck:
return self.deck
class Belot(Game):
def __init__(self) -> None:
super().__init__(4, LEFT_TO_RIGHT, (2, 3, 3),
["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self) -> None:
super().__init__(9, RIGHT_TO_LEFT, (1, 1, 1, 1, 1))

Емилиан обнови решението на 15.11.2022 17:44 (преди около 2 години)

import random
FACES = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
SUITS = ["clubs", "diamonds", "hearts", "spades"]
LEFT_TO_RIGHT = "ltr"
RIGHT_TO_LEFT = "rtl"
class Card:
def __init__(self, suit: str, face: str) -> None:
self.suit = suit
self.face = face
def get_suit(self) -> str:
return self.suit
def get_face(self) -> str:
return self.face
def __str__(self) -> str:
return f'{self.get_suit()}-{self.get_face()}'
class Deck:
def __init__(self, face_filter: list[str] = None) -> None:
self.cards = self.__create_deck(face_filter)
def __generate_deck(self) -> list[Card]:
cards = []
for suit in SUITS:
for face in FACES:
cards.append(Card(suit, face))
return cards
def __create_deck(self, face_filter: list[str] = None) -> list[Card]:
deck = self.__generate_deck()
if face_filter is None:
return deck
-
return [card for card in deck if card.face in set(face_filter)]
def add_to_deck(self, cards: Card) -> None:
self.cards.extend(cards)
def remove_from_top(self, number_of_cards: int) -> list[Card]:
top_cards = self.cards[:number_of_cards]
self.cards = self.cards[number_of_cards:]
return top_cards
def cut(self) -> None:
cut_idx = random.randint(1, len(self.cards) - 1)
self.cards = self.cards[cut_idx:] + self.cards[0: cut_idx]
def shuffle(self) -> None:
random.shuffle(self.cards)
def get_cards(self) -> list[Card]:
return self.cards
class Player:
def __init__(self) -> None:
self.cards = []
def empty_cards(self) -> None:
self.cards = []
def receive_cards(self, new_cards: list[Card]) -> None:
self.cards.extend(new_cards)
def get_cards(self) -> list[Card]:
return self.cards
class Game:
def __init__(self,
number_of_players: int,
dealing_direction: str,
dealing_instructions: tuple[int, ...],
face_filter: list[str] = None) -> None:
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = self.__init_players(self.number_of_players)
self.deck = Deck(face_filter=face_filter)
def __init_players(self, number_of_players: int) -> list[Player]:
return [Player() for _ in range(number_of_players)]
def __get_player_dealing_indecies(self, start_idx: int) -> list[int]:
if self.dealing_direction == LEFT_TO_RIGHT:
return [x for x in range(start_idx, len(self.players))] + \
[x for x in range(0, start_idx)]
elif self.dealing_direction == RIGHT_TO_LEFT:
return [x for x in range(start_idx, -1, -1)] + \
[x for x in range(len(self.players) - 1, start_idx, -1)]
return []
def get_players(self) -> list[Player]:
return self.players
def prepare_deck(self) -> None:
for player in self.players:
cards = player.get_cards()
self.deck.add_to_deck(cards)
player.empty_cards()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player) -> None:
start_idx = self.players.index(player)
indecies = self.__get_player_dealing_indecies(start_idx)
for number_of_cards in self.dealing_instructions:
for idx in indecies:
top_cards = self.deck.remove_from_top(number_of_cards)
player = self.players[idx]
player.receive_cards(top_cards)
def get_deck(self) -> Deck:
return self.deck
class Belot(Game):
def __init__(self) -> None:
super().__init__(4, LEFT_TO_RIGHT, (2, 3, 3),
["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self) -> None:
super().__init__(9, RIGHT_TO_LEFT, (1, 1, 1, 1, 1))