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

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

Към профила на Мария Кукова

Резултати

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

Код

import random
from collections import deque
suits = ('clubs', 'diamonds', 'hearts', 'spades')
poker_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:
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=belot_faces):
self.face_filter = face_filter
self._deck = deque()
for face in face_filter:
for suit in suits:
self._deck.append(Card(suit, face))
def cut(self):
number_cards_to_cut = random.randrange(1, len(self._deck))
self._deck.rotate(number_cards_to_cut)
def shuffle(self):
random.shuffle(self._deck)
def get_cards(self):
return list(self._deck)
def get_cards_deque(self):
return self._deck
def add_card(self, card: Card):
self._deck.append(card)
def pop_card(self):
return self._deck.pop()
class Player:
def __init__(self):
self.cards = Deck([])
def get_cards(self):
return list(self.cards)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.deck = Deck([])
self.players = deque()
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
def get_players(self):

Грешно си интерпретирала инструкциите. Този метод се използва за да видим какви ирачи имаш, а не за да ги инициализираш. Играчите трябва да се инициализират още при инициализиране на класа. Този метод просто ни ги показва, но и без него трябва да можеш да работиш с класа.

i = 0
while i < self.number_of_players:
new_player = Player()
self.players.append(new_player)
i += 1
return self.players
def prepare_deck(self):

Тук не си имплементирала събирането на карти от играчите. Виждам, че по-долу се дефинирала ново тесте при конкретните игри, което до известна степен се справя с проблема, но идеологически погледнато, ако седнеш да играеш карти, не вадиш ново тесте на всяко раздаване. Не гарантирам, че всички тестове ще минат при тази имплементация.

self.deck.shuffle()
self.deck.cut()
def deal(self, player):
index = self.players.index(player)
if self.dealing_direction == 'ltr':
self.players.rotate(index)
else:
self.players.rotate(0 - index)
for number_cards in self.dealing_instructions:
for curr_player in self.players:
card_count = 0
while card_count < number_cards:
curr_player.cards.add_card(self.deck.pop_card())
card_count += 1
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
def prepare_deck(self):
self.deck = Deck(belot_faces)
self.deck.shuffle()
self.deck.cut()
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
def prepare_deck(self):
self.deck = Deck(poker_faces)
self.deck.shuffle()
self.deck.cut()

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

FE....F.EEEE.FE
======================================================================
ERROR: test_correct_direction_and_players_deal (test.TestBelot)
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
TypeError: sequence index must be integer, not 'slice'

======================================================================
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
TypeError: sequence index must be integer, not 'slice'

======================================================================
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
TypeError: sequence index must be integer, not 'slice'

======================================================================
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
TypeError: sequence index must be integer, not 'slice'

======================================================================
ERROR: 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
TypeError: sequence index must be integer, not 'slice'

======================================================================
ERROR: 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
TypeError: sequence index must be integer, not 'slice'

======================================================================
FAIL: test_correct_deck_init (test.TestBelot)
Test initialization with correct deck.
----------------------------------------------------------------------
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: Items in the first set but not the second:
('hearts', '10')
('spades', 'Q')
('diamonds', '10')
('hearts', 'Q')
('diamonds', 'Q')
('spades', 'K')
('spades', '10')
('spades', '7')
('hearts', 'K')
('hearts', '7')
('spades', 'A')
('clubs', '8')
('clubs', '9')
('diamonds', '7')
('diamonds', 'K')
('clubs', 'J')
('clubs', '10')
('hearts', 'A')
('diamonds', 'A')
('clubs', 'Q')
('clubs', 'K')
('spades', '9')
('clubs', '7')
('spades', 'J')
('spades', '8')
('hearts', 'J')
('hearts', '8')
('hearts', '9')
('clubs', 'A')
('diamonds', 'J')
('diamonds', '8')
('diamonds', '9')

======================================================================
FAIL: test_init_regular (test.TestDeck)
Test initialized cards without filter.
----------------------------------------------------------------------
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: Items in the first set but not the second:
('diamonds', '6')
('clubs', '4')
('clubs', '5')
('clubs', '3')
('spades', '2')
('hearts', '2')
('diamonds', '2')
('clubs', '6')
('spades', '4')
('spades', '5')
('spades', '3')
('hearts', '4')
('hearts', '3')
('hearts', '5')
('diamonds', '4')
('diamonds', '5')
('diamonds', '3')
('clubs', '2')
('spades', '6')
('hearts', '6')

======================================================================
FAIL: test_correct_deck_init (test.TestPoker)
Test initialization with correct deck.
----------------------------------------------------------------------
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: Items in the first set but not the second:
('diamonds', '6')
('clubs', '4')
('hearts', '10')
('clubs', '5')
('spades', 'Q')
('diamonds', '10')
('clubs', '3')
('hearts', 'Q')
('spades', '2')
('diamonds', 'Q')
('spades', 'K')
('spades', '10')
('spades', '7')
('hearts', 'K')
('hearts', '2')
('hearts', '7')
('spades', 'A')
('diamonds', '2')
('clubs', '8')
('clubs', '9')
('diamonds', '7')
('clubs', '6')
('diamonds', 'K')
('spades', '4')
('clubs', 'J')
('clubs', '10')
('hearts', 'A')
('hearts', '4')
('spades', '3')
('spades', '5')
('diamonds', 'A')
('hearts', '3')
('hearts', '5')
('clubs', 'Q')
('diamonds', '4')
('diamonds', '5')
('diamonds', '3')
('clubs', '2')
('clubs', 'K')
('hearts', '6')
('clubs', '7')
('spades', '6')
('spades', '9')
('spades', 'J')
('spades', '8')
('hearts', 'J')
('hearts', '8')
('hearts', '9')
('clubs', 'A')
('diamonds', 'J')
('diamonds', '8')
('diamonds', '9')

----------------------------------------------------------------------
Ran 15 tests in 0.174s

FAILED (failures=3, errors=6)

История (1 версия и 6 коментара)

Мария обнови решението на 15.11.2022 15:05 (преди около 2 години)

+import random
+from collections import deque
+
+suits = ('clubs', 'diamonds', 'hearts', 'spades')
+poker_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:
+ 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=belot_faces):
+ self.face_filter = face_filter
+ self._deck = deque()
+ for face in face_filter:
+ for suit in suits:
+ self._deck.append(Card(suit, face))
+
+ def cut(self):
+ number_cards_to_cut = random.randrange(1, len(self._deck))
+ self._deck.rotate(number_cards_to_cut)
+
+ def shuffle(self):
+ random.shuffle(self._deck)
+
+ def get_cards(self):
+ return list(self._deck)
+
+ def get_cards_deque(self):
+ return self._deck
+
+ def add_card(self, card: Card):
+ self._deck.append(card)
+
+ def pop_card(self):
+ return self._deck.pop()
+
+
+class Player:
+ def __init__(self):
+ self.cards = Deck([])
+
+ def get_cards(self):
+ return list(self.cards)
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.deck = Deck([])
+ self.players = deque()
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+
+ def get_players(self):

Грешно си интерпретирала инструкциите. Този метод се използва за да видим какви ирачи имаш, а не за да ги инициализираш. Играчите трябва да се инициализират още при инициализиране на класа. Този метод просто ни ги показва, но и без него трябва да можеш да работиш с класа.

+ i = 0
+ while i < self.number_of_players:
+ new_player = Player()
+ self.players.append(new_player)
+ i += 1
+ return self.players
+
+ def prepare_deck(self):

Тук не си имплементирала събирането на карти от играчите. Виждам, че по-долу се дефинирала ново тесте при конкретните игри, което до известна степен се справя с проблема, но идеологически погледнато, ако седнеш да играеш карти, не вадиш ново тесте на всяко раздаване. Не гарантирам, че всички тестове ще минат при тази имплементация.

+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, player):
+ index = self.players.index(player)
+ if self.dealing_direction == 'ltr':
+ self.players.rotate(index)
+ else:
+ self.players.rotate(0 - index)
+
+ for number_cards in self.dealing_instructions:
+ for curr_player in self.players:
+ card_count = 0
+ while card_count < number_cards:
+ curr_player.cards.add_card(self.deck.pop_card())
+ card_count += 1
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+
+ def prepare_deck(self):
+ self.deck = Deck(belot_faces)
+ self.deck.shuffle()
+ self.deck.cut()
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
+
+ def prepare_deck(self):
+ self.deck = Deck(poker_faces)
+ self.deck.shuffle()
+ self.deck.cut()

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