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

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

Към профила на Роберт Борисов

Резултати

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

Код

import random
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=[]):
faces = [str(x) for x in range(2, 11)]
faces.extend(['J', 'Q', 'K', 'A'])
if face_filter:
self._cards = [Card(suit, face) for face in face_filter for suit in
['clubs', 'diamonds', 'hearts', 'spades']]
else:
self._cards = [Card(suit, face) for face in faces for suit in ['clubs', 'spades', 'diamonds', 'hearts']]
def cut(self):
if len(self._cards) > 1:
for current_cut in range(random.randint(1, len(self._cards) - 1)):
self._cards.insert(0, self._cards.pop())
def shuffle(self):
random.shuffle(self._cards)
def get_cards(self):
return self._cards
def add_cards(self, cards):
self._cards.extend(cards)
def add_card(self, card):
self._cards.append(card)
def draw_card(self):
if self._cards:
return self._cards.pop()
class Player:
def __init__(self):
self._hand = []
def get_cards(self):
return self._hand
def clear_hand(self):
self._hand.clear()
def add_card_to_hand(self, card):
self._hand.append(card)
def add_cards_to_hand(self, cards):
self._hand.extend(cards)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions, face_filter=[]):
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._players = [Player() for _ in range(self._number_of_players)]
self._deck = Deck(face_filter)
def get_players(self):
return self._players
def prepare_deck(self):
for current_player in self._players:
self._deck.add_cards(current_player.get_cards())
current_player.clear_hand()
self._deck.shuffle()
self._deck.cut()
def deal(self, player_search):
given_player = -1
for current_player in range(self._number_of_players):
if self._players[current_player] is player_search:
given_player = current_player
break
if given_player == -1:
return
order = []
if self._dealing_direction == 'ltr':
order = [x for x in range(given_player, self._number_of_players)]
order.extend([x for x in range(0, given_player)])
elif self._dealing_direction == 'rtl':
order = [x for x in range(given_player, -1, -1)]
order.extend([x for x in range(len(self._players) - 1, given_player, -1)])
for instruction in self._dealing_instructions:
for current_player in order:
self._players[current_player].add_cards_to_hand([self._deck.draw_card() for card in range(instruction)])
def get_deck(self):
return self._deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3), ['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.172s

OK

История (4 версии и 7 коментара)

Роберт обнови решението на 13.11.2022 17:33 (преди около 2 години)

+import random
+
+
+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=[]):
+ types = [str(x) for x in range(2, 11)]
+ types.extend(['J', 'Q', 'K', 'A'])
+ if face_filter:
+ self.cards = [Card(x, y) for x in face_filter for y in ['clubs', 'diamonds', 'hearts', 'spades']]
+ else:
+ self.cards = [Card(x, y) for x in types for y in ['clubs', 'spades', 'diamonds', 'hearts']]
+
+ def cut(self):
+ for a in range(random.randint(1, len(self.cards) - 1)):
+ self.cards.insert(0, self.cards.pop())
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def add_cards(self, cards):
+ self.cards.extend(cards)
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+ def draw_card(self):
+ if self.cards:
+ return self.cards.pop()
+
+
+class Player:
+ def __init__(self):
+ self.hand = []
+
+ def get_cards(self):
+ return self.hand
+
+ def clear_hand(self):
+ self.hand.clear()
+
+ def add_card_to_hand(self, card):
+ self.hand.append(card)
+
+ def add_cards_to_hand(self, cards):
+ self.hand.extend(cards)
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions, face_filter=[]):
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+ self.players = [Player() for i in range(self.number_of_players)]
+ self.deck = Deck(face_filter)
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for current_player in self.players:
+ self.deck.add_cards(current_player.get_cards())
+ current_player.clear_hand()
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, player_search):
+ given_player = -1
+ for current_player in range(self.number_of_players):
+ if self.players[current_player] is player_search:
+ given_player = current_player
+ break
+ if given_player == -1:
+ return
+ order = []
+ if self.dealing_direction == 'ltr':
+ order = [x for x in range(given_player, self.number_of_players)]
+ order.extend([x for x in range(0, given_player)])
+ elif self.dealing_direction == 'rtl':
+ order = [x for x in range(given_player, -1, -1)]
+ order.extend([x for x in range(len(self.players) - 1, given_player, -1)])
+ for instruction in self.dealing_instructions:
+ for current_player in order:
+ self.players[current_player].add_cards_to_hand([self.deck.draw_card() for card in range(instruction)])
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3), ['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))

Роберт обнови решението на 13.11.2022 17:48 (преди около 2 години)

import random
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=[]):
types = [str(x) for x in range(2, 11)]
types.extend(['J', 'Q', 'K', 'A'])
if face_filter:
self.cards = [Card(x, y) for x in face_filter for y in ['clubs', 'diamonds', 'hearts', 'spades']]

Не те съветвам да използваш еднобуквени променливи дори тук. Ако не го беше направил, сигурно щеше да хванеш сам, че си разменил местата на face и suit :)

else:
self.cards = [Card(x, y) for x in types for y in ['clubs', 'spades', 'diamonds', 'hearts']]
def cut(self):
- for a in range(random.randint(1, len(self.cards) - 1)):
- self.cards.insert(0, self.cards.pop())
+ if len(self.cards) > 1:
+ for current_cut in range(random.randint(1, len(self.cards) - 1)):
+ self.cards.insert(0, self.cards.pop())
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def add_cards(self, cards):
self.cards.extend(cards)
def add_card(self, card):
self.cards.append(card)
def draw_card(self):
if self.cards:
return self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
def clear_hand(self):
self.hand.clear()
def add_card_to_hand(self, card):
self.hand.append(card)
def add_cards_to_hand(self, cards):
self.hand.extend(cards)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions, face_filter=[]):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = [Player() for i in range(self.number_of_players)]
self.deck = Deck(face_filter)
def get_players(self):
return self.players
def prepare_deck(self):
for current_player in self.players:
self.deck.add_cards(current_player.get_cards())
current_player.clear_hand()
self.deck.shuffle()
self.deck.cut()
def deal(self, player_search):
given_player = -1
for current_player in range(self.number_of_players):
if self.players[current_player] is player_search:
given_player = current_player
break
if given_player == -1:
return
order = []
if self.dealing_direction == 'ltr':

Би било по-удобно да работиш с играчи, а не с индексите им. Нека това не е причина да преправяш кода си и евентуално да го счупиш. Просто препоръка.

order = [x for x in range(given_player, self.number_of_players)]
order.extend([x for x in range(0, given_player)])
elif self.dealing_direction == 'rtl':
order = [x for x in range(given_player, -1, -1)]
order.extend([x for x in range(len(self.players) - 1, given_player, -1)])
for instruction in self.dealing_instructions:
for current_player in order:
self.players[current_player].add_cards_to_hand([self.deck.draw_card() for card in range(instruction)])
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3), ['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 12:42 (преди около 2 години)

import random
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
+ def face(self):
+ return self._face
+ def suit(self):
+ return self._suit
+
+
class Deck:
def __init__(self, face_filter=[]):
- types = [str(x) for x in range(2, 11)]
- types.extend(['J', 'Q', 'K', 'A'])
+ faces = [str(x) for x in range(2, 11)]
+ faces.extend(['J', 'Q', 'K', 'A'])
if face_filter:
- self.cards = [Card(x, y) for x in face_filter for y in ['clubs', 'diamonds', 'hearts', 'spades']]
+ self._cards = [Card(suit, face) for face in face_filter for suit in
+ ['clubs', 'diamonds', 'hearts', 'spades']]
else:
- self.cards = [Card(x, y) for x in types for y in ['clubs', 'spades', 'diamonds', 'hearts']]
+ self._cards = [Card(suit, face) for face in faces for suit in ['clubs', 'spades', 'diamonds', 'hearts']]
def cut(self):
- if len(self.cards) > 1:
- for current_cut in range(random.randint(1, len(self.cards) - 1)):
- self.cards.insert(0, self.cards.pop())
+ if len(self._cards) > 1:
+ for current_cut in range(random.randint(1, len(self._cards) - 1)):
+ self._cards.insert(0, self._cards.pop())
def shuffle(self):
- random.shuffle(self.cards)
+ random.shuffle(self._cards)
def get_cards(self):
- return self.cards
+ return self._cards
def add_cards(self, cards):
- self.cards.extend(cards)
+ self._cards.extend(cards)
def add_card(self, card):
- self.cards.append(card)
+ self._cards.append(card)
def draw_card(self):
- if self.cards:
- return self.cards.pop()
+ if self._cards:
+ return self._cards.pop()
class Player:
def __init__(self):
- self.hand = []
+ self._hand = []
def get_cards(self):
- return self.hand
+ return self._hand
def clear_hand(self):
- self.hand.clear()
+ self._hand.clear()
def add_card_to_hand(self, card):
- self.hand.append(card)
+ self._hand.append(card)
def add_cards_to_hand(self, cards):
- self.hand.extend(cards)
+ self._hand.extend(cards)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions, face_filter=[]):
- self.number_of_players = number_of_players
- self.dealing_direction = dealing_direction
- self.dealing_instructions = dealing_instructions
- self.players = [Player() for i in range(self.number_of_players)]
- self.deck = Deck(face_filter)
+ self._number_of_players = number_of_players
+ self._dealing_direction = dealing_direction
+ self._dealing_instructions = dealing_instructions
+ self._players = [Player() for _ in range(self._number_of_players)]
+ self._deck = Deck(face_filter)
def get_players(self):
- return self.players
+ return self._players
def prepare_deck(self):
- for current_player in self.players:
- self.deck.add_cards(current_player.get_cards())
+ for current_player in self._players:
+ self._deck.add_cards(current_player.get_cards())
current_player.clear_hand()
- self.deck.shuffle()
- self.deck.cut()
+ self._deck.shuffle()
+ self._deck.cut()
def deal(self, player_search):
given_player = -1
- for current_player in range(self.number_of_players):
- if self.players[current_player] is player_search:
+ for current_player in range(self._number_of_players):
+ if self._players[current_player] is player_search:
given_player = current_player
break
if given_player == -1:
return
order = []
- if self.dealing_direction == 'ltr':
- order = [x for x in range(given_player, self.number_of_players)]
+ if self._dealing_direction == 'ltr':
+ order = [x for x in range(given_player, self._number_of_players)]
order.extend([x for x in range(0, given_player)])
- elif self.dealing_direction == 'rtl':
+ elif self._dealing_direction == 'rtl':
order = [x for x in range(given_player, -1, -1)]
- order.extend([x for x in range(len(self.players) - 1, given_player, -1)])
- for instruction in self.dealing_instructions:
+ order.extend([x for x in range(len(self._players) - 1, given_player, -1)])
+ for instruction in self._dealing_instructions:
for current_player in order:
- self.players[current_player].add_cards_to_hand([self.deck.draw_card() for card in range(instruction)])
+ self._players[current_player].add_cards_to_hand([self._deck.draw_card() for card in range(instruction)])
def get_deck(self):
- return self.deck
+ return self._deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3), ['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 12:28 (преди около 2 години)

import random
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
-
- def face(self):
return self._face
-
- def suit(self):
- return self._suit
class Deck:
def __init__(self, face_filter=[]):
faces = [str(x) for x in range(2, 11)]
faces.extend(['J', 'Q', 'K', 'A'])
if face_filter:
self._cards = [Card(suit, face) for face in face_filter for suit in
['clubs', 'diamonds', 'hearts', 'spades']]
else:
self._cards = [Card(suit, face) for face in faces for suit in ['clubs', 'spades', 'diamonds', 'hearts']]
def cut(self):
if len(self._cards) > 1:
for current_cut in range(random.randint(1, len(self._cards) - 1)):
self._cards.insert(0, self._cards.pop())
def shuffle(self):
random.shuffle(self._cards)
def get_cards(self):
return self._cards
def add_cards(self, cards):
self._cards.extend(cards)
def add_card(self, card):
self._cards.append(card)
def draw_card(self):
if self._cards:
return self._cards.pop()
class Player:
def __init__(self):
self._hand = []
def get_cards(self):
return self._hand
def clear_hand(self):
self._hand.clear()
def add_card_to_hand(self, card):
self._hand.append(card)
def add_cards_to_hand(self, cards):
self._hand.extend(cards)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions, face_filter=[]):
self._number_of_players = number_of_players
self._dealing_direction = dealing_direction
self._dealing_instructions = dealing_instructions
self._players = [Player() for _ in range(self._number_of_players)]
self._deck = Deck(face_filter)
def get_players(self):
return self._players
def prepare_deck(self):
for current_player in self._players:
self._deck.add_cards(current_player.get_cards())
current_player.clear_hand()
self._deck.shuffle()
self._deck.cut()
def deal(self, player_search):
given_player = -1
for current_player in range(self._number_of_players):
if self._players[current_player] is player_search:
given_player = current_player
break
if given_player == -1:
return
order = []
if self._dealing_direction == 'ltr':
order = [x for x in range(given_player, self._number_of_players)]
order.extend([x for x in range(0, given_player)])
elif self._dealing_direction == 'rtl':
order = [x for x in range(given_player, -1, -1)]
order.extend([x for x in range(len(self._players) - 1, given_player, -1)])
for instruction in self._dealing_instructions:
for current_player in order:
self._players[current_player].add_cards_to_hand([self._deck.draw_card() for card in range(instruction)])
def get_deck(self):
return self._deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3), ['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))