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

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

Към профила на Адриана Атанасова

Резултати

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

Код

import random
card_number = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
card_suit = ['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:
def __init__(self, face_filter=card_number):
self.cards = []
for color in card_suit:
for number in face_filter:
self.cards.append(Card(color, number))
def cut(self):
length = len(self.cards)
random_number = random.randint(0, length - 1)
self.cards = self.cards[random_number:] + self.cards[:random_number]
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
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instruction):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instruction = dealing_instruction
self.players = []
self.deck = []
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.append(player.get_cards())
Deck.shuffle(self.deck)
Deck.cut(self.deck)
def deal(self, player):
first_players = []
second_players = []
index = 0
index_rtl = 0
index_ltr = 0
if self.dealing_direction == 'rtl':
for player_ in self.players:
index_rtl += 1
if self.players[player_] == player:
index = index_rtl
while index != 0:
first_players.append(self.players[index])
index -= 1
index = index_rtl
while index != len(self.players):
second_players.append(self.players[index])
index += 1
elif self.dealing_direction == 'ltr':
for player_ in self.players:
index_ltr += 1
if self.players[player_] == player:
index = index_ltr
while index != 0:
second_players.append(self.players[index])
index -= 1
index = index_ltr
while index != len(self.players):
first_players.append(self.players[index])
index += 1
self.players = []
self.players = first_players + second_players
for cards in self.dealing_instruction:
for player in self.players:
while cards != 0:
player.cards.append(self.deck.pop(0))
cards = cards - 1
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
Game.__init__(self, 4, 'ltr', (2, 3, 3))
self.cards_number = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
class Poker(Game):
def __init__(self):
Game.__init__(self, 9, 'rtl', (1, 1, 1, 1, 1))
self.cards_number = card_number

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

EE......EEEF.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 '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'

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

FAILED (failures=1, errors=7)

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

Адриана обнови решението на 14.11.2022 20:57 (преди около 2 години)

+import random
+card_number = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+card_colors = ['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:
+ def __init__(self, *face_filter):

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

+ self.cards = []
+ for color in card_colors:
+ for number in face_filter:
+ self.cards.append(Card(color, number))
+
+ def cut(self):
+ length = len(self.cards)
+ random_number = random.randint(0, length - 1)
+ first = []
+ second = []
+ for el in range(0, random_number):
+ first.append(self.cards[el])
+ for el in range(random_number, length):
+ second.append(self.cards[el])
+ self.cards = []
+ self.cards = second + first
+
+ 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
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instruction):
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instruction = dealing_instruction
+ self.players = []
+ self.deck = []
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ self.deck.append(player.get_cards())
+ Deck.shuffle(self.deck)
+ Deck.cut(self.deck)
+
+ def deal(self, player):
+ first_players = []
+ second_players = []
+ if self.dealing_direction == 'rtl':
+ for i in self.players:
+ if self.players[i] <= player:
+ second_players.append(self.players[i])
+ else:
+ first_players.append(self.players[i])
+ elif self.dealing_direction == 'ltr':
+ for i in self.players:
+ if self.players[i] >= player:
+ first_players.append(self.players[i])
+ else:
+ second_players.append(self.players[i])
+ self.players = []
+ self.players = first_players + second_players
+ for cards in self.dealing_instruction:
+ for player in self.players:
+ while cards != 0:
+ player.cards.append(self.deck.pop(0))
+ cards = cards - 1
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ Game.__init__(self, 4, 'ltr', (2, 3, 3))
+ self.cards_number = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+
+
+class Poker(Game):
+ def __init__(self):
+ Game.__init__(self, 9, 'rtl', (1, 1, 1, 1, 1))
+ self.cards_number = card_number

Адриана обнови решението на 15.11.2022 17:13 (преди около 2 години)

import random
+
+
card_number = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
-card_colors = ['clubs', 'diamonds', 'hearts', 'spades']
+card_suit = ['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=card_number):
self.cards = []
- for color in card_colors:
+ for color in card_suit:
for number in face_filter:
self.cards.append(Card(color, number))
def cut(self):
length = len(self.cards)
random_number = random.randint(0, length - 1)
- first = []
- second = []
- for el in range(0, random_number):
- first.append(self.cards[el])
- for el in range(random_number, length):
- second.append(self.cards[el])
- self.cards = []
- self.cards = second + first
+ self.cards = self.cards[random_number:] + self.cards[:random_number]
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
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instruction):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instruction = dealing_instruction
self.players = []
self.deck = []
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.append(player.get_cards())
Deck.shuffle(self.deck)
Deck.cut(self.deck)
def deal(self, player):
first_players = []
second_players = []
+ index = 0
+ index_rtl = 0
+ index_ltr = 0
if self.dealing_direction == 'rtl':
- for i in self.players:
- if self.players[i] <= player:
- second_players.append(self.players[i])
- else:
- first_players.append(self.players[i])
+ for player_ in self.players:
+ index_rtl += 1
+ if self.players[player_] == player:
+ index = index_rtl
+ while index != 0:
+ first_players.append(self.players[index])
+ index -= 1
+ index = index_rtl
+ while index != len(self.players):
+ second_players.append(self.players[index])
+ index += 1
elif self.dealing_direction == 'ltr':
- for i in self.players:
- if self.players[i] >= player:
- first_players.append(self.players[i])
- else:
- second_players.append(self.players[i])
+ for player_ in self.players:
+ index_ltr += 1
+ if self.players[player_] == player:
+ index = index_ltr
+ while index != 0:
+ second_players.append(self.players[index])
+ index -= 1
+ index = index_ltr
+ while index != len(self.players):
+ first_players.append(self.players[index])
+ index += 1
self.players = []
self.players = first_players + second_players
for cards in self.dealing_instruction:
for player in self.players:
while cards != 0:
player.cards.append(self.deck.pop(0))
cards = cards - 1
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
Game.__init__(self, 4, 'ltr', (2, 3, 3))
self.cards_number = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
class Poker(Game):
def __init__(self):
Game.__init__(self, 9, 'rtl', (1, 1, 1, 1, 1))
self.cards_number = card_number