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

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

Към профила на Румен Тошев

Резултати

  • 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
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is None:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]:
self.cards.append(Card(suit, face))
else:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in face_filter:
self.cards.append(Card(suit, face))
def cut(self):
random_num = random.randint(1,len(self.cards))
first_half = self.cards[:random_num]
second_half = self.cards[random_num:]
self.cards = second_half + first_half
def get_cards(self):
return self.cards
def shuffle(self):
random.shuffle(self.cards)
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._players = [Player() for _ in range(self.number_of_players)]
self.deck = Deck()
def get_players(self):
return self._players
def deal(self, player):
index = self._players.index(player)
if self.dealing_direction == 'ltr':
first_half = self._players[:index]
second_half = self._players[index:]
self._players = second_half + first_half
for num in self.dealing_instructions:
for player in self._players:
[player.cards.append(self.deck.cards.pop())
for _ in range(num)]
else:
self.players.reverse()
first_half = self._players[:index]
second_half = self._players[index:]
self._players = second_half + first_half
for num in self.dealing_instructions:
for player in self._players:
[player.cards.append(self.cards.pop()) for _ in range(num)]
def prepare_deck(self):
for player in self._players:
[self.deck.cards.append(card) for card in player.cards]
player.cards.clear()
self.deck.shuffle()
self.deck.cut()
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(number_of_players=4,
dealing_direction="ltr", dealing_instructions=(2, 3, 3))
self.deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
super().__init__(number_of_players=9, dealing_direction="rtl",
dealing_instructions=(1, 1, 1, 1, 1))

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

..........E...E
======================================================================
ERROR: 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
AttributeError: 'Game' object has no attribute 'players'

======================================================================
ERROR: 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
AttributeError: 'Poker' object has no attribute 'players'

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

FAILED (errors=2)

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

Румен обнови решението на 11.11.2022 23:48 (преди около 2 години)

+from random import shuffle
+
+
+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 is None:
+ for suit in ["clubs", "diamonds", "hearts", "spades"]:
+ for face in ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]:
+ self.cards.append(Card(suit, face))
+ else:
+ for suit in ["clubs", "diamonds", "hearts", "spades"]:
+ for face in face_filter:
+ self.cards.append(Card(suit, face))
+
+ def cut(self):
+ deck_size = len(self.cards)

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

+ deck_mid = int(deck_size / 2)
+ first_half = self.cards[:deck_mid]
+ second_half = self.cards[deck_mid:]
+ self.cards = second_half + first_half
+
+ def get_cards(self):
+ return self.cards
+
+ def shuffle(self):
+ shuffle(self.cards)
+
+
+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.players = []
+ self.deck = Deck()
+
+ def get_players(self):
+ for _ in range(self.number_of_players):

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

Също така, можеш да пропуснеш temp:
self.players.append(Player())

+ temp = Player()
+ self.players.append(temp)
+ return self.players
+
+ def deal(self, player):
+ for i in range(len(self.players)):
+ if player == self.players[i]:
+ index = i
+ break
+ if self.dealing_direction == 'ltr':
+ first_half = self.players[:i]
+ second_half = self.players[i:]
+ self.players = second_half + first_half
+ for player in self.players:

Раздаването трябва да е наобратно. Вместо да даваш всички карти на един играч, преди да дадеш на следващия, всеки играч трябва да получи толкова карти, колкото е съответния броя в първия елемент на dealing_instructions, после всеки играч да получи от втория елемент и т.н.

+ for num in self.dealing_instructions:
+ [player.cards.append(self.deck.cards.pop())
+ for _ in range(num)]
+ else:
+ self.players.reverse()
+ first_half = self.players[:i]
+ second_half = self.players[i:]
+ self.players = second_half + first_half
+ for player in self.players:
+ for num in self.dealing_instructions:
+ [player.cards.append(self.cards.pop()) for _ in range(num)]
+
+ def prepare_deck(self):
+ for player in self.players:
+ [self.deck.cards.append(card) for card in player.cards]
+ player.cards.clear()
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def get_deck(self):
+ return self.deck.get_cards()
+
+
+class Belot(Game):
+
+ def __init__(self):
+ super().__init__(number_of_players = 4,
+ dealing_direction = "ltr", dealing_instructions = (2, 3, 3))
+ self.deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+
+
+class Poker(Game):
+
+ def __init__(self):
+ super().__init__(number_of_players = 9, dealing_direction = "rtl",
+ dealing_instructions=(1, 1, 1, 1, 1))

Румен обнови решението на 12.11.2022 22:53 (преди около 2 години)

-from random import shuffle
+import random
class Card:
def __init__(self, suit, face):
- self.suit = suit
- self.face = face
+ self._suit = suit
+ 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 is None:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]:
self.cards.append(Card(suit, face))
else:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in face_filter:
self.cards.append(Card(suit, face))
def cut(self):
- deck_size = len(self.cards)
- deck_mid = int(deck_size / 2)
- first_half = self.cards[:deck_mid]
- second_half = self.cards[deck_mid:]
+ random_num = random.randint(1,len(self.cards))
+ first_half = self.cards[:random_num]
+ second_half = self.cards[random_num:]
self.cards = second_half + first_half
def get_cards(self):
return self.cards
def shuffle(self):
- shuffle(self.cards)
+ random.shuffle(self.cards)
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.players = []
+ self._players = [Player() for _ in range(self.number_of_players)]
self.deck = Deck()
def get_players(self):
- for _ in range(self.number_of_players):
- temp = Player()
- self.players.append(temp)
- return self.players
+ return self._players
def deal(self, player):
- for i in range(len(self.players)):
- if player == self.players[i]:
- index = i
- break
+ index = self._players.index(player)
if self.dealing_direction == 'ltr':
- first_half = self.players[:i]
- second_half = self.players[i:]
- self.players = second_half + first_half
- for player in self.players:
- for num in self.dealing_instructions:
+ first_half = self._players[:index]
+ second_half = self._players[index:]
+ self._players = second_half + first_half
+ for num in self.dealing_instructions:
+ for player in self._players:
[player.cards.append(self.deck.cards.pop())
for _ in range(num)]
else:
self.players.reverse()
- first_half = self.players[:i]
- second_half = self.players[i:]
- self.players = second_half + first_half
- for player in self.players:
- for num in self.dealing_instructions:
+ first_half = self._players[:i]
+ second_half = self._players[i:]
+ self._players = second_half + first_half
+ for num in self.dealing_instructions:
+ for player in self._players:
[player.cards.append(self.cards.pop()) for _ in range(num)]
def prepare_deck(self):
- for player in self.players:
+ for player in self._players:
[self.deck.cards.append(card) for card in player.cards]
player.cards.clear()
self.deck.shuffle()
self.deck.cut()
def get_deck(self):
- return self.deck.get_cards()
+ return self.deck
class Belot(Game):
def __init__(self):
- super().__init__(number_of_players = 4,
- dealing_direction = "ltr", dealing_instructions = (2, 3, 3))
+ super().__init__(number_of_players=4,
+ dealing_direction="ltr", dealing_instructions=(2, 3, 3))
self.deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
- super().__init__(number_of_players = 9, dealing_direction = "rtl",
+ super().__init__(number_of_players=9, dealing_direction="rtl",
dealing_instructions=(1, 1, 1, 1, 1))

Румен обнови решението на 14.11.2022 14:28 (преди около 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
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is None:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]:
self.cards.append(Card(suit, face))
else:
for suit in ["clubs", "diamonds", "hearts", "spades"]:
for face in face_filter:
self.cards.append(Card(suit, face))
def cut(self):
random_num = random.randint(1,len(self.cards))
first_half = self.cards[:random_num]
second_half = self.cards[random_num:]
self.cards = second_half + first_half
def get_cards(self):
return self.cards
def shuffle(self):
random.shuffle(self.cards)
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._players = [Player() for _ in range(self.number_of_players)]
self.deck = Deck()
def get_players(self):
return self._players
def deal(self, player):
index = self._players.index(player)
if self.dealing_direction == 'ltr':
first_half = self._players[:index]
second_half = self._players[index:]
self._players = second_half + first_half
for num in self.dealing_instructions:
for player in self._players:
[player.cards.append(self.deck.cards.pop())
for _ in range(num)]
else:
self.players.reverse()
- first_half = self._players[:i]
- second_half = self._players[i:]
+ first_half = self._players[:index]
+ second_half = self._players[index:]
self._players = second_half + first_half
for num in self.dealing_instructions:
for player in self._players:
[player.cards.append(self.cards.pop()) for _ in range(num)]
def prepare_deck(self):
for player in self._players:
[self.deck.cards.append(card) for card in player.cards]
player.cards.clear()
self.deck.shuffle()
self.deck.cut()
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(number_of_players=4,
dealing_direction="ltr", dealing_instructions=(2, 3, 3))
self.deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
class Poker(Game):
def __init__(self):
super().__init__(number_of_players=9, dealing_direction="rtl",
dealing_instructions=(1, 1, 1, 1, 1))