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

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

Към профила на Боян Богданов

Резултати

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

Код

import random
suits = {"clubs", "diamonds", "hearts", "spades"}
honors = {"J", "Q", "K", "A"}
faces = {str(x) for x in range(2, 11)}.union(honors)
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=list(faces)):
self._cards = []
for face in face_filter:
for suit in suits:
self._cards.append(Card(suit, face))
def cut(self):
cut_index = random.randint(1, len(self._cards) - 1)
self._cards = self._cards[cut_index:] + self._cards[:cut_index]
def shuffle(self):
random.shuffle(self._cards)
def get_cards(self):
return self._cards
def pop_card(self):
return self._cards.pop()
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
def add_card(self, card):
self._cards.append(card)
def clear_cards(self):
cards_copy = self._cards.copy()
self._cards.clear()
return cards_copy
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self._players = [Player() for _ in range(number_of_players)]
self._direction = dealing_direction
self._instructions = dealing_instructions
self._deck = Deck()
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
self._deck._cards.extend(player.clear_cards())
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
ordered_players = self._players
if (self._direction == 'rtl'):
ordered_players = list(reversed(self._players))
player_index = ordered_players.index(player)
ordered_players = ordered_players[player_index:] + \
ordered_players[:player_index]
for card_cnt in self._instructions:
for player in ordered_players if self._direction == "ltr" else reversed(ordered_players):
for _ in range(card_cnt):
player.add_card(self._deck.pop_card())
def set_deck(self, deck):
self._deck = deck
def get_deck(self):
return self._deck
def set_dealing_direction(self, dir):
self._direction = dir
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
belot_cards = ["7", "8", "9", "10"] + list(honors)
super().set_deck(Deck(belot_cards))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))
super().set_deck(Deck())

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

..........F...F
======================================================================
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: {('hearts', 'A'), ('clubs', '5'), ('hearts', '5')} not found in ({('clubs', '6'), ('hearts', '6'), ('hearts', '7')}, {('spades', '8'), ('diamonds', '8'), ('diamonds', 'A')})

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

----------------------------------------------------------------------
Ran 15 tests in 0.167s

FAILED (failures=2)

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

Боян обнови решението на 06.11.2022 21:34 (преди около 2 години)

+import random
+
+suits = {"clubs", "diamonds", "hearts", "spades"}
+honors = {"J", "Q", "K", "A"}
+faces = {str(x) for x in range(2, 10)}.union(honors)
+directions = {"ltr", "rtl"}
+
+
+class Card:
+ def __init__(self, suit, face):
+ if suit not in suits:
+ print("Bad suit value. Expected: " +
+ str(suits) + ". Actual: " + str(suit))
+ return
+ if face not in faces:
+ print("Bad face value. Expected: " +
+ str(faces) + ". Actual: " + str(face))
+ return
+ self.suit = suit
+ self.face = face
+
+ def get_suit(self):
+ return str(self.suit)
+
+ def get_face(self):
+ return str(self.face)
+
+
+class Deck:
+ def __init__(self, face_filter=list(faces)):
+ cards = []
+ for face in face_filter:
+ for suit in suits:
+ cards.append(Card(suit, face))
+ self.cards = cards
+
+ def cut(self):
+ cards_to_cut = random.randint(1, len(self.cards) - 1)
+ cards_copy = self.cards.copy()
+ self.cards = cards_copy[cards_to_cut:]
+ self.cards.extend(cards_copy[:cards_to_cut])
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def pop_card(self):
+ return self.cards.pop()
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+ def clear_cards(self):
+ x = self.cards.copy()
+ self.cards.clear()
+ return x
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.players = [Player() for x in range(0, number_of_players)]
+ self.direction = dealing_direction
+ self.instructions = dealing_instructions
+ self.deck = Deck()
+ self.has_dealt = False
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ if self.has_dealt == True:
+ cards = []
+ for player in self.players:
+ cards.extend(player.clear_cards())
+ self.deck.cards = cards
+ self.has_dealt = False
+ else:
+ self.has_dealt = True
+
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, player):
+
+ start_index = self.players.index(player)
+ ordered_players = self.players[start_index:]
+ ordered_players.extend(self.players[0:start_index])
+
+ for card_cnt in self.instructions:
+ for player in ordered_players if self.direction == "ltr" else reversed(ordered_players):
+ for deals in range(0, card_cnt):
+ player.add_card(self.deck.pop_card())
+
+ def set_deck(self, deck):
+ self.deck = deck
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, "ltr", (2, 3, 3))
+ belot_cards = ["7", "8", "9", "10"]
+ belot_cards.extend(honors)
+ super().set_deck(Deck(belot_cards))
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))
+ super().set_deck(Deck())

Боян обнови решението на 09.11.2022 21:04 (преди около 2 години)

import random
suits = {"clubs", "diamonds", "hearts", "spades"}
honors = {"J", "Q", "K", "A"}
-faces = {str(x) for x in range(2, 10)}.union(honors)
+faces = {str(x) for x in range(2, 11)}.union(honors)
directions = {"ltr", "rtl"}
class Card:
def __init__(self, suit, face):
if suit not in suits:
print("Bad suit value. Expected: " +

Ние няма да тестваме с невалидни неща, пък и ти сам правиш тестето и картите, но допълнителната проверка не е проблем. Проблемът е, че просто print-ваш. Ако ще индикираш грешка, хвърли VlueError.

str(suits) + ". Actual: " + str(suit))
return
if face not in faces:
print("Bad face value. Expected: " +
str(faces) + ". Actual: " + str(face))
return
self.suit = suit
self.face = face
def get_suit(self):
return str(self.suit)
def get_face(self):
return str(self.face)
class Deck:
def __init__(self, face_filter=list(faces)):
cards = []
for face in face_filter:
for suit in suits:
cards.append(Card(suit, face))
self.cards = cards
def cut(self):
cards_to_cut = random.randint(1, len(self.cards) - 1)
cards_copy = self.cards.copy()
self.cards = cards_copy[cards_to_cut:]
self.cards.extend(cards_copy[:cards_to_cut])
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def pop_card(self):
return self.cards.pop()
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
def add_card(self, card):
self.cards.append(card)
def clear_cards(self):
x = self.cards.copy()

Бих добавил нещо по-описателно от x. Да, съгласен съм, че е повече от очевидно какво правиш, но добрите навици се създават от простите случаи, а не от сложните.

self.cards.clear()
return x
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.players = [Player() for x in range(0, number_of_players)]
self.direction = dealing_direction
self.instructions = dealing_instructions
self.deck = Deck()
self.has_dealt = False
def get_players(self):
return self.players
def prepare_deck(self):
if self.has_dealt == True:

Ако ще проверяваш стойност на булева променлива, не го прави с ==. Просто if self.has_dealt.

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

cards = []
for player in self.players:
cards.extend(player.clear_cards())
self.deck.cards = cards
self.has_dealt = False
else:
self.has_dealt = True
self.deck.shuffle()
self.deck.cut()
def deal(self, player):
start_index = self.players.index(player)
ordered_players = self.players[start_index:]
ordered_players.extend(self.players[0:start_index])
for card_cnt in self.instructions:
for player in ordered_players if self.direction == "ltr" else reversed(ordered_players):
for deals in range(0, card_cnt):
player.add_card(self.deck.pop_card())
def set_deck(self, deck):
self.deck = deck
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
belot_cards = ["7", "8", "9", "10"]
belot_cards.extend(honors)
super().set_deck(Deck(belot_cards))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))
super().set_deck(Deck())

Боян обнови решението на 11.11.2022 18:44 (преди около 2 години)

import random
suits = {"clubs", "diamonds", "hearts", "spades"}
honors = {"J", "Q", "K", "A"}
faces = {str(x) for x in range(2, 11)}.union(honors)
-directions = {"ltr", "rtl"}
class Card:
def __init__(self, suit, face):
- if suit not in suits:
- print("Bad suit value. Expected: " +
- str(suits) + ". Actual: " + str(suit))
- return
- if face not in faces:
- print("Bad face value. Expected: " +
- str(faces) + ". Actual: " + str(face))
- return
- self.suit = suit
- self.face = face
+ self._suit = suit
+ self._face = face
def get_suit(self):
- return str(self.suit)
+ return self._suit
def get_face(self):
- return str(self.face)
+ return self._face
class Deck:
def __init__(self, face_filter=list(faces)):
- cards = []
+ self._cards = []
for face in face_filter:
for suit in suits:
- cards.append(Card(suit, face))
- self.cards = cards
+ self._cards.append(Card(suit, face))
def cut(self):
- cards_to_cut = random.randint(1, len(self.cards) - 1)
- cards_copy = self.cards.copy()
- self.cards = cards_copy[cards_to_cut:]
- self.cards.extend(cards_copy[:cards_to_cut])
+ cut_index = random.randint(1, len(self._cards) - 1)
+ self._cards = self._cards[cut_index:] + self._cards[:cut_index]
def shuffle(self):
- random.shuffle(self.cards)
+ random.shuffle(self._cards)
def get_cards(self):
- return self.cards
+ return self._cards
def pop_card(self):
- return self.cards.pop()
+ return self._cards.pop()
class Player:
def __init__(self):
- self.cards = []
+ self._cards = []
def get_cards(self):
- return self.cards
+ return self._cards
def add_card(self, card):
- self.cards.append(card)
+ self._cards.append(card)
def clear_cards(self):
- x = self.cards.copy()
- self.cards.clear()
- return x
+ cards_copy = self._cards.copy()
+ self._cards.clear()
+ return cards_copy
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
- self.players = [Player() for x in range(0, number_of_players)]
- self.direction = dealing_direction
- self.instructions = dealing_instructions
- self.deck = Deck()
- self.has_dealt = False
+ self._players = [Player() for _ in range(number_of_players)]
+ self._direction = dealing_direction
+ self._instructions = dealing_instructions
+ self._deck = Deck()
def get_players(self):
- return self.players
+ return self._players
def prepare_deck(self):
- if self.has_dealt == True:
- cards = []
- for player in self.players:
- cards.extend(player.clear_cards())
- self.deck.cards = cards
- self.has_dealt = False
- else:
- self.has_dealt = True
+ for player in self._players:
+ self._deck._cards.extend(player.clear_cards())
+ self._deck.shuffle()
+ self._deck.cut()
- self.deck.shuffle()
- self.deck.cut()
-
def deal(self, player):
+ ordered_players = self._players
+ if (self._direction == 'rtl'):
+ ordered_players = list(reversed(self._players))
- start_index = self.players.index(player)
- ordered_players = self.players[start_index:]
- ordered_players.extend(self.players[0:start_index])
+ player_index = ordered_players.index(player)
+ ordered_players = ordered_players[player_index:] + \
+ ordered_players[:player_index]
- for card_cnt in self.instructions:
- for player in ordered_players if self.direction == "ltr" else reversed(ordered_players):
- for deals in range(0, card_cnt):
- player.add_card(self.deck.pop_card())
+ for card_cnt in self._instructions:
+ for player in ordered_players if self._direction == "ltr" else reversed(ordered_players):
+ for _ in range(card_cnt):
+ player.add_card(self._deck.pop_card())
def set_deck(self, deck):
- self.deck = deck
+ self._deck = deck
def get_deck(self):
- return self.deck
+ return self._deck
+ def set_dealing_direction(self, dir):
+ self._direction = dir
+
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
- belot_cards = ["7", "8", "9", "10"]
- belot_cards.extend(honors)
+ belot_cards = ["7", "8", "9", "10"] + list(honors)
super().set_deck(Deck(belot_cards))
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))
super().set_deck(Deck())