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

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

Към профила на Надежда Панделиева

Резултати

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

Код

from random import shuffle, randint
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(Card):
def __init__(self, face_filter=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self.face_filter = face_filter
self.suits = ['clubs', 'diamonds', 'hearts', 'spades']
self.cards = [Card(suit, face) for face in self.face_filter for suit in self.suits]
def cut(self):
random_length = randint(0, len(self.cards))
self.cards = self.cards[random_length:] + self.cards[:random_length]
def shuffle(self):
shuffle(self.cards)
def get_cards(self):
return self.cards
class Player(Card):
def __init__(self):
self.holding_cards = []
def get_cards(self):
return self.holding_cards
def add_cards(self, card):
self.holding_cards += card
class Game(Deck, Player):
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.dealing_instructions = dealing_instructions
self.dealing_direction = dealing_direction
self.number_of_players = number_of_players
self.players = [Player() for _ in range(self.number_of_players)]
self.initial_cards = Deck()
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
taken_cards, player.holding_cards = player.holding_cards[:], []
self.initial_cards.cards += taken_cards
self.initial_cards.shuffle()
self.initial_cards.cut()
def deal_helper(self, num_of_cards, n_players):
if self.dealing_direction == 'ltr':
for player in self.players[self.players.index(n_players):]:
given_cards = []
for card in range(num_of_cards):
given_cards.append(self.initial_cards.cards.pop())
player.add_cards(given_cards)
for player in self.players[:self.players.index(n_players)]:
given_cards = []
for card in range(num_of_cards):
given_cards.append(self.initial_cards.cards.pop())
player.add_cards(given_cards)
else:
for player in self.players[self.players.index(n_players)::-1]:
given_cards = []
for card in range(num_of_cards):
given_cards.append(self.initial_cards.cards.pop())
player.add_cards(given_cards)
for player in self.players[:self.players.index(n_players):-1]:
given_cards = []
for card in range(num_of_cards):
given_cards.append(self.initial_cards.cards.pop())
player.add_cards(given_cards)
def deal(self, number_of_player):
if len(self.players) * sum(self.dealing_instructions) > len(self.initial_cards.cards):
raise Exception('Not enough cards.')
for instruction in self.dealing_instructions:
self.deal_helper(instruction, number_of_player)
def get_deck(self):
return self.initial_cards.cards
def set_deck(self, deck):
self.initial_cards = deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
super().set_deck(Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A']))
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
super().set_deck(Deck())

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

EE......EEE..EE
======================================================================
ERROR: 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
AttributeError: 'list' object has no attribute 'get_cards'

======================================================================
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
AttributeError: 'list' object has no attribute 'get_cards'

======================================================================
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
AttributeError: 'list' object has no attribute 'get_cards'

======================================================================
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
AttributeError: 'list' object has no attribute 'get_cards'

======================================================================
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
AttributeError: 'list' object has no attribute 'get_cards'

======================================================================
ERROR: 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
AttributeError: 'list' object has no attribute 'get_cards'

======================================================================
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
AttributeError: 'list' object has no attribute 'get_cards'

----------------------------------------------------------------------
Ran 15 tests in 0.170s

FAILED (errors=7)

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

Надежда обнови решението на 15.11.2022 06:08 (преди около 2 години)

+from random import *

Ако ти трябва конкретен елемент ор модул, импортирай само него (from random import shuffle). Ако не знаеш какво ти трябва, импортирай целият модул (import random), след което си използвай конкретен обект, префиксирайски с модула (random.shufle(something)).
Още не сме говорили за модули, но да импортираш със звезда е антипатърн, защото рискуваш конфликт на имена.

+
+
+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(Card):
+ def __init__(self, face_filter=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
+ self.face_filter = face_filter
+ self.suits = ['clubs', 'diamonds', 'hearts', 'spades']
+ self.all_cards = [Card(suit, face) for face in self.face_filter for suit in self.suits]
+
+ def cut(self):
+ random_length = randint(0, int(len(self.all_cards)))
+ random_cards = self.all_cards[0:random_length]

self.all_cards = self.all_cards[random_length:] + self.all_cards[:random_length] прави същото.
Забележи, че нулата е ненужна. Слайсването така или иначе започва от нула и приключва с последния елемент.

+ del self.all_cards[0:random_length]
+ self.all_cards += random_cards
+
+ def shuffle(self):
+ shuffle(self.all_cards)
+
+ def get_cards(self):
+ return self.all_cards
+
+
+class Player(Card):
+ def __init__(self):
+ self.holding_cards = []
+
+ def get_cards(self):
+ return self.holding_cards
+
+ def add_cards(self, card):
+ self.holding_cards += card
+
+
+class Game(Deck, Player):
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.dealing_instructions = dealing_instructions
+ self.dealing_direction = dealing_direction
+ self.number_of_players = number_of_players
+ self.players = [Player() for _ in range(self.number_of_players)]
+ self.initial_cards = Deck()
+ self.left_cards = []
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ taken_cards, player.holding_cards[:] = player.holding_cards[:], []
+ self.left_cards += taken_cards
+
+ self.initial_cards.shuffle()
+ self.initial_cards.cut()
+ self.left_cards = self.initial_cards.get_cards()
+
+ def deal_helper(self, num_of_cards, n_players):
+ if self.dealing_direction == 'ltr':
+ for i, player in enumerate(self.players[self.players.index(n_players):]):
+ given_cards = []
+ for card in range(num_of_cards):
+ given_cards.append(self.left_cards.pop())
+ player.add_cards(given_cards)
+ for player in self.players[:self.players.index(n_players)]:
+ given_cards = []
+ for card in range(num_of_cards):
+ given_cards.append(self.left_cards.pop())
+ player.add_cards(given_cards)
+ else:
+ for player in self.players[self.players.index(n_players)::-1]:
+ given_cards = []
+ for card in range(num_of_cards):
+ given_cards.append(self.left_cards.pop())
+ player.add_cards(given_cards)
+ for player in self.players[:self.players.index(n_players):-1]:
+ given_cards = []
+ for card in range(num_of_cards):
+ given_cards.append(self.left_cards.pop())
+ player.add_cards(given_cards)
+
+ def deal(self, number_of_player):
+ if len(self.players) * sum(self.dealing_instructions) > len(self.left_cards):
+ raise Exception('Not enough cards.')
+
+ for instruction in self.dealing_instructions:
+ self.deal_helper(instruction, number_of_player)
+
+ def get_deck(self):
+ return self.left_cards

Обърка ме с двете променливи за карти, но съм почти сигурен, че това, което връщаш тук, не е един елемент от клас Deck, както е по условие, а просто списък с карти.

+
+ def set_deck(self, deck):
+ self.initial_cards = deck
+ self.left_cards = self.initial_cards.get_cards()
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+ super().set_deck(Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A']))
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
+ super().set_deck(Deck())

Надежда обнови решението на 15.11.2022 14:52 (преди около 2 години)

-from random import *
+from random import shuffle, randint
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(Card):
def __init__(self, face_filter=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self.face_filter = face_filter
self.suits = ['clubs', 'diamonds', 'hearts', 'spades']
- self.all_cards = [Card(suit, face) for face in self.face_filter for suit in self.suits]
+ self.cards = [Card(suit, face) for face in self.face_filter for suit in self.suits]
def cut(self):
- random_length = randint(0, int(len(self.all_cards)))
- random_cards = self.all_cards[0:random_length]
- del self.all_cards[0:random_length]
- self.all_cards += random_cards
+ random_length = randint(0, len(self.cards))
+ self.cards = self.cards[random_length:] + self.cards[:random_length]
def shuffle(self):
- shuffle(self.all_cards)
+ shuffle(self.cards)
def get_cards(self):
- return self.all_cards
+ return self.cards
class Player(Card):
def __init__(self):
self.holding_cards = []
def get_cards(self):
return self.holding_cards
def add_cards(self, card):
self.holding_cards += card
class Game(Deck, Player):
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.dealing_instructions = dealing_instructions
self.dealing_direction = dealing_direction
self.number_of_players = number_of_players
self.players = [Player() for _ in range(self.number_of_players)]
self.initial_cards = Deck()
- self.left_cards = []
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
- taken_cards, player.holding_cards[:] = player.holding_cards[:], []
- self.left_cards += taken_cards
+ taken_cards, player.holding_cards = player.holding_cards[:], []
+ self.initial_cards.cards += taken_cards
self.initial_cards.shuffle()
self.initial_cards.cut()
- self.left_cards = self.initial_cards.get_cards()
def deal_helper(self, num_of_cards, n_players):
if self.dealing_direction == 'ltr':
- for i, player in enumerate(self.players[self.players.index(n_players):]):
+ for player in self.players[self.players.index(n_players):]:
given_cards = []
for card in range(num_of_cards):
- given_cards.append(self.left_cards.pop())
+ given_cards.append(self.initial_cards.cards.pop())
player.add_cards(given_cards)
for player in self.players[:self.players.index(n_players)]:
given_cards = []
for card in range(num_of_cards):
- given_cards.append(self.left_cards.pop())
+ given_cards.append(self.initial_cards.cards.pop())
player.add_cards(given_cards)
else:
for player in self.players[self.players.index(n_players)::-1]:
given_cards = []
for card in range(num_of_cards):
- given_cards.append(self.left_cards.pop())
+ given_cards.append(self.initial_cards.cards.pop())
player.add_cards(given_cards)
for player in self.players[:self.players.index(n_players):-1]:
given_cards = []
for card in range(num_of_cards):
- given_cards.append(self.left_cards.pop())
+ given_cards.append(self.initial_cards.cards.pop())
player.add_cards(given_cards)
def deal(self, number_of_player):
- if len(self.players) * sum(self.dealing_instructions) > len(self.left_cards):
+ if len(self.players) * sum(self.dealing_instructions) > len(self.initial_cards.cards):
raise Exception('Not enough cards.')
for instruction in self.dealing_instructions:
self.deal_helper(instruction, number_of_player)
def get_deck(self):
- return self.left_cards
+ return self.initial_cards.cards
def set_deck(self, deck):
self.initial_cards = deck
- self.left_cards = self.initial_cards.get_cards()
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
super().set_deck(Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A']))
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
super().set_deck(Deck())