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

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

Към профила на Лъчезар Цветков

Резултати

  • 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
def print_card(self):
print ("{} of {}".format(self.suit, self.face))
class Deck:
def __init__(self, face_filter=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self.cards = []
self.build(face_filter)
def cut(self):
number = random.randint(1, len(self.cards) - 1)
for _ in range(number):
self.cards.insert(0, self.cards.pop())
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def build(self, face_filter):
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in face_filter:
self.cards.append(Card(suit, face))
def print_deck(self):
for card in self.cards:
card.print_card()
def draw_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 print_hand(self):
for card in self.cards:
card.print_card()
def clear_hand(self):
self.cards = []
class Game:
def __init__(self, number_of_players, dealing_direction, dealing_instructions, faces=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.players = []
self.deck = Deck(faces)
for pl in range(self.number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for pl in self.players:
self.deck.cards.extend(pl.get_cards())
pl.clear_hand()
self.deck.shuffle()
self.deck.cut()
def deal(self, player):
playerIndex = self.players.index(player)
playerOrder = self.players[playerIndex:] + self.players[:playerIndex]
if(self.dealing_direction == 'ltr'):
playerOrder[1:] = reversed(playerOrder[1:])
for number_cards in self.dealing_instructions:
for pl in playerOrder:
for _ in range (number_cards):
pl.add_card(self.deck.draw_card())
def get_deck(self):
return self.deck
def set_faces(self, faces):
self.faces = faces
def print_deck(self):
self.deck.print_deck()
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3), ['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', 'Q'), ('clubs', '9'), ('clubs', '8'), ('diamonds', 'J'), ('clubs', '7'), ('spades', '7'), ('diamonds', 'K'), ('spades', '8')} not found in ({('diamonds', 'Q'), ('clubs', '9'), ('diamonds', '10'), ('hearts', 'A'), ('clubs', '10'), ('diamonds', 'J'), ('spades', '7'), ('spades', '8')}, {('hearts', '10'), ('clubs', 'K'), ('spades', 'Q'), ('hearts', 'J'), ('clubs', 'A'), ('spades', 'J'), ('diamonds', '7'), ('hearts', '9')})

======================================================================
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: {('spades', '9'), ('spades', 'Q'), ('spades', '6')} not found in ({('clubs', '3'), ('clubs', '9'), ('clubs', '6')}, {('spades', 'K'), ('spades', '10'), ('spades', '7')})

======================================================================
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: {('spades', '3'), ('spades', '8'), ('spades', '7')} not found in ({('clubs', '5'), ('clubs', '4'), ('clubs', 'J')}, {('spades', 'J'), ('spades', 'Q'), ('spades', '5')})

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

----------------------------------------------------------------------
Ran 15 tests in 0.160s

FAILED (failures=4)

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

Лъчезар обнови решението на 15.11.2022 13:59 (преди около 2 години)

+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
+
+
+
+ def print_card(self):
+ print ("{} of {}".format(self.suit, self.face))
+
+
+class Deck:
+ def __init__(self, face_filter=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
+ self.cards = []
+ self.build(face_filter)
+
+ def cut(self):
+ numb = random.randint(1,len(self.cards)-1)

Може да не знам за конкретно значение на думата, но ми се струва, че numb е е удачно име на тази променлива.
Моля слагай интервали след запетаите и около аритметични оператори като -.

+ for x in range(numb):
+ self.cards.insert(0, self.cards.pop())
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+
+
+ def build(self, face_filter):
+ for suit in ["clubs", "diamonds", "hearts", "spades"]:
+ for face in face_filter:
+ self.cards.append(Card(suit, face))
+
+ def print_deck(self):
+ for card in self.cards:
+ card.print_card()
+
+ def draw_card(self):
+ return self.cards.pop()
+
+
+
+class Player:
+ def __init__(self):
+ self.list_Cards = []

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

+
+ def get_cards(self):
+ return self.list_Cards
+
+
+
+ def add_card(self, card):
+ self.list_Cards.append(card)
+
+ def print_hand(self):
+ for card in self.list_Cards:
+ card.print_card()
+
+ def clear_hand(self):
+ self.list_Cards = []

Лъчезар обнови решението на 15.11.2022 15:43 (преди около 2 години)

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
-
-
def print_card(self):
print ("{} of {}".format(self.suit, self.face))
class Deck:
- def __init__(self, face_filter=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
+ def __init__(self, face_filter = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self.cards = []
self.build(face_filter)
def cut(self):
- numb = random.randint(1,len(self.cards)-1)
- for x in range(numb):
+ number = random.randint(1, len(self.cards) - 1)
+
+ for _ in range(number):
self.cards.insert(0, self.cards.pop())
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
-
-
def build(self, face_filter):
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in face_filter:
self.cards.append(Card(suit, face))
def print_deck(self):
for card in self.cards:
card.print_card()
def draw_card(self):
return self.cards.pop()
-
-
class Player:
def __init__(self):
- self.list_Cards = []
+ self.cards = []
def get_cards(self):
- return self.list_Cards
-
+ return self.cards
-
def add_card(self, card):
- self.list_Cards.append(card)
+ self.cards.append(card)
def print_hand(self):
- for card in self.list_Cards:
+ for card in self.cards:
card.print_card()
def clear_hand(self):
- self.list_Cards = []
+ 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 = []
+ for pl in range(self.number_of_players):
+ self.players.append(Player())
+
+ self.faces = []
+ self.deck = Deck()
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for pl in self.players:
+ pl.clear_hand()
+
+ self.deck = Deck(self.faces)
+
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, player):
+ for number_cards in self.dealing_instructions:
+ for value in range (number_cards):
+ player.add_card(self.deck.draw_card())
+
+ playerIndex = self.players.index(player)
+ print("Lentgh is: " + str(len(self.players))+"\n\n")
+ if self.dealing_direction == 'ltr':
+ while playerIndex < len(self.players):
+ playerIndex += 1
+ for value in range (number_cards):
+ self.players[playerIndex].add_card(self.deck.draw_card())
+ playerIndex += 1
+
+ playerIndex = self.players.index(player)
+
+ while playerIndex > 0:
+ playerIndex -= 1
+ for value in range (number_cards):
+ self.players[playerIndex].add_card(self.deck.draw_card())
+ playerIndex -= 1
+ else:
+ while playerIndex > 0:
+ playerIndex -= 1
+ for value in range (number_cards):
+ self.players[playerIndex].add_card(self.deck.draw_card())
+ playerIndex -= 1
+
+ playerIndex = self.players.index(player)
+
+ while playerIndex < len(self.players):
+ playerIndex += 1
+ for value in range (number_cards):
+ self.players[playerIndex].add_card(self.deck.draw_card())
+ playerIndex += 1
+
+ def get_deck(self):
+ return self.deck
+
+ def set_faces(self, faces):
+ self.faces = faces
+
+ def print_deck(self):
+ self.deck.print_deck()

Лъчезар обнови решението на 15.11.2022 16:50 (преди около 2 години)

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
def print_card(self):
print ("{} of {}".format(self.suit, self.face))
-
class Deck:
- def __init__(self, face_filter = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
+ def __init__(self, face_filter=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self.cards = []
self.build(face_filter)
def cut(self):
number = random.randint(1, len(self.cards) - 1)
for _ in range(number):
self.cards.insert(0, self.cards.pop())
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def build(self, face_filter):
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in face_filter:
self.cards.append(Card(suit, face))
def print_deck(self):
for card in self.cards:
card.print_card()
def draw_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 print_hand(self):
for card in self.cards:
card.print_card()
def clear_hand(self):
self.cards = []
class Game:
- def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions, faces=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
-
+
self.players = []
+ self.deck = Deck(faces)
for pl in range(self.number_of_players):
self.players.append(Player())
- self.faces = []
- self.deck = Deck()
-
def get_players(self):
return self.players
def prepare_deck(self):
for pl in self.players:
+ self.deck.cards.extend(pl.get_cards())
pl.clear_hand()
-
- self.deck = Deck(self.faces)
-
+
self.deck.shuffle()
self.deck.cut()
def deal(self, player):
+ playerIndex = self.players.index(player)
+ playerOrder = self.players[playerIndex:] + self.players[:playerIndex]
+
+ if(self.dealing_direction == 'ltr'):
+ playerOrder[1:] = reversed(playerOrder[1:])
+
for number_cards in self.dealing_instructions:
- for value in range (number_cards):
- player.add_card(self.deck.draw_card())
+ for pl in playerOrder:
+ for _ in range (number_cards):
+ pl.add_card(self.deck.draw_card())
- playerIndex = self.players.index(player)
- print("Lentgh is: " + str(len(self.players))+"\n\n")
- if self.dealing_direction == 'ltr':
- while playerIndex < len(self.players):
- playerIndex += 1
- for value in range (number_cards):
- self.players[playerIndex].add_card(self.deck.draw_card())
- playerIndex += 1
-
- playerIndex = self.players.index(player)
-
- while playerIndex > 0:
- playerIndex -= 1
- for value in range (number_cards):
- self.players[playerIndex].add_card(self.deck.draw_card())
- playerIndex -= 1
- else:
- while playerIndex > 0:
- playerIndex -= 1
- for value in range (number_cards):
- self.players[playerIndex].add_card(self.deck.draw_card())
- playerIndex -= 1
-
- playerIndex = self.players.index(player)
-
- while playerIndex < len(self.players):
- playerIndex += 1
- for value in range (number_cards):
- self.players[playerIndex].add_card(self.deck.draw_card())
- playerIndex += 1
-
def get_deck(self):
return self.deck
def set_faces(self, faces):
self.faces = faces
def print_deck(self):
- self.deck.print_deck()
+ self.deck.print_deck()
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3), ['7','8','9','10','J','Q','K','A'])
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
+