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

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

Към профила на Тина Томова

Резултати

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

Код

import random
card_face = ("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=None):
self.cards = []
if face_filter == None:
face_filter = card_face
for face in face_filter:
for suit in card_suit:
self.cards.append(Card(suit, face))
def get_cards(self):
return self.cards
def shuffle(self):
random.shuffle(self.cards)
def cut(self):
cards_to_cut = random.randrange(0, len(self.cards))
self.cards = self.cards[cards_to_cut:] + self.cards[:cards_to_cut]
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
def add_card(self, card):
self.cards.append(card)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.number_of_players = number_of_players
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
def prepare_deck(self):
for player in self.players:
self.deck.cards.extend(player.cards)
player.cards.clear()
self.deck.shuffle()
self.deck.cut()
def get_players(self):
first_player = random.randrange(0, self.number_of_players)
list_of_players = []
for position in range(first_player, self.number_of_players):
list_of_players.append(self.players[position])
for position in range(0, first_player):
list_of_players.append(self.players[position])
return list_of_players
def deal(self, player):
position = self.players.index(player)
if self.dealing_direction == "ltr":
for cards_to_deal in self.dealing_instructions:
for idx in range(position, self.number_of_players):
for _ in range(cards_to_deal):
self.players[idx].add_card(self.deck.cards.pop(0))
for idx in range(0, position):
for _ in range(cards_to_deal):
self.players[idx].add_card(self.deck.cards.pop(0))
else:
for cards_to_deal in self.dealing_instructions:
for idx in range(position, -1, -1):
for _ in range(cards_to_deal):
self.players[idx].add_card(self.deck.cards.pop(0))
for idx in range(self.number_of_players - 1, position, -1):
for _ in range(cards_to_deal):
self.players[idx].add_card(self.deck.cards.pop(0))
def get_deck(self):
return self.deck
class Belot(Game):

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

def __init__(self):
super(Belot,self).__init__(4, "ltr", (2, 3, 3))
self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self):
super(Poker, self).__init__(9, "rtl", (1, 1, 1, 1, 1))

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

....F..........
======================================================================
FAIL: test_cutting_deck (test.TestDeck)
Test cutting a 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
AssertionError: [<solution.Card object at 0xffff8b09a080>, <solution.Card object at 0xffff8b09a0e0>, <solution.Card object at 0xffff8b09a140>, <solution.Card object at 0xffff8b09a1a0>, <solution.Card object at 0xffff8b09a200>, <solution.Card object at 0xffff8b09a260>, <solution.Card object at 0xffff8b09a2c0>, <solution.Card object at 0xffff8b09a320>, <solution.Card object at 0xffff8b09a380>, <solution.Card object at 0xffff8b09a3e0>, <solution.Card object at 0xffff8b09a440>, <solution.Card object at 0xffff8b09a4a0>, <solution.Card object at 0xffff8b09a500>, <solution.Card object at 0xffff8b09a560>, <solution.Card object at 0xffff8b09a5c0>, <solution.Card object at 0xffff8b09a620>, <solution.Card object at 0xffff8b09a680>, <solution.Card object at 0xffff8b09a6e0>, <solution.Card object at 0xffff8b09a740>, <solution.Card object at 0xffff8b09a7a0>] == [<solution.Card object at 0xffff8b09a080>, <solution.Card object at 0xffff8b09a0e0>, <solution.Card object at 0xffff8b09a140>, <solution.Card object at 0xffff8b09a1a0>, <solution.Card object at 0xffff8b09a200>, <solution.Card object at 0xffff8b09a260>, <solution.Card object at 0xffff8b09a2c0>, <solution.Card object at 0xffff8b09a320>, <solution.Card object at 0xffff8b09a380>, <solution.Card object at 0xffff8b09a3e0>, <solution.Card object at 0xffff8b09a440>, <solution.Card object at 0xffff8b09a4a0>, <solution.Card object at 0xffff8b09a500>, <solution.Card object at 0xffff8b09a560>, <solution.Card object at 0xffff8b09a5c0>, <solution.Card object at 0xffff8b09a620>, <solution.Card object at 0xffff8b09a680>, <solution.Card object at 0xffff8b09a6e0>, <solution.Card object at 0xffff8b09a740>, <solution.Card object at 0xffff8b09a7a0>]

----------------------------------------------------------------------
Ran 15 tests in 0.165s

FAILED (failures=1)

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

Тина обнови решението на 12.11.2022 02:06 (преди около 2 години)

+import random
+from turtle import position

Това като че ли текстовият ти редактор е добавил сам. Не се използва.

Оставяй два нови реда между код на топ ниво - след импортите, около дефиниране на променливи, около класовете...

+card_face = ("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):
+ if suit in card_suit:
+ self.suit = suit
+ if face in card_face:

Може би е добра идея да хвършил грешка, ако подадеш невалидна стойност и този if не се изпълни. Ние няма да тестваме за такива неща, но след като изрично си сложила if, би било добре да довършиш логиката си.

+ self.face = face
+
+ def get_suit(self):
+ return self.suit
+
+ def get_face(self):
+ return self.face
+
+
+class Deck:
+ def __init__(self, face_filter = None):
+ self.cards = []
+ if face_filter == None:
+ for face in card_face:
+ for suit in card_suit:
+ self.cards.append(Card(suit, face))
+ else:
+ for face in face_filter:
+ for suit in card_suit:
+ self.cards.append(Card(suit, face))
+
+ def get_cards(self):
+ return self.cards
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def cards_length(self):

Имайки предвид, че това ползваш само на едно място, вътре в самия клас, а реално ти трябват повече символи да извикаш метода, бух минал и без него. Направо си ползвай len в cut метода.

+ return len(self.cards)
+
+ def cut(self):
+ cards_to_cut = random.randrange(1, self.cards_length())
+ for i in range(1, cards_to_cut):

Ако обхождаш с променлива, която няма да използваш, изполвай _ по конвенция.
Освен това не виждам причина изрично да започваш от 1. Числото е случайно, можеш просто да кажеш for _ in range(cards_to_cut):

+ self.cards += [self.cards.pop(0)]
+
+
+class Player:
+ def __init__(self):
+ self.cards = []
+
+ def get_cards(self):
+ return self.cards
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+
+class Game:
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.number_of_players = number_of_players
+ self.players = []
+ for i in range(0, number_of_players):
+ self.players.append(Player())
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+ self.deck = Deck()
+
+ def prepare_deck(self):
+ for player in self.players:
+ while len(player.cards) != 0:
+ self.deck.cards.append(player.cards[0])
+ player.cards.pop(0)
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def get_players(self):
+ first_player = random.randrange(0, self.number_of_players)
+ list_of_players = []
+ for position in range(first_player, self.number_of_players):
+ list_of_players.append(self.players[position])
+ for position in range(0, first_player):
+ list_of_players.append(self.players[position])
+ return list_of_players
+
+ def deal(self, player):
+ position = 0
+ for pos in range(0, self.number_of_players):
+ if player == self.players[pos]:
+ position = pos
+ break
+ for cards_to_deal in self.dealing_instructions:
+ for idx in range(position, self.number_of_players):
+ for i in range(0, cards_to_deal):
+ self.players[idx].add_card(self.deck.cards.pop(0))
+ for idx in range(0, position):
+ for i in range(0, cards_to_deal):
+ self.players[idx].add_card(self.deck.cards.pop(0))
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):

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

+ def __init__(self):
+ self.number_of_players = 4
+ self.dealing_direction = "ltr"
+ self.dealing_instructions = (2, 3, 3)
+ self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
+ self.players = []
+ for i in range(0, 4):
+ self.players.append(Player())
+
+ def deal(self, player):

Същото и тук. Да, така работи, но ако вземеш предвид посоката в оригиналния Game, ще спестиш дублиране на код. Освен това, ние ще тестваме Game и самостоятелно - той трябва да е способен да взима предвид посоката без да е наследен.

+ position = 0
+ for pos in range(0, self.number_of_players):
+ if player == self.players[pos]:
+ position = pos
+ break
+ for cards_to_deal in self.dealing_instructions:
+ for idx in range(position, -1, -1):
+ for i in range(0, cards_to_deal):
+ self.players[idx].add_card(self.deck.cards.pop(0))
+ for idx in range(self.number_of_players - 1, position, -1):
+ for i in range(0, cards_to_deal):
+ self.players[idx].add_card(self.deck.cards.pop(0))
+
+
+class Poker(Game):
+ def __init__(self):
+ self.number_of_players = 9
+ self.dealing_direction = "rtl"
+ self.dealing_instructions = (1, 1, 1, 1, 1)
+ self.deck = Deck()
+ self.players = []
+ for i in range(0, 9):
+ self.players.append(Player())

Тина обнови решението на 14.11.2022 18:00 (преди около 2 години)

import random
-from turtle import position
+
+
card_face = ("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):
if suit in card_suit:
- self.suit = suit
+ self._suit = suit
if face in card_face:
- self.face = face
+ 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 = None):
+ def __init__(self, face_filter=None):
self.cards = []
if face_filter == None:
- for face in card_face:
- for suit in card_suit:
- self.cards.append(Card(suit, face))
- else:
- for face in face_filter:
- for suit in card_suit:
- self.cards.append(Card(suit, face))
+ face_filter = card_face
+ for face in face_filter:
+ for suit in card_suit:
+ self.cards.append(Card(suit, face))
def get_cards(self):
return self.cards
def shuffle(self):
random.shuffle(self.cards)
- def cards_length(self):
- return len(self.cards)
-
def cut(self):
- cards_to_cut = random.randrange(1, self.cards_length())
- for i in range(1, cards_to_cut):
- self.cards += [self.cards.pop(0)]
+ cards_to_cut = random.randrange(1, len(self.cards))
+ for _ in range(cards_to_cut):
+ self.cards = self.cards[cards_to_cut:] + self.cards[:cards_to_cut]
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
def add_card(self, card):
self.cards.append(card)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.number_of_players = number_of_players
self.players = []
- for i in range(0, number_of_players):
+ for _ in range(number_of_players):
self.players.append(Player())
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
def prepare_deck(self):
for player in self.players:
- while len(player.cards) != 0:
- self.deck.cards.append(player.cards[0])
- player.cards.pop(0)
+ self.deck.cards.extend(player.cards)
+ player.cards.clear()
self.deck.shuffle()
self.deck.cut()
def get_players(self):
first_player = random.randrange(0, self.number_of_players)
list_of_players = []
for position in range(first_player, self.number_of_players):
list_of_players.append(self.players[position])
for position in range(0, first_player):
list_of_players.append(self.players[position])
return list_of_players
def deal(self, player):
- position = 0
- for pos in range(0, self.number_of_players):
- if player == self.players[pos]:
- position = pos
- break
- for cards_to_deal in self.dealing_instructions:
- for idx in range(position, self.number_of_players):
- for i in range(0, cards_to_deal):
- self.players[idx].add_card(self.deck.cards.pop(0))
- for idx in range(0, position):
- for i in range(0, cards_to_deal):
- self.players[idx].add_card(self.deck.cards.pop(0))
+ position = self.players.index(player)
+ if self.dealing_direction == "ltr":
+ for cards_to_deal in self.dealing_instructions:
+ for idx in range(position, self.number_of_players):
+ for _ in range(cards_to_deal):
+ self.players[idx].add_card(self.deck.cards.pop(0))
+ for idx in range(0, position):
+ for _ in range(cards_to_deal):
+ self.players[idx].add_card(self.deck.cards.pop(0))
+ else:
+ for cards_to_deal in self.dealing_instructions:
+ for idx in range(position, -1, -1):
+ for _ in range(cards_to_deal):
+ self.players[idx].add_card(self.deck.cards.pop(0))
+ for idx in range(self.number_of_players - 1, position, -1):
+ for _ in range(cards_to_deal):
+ self.players[idx].add_card(self.deck.cards.pop(0))
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
- self.number_of_players = 4
- self.dealing_direction = "ltr"
- self.dealing_instructions = (2, 3, 3)
+ super(Belot,self).__init__(4, "ltr", (2, 3, 3))
self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
- self.players = []
- for i in range(0, 4):
- self.players.append(Player())
- def deal(self, player):
- position = 0
- for pos in range(0, self.number_of_players):
- if player == self.players[pos]:
- position = pos
- break
- for cards_to_deal in self.dealing_instructions:
- for idx in range(position, -1, -1):
- for i in range(0, cards_to_deal):
- self.players[idx].add_card(self.deck.cards.pop(0))
- for idx in range(self.number_of_players - 1, position, -1):
- for i in range(0, cards_to_deal):
- self.players[idx].add_card(self.deck.cards.pop(0))
-
class Poker(Game):
def __init__(self):
- self.number_of_players = 9
- self.dealing_direction = "rtl"
+ super(Poker, self).__init__(9, "rtl", (1, 1, 1, 1, 1))
- self.dealing_instructions = (1, 1, 1, 1, 1)
- self.deck = Deck()
- self.players = []
- for i in range(0, 9):
- self.players.append(Player())

Тина обнови решението на 15.11.2022 16:52 (преди около 2 години)

import random
card_face = ("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):
- if suit in card_suit:
- self._suit = suit
- if face in card_face:
- self._face = 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=None):
self.cards = []
if face_filter == None:
face_filter = card_face
for face in face_filter:
for suit in card_suit:
self.cards.append(Card(suit, face))
def get_cards(self):
return self.cards
def shuffle(self):
random.shuffle(self.cards)
def cut(self):
- cards_to_cut = random.randrange(1, len(self.cards))
- for _ in range(cards_to_cut):
- self.cards = self.cards[cards_to_cut:] + self.cards[:cards_to_cut]
+ cards_to_cut = random.randrange(0, len(self.cards))
+ self.cards = self.cards[cards_to_cut:] + self.cards[:cards_to_cut]
class Player:
def __init__(self):
self.cards = []
def get_cards(self):
return self.cards
def add_card(self, card):
self.cards.append(card)
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.number_of_players = number_of_players
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
def prepare_deck(self):
for player in self.players:
self.deck.cards.extend(player.cards)
player.cards.clear()
self.deck.shuffle()
self.deck.cut()
def get_players(self):
first_player = random.randrange(0, self.number_of_players)
list_of_players = []
for position in range(first_player, self.number_of_players):
list_of_players.append(self.players[position])
for position in range(0, first_player):
list_of_players.append(self.players[position])
return list_of_players
def deal(self, player):
position = self.players.index(player)
if self.dealing_direction == "ltr":
for cards_to_deal in self.dealing_instructions:
for idx in range(position, self.number_of_players):
for _ in range(cards_to_deal):
self.players[idx].add_card(self.deck.cards.pop(0))
for idx in range(0, position):
for _ in range(cards_to_deal):
self.players[idx].add_card(self.deck.cards.pop(0))
else:
for cards_to_deal in self.dealing_instructions:
for idx in range(position, -1, -1):
for _ in range(cards_to_deal):
self.players[idx].add_card(self.deck.cards.pop(0))
for idx in range(self.number_of_players - 1, position, -1):
for _ in range(cards_to_deal):
self.players[idx].add_card(self.deck.cards.pop(0))
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super(Belot,self).__init__(4, "ltr", (2, 3, 3))
self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self):
super(Poker, self).__init__(9, "rtl", (1, 1, 1, 1, 1))
+