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

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

Към профила на Иван Лаков

Резултати

  • 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 str(self._suit)
def get_face(self):
return str(self._face)
def __str__(self):
return f"{self._suit} | {self._face}"
class Deck:
def __init__(self, face_filter=None):
self._cards = []
if face_filter == None:
for i in range(13):

Далеч по-лесно и четимо ще е предварително да дефинираш списъка с карти като стрингове (където ако искаш пак можеш да използваш range и да кастнеш към стринг. Сега имаш петнайсетина реда, който просто кастват int към str.

match i:
case 0: cur_face = '2'
case 1: cur_face = '3'
case 2: cur_face = '4'
case 3: cur_face = '5'
case 4: cur_face = '6'
case 5: cur_face = '7'
case 6: cur_face = '8'
case 7: cur_face = '9'
case 8: cur_face = '10'
case 9: cur_face = 'J'
case 10: cur_face = 'Q'
case 11: cur_face = 'K'
case 12: cur_face = 'A'
for j in range(4):
match j:
case 0: cur_suit = 'clubs'
case 1: cur_suit = 'diamonds'
case 2: cur_suit = 'hearts'
case 3: cur_suit = 'spades'
card = Card(cur_suit, cur_face)
self._cards.append(card)
else:
for filter in face_filter:
for i in range(4):
match i:
case 0: cur_suit = 'clubs'
case 1: cur_suit = 'diamonds'
case 2: cur_suit = 'hearts'
case 3: cur_suit = 'spades'
card = Card(cur_suit, filter)
self._cards.append(card)
def cut(self):
# At least one card to be moved
index = random.randrange(1, len(self._cards))
self._cards = self._cards[index:] + self._cards[:index]
def shuffle(self):
random.shuffle(self._cards)
def get_cards(self):
return self._cards
def insert_cards(self, cards):
self._cards += cards
def pop_card(self):
return self._cards.pop()
def print(self):
for card in self._cards:
print(card)
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
def give_card(self, card):
self._cards.append(card)
def remove_cards(self):
self._cards = []
def print(self):
print("===== PLAYER =====")
for card in self._cards:
print(card)
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 _ in range(number_of_players):
player = Player()
self._players.append(player)
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
self._deck.insert_cards(player.get_cards())
player.remove_cards()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
player_index = self._players.index(player)
player_list = self._players[player_index:] + \
self._players[:player_index]
if self._dealing_direction == 'ltr':
player_list = player_list[::-1]
for instruction in self._dealing_instructions:
for pl in player_list:
for _ in range(instruction):
pl.give_card(self._deck.pop_card())
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', 'K'), ('hearts', '10'), ('diamonds', '7'), ('hearts', '7'), ('clubs', '10'), ('diamonds', 'K'), ('clubs', '7')} not found in ({('diamonds', '9'), ('diamonds', 'Q'), ('hearts', '9'), ('diamonds', '7'), ('hearts', 'Q'), ('clubs', '9'), ('clubs', 'Q'), ('clubs', '7')}, {('spades', 'A'), ('diamonds', '9'), ('diamonds', 'Q'), ('hearts', '9'), ('hearts', 'Q'), ('spades', '9'), ('spades', 'Q'), ('hearts', '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: {('diamonds', 'A'), ('spades', 'Q'), ('hearts', 'K')} not found in ({('hearts', '3'), ('clubs', '2'), ('spades', '2')}, {('spades', 'A'), ('clubs', 'A'), ('diamonds', 'K')})

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

----------------------------------------------------------------------
Ran 15 tests in 0.162s

FAILED (failures=4)

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

Иван обнови решението на 14.11.2022 22:31 (преди над 1 година)

+import random
+
+class Card:
+ def __init__(self, suit, face):
+ self._suit = suit
+ self._face = face
+
+ def get_suit(self):
+ return str(self._suit)
+
+ def get_face(self):
+ return str(self._face)
+
+ def __str__(self):
+ return f"{self._suit} | {self._face}"
+
+class Deck:
+ def __init__(self, face_filter = "@"):

При дефиниране на стойност по подразбиране не се слагат интервали около равното.
Освен това, по-добре стойността по подразбиране на е None. Това с някакъв случаен символ е просто хак.

+ self._cards = []
+
+ if face_filter == "@":
+ for i in range(13):

Далеч по-лесно и четимо ще е предварително да дефинираш списъка с карти като стрингове (където ако искаш пак можеш да използваш range и да кастнеш към стринг. Сега имаш петнайсетина реда, който просто кастват int към str.

+ match i:
+ case 0: cur_face = '2'
+ case 1: cur_face = '3'
+ case 2: cur_face = '4'
+ case 3: cur_face = '5'
+ case 4: cur_face = '6'
+ case 5: cur_face = '7'
+ case 6: cur_face = '8'
+ case 7: cur_face = '9'
+ case 8: cur_face = '10'
+ case 9: cur_face = 'J'
+ case 10: cur_face = 'Q'
+ case 11: cur_face = 'K'
+ case 12: cur_face = 'A'
+ for j in range(4):
+ match j:
+ case 0: cur_suit = 'clubs'
+ case 1: cur_suit = 'diamonds'
+ case 2: cur_suit = 'hearts'
+ case 3: cur_suit = 'spades'
+ card = Card(cur_face, cur_suit)
+ self._cards.append(card)
+ else:
+ for filter in face_filter:
+ for i in range(4):
+ match i:
+ case 0: cur_suit = 'clubs'
+ case 1: cur_suit = 'diamonds'
+ case 2: cur_suit = 'hearts'
+ case 3: cur_suit = 'spades'
+ card = Card(filter, cur_suit)
+ self._cards.append(card)
+
+ def cut(self):
+ index = random.randrange(1, len(self._cards)) # At least one card to be moved
+ self._cards = self._cards[index:] + self._cards[:index]
+
+ def shuffle(self):
+ random.shuffle(self._cards)
+
+ def get_cards(self):
+ return self._cards
+
+ def insert_cards(self, cards):
+ self._cards += cards
+
+ def pop_card(self):
+ return self._cards.pop()
+
+ def print(self):
+ for card in self._cards:
+ print(card)
+
+class Player:
+ def __init__(self):
+ self._cards = []
+
+ def get_cards(self):
+ return self._cards
+
+ def give_card(self, card):
+ self._cards.append(card)
+
+ def remove_cards(self):
+ self._cards = []
+
+ def print(self):
+ print("===== PLAYER =====")
+ for card in self._cards:
+ print(card)
+
+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 _ in range(number_of_players):
+ player = Player()
+ self._players.append(player)
+
+ def get_players(self):
+ return self._players
+
+ def prepare_deck(self):
+ for player in self._players:
+ self._deck.insert_cards(player.get_cards())
+ player.remove_cards()
+ self._deck.shuffle()
+ self._deck.cut()
+
+ def deal(self, player):
+ player_index = self._players.index(player)
+ player_list = self._players[player_index:] + self._players[:player_index]
+ if self._dealing_direction == 'ltr':
+ player_list = player_list[::-1]
+
+ for instruction in self._dealing_instructions:
+ for pl in player_list:
+ for _ in range(instruction):
+ pl.give_card(self._deck.pop_card())
+
+ 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))

Иван обнови решението на 15.11.2022 14:59 (преди над 1 година)

import random
+
class Card:
def __init__(self, suit, face):
self._suit = suit
self._face = face
-
+
def get_suit(self):
return str(self._suit)
def get_face(self):
return str(self._face)
def __str__(self):
return f"{self._suit} | {self._face}"
+
class Deck:
- def __init__(self, face_filter = "@"):
+ def __init__(self, face_filter=None):
self._cards = []
- if face_filter == "@":
+ if face_filter == None:
for i in range(13):
match i:
case 0: cur_face = '2'
case 1: cur_face = '3'
case 2: cur_face = '4'
case 3: cur_face = '5'
case 4: cur_face = '6'
case 5: cur_face = '7'
case 6: cur_face = '8'
case 7: cur_face = '9'
case 8: cur_face = '10'
case 9: cur_face = 'J'
case 10: cur_face = 'Q'
case 11: cur_face = 'K'
case 12: cur_face = 'A'
for j in range(4):
match j:
case 0: cur_suit = 'clubs'
case 1: cur_suit = 'diamonds'
case 2: cur_suit = 'hearts'
case 3: cur_suit = 'spades'
card = Card(cur_face, cur_suit)
self._cards.append(card)
else:
for filter in face_filter:
for i in range(4):
match i:
case 0: cur_suit = 'clubs'
case 1: cur_suit = 'diamonds'
case 2: cur_suit = 'hearts'
case 3: cur_suit = 'spades'
- card = Card(filter, cur_suit)
+ card = Card(cur_suit, filter)
self._cards.append(card)
-
+
def cut(self):
- index = random.randrange(1, len(self._cards)) # At least one card to be moved
+ # At least one card to be moved
+ index = random.randrange(1, len(self._cards))
self._cards = self._cards[index:] + self._cards[:index]
-
+
def shuffle(self):
random.shuffle(self._cards)
-
+
def get_cards(self):
return self._cards
def insert_cards(self, cards):
self._cards += cards
-
+
def pop_card(self):
return self._cards.pop()
def print(self):
for card in self._cards:
print(card)
+
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
def give_card(self, card):
self._cards.append(card)
def remove_cards(self):
self._cards = []
def print(self):
print("===== PLAYER =====")
for card in self._cards:
print(card)
+
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 _ in range(number_of_players):
player = Player()
self._players.append(player)
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
self._deck.insert_cards(player.get_cards())
player.remove_cards()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
player_index = self._players.index(player)
- player_list = self._players[player_index:] + self._players[:player_index]
+ player_list = self._players[player_index:] + \
+ self._players[:player_index]
if self._dealing_direction == 'ltr':
player_list = player_list[::-1]
for instruction in self._dealing_instructions:
for pl in player_list:
for _ in range(instruction):
pl.give_card(self._deck.pop_card())
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))

Иван обнови решението на 15.11.2022 15:39 (преди над 1 година)

import random
class Card:
def __init__(self, suit, face):
self._suit = suit
self._face = face
def get_suit(self):
return str(self._suit)
def get_face(self):
return str(self._face)
def __str__(self):
return f"{self._suit} | {self._face}"
class Deck:
def __init__(self, face_filter=None):
self._cards = []
if face_filter == None:
for i in range(13):
match i:
case 0: cur_face = '2'
case 1: cur_face = '3'
case 2: cur_face = '4'
case 3: cur_face = '5'
case 4: cur_face = '6'
case 5: cur_face = '7'
case 6: cur_face = '8'
case 7: cur_face = '9'
case 8: cur_face = '10'
case 9: cur_face = 'J'
case 10: cur_face = 'Q'
case 11: cur_face = 'K'
case 12: cur_face = 'A'
for j in range(4):
match j:
case 0: cur_suit = 'clubs'
case 1: cur_suit = 'diamonds'
case 2: cur_suit = 'hearts'
case 3: cur_suit = 'spades'
- card = Card(cur_face, cur_suit)
+ card = Card(cur_suit, cur_face)
self._cards.append(card)
else:
for filter in face_filter:
for i in range(4):
match i:
case 0: cur_suit = 'clubs'
case 1: cur_suit = 'diamonds'
case 2: cur_suit = 'hearts'
case 3: cur_suit = 'spades'
card = Card(cur_suit, filter)
self._cards.append(card)
def cut(self):
# At least one card to be moved
index = random.randrange(1, len(self._cards))
self._cards = self._cards[index:] + self._cards[:index]
def shuffle(self):
random.shuffle(self._cards)
def get_cards(self):
return self._cards
def insert_cards(self, cards):
self._cards += cards
def pop_card(self):
return self._cards.pop()
def print(self):
for card in self._cards:
print(card)
class Player:
def __init__(self):
self._cards = []
def get_cards(self):
return self._cards
def give_card(self, card):
self._cards.append(card)
def remove_cards(self):
self._cards = []
def print(self):
print("===== PLAYER =====")
for card in self._cards:
print(card)
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 _ in range(number_of_players):
player = Player()
self._players.append(player)
def get_players(self):
return self._players
def prepare_deck(self):
for player in self._players:
self._deck.insert_cards(player.get_cards())
player.remove_cards()
self._deck.shuffle()
self._deck.cut()
def deal(self, player):
player_index = self._players.index(player)
player_list = self._players[player_index:] + \
self._players[:player_index]
if self._dealing_direction == 'ltr':
player_list = player_list[::-1]
for instruction in self._dealing_instructions:
for pl in player_list:
for _ in range(instruction):
pl.give_card(self._deck.pop_card())
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))