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

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

Към профила на Ангел Марински

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 13 успешни тест(а)
  • 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
BELOT_PLAYERS = 4
BELOT_DEAL = "ltr"
POKER_PLAYERS = 9
POKER_DEAL = "rtl"
belot_suits = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
default_deck = [
Card('clubs', 'A'),
Card('diamonds', 'A'),
Card('hearts', 'A'),
Card('spades', 'A'),
Card('clubs', '2'),
Card('diamonds', '2'),
Card('hearts', '2'),
Card('spades', '2'),
Card('clubs', '3'),
Card('diamonds', '3'),
Card('hearts', '3'),
Card('spades', '3'),
Card('clubs', '4'),
Card('diamonds', '4'),
Card('hearts', '4'),
Card('spades', '4'),
Card('clubs', '5'),
Card('diamonds', '5'),
Card('hearts', '5'),
Card('spades', '5'),
Card('clubs', '6'),
Card('diamonds', '6'),
Card('hearts', '6'),
Card('spades', '6'),
Card('clubs', '7'),
Card('diamonds', '7'),
Card('hearts', '7'),
Card('spades', '7'),
Card('clubs', '8'),
Card('diamonds', '8'),
Card('hearts', '8'),
Card('spades', '8'),
Card('clubs', '9'),
Card('diamonds', '9'),
Card('hearts', '9'),
Card('spades', '9'),
Card('clubs', '10'),
Card('diamonds', '10'),
Card('hearts', '10'),
Card('spades', '10'),
Card('clubs', 'J'),
Card('diamonds', 'J'),
Card('hearts', 'J'),
Card('spades', 'J'),
Card('clubs', 'Q'),
Card('diamonds', 'Q'),
Card('hearts', 'Q'),
Card('spades', 'Q'),
Card('clubs', 'K'),
Card('diamonds', 'K'),
Card('hearts', 'K'),
Card('spades', 'K'),
]
class Deck:
def __init__(self, face_filter=None):
if face_filter == None:
self.deck = default_deck
else:
self.deck = []
for suit in face_filter:
self.deck.append(Card(suit, 'clubs'))
self.deck.append(Card(suit, 'spades'))
self.deck.append(Card(suit, 'diamonds'))
self.deck.append(Card(suit, 'hearts'))
def cut(self):
cut_lenght = random.randint(1, len(self.deck))
cutted = self.deck[cut_lenght:len(self.deck):1]
del self.deck[-(len(self.deck) - cut_lenght):]
cutted.reverse()
for card in cutted:
self.deck.insert(0, card)
def shuffle(self):
random.shuffle(self.deck)
def get_cards(self):
return self.deck
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_instructions):
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = [Player() for _ in range(number_of_players)]
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
cards = player.get_cards()
self.deck.get_cards().extend(cards)
player.cards.clear()
self.deck.cut()
self.deck.shuffle()
def deal(self, player):
start_idx = self.players.index(player)
first_half = self.get_players()[:start_idx:1]
second_half = self.get_players()[start_idx::1]
players = second_half + first_half
if self.dealing_direction == "rtl":
players = players[:1:1] + players[1::1][::-1]
for instruction in self.dealing_instructions:
for player in players:
player.get_cards().extend(self.deck.get_cards()[:instruction])
del self.deck.get_cards()[:instruction]
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(BELOT_PLAYERS, BELOT_DEAL, (2, 3, 3))
self.deck = Deck(belot_suits)
class Poker(Game):
def __init__(self):
super().__init__(POKER_PLAYERS, POKER_DEAL, (1, 1, 1, 1, 1))

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

F....F.........
======================================================================
FAIL: 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
AssertionError: Items in the first set but not the second:
('clubs', '7')
('diamonds', 'Q')
('spades', 'Q')
('hearts', '10')
('diamonds', '7')
('spades', '7')
('hearts', 'K')
('hearts', '9')
('hearts', 'J')
('hearts', '8')
('clubs', '10')
('clubs', '9')
('spades', '10')
('clubs', 'J')
('hearts', 'A')
('clubs', 'K')
('diamonds', '10')
('spades', 'J')
('diamonds', 'J')
('spades', 'K')
('diamonds', 'K')
('spades', '9')
('diamonds', '9')
('hearts', 'Q')
('hearts', '7')
('clubs', '8')
('spades', '8')
('diamonds', '8')
('clubs', 'A')
('diamonds', 'A')
('spades', 'A')
('clubs', 'Q')
Items in the second set but not the first:
('9', 'clubs')
('7', 'spades')
('J', 'hearts')
('7', 'clubs')
('K', 'diamonds')
('A', 'hearts')
('8', 'diamonds')
('10', 'hearts')
('J', 'spades')
('A', 'spades')
('10', 'spades')
('J', 'clubs')
('Q', 'diamonds')
('8', 'hearts')
('K', 'hearts')
('10', 'clubs')
('A', 'clubs')
('K', 'spades')
('9', 'diamonds')
('8', 'spades')
('K', 'clubs')
('Q', 'hearts')
('7', 'diamonds')
('8', 'clubs')
('9', 'hearts')
('Q', 'spades')
('Q', 'clubs')
('J', 'diamonds')
('7', 'hearts')
('A', 'diamonds')
('9', 'spades')
('10', 'diamonds')

======================================================================
FAIL: test_init_filtered (test.TestDeck)
Test initialized cards with filter.
----------------------------------------------------------------------
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: Items in the first set but not the second:
('hearts', '2')
('hearts', '10')
('clubs', '5')
('spades', '5')
('hearts', 'J')
('diamonds', '5')
('clubs', '2')
('diamonds', '2')
('spades', '2')
('clubs', '10')
('clubs', 'J')
('spades', '10')
('hearts', 'A')
('diamonds', '10')
('spades', 'J')
('diamonds', 'J')
('hearts', '5')
('clubs', 'A')
('diamonds', 'A')
('spades', 'A')
Items in the second set but not the first:
('5', 'spades')
('2', 'diamonds')
('J', 'hearts')
('5', 'clubs')
('A', 'hearts')
('10', 'hearts')
('J', 'spades')
('A', 'spades')
('10', 'spades')
('2', 'hearts')
('J', 'clubs')
('A', 'clubs')
('10', 'clubs')
('2', 'spades')
('2', 'clubs')
('5', 'diamonds')
('J', 'diamonds')
('A', 'diamonds')
('5', 'hearts')
('10', 'diamonds')

----------------------------------------------------------------------
Ran 15 tests in 0.170s

FAILED (failures=2)

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

Ангел обнови решението на 15.11.2022 16:18 (преди около 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
+
+
+BELOT_PLAYERS = 4
+BELOT_DEAL = "ltr"
+POKER_PLAYERS = 9
+POKER_DEAL = "rtl"
+
+belot_suits = ['7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+
+default_deck = [
+ Card('clubs', 'A'),
+ Card('diamonds', 'A'),
+ Card('hearts', 'A'),
+ Card('spades', 'A'),
+
+ Card('clubs', '2'),
+ Card('diamonds', '2'),
+ Card('hearts', '2'),
+ Card('spades', '2'),
+
+ Card('clubs', '3'),
+ Card('diamonds', '3'),
+ Card('hearts', '3'),
+ Card('spades', '3'),
+
+ Card('clubs', '4'),
+ Card('diamonds', '4'),
+ Card('hearts', '4'),
+ Card('spades', '4'),
+
+ Card('clubs', '5'),
+ Card('diamonds', '5'),
+ Card('hearts', '5'),
+ Card('spades', '5'),
+
+ Card('clubs', '6'),
+ Card('diamonds', '6'),
+ Card('hearts', '6'),
+ Card('spades', '6'),
+
+ Card('clubs', '7'),
+ Card('diamonds', '7'),
+ Card('hearts', '7'),
+ Card('spades', '7'),
+
+ Card('clubs', '8'),
+ Card('diamonds', '8'),
+ Card('hearts', '8'),
+ Card('spades', '8'),
+
+ Card('clubs', '9'),
+ Card('diamonds', '9'),
+ Card('hearts', '9'),
+ Card('spades', '9'),
+
+ Card('clubs', '10'),
+ Card('diamonds', '10'),
+ Card('hearts', '10'),
+ Card('spades', '10'),
+
+ Card('clubs', 'J'),
+ Card('diamonds', 'J'),
+ Card('hearts', 'J'),
+ Card('spades', 'J'),
+
+ Card('clubs', 'Q'),
+ Card('diamonds', 'Q'),
+ Card('hearts', 'Q'),
+ Card('spades', 'Q'),
+
+ Card('clubs', 'K'),
+ Card('diamonds', 'K'),
+ Card('hearts', 'K'),
+ Card('spades', 'K'),
+]
+
+
+class Deck:
+ def __init__(self, face_filter=None):
+ if face_filter == None:
+ self.deck = default_deck
+ else:
+ self.deck = []
+ for suit in face_filter:
+ self.deck.append(Card(suit, 'clubs'))
+ self.deck.append(Card(suit, 'spades'))
+ self.deck.append(Card(suit, 'diamonds'))
+ self.deck.append(Card(suit, 'hearts'))
+
+ def cut(self):
+ cut_lenght = random.randint(1, len(self.deck))
+
+ cutted = self.deck[cut_lenght:len(self.deck):1]
+ del self.deck[-(len(self.deck) - cut_lenght):]
+
+ cutted.reverse()
+
+ for card in cutted:
+ self.deck.insert(0, card)
+
+ def shuffle(self):
+ random.shuffle(self.deck)
+
+ def get_cards(self):
+ return self.deck
+
+
+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_instructions):
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+
+ self.deck = Deck()
+ self.players = [Player() for _ in range(number_of_players)]
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ cards = player.get_cards()
+
+ self.deck.get_cards().extend(cards)
+ player.cards.clear()
+
+ self.deck.cut()
+ self.deck.shuffle()
+
+ def deal(self, player):
+ start_idx = self.players.index(player)
+ first_half = self.get_players()[:start_idx:1]
+ second_half = self.get_players()[start_idx::1]
+ players = second_half + first_half
+
+ if self.dealing_direction == "rtl":
+ players = players[:1:1] + players[1::1][::-1]
+
+ for instruction in self.dealing_instructions:
+ for player in players:
+ player.get_cards().extend(self.deck.get_cards()[:instruction])
+
+ del self.deck.get_cards()[:instruction]
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(BELOT_PLAYERS, BELOT_DEAL, (2, 3, 3))
+
+ self.deck = Deck(belot_suits)
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(POKER_PLAYERS, POKER_DEAL, (1, 1, 1, 1, 1))