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

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

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

Резултати

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

Код

import random
suits = ('clubs', 'diamonds', 'hearts', 'spades')
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:

Този клас трябва да инициализира тесте с 52 карти, дори да не е подаден face_filter. Нашите тестове ще опитат да го инстанцират без филтър и ще очакват карти.

def __init__(self, face_filter=('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')):
self.deck = []
for face in face_filter:
for suit in suits:
self.deck.append(Card(suit, face))
def cut(self):
new_beg = random.randint(0, len(self.deck) - 1)
left = self.deck[:new_beg]
right = self.deck[new_beg:]
self.deck = right + left
def shuffle(self):
random.shuffle(self.deck)
def get_cards(self):
return self.deck
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
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 = []
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.extend(player.get_cards())
self.deck.shuffle()
self.deck.cut()
def deal(self, player):
if self.dealing_direction == 'ltr':
new_beg = self.players.index(player)
left = self.deck[:new_beg]
right = self.deck[new_beg:]
self.players = right + left
elif self.dealing_direction == 'rtl':
new_beg = self.players.index(player)
left = self.deck[new_beg::-1]
right = self.deck[:new_beg:-1]
self.players = left + right
for count_of_cards in self.dealing_instructions:
for player in self.players:
while count_of_cards != 0:
count_of_cards -= 1
player.cards.append(self.deck.pop(0))
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self.face_filter = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
class Poker(Game):
def __init__(self):
super().__init__(4, 'rtl', (1, 1, 1, 1, 1))
self.face_filter = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

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

EE......EEEFEEE
======================================================================
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 'shuffle'

======================================================================
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_prepare_deck (test.TestGame)
Test preparing the deck for dealing.
----------------------------------------------------------------------
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 'shuffle'

======================================================================
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'

======================================================================
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.158s

FAILED (failures=1, errors=8)

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

Велислава обнови решението на 14.11.2022 13:46 (преди около 2 години)

+import random
+from random import randint
+
+suit_tuple = ('clubs', 'diamonds', 'hearts', 'spades')

Използвай suits вместо suit_tuple. Не е добра идея да слагаш типа на променливата в името. Не носи никаква информация, а някой ден може да се наложи да смениш типа.

+
+
+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:

Този клас трябва да инициализира тесте с 52 карти, дори да не е подаден face_filter. Нашите тестове ще опитат да го инстанцират без филтър и ще очакват карти.

+ def __init__(self, *face_filter):

Използването на звездата е подвеждащо. Реално ти не искаш да позволяваш повече от един аргумент, а го правиш. По-скоро деифнирай face_filter със стойност по подразбиране, което ше го направи optional.

+ self.deck = []
+ for face in face_filter:
+ for suit in suit_tuple:
+ self.deck.append(Card(suit, face))
+
+ def cut(self):
+ value = randint(0, len(self.deck) - 1)
+ left = []
+ right = []
+ for i in range(0, value):
+ left.append(self.deck[i])
+ for i in range(value, len(self.deck)):
+ right.append(self.deck[i])
+ self.deck = []
+ self.deck = right + left
+
+ def shuffle(self):
+ random.shuffle(self.deck)
+
+ def get_cards(self):
+ return self.deck
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.number_of_players = number_of_players
+ self.players = []
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+ self.newDeck = []
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ self.newDeck.extend(player.get_cards())
+ Deck.shuffle(self.newDeck)
+ Deck.cut(self.newDeck)
+
+ def deal(self, player):
+ left = []
+ right = []
+ if self.dealing_direction == 'ltr':
+ i = 0
+ while self.players[i] != player:
+ i += 1
+ left.append(self.players[i])
+ while i < self.number_of_players:
+ i += 1
+ right.append(self.players[i])
+ elif self.dealing_direction == 'rtl':
+ j = 0
+ while self.players[j] != player:
+ j += 1
+ index_of_player = j
+ while j < self.number_of_players:
+ j += 1
+ right.append(self.players[j])
+ while index_of_player > 0:
+ index_of_player -= 1
+ left.append(self.players[index_of_player])
+ self.players = []
+ self.players = right + left
+ for count_of_cards in self.dealing_instructions:
+ for player in self.players:
+ while count_of_cards != 0:
+ count_of_cards -= 1
+ player.cards.append(self.newDeck.pop(0))
+
+ def get_deck(self):
+ return self.newDeck
+
+
+class Belot(Game):
+ def __init__(self):
+ Game.__init__(self, 4, 'ltr', (2, 3, 3))
+ self.face_filter = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+
+
+class Poker(Game):
+ def __init__(self):
+ Game.__init__(self, 4, 'rtl', (1, 1, 1, 1, 1))
+ self.face_filter = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

Велислава обнови решението на 15.11.2022 01:07 (преди около 2 години)

import random
-from random import randint
-suit_tuple = ('clubs', 'diamonds', 'hearts', 'spades')
+suits = ('clubs', 'diamonds', 'hearts', 'spades')
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):
+ def __init__(self, face_filter=('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')):
self.deck = []
for face in face_filter:
- for suit in suit_tuple:
+ for suit in suits:
self.deck.append(Card(suit, face))
def cut(self):
- value = randint(0, len(self.deck) - 1)
- left = []
- right = []
- for i in range(0, value):
- left.append(self.deck[i])
- for i in range(value, len(self.deck)):
- right.append(self.deck[i])
- self.deck = []
+ new_beg = random.randint(0, len(self.deck) - 1)
+ left = self.deck[:new_beg]
+ right = self.deck[new_beg:]
self.deck = right + left
def shuffle(self):
random.shuffle(self.deck)
def get_cards(self):
return self.deck
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.number_of_players = number_of_players
- self.players = []
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
- self.newDeck = []
+ self.players = []
+ self.deck = []
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
- self.newDeck.extend(player.get_cards())
- Deck.shuffle(self.newDeck)
- Deck.cut(self.newDeck)
+ self.deck.extend(player.get_cards())
+ self.deck.shuffle()
+ self.deck.cut()
def deal(self, player):
- left = []
- right = []
if self.dealing_direction == 'ltr':
- i = 0
- while self.players[i] != player:
- i += 1
- left.append(self.players[i])
- while i < self.number_of_players:
- i += 1
- right.append(self.players[i])
+ new_beg = self.players.index(player)
+ left = self.deck[:new_beg]
+ right = self.deck[new_beg:]
+ self.players = right + left
elif self.dealing_direction == 'rtl':
- j = 0
- while self.players[j] != player:
- j += 1
- index_of_player = j
- while j < self.number_of_players:
- j += 1
- right.append(self.players[j])
- while index_of_player > 0:
- index_of_player -= 1
- left.append(self.players[index_of_player])
- self.players = []
- self.players = right + left
+ new_beg = self.players.index(player)
+ left = self.deck[new_beg::-1]
+ right = self.deck[:new_beg:-1]
+ self.players = left + right
for count_of_cards in self.dealing_instructions:
for player in self.players:
while count_of_cards != 0:
count_of_cards -= 1
- player.cards.append(self.newDeck.pop(0))
+ player.cards.append(self.deck.pop(0))
def get_deck(self):
- return self.newDeck
+ return self.deck
class Belot(Game):
def __init__(self):
- Game.__init__(self, 4, 'ltr', (2, 3, 3))
+ super().__init__(4, 'ltr', (2, 3, 3))
self.face_filter = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
class Poker(Game):
def __init__(self):
- Game.__init__(self, 4, 'rtl', (1, 1, 1, 1, 1))
+ super().__init__(4, 'rtl', (1, 1, 1, 1, 1))
self.face_filter = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']