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

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

Към профила на Илия Димов

Резултати

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

Код

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 = []):
self._face_filter = face_filter
self.cards = []
self.__suit_types = ['clubs', 'diamonds', 'hearts', 'spades']
self.__face_types = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
if len(face_filter) != 0:
self.__face_types = self._face_filter
for suit in self.__suit_types:
for face in self.__face_types:
self.cards.append(Card(suit, face))
def cut(self):
random_number = random.randint(0, len(self.cards) - 1)
self.cards = self.cards[random_number:] + self.cards[:random_number]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game(Deck):
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()
while number_of_players != 0:
self._players.append(Player())
number_of_players = number_of_players - 1
def get_players(self):
return self._players
def deal(self, ind_player):
if self._dealing_direction == 'ltr':

Много голяма част от тялото на if-а и else-а съвпадат. Би било добре да ги обединиш, като предварително определеш реда на играчите и го запазиш в някаква локална променлива.

index_instructions = 0
while index_instructions < len(self._dealing_instructions):
curr_player = self._players.index(ind_player)
cards_dealt = self._dealing_instructions[index_instructions]
while curr_player < len(self._players):
i = 0
while i < cards_dealt:
self._players[curr_player].hand.append(self._deck.cards.pop(0))
i+=1
curr_player = curr_player + 1
count = 0
curr_player = self._players.index(ind_player)
while count < curr_player:
i = 0
while i < cards_dealt:
self._players[count].hand.append(self._deck.cards.pop(0))
i+=1
count = count + 1
index_instructions = index_instructions + 1
else:
index_instructions = 0
while index_instructions < len(self._dealing_instructions):
curr_player = self._players.index(ind_player)
cards_dealt = self._dealing_instructions[index_instructions]
count = 0
while curr_player >= count:
i = 0
while i < cards_dealt:
self._players[curr_player].hand.append(self._deck.cards.pop(0))
i+=1
curr_player = curr_player - 1
count = len(self._players) - 1
curr_player = self._players.index(ind_player)
while count > curr_player:
i = 0
while i < cards_dealt:
self._players[count].hand.append(self._deck.cards.pop(0))
i+=1
count = count - 1
index_instructions = index_instructions + 1
def prepare_deck(self):
for curr_player in self._players:
while len(curr_player.hand) != 0:
self._deck.cards.append(curr_player.hand.pop())
self._deck.shuffle()
self._deck.cut()
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.167s

OK

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

Илия обнови решението на 15.11.2022 13:56 (преди около 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 = []):
+ self._face_filter = face_filter
+ self.cards = []
+ self.__suit_types = ['clubs', 'diamonds', 'hearts', 'spades']
+ self.__face_types = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+
+ if len(face_filter) != 0:
+ self.__face_types = self._face_filter
+
+ for suit in self.__suit_types:
+ for face in self.__face_types:
+ self.cards.append(Card(suit, face))
+
+ def cut(self):
+ random_number = random.randint(0, len(self.cards) - 1)
+ self.cards = self.cards[random_number:] + self.cards[:random_number]
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+class Player:
+ def __init__(self):
+ self.hand = []
+
+ def get_cards(self):
+ return self.hand
+
+class Game(Deck):
+ 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()
+
+ while number_of_players != 0:
+ self._players.append(Player())
+ number_of_players = number_of_players - 1
+
+ def get_players(self):
+ return self._players
+
+ def deal(self, ind_player):
+ if self._dealing_direction == 'ltr':

Много голяма част от тялото на if-а и else-а съвпадат. Би било добре да ги обединиш, като предварително определеш реда на играчите и го запазиш в някаква локална променлива.

+ index_instructions = 0
+ while index_instructions < len(self._dealing_instructions):
+ curr_player = self._players.index(ind_player)
+ cards_dealt = self._dealing_instructions[index_instructions]
+ while curr_player < len(self._players):
+ i = 0
+ while i < cards_dealt:
+ self._players[curr_player].hand.append(self._deck.cards.pop(0))
+ i+=1
+ curr_player = curr_player + 1
+ count = 0
+ curr_player = self._players.index(ind_player)
+ while count < curr_player:
+ i = 0
+ while i < cards_dealt:
+ self._players[count].hand.append(self._deck.cards.pop(0))
+ i+=1
+ count = count + 1
+ index_instructions = index_instructions + 1
+ else:
+ index_instructions = 0
+ while index_instructions < len(self._dealing_instructions):
+ curr_player = self._players.index(ind_player)
+ cards_dealt = self._dealing_instructions[index_instructions]
+ count = 0
+ while curr_player >= count:
+ i = 0
+ while i < cards_dealt:
+ self._players[curr_player].hand.append(self._deck.cards.pop(0))
+ i+=1
+ curr_player = curr_player - 1
+
+ count = len(self._players) - 1
+ curr_player = self._players.index(ind_player)
+
+ while count > curr_player:
+ i = 0
+ while i < cards_dealt:
+ self._players[count].hand.append(self._deck.cards.pop(0))
+ i+=1
+ count = count - 1
+ index_instructions = index_instructions + 1
+
+ def prepare_deck(self):
+ for curr_player in self._players:
+ while len(curr_player.hand) != 0:
+ self._deck.cards.append(curr_player.hand.pop())
+
+ self._deck.shuffle()
+ self._deck.cut()
+
+ 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))