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

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

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

Резултати

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

Код

import random
all_faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
bellot_faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
all_suits = ["clubs", "diamonds", "hearts", "spades"]
belot_num_players = 4
poker_num_players = 9
left_to_right = "ltr"
right_to_left = "rtl"
belot_dealing_instructions = (2, 3, 3)
poker_dealing_instructions = (1, 1, 1, 1, 1)
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=all_faces):
self._face_filter = face_filter
self._cards = self.generate_deck()
def generate_deck(self):
cards = []
for face in self._face_filter:
for suit in all_suits:
cards.append(Card(suit, face))
return cards
def get_cards(self):
return self._cards
def shuffle(self):
random.shuffle(self._cards)
def cut(self):
cut = random.randrange(1, len(self._cards))
self._cards = self._cards[cut:] + self._cards[:cut]
def add_cards(self, cards):
self._cards += cards
def get_card(self):
return self._cards.pop(0)
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
def clear_cards(self):
self._cards = []
def add_card(self, card):
self._cards.append(card)
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_instructions = dealing_instructions
self._deck = Deck()
self._players = [Player() for _ in range(number_of_players)]
def get_players(self):
return self._players
def get_deck(self):
return self._deck
def prepare_deck(self):
for player in self._players:
self._deck.add_cards(player.get_cards())
player.clear_cards()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
order = self.get_order_of_deal(player)
for num_of_card_per_deal in self._dealing_instructions:
for player in range(self._number_of_players):
for _ in range(num_of_card_per_deal):
order[player].add_card(self._deck.get_card())
def get_order_of_deal(self, player):
index_of_player = self._players.index(player)
order = self._players[index_of_player:]
order += self._players[:index_of_player]
if self._dealing_direction == right_to_left:
order.reverse()
return order
class Belot(Game):
def __init__(self, number_of_players=belot_num_players, dealing_direction=left_to_right,
dealing_instructions=belot_dealing_instructions):
super().__init__(number_of_players, dealing_direction, dealing_instructions)
self._deck = Deck(bellot_faces)
class Poker(Game):
def __init__(self, number_of_players=poker_num_players, dealing_direction=right_to_left,
dealing_instructions=poker_dealing_instructions):
super().__init__(number_of_players, dealing_direction, dealing_instructions)

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

..........F...F
======================================================================
FAIL: test_dealing_rtl (test.TestGame)
Test dealing the cards right to left.
----------------------------------------------------------------------
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: {('spades', '4'), ('hearts', '3'), ('spades', '3')} not found in ({('clubs', '4'), ('clubs', '2'), ('diamonds', '2')}, {('spades', 'Q'), ('hearts', 'A'), ('spades', 'A')})

======================================================================
FAIL: test_correct_direction_and_players_deal (test.TestPoker)
Test dealing with correct direction and players.
----------------------------------------------------------------------
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: {('clubs', '4'), ('clubs', 'K'), ('diamonds', '6'), ('hearts', '8'), ('spades', '10')} not found in ({('clubs', '2'), ('hearts', '6'), ('spades', '8'), ('diamonds', '4'), ('clubs', 'J')}, {('hearts', 'Q'), ('clubs', '8'), ('spades', '5'), ('diamonds', '10'), ('spades', 'A')})

----------------------------------------------------------------------
Ran 15 tests in 0.162s

FAILED (failures=2)

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

Кристиан обнови решението на 12.11.2022 15:53 (преди около 2 години)

+import random
+
+all_faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
+all_suits = ["clubs", "diamonds", "hearts", "spades"]
+belot_num_players = 4
+poker_num_players = 9
+left_to_right = "ltr"
+right_to_left = "rtl"
+belot_dealing_instructions = (2, 3, 3)
+poker_dealing_instructions = (1, 1, 1, 1, 1)
+
+
+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=all_faces):
+ self.face_filter = face_filter
+ self.cards = self.generate_deck()
+
+ def generate_deck(self):
+ cards = []
+ for face in self.face_filter:
+ for suit in all_suits:
+ cards.append(Card(suit, face))
+ return cards
+
+ def get_cards(self):
+ return self.cards
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def cut(self):
+ cut = random.randrange(1, len(self.cards))
+ first_half = self.cards[:cut]
+ second_half = self.cards[cut:]
+ self.cards = second_half + first_half
+
+ def add_cards(self, cards):
+ self.cards += cards
+
+ def get_card(self):
+ return self.cards.pop(0)
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+ def clear_cards(self):
+ self.cards = []
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+
+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_instructions = dealing_instructions
+ self.deck = Deck()
+ self.players = [Player() for i in range(number_of_players)]
+
+ def get_players(self):
+ return self.players
+
+ def get_deck(self):
+ return self.deck
+
+ def prepare_deck(self):
+ for player in self.players:
+ self.deck.add_cards(player.get_cards())
+ player.clear_cards()
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, player):
+ order = self.get_order_of_deal(player)
+ for deal in range(len(self.dealing_instructions)):
+ num_of_card_per_deal = self.dealing_instructions[deal]
+ for player in range(self.number_of_players):
+ for _ in range(num_of_card_per_deal):
+ order[player].add_card(self.deck.get_card())
+
+ def get_order_of_deal(self, player):
+ index_of_player = self.players.index(player)
+ order = self.players[index_of_player:]
+ order += self.players[:index_of_player]
+ if self.dealing_direction == right_to_left:
+ order.reverse()
+ return order
+
+
+class Belot(Game):
+ def __init__(self, number_of_players=belot_num_players, dealing_direction=left_to_right,
+ dealing_instructions=belot_dealing_instructions):
+ super().__init__(number_of_players, dealing_direction, dealing_instructions)
+ self.deck = Deck(all_faces[5:])

Не съм почитател на хардкоуднати магически числа. Не е ясно защо точно 5 и кога би се наложило да го сменя. Бих го дефинирал като константа, или бих дефинирал отделен списък за белот, или поне бих сложил коментар...

+
+
+class Poker(Game):
+ def __init__(self, number_of_players=poker_num_players, dealing_direction=right_to_left,
+ dealing_instructions=poker_dealing_instructions):
+ super().__init__(number_of_players, dealing_direction, dealing_instructions)

Кристиан обнови решението на 13.11.2022 11:54 (преди около 2 години)

import random
all_faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
+bellot_faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
all_suits = ["clubs", "diamonds", "hearts", "spades"]
belot_num_players = 4
poker_num_players = 9
left_to_right = "ltr"
right_to_left = "rtl"
belot_dealing_instructions = (2, 3, 3)
poker_dealing_instructions = (1, 1, 1, 1, 1)
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=all_faces):
- self.face_filter = face_filter
- self.cards = self.generate_deck()
+ self._face_filter = face_filter
+ self._cards = self.generate_deck()
def generate_deck(self):
cards = []
- for face in self.face_filter:
+ for face in self._face_filter:
for suit in all_suits:
cards.append(Card(suit, face))
return cards
def get_cards(self):
- return self.cards
+ return self._cards
def shuffle(self):
- random.shuffle(self.cards)
+ random.shuffle(self._cards)
def cut(self):
- cut = random.randrange(1, len(self.cards))
- first_half = self.cards[:cut]
- second_half = self.cards[cut:]
- self.cards = second_half + first_half
+ cut = random.randrange(1, len(self._cards))
+ self._cards = self._cards[cut:] + self._cards[:cut]
def add_cards(self, cards):
- self.cards += cards
+ self._cards += cards
def get_card(self):
- return self.cards.pop(0)
+ return self._cards.pop(0)
class Player:
def __init__(self):
- self.cards = []
+ self._cards = []
def get_cards(self):
- return self.cards
+ return self._cards
def clear_cards(self):
- self.cards = []
+ self._cards = []
def add_card(self, card):
- self.cards.append(card)
+ self._cards.append(card)
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_instructions = dealing_instructions
- self.deck = Deck()
- self.players = [Player() for i in range(number_of_players)]
+ self._number_of_players = number_of_players
+ self._dealing_direction = dealing_direction
+ self._dealing_instructions = dealing_instructions
+ self._deck = Deck()
+ self._players = [Player() for _ in range(number_of_players)]
def get_players(self):
- return self.players
+ return self._players
def get_deck(self):
- return self.deck
+ return self._deck
def prepare_deck(self):
- for player in self.players:
- self.deck.add_cards(player.get_cards())
+ for player in self._players:
+ self._deck.add_cards(player.get_cards())
player.clear_cards()
- self.deck.shuffle()
- self.deck.cut()
+ self._deck.shuffle()
+ self._deck.cut()
def deal(self, player):
order = self.get_order_of_deal(player)
- for deal in range(len(self.dealing_instructions)):
- num_of_card_per_deal = self.dealing_instructions[deal]
- for player in range(self.number_of_players):
+ for num_of_card_per_deal in self._dealing_instructions:
+ for player in range(self._number_of_players):
for _ in range(num_of_card_per_deal):
- order[player].add_card(self.deck.get_card())
+ order[player].add_card(self._deck.get_card())
def get_order_of_deal(self, player):
- index_of_player = self.players.index(player)
- order = self.players[index_of_player:]
- order += self.players[:index_of_player]
- if self.dealing_direction == right_to_left:
+ index_of_player = self._players.index(player)
+ order = self._players[index_of_player:]
+ order += self._players[:index_of_player]
+ if self._dealing_direction == right_to_left:
order.reverse()
return order
class Belot(Game):
def __init__(self, number_of_players=belot_num_players, dealing_direction=left_to_right,
dealing_instructions=belot_dealing_instructions):
super().__init__(number_of_players, dealing_direction, dealing_instructions)
- self.deck = Deck(all_faces[5:])
+ self._deck = Deck(bellot_faces)
class Poker(Game):
def __init__(self, number_of_players=poker_num_players, dealing_direction=right_to_left,
dealing_instructions=poker_dealing_instructions):
super().__init__(number_of_players, dealing_direction, dealing_instructions)