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

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

Към профила на Александър Томов

Резултати

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

Код

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 = []):
self._face_filter = face_filter
self._all_faces = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
self._all_suits = ['clubs','diamonds','hearts','spades']
self._current_deck = []
if len(self._face_filter) == 0:
for suit in self._all_suits:
for face in self._all_faces:
self._current_deck.append(Card(suit, face))
else:
for suit in self._all_suits:
for face in self._face_filter:
self._current_deck.append(Card(suit, face))
def cut(self):
rand = random.randint(0, len(self._current_deck)-1)
self._current_deck = self._current_deck[rand:] + self._current_deck[:rand]
def shuffle(self):
return random.shuffle(self._current_deck)
def get_cards(self):
return self._current_deck
class Player:
def __init__(self, hand = []):
self.hand = 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._deck = Deck()
self._players = []
for i in range(number_of_players):
self._players.append(Player())
def get_players(self):
return self._players
def prepare_deck(self): #ne bachka
for pl in self._players:
for card in pl.hand:
self._deck._current_deck.append(pl.hand.pop())
self._deck.shuffle()
self._deck.cut()
def deal(self, pl):
ind = self._players.index(pl)
self._players = self._players[ind:] + self._players[:ind]
if self._dealing_direction == 'rtl':
top_index = self._players.pop(0)
self._players.reverse()
self._players.insert(0, top_index)
for deal_cards in self._dealing_instructions:
br = 0

br не е добро име на променлива. Сигурно идва от break, но е неясно. Предполагам, че ако напишеш for ще си спестиш тази променлива като цяло. просто трябва да си подредиш списъка по правилния начин с малко слайсване и сие.

while br < len(self._players):
for i in range(deal_cards):
self._players[br].hand.append(self._deck._current_deck.pop(0))
br = br+1
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))

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

.F.......FF...F
======================================================================
FAIL: 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
AssertionError: {('diamonds', '10'), ('clubs', '9'), ('hearts', 'A'), ('spades', '10'), ('spades', 'K'), ('diamonds', 'K'), ('diamonds', 'J'), ('spades', 'J'), ('hearts', '8'), ('clubs', '7'), ('hearts', 'Q'), ('spades', '9'), ('diamonds', '9'), ('clubs', 'A'), ('hearts', '10'), ('diamonds', '7'), ('spades', '7'), ('hearts', 'K'), ('diamonds', 'A'), ('clubs', '8'), ('spades', 'A'), ('clubs', 'Q'), ('hearts', 'J'), ('hearts', '9'), ('clubs', '10'), ('diamonds', '8'), ('clubs', 'K'), ('diamonds', 'Q'), ('spades', '8'), ('spades', 'Q'), ('hearts', '7'), ('clubs', 'J')} not found in ({('clubs', '7'), ('hearts', 'Q'), ('hearts', 'K'), ('diamonds', '9'), ('diamonds', '8'), ('clubs', '8'), ('hearts', 'J'), ('diamonds', '7')}, {('diamonds', '10'), ('hearts', 'Q'), ('hearts', 'A'), ('hearts', 'K'), ('diamonds', '9'), ('spades', 'K'), ('diamonds', '8'), ('spades', 'A')})

======================================================================
FAIL: 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
AssertionError: {('clubs', '7'), ('clubs', '9'), ('clubs', '10'), ('clubs', '5'), ('clubs', '8'), ('clubs', '6'), ('clubs', '3'), ('clubs', '2'), ('clubs', '4')} not found in ({('clubs', '5'), ('clubs', '2'), ('clubs', '8')}, {('spades', 'A'), ('spades', '8'), ('spades', 'J')})

======================================================================
FAIL: 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
AssertionError: {('clubs', '7'), ('clubs', '9'), ('clubs', '10'), ('clubs', '5'), ('clubs', 'K'), ('clubs', '8'), ('clubs', '6'), ('clubs', 'Q'), ('clubs', '3'), ('clubs', '2'), ('clubs', 'J'), ('clubs', '4')} not found in ({('clubs', '3'), ('clubs', '2'), ('clubs', '10')}, {('spades', 'A'), ('spades', 'K'), ('spades', '6')})

======================================================================
FAIL: 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
AssertionError: {('diamonds', '10'), ('clubs', '9'), ('hearts', 'A'), ('clubs', '5'), ('hearts', '6'), ('diamonds', 'K'), ('hearts', '2'), ('hearts', '4'), ('diamonds', '3'), ('spades', '3'), ('diamonds', 'J'), ('hearts', '8'), ('clubs', '7'), ('hearts', 'Q'), ('diamonds', '5'), ('diamonds', '9'), ('spades', '5'), ('clubs', '6'), ('clubs', 'A'), ('clubs', '2'), ('hearts', '10'), ('diamonds', '7'), ('clubs', '4'), ('spades', '7'), ('hearts', 'K'), ('hearts', '3'), ('diamonds', 'A'), ('clubs', '8'), ('clubs', 'Q'), ('diamonds', '6'), ('hearts', 'J'), ('spades', '2'), ('spades', '6'), ('diamonds', '2'), ('diamonds', '4'), ('hearts', '5'), ('hearts', '9'), ('spades', '4'), ('clubs', '10'), ('diamonds', '8'), ('clubs', 'K'), ('diamonds', 'Q'), ('clubs', '3'), ('hearts', '7'), ('clubs', 'J')} not found in ({('hearts', 'Q'), ('hearts', '3'), ('clubs', '2'), ('clubs', 'J'), ('diamonds', '7')}, {('spades', '5'), ('diamonds', 'K'), ('spades', 'A'), ('diamonds', '4'), ('hearts', '9')})

----------------------------------------------------------------------
Ran 15 tests in 0.179s

FAILED (failures=4)

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

Александър обнови решението на 15.11.2022 17:44 (преди над 1 година)

+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 = []):
+ self._face_filter = face_filter
+ self._all_faces = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
+ self._all_suits = ['clubs','diamonds','hearts','spades']
+ self._current_deck = []
+
+ if len(self._face_filter) == 0:
+ for suit in self._all_suits:
+ for face in self._all_faces:
+ self._current_deck.append(Card(suit, face))
+ else:
+ for suit in self._all_suits:
+ for face in self._face_filter:
+ self._current_deck.append(Card(suit, face))
+
+
+ def cut(self):
+ rand = random.randint(0, len(self._current_deck)-1)
+ self._current_deck = self._current_deck[rand:] + self._current_deck[:rand]
+
+ def shuffle(self):
+ return random.shuffle(self._current_deck)
+
+ def get_cards(self):
+ return self._current_deck
+
+class Player:
+ def __init__(self, hand = []):
+ self.hand = 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._deck = Deck()
+ self._players = []
+ for i in range(number_of_players):
+ self._players.append(Player())
+ def get_players(self):
+ return self._players
+
+ def prepare_deck(self): #ne bachka
+ for pl in self._players:
+ for card in pl.hand:
+ self._deck._current_deck.append(pl.hand.pop())
+ self._deck.shuffle()
+ self._deck.cut()
+
+ def deal(self, pl):
+ ind = self._players.index(pl)
+ self._players = self._players[ind:] + self._players[:ind]
+ if self._dealing_direction == 'rtl':
+ top_index = self._players.pop(0)
+ self._players.reverse()
+ self._players.insert(0, top_index)
+
+ for deal_cards in self._dealing_instructions:
+ br = 0

br не е добро име на променлива. Сигурно идва от break, но е неясно. Предполагам, че ако напишеш for ще си спестиш тази променлива като цяло. просто трябва да си подредиш списъка по правилния начин с малко слайсване и сие.

+ while br < len(self._players):
+ for i in range(deal_cards):
+ self._players[br].hand.append(self._deck._current_deck.pop(0))
+ br = br+1
+
+ 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))