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

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

Към профила на Петър Тодоров

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

import random
suits = ["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=("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")):
self.cards = []
self._face_filter = face_filter
for face in self._face_filter:
self.cards += [Card(suit, face) for suit in suits]
def cut(self):
if len(self.cards) <= 1:
return
if len(self.cards) == 2:
ind = 1
else:
ind = random.randrange(1, len(self.cards) - 1)
self.cards = self.cards[ind:] + self.cards[:ind]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return 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.deck = Deck()
self._players = [Player() for i in range(number_of_players)]
def get_players(self):
return self._players
def prepare_deck(self):
for p in self._players:
self.deck.cards += p.cards
p.cards = []
self.deck.shuffle()
self.deck.cut()
def deal(self, player):
current_index = self._players.index(player)
finish = current_index
n = len(self._players)
direction = 1
if self._dealing_direction == "rtl":
direction = -1
for i in self._dealing_instructions:
while 0 <= current_index < n:
self._players[current_index].cards += self.deck.cards[:i]
self.deck.cards = self.deck.cards[i:]
current_index += direction
current_index -= n * direction
while not current_index == finish:
self._players[current_index].cards += self.deck.cards[:i]
self.deck.cards = self.deck.cards[i:]
current_index += direction
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))

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

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

OK

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

Петър обнови решението на 08.11.2022 00:58 (преди над 1 година)

+import random
+
+suits = ["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):
+ self.cards = []
+ if not face_filter:
+ self.face_filter = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
+ else:
+ self.face_filter = face_filter[0]
+ for face in self.face_filter:
+ for suit in suits:
+ self.cards.append(Card(suit, face))
+
+ def cut(self):
+ self.cards = self.cards[1:] + [self.cards[0]]
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+
+class Player:
+ 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 = []
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for p in self.players:
+ self.deck += p.cards
+ p.cards = []
+ self.deck.shuffle()
+ self.deck.cut()
+
+ def deal(self, player):
+ current_index = players.index(player)
+ finish = current_index
+ n = len(self.players)
+ direction = 1
+ if self.dealing_direction == "rtl":
+ direction = -1
+ for i in self.dealing_instructions:
+ while 0 <= current_index < n:
+ self.players[current_index] += self.deck[:i]
+ self.deck = self.deck[i:]
+ current_index += direction
+ current_index -= (n - 1) * direction
+ while not current_index == finish:
+ self.players[current_index] += self.deck[:i]
+ self.deck = self.deck[i:]
+ current_index += direction
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, "ltr", (2, 3, 3))
+ self.deck = Deck()
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(6, "rtl", (1, 1, 1, 1, 1))

Петър обнови решението на 14.11.2022 00:08 (преди над 1 година)

import random
-
suits = ["clubs", "diamonds", "hearts", "spades"]
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):
self.cards = []
if not face_filter:
- self.face_filter = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
+ self._face_filter = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
else:
- self.face_filter = face_filter[0]
- for face in self.face_filter:
- for suit in suits:
- self.cards.append(Card(suit, face))
+ self._face_filter = face_filter[0]
+ for face in self._face_filter:
+ self.cards += [Card(suit, face) for suit in suits]
def cut(self):
self.cards = self.cards[1:] + [self.cards[0]]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return 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._number_of_players = number_of_players
+ self._dealing_direction = dealing_direction
+ self._dealing_instructions = dealing_instructions
self.deck = Deck()
- self.players = []
+ self._players = [Player() for i in range(number_of_players)]
def get_players(self):
- return self.players
+ return self._players
def prepare_deck(self):
- for p in self.players:
- self.deck += p.cards
+ for p in self._players:
+ self.deck.cards += p.cards
p.cards = []
self.deck.shuffle()
self.deck.cut()
def deal(self, player):
- current_index = players.index(player)
+ current_index = self._players.index(player)
finish = current_index
- n = len(self.players)
+ n = len(self._players)
direction = 1
- if self.dealing_direction == "rtl":
+ if self._dealing_direction == "rtl":
direction = -1
- for i in self.dealing_instructions:
+ for i in self._dealing_instructions:
while 0 <= current_index < n:
- self.players[current_index] += self.deck[:i]
- self.deck = self.deck[i:]
+ self._players[current_index].cards += self.deck.cards[:i]
+ self.deck.cards = self.deck.cards[i:]
current_index += direction
- current_index -= (n - 1) * direction
+ current_index -= n * direction
while not current_index == finish:
- self.players[current_index] += self.deck[:i]
- self.deck = self.deck[i:]
+ self._players[current_index].cards += self.deck.cards[:i]
+ self.deck.cards = self.deck.cards[i:]
current_index += direction
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
- self.deck = Deck()
+ self.deck = Deck(["7", "8", "9", "10", "J", "Q", "K", "A"])
class Poker(Game):
def __init__(self):
- super().__init__(6, "rtl", (1, 1, 1, 1, 1))
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))

Петър обнови решението на 14.11.2022 22:00 (преди над 1 година)

import random
suits = ["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):
+ def __init__(self, face_filter=("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")):
self.cards = []
- if not face_filter:
- self._face_filter = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
- else:
- self._face_filter = face_filter[0]
+ self._face_filter = face_filter
for face in self._face_filter:
self.cards += [Card(suit, face) for suit in suits]
def cut(self):
- self.cards = self.cards[1:] + [self.cards[0]]
+ if len(self.cards) <= 1:
+ return
+ if len(self.cards) == 2:
+ ind = 1
+ else:
+ ind = random.randrange(1, len(self.cards) - 1)
+ self.cards = self.cards[ind:] + self.cards[:ind]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return 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.deck = Deck()
self._players = [Player() for i in range(number_of_players)]
def get_players(self):
return self._players
def prepare_deck(self):
for p in self._players:
self.deck.cards += p.cards
p.cards = []
self.deck.shuffle()
self.deck.cut()
def deal(self, player):
current_index = self._players.index(player)
finish = current_index
n = len(self._players)
direction = 1
if self._dealing_direction == "rtl":
direction = -1
for i in self._dealing_instructions:
while 0 <= current_index < n:
self._players[current_index].cards += self.deck.cards[:i]
self.deck.cards = self.deck.cards[i:]
current_index += direction
current_index -= n * direction
while not current_index == finish:
self._players[current_index].cards += self.deck.cards[:i]
self.deck.cards = self.deck.cards[i:]
current_index += direction
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))