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

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

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

Резултати

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

Код

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=('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')):
self.cards = [Card(suit, face) for face in face_filter for suit in ['clubs', 'diamonds', 'hearts', 'spades']]
def cut(self):
index_to_cut = random.randint(0, len(self.cards))
temp = self.cards[0:index_to_cut + 1]
del self.cards[0:index_to_cut + 1]
self.cards.extend(temp)
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
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() for _ in range(number_of_players)]
self.deck = Deck()
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.cards.extend(player.get_cards())
del player.hand
self.deck.shuffle()
self.deck.cut()
def deal(self, first_player):
if self.dealing_direction == 'rtl':
players = reversed(self.players)
else:
players = self.players
index = players.index(first_player)
temp_players = players[index:]
temp_players.extend(players[:index])
for number_of_cards in self.dealing_instructions:
for player in temp_players:
player.hand.extend(self.deck.cards[0:number_of_cards])
del self.deck.cards[0:number_of_cards]
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self.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))
self.deck = Deck()

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

........E.E...E
======================================================================
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: 'Player' object has no attribute 'hand'

======================================================================
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_reverseiterator' object has no attribute 'index'

======================================================================
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_reverseiterator' object has no attribute 'index'

----------------------------------------------------------------------
Ran 15 tests in 0.185s

FAILED (errors=3)

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

Пламена обнови решението на 15.11.2022 17:35 (преди около 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=('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')):
+ self.cards = [Card(suit, face) for face in face_filter for suit in ['clubs', 'diamonds', 'hearts', 'spades']]
+
+ def cut(self):
+ index_to_cut = random.randint(0, len(self.cards))
+ temp = self.cards[0:index_to_cut + 1]
+ del self.cards[0:index_to_cut + 1]
+ self.cards.extend(temp)
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+
+class Player:
+ def __init__(self):
+ self.hand = []
+
+ def get_cards(self):
+ return self.hand
+
+
+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() for _ in range(number_of_players)]
+ self.deck = Deck()
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ self.deck.cards.extend(player.get_cards())
+ del player.hand
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, first_player):
+ if self.dealing_direction == 'rtl':
+ players = reversed(self.players)
+ else:
+ players = self.players
+ index = players.index(first_player)
+ temp_players = players[index:]
+ temp_players.extend(players[:index])
+ for number_of_cards in self.dealing_instructions:
+ for player in temp_players:
+ player.hand.extend(self.deck.cards[0:number_of_cards])
+ del self.deck.cards[0:number_of_cards]
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))
+ self.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))
+ self.deck = Deck()