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

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

Към профила на Весела Петрова

Резултати

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

Код

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=None):
self.__cards = []
suits = ['clubs', 'diamonds', 'hearts', 'spades']
faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
if face_filter is None:
self.add_cards_to_the_deck(faces, suits)
else:
self.add_cards_to_the_deck(face_filter, suits)
def add_cards_to_the_deck(self, faces, suits):
for face in faces:
self.__cards.extend([Card(suit, face) for suit in suits])
def cut(self):
random_number = random.randint(0, len(self.__cards))
new_deck = self.__cards[random_number:] + self.__cards[:random_number]
self.__cards = new_deck
def shuffle(self):
random.shuffle(self.__cards)
def get_cards(self):
return self.__cards
class Player:
def __init__(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_cards(self, cards):
self.__cards.extend(cards)
def remove_all_cards(self):
self.__cards.clear()
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._players = []
self._deck = Deck()
def get_players(self):
return self._players
def prepare_deck(self):
if len(self._deck.get_cards()) == 0:
all_cards_from_players = set()
for player in self._players:
for card in player.get_cards():
all_cards_from_players.add(card.get_face())
player.remove_all_cards()
self._deck = Deck(all_cards_from_players)
self._deck.shuffle()
self._deck.cut()
def give_cards(self, players_list, number_of_cards_to_give):
for player in players_list:
player.add_cards(self._deck.get_cards()[-number_of_cards_to_give:])
del self._deck.get_cards()[-number_of_cards_to_give:]
def deal(self, player):
deal_player_index = self.get_players().index(player, 0, self._number_of_players)
ltr_list = self.get_players()[deal_player_index + 1:self._number_of_players] + self.get_players()[

Този и следващият ред ми идват в повече и не мога да ги интерпретирам наум. Да кажем, че работят, но бих се опитал да разбия малко логиката на парчета, за да е по-ясно какво правя.

0:deal_player_index]
rest_players = ltr_list if self._dealing_direction == 'ltr' else list(reversed(ltr_list))
for number_of_cards_to_give in self._dealing_instructions:
self.give_cards([player], number_of_cards_to_give)
self.give_cards(rest_players, number_of_cards_to_give)
def get_deck(self):
return self._deck
def add_cards(self, new_cards_for_the_game=None):
self._deck = Deck(new_cards_for_the_game)
def add_players(self):
players_number = self._number_of_players
while players_number > 0:
self._players.append(Player())
players_number -= 1
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
super().add_cards(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
super().add_players()
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
super().add_cards()
super().add_players()

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

........EEEF...
======================================================================
ERROR: test_collecting_cards_before_dealing (test.TestGame)
Test collecting the cards before a new deal.
----------------------------------------------------------------------
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
IndexError: list index out of range

======================================================================
ERROR: test_dealing_ltr (test.TestGame)
Test dealing the cards left to right.
----------------------------------------------------------------------
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
IndexError: list index out of range

======================================================================
ERROR: 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
IndexError: list index out of range

======================================================================
FAIL: test_players_creation (test.TestGame)
Test creation and retrieval of 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: 0 != 2

----------------------------------------------------------------------
Ran 15 tests in 0.176s

FAILED (failures=1, errors=3)

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

Весела обнови решението на 15.11.2022 14:42 (преди около 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 add_cards_to_the_deck(self, faces, suits):
+ for face in faces:
+ self.__cards.extend([Card(suit, face) for suit in suits])
+
+ def __init__(self, face_filter=None):
+ self.__cards = []
+ suits = ['clubs', 'diamonds', 'hearts', 'spades']
+ faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+
+ if face_filter is None:
+ self.add_cards_to_the_deck(faces, suits)
+ return
+
+ self.add_cards_to_the_deck(face_filter, suits)
+
+ def cut(self):
+ random_number = random.randint(0, 26)

Ако играеш белот, картите не са 52. Вземи бройката динамично. Освен това не е нужно да спираш в средата. Всяка модификация на тестето, която мести поне една карта, е валидна.

+ new_deck = self.__cards[random_number:] + self.__cards[:random_number]
+ self.__cards = new_deck
+
+ def shuffle(self):
+ random.shuffle(self.__cards)
+
+ def get_cards(self):
+ return self.__cards
+
+
+class Player:
+ def __init__(self):
+ self.__cards = []
+
+ def get_cards(self):
+ return self.__cards
+
+ def add_cards(self, cards):
+ self.__cards.extend(cards)
+
+ def remove_all_cards(self):
+ self.__cards.clear()
+
+
+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._players = [Player()] * self._number_of_players

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

+ self._deck = Deck()
+
+ def get_players(self):
+ return self._players
+
+ def prepare_deck(self):
+ if len(self._deck.get_cards()) == 0:
+ all_cards_from_players = set()
+ for player in self._players:
+ for card in player.get_cards():
+ all_cards_from_players.add(card.get_face())
+ player.remove_all_cards()
+ self._deck = Deck(all_cards_from_players)
+ self._deck.shuffle()
+ self._deck.cut()
+
+ def give_cards(self, players_list, number_of_cards_to_give):
+ for player in players_list:
+ player.add_cards(self._deck.get_cards()[-number_of_cards_to_give:])
+ del self._deck.get_cards()[-number_of_cards_to_give:]
+
+ def deal(self, player):
+ deal_player_index = self.get_players().index(player, 0, self._number_of_players)
+ ltr_list = self.get_players()[deal_player_index + 1:self._number_of_players] + self.get_players()[

Този и следващият ред ми идват в повече и не мога да ги интерпретирам наум. Да кажем, че работят, но бих се опитал да разбия малко логиката на парчета, за да е по-ясно какво правя.

+ 0:deal_player_index]
+ rest_players = ltr_list if self._dealing_direction == 'ltr' else list(reversed(ltr_list))
+
+ for number_of_cards_to_give in self._dealing_instructions:
+ self.give_cards([player], number_of_cards_to_give)
+ self.give_cards(rest_players, number_of_cards_to_give)
+
+ def get_deck(self):
+ return self._deck
+
+ def add_cards(self, needed_cards=None):
+ self._deck = Deck(needed_cards)
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+ super().add_cards(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
+ super().add_cards()

Весела обнови решението на 15.11.2022 17:29 (преди около 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 add_cards_to_the_deck(self, faces, suits):
- for face in faces:
- self.__cards.extend([Card(suit, face) for suit in suits])
-
def __init__(self, face_filter=None):
self.__cards = []
suits = ['clubs', 'diamonds', 'hearts', 'spades']
faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
if face_filter is None:
self.add_cards_to_the_deck(faces, suits)
- return
+ else:
+ self.add_cards_to_the_deck(face_filter, suits)
- self.add_cards_to_the_deck(face_filter, suits)
+ def add_cards_to_the_deck(self, faces, suits):
+ for face in faces:
+ self.__cards.extend([Card(suit, face) for suit in suits])
def cut(self):
- random_number = random.randint(0, 26)
+ random_number = random.randint(0, len(self.__cards))
new_deck = self.__cards[random_number:] + self.__cards[:random_number]
self.__cards = new_deck
def shuffle(self):
random.shuffle(self.__cards)
def get_cards(self):
return self.__cards
class Player:
def __init__(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_cards(self, cards):
self.__cards.extend(cards)
def remove_all_cards(self):
self.__cards.clear()
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._players = [Player()] * self._number_of_players
+ self._players = []
self._deck = Deck()
def get_players(self):
return self._players
def prepare_deck(self):
if len(self._deck.get_cards()) == 0:
all_cards_from_players = set()
for player in self._players:
for card in player.get_cards():
all_cards_from_players.add(card.get_face())
player.remove_all_cards()
self._deck = Deck(all_cards_from_players)
self._deck.shuffle()
self._deck.cut()
def give_cards(self, players_list, number_of_cards_to_give):
for player in players_list:
player.add_cards(self._deck.get_cards()[-number_of_cards_to_give:])
del self._deck.get_cards()[-number_of_cards_to_give:]
def deal(self, player):
deal_player_index = self.get_players().index(player, 0, self._number_of_players)
ltr_list = self.get_players()[deal_player_index + 1:self._number_of_players] + self.get_players()[
0:deal_player_index]
rest_players = ltr_list if self._dealing_direction == 'ltr' else list(reversed(ltr_list))
for number_of_cards_to_give in self._dealing_instructions:
self.give_cards([player], number_of_cards_to_give)
self.give_cards(rest_players, number_of_cards_to_give)
def get_deck(self):
return self._deck
- def add_cards(self, needed_cards=None):
- self._deck = Deck(needed_cards)
+ def add_cards(self, new_cards_for_the_game=None):
+ self._deck = Deck(new_cards_for_the_game)
+ def add_players(self):
+ players_number = self._number_of_players
+ while players_number > 0:
+ self._players.append(Player())
+ players_number -= 1
+
+
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
super().add_cards(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+ super().add_players()
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
super().add_cards()
+ super().add_players()