Решение на От ливадите до Лас Вегас (и назад) от Айше Джинджи

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

Към профила на Айше Джинджи

Резултати

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

Код

import random
ALL_FACES = [

Ок ли е да имам константи извън класовете? Основно пиша на C# и там бих ги сложила като private const property, но за Python така и не успях да намеря каква е конвенцията за такива случаи

'2', '3', '4', '5',
'6', '7', '8', '9',
'10', 'J', 'Q', 'K', 'A'
]
SUITS = [
'clubs',
'diamonds',
'hearts',
'spades'
]
BELOT_FACES = [
'7', '8', '9',
'10', 'J', 'Q',
'K', 'A'
]
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=ALL_FACES):
self.__cards = []
for face in face_filter:
for suit in SUITS:
self.__cards.append(Card(suit, face))
def get_cards(self):
return self.__cards
def cut(self):
number_of_cards = len(self.__cards)
shuffle_base = random.randrange(1, number_of_cards)
self.__cards= self.__cards[shuffle_base:] + self.__cards[:shuffle_base]
def shuffle(self):
random.shuffle(self.__cards)
def add_cards(self, cards):
self.__cards += cards
def remove_cards(self, cards):
for card in cards:
self.__cards.remove(card)
class Player:
def __init__(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_cards(self, cards):
self.__cards += cards
def collect_cards(self):
cards = self.__cards
self.__cards = []
return 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 get_deck(self):
return self.__deck
def prepare_deck(self):
for player in self.__players:
cards = player.collect_cards()
self.__deck.add_cards(cards)
self.__deck.shuffle()
self.__deck.cut()
def deal(self, player):
player_index = self.__players.index(player)
cards = self.__deck.get_cards()
for instruction in self.__dealing_instructions:
if self.__dealing_direction == 'ltr':
for player in self.__players[player_index:] + self.__players[:player_index]:
player.add_cards(cards[:instruction])
self.__deck.remove_cards(cards[:instruction])
elif self.__dealing_direction == 'rtl':
first_half = self.__players[:player_index + 1]
first_half.reverse()
second_half = self.__players[player_index + 1:]
second_half.reverse()
for player in first_half + second_half:
player.add_cards(cards[:instruction])
self.__deck.remove_cards(cards[:instruction])
def _set_deck(self, face_filter):
self.__deck = Deck(face_filter)
class Belot(Game):
def __init__(self):
Game.__init__(self, 4, 'ltr', (2, 3, 3))
self._set_deck(BELOT_FACES)
class Poker(Game):
def __init__(self):
Game.__init__(self, 9, 'rtl', (1, 1, 1, 1, 1))

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.179s

OK

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

Айше обнови решението на 15.11.2022 00:01 (преди около 2 години)

+import random
+
+ALL_FACES = [

Ок ли е да имам константи извън класовете? Основно пиша на C# и там бих ги сложила като private const property, но за Python така и не успях да намеря каква е конвенцията за такива случаи

+ '2', '3', '4', '5',
+ '6', '7', '8', '9',
+ '10', 'J', 'Q', 'K', 'A'
+]
+
+SUITS = [
+ 'clubs',
+ 'diamonds',
+ 'hearts',
+ 'spades'
+]
+
+BELOT_FACES = [
+ '7', '8', '9',
+ '10', 'J', 'Q',
+ 'K', 'A'
+]
+
+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 = ALL_FACES):
+ self.__cards = []
+ for face in face_filter:
+ for suit in SUITS:
+ self.__cards.append(Card(suit, face))
+
+ def get_cards(self):
+ return self.__cards
+
+ def cut(self):
+ number_of_cards = len(self.__cards)
+ shuffle_base = int((random.random() * 100) % number_of_cards)

Това е ок, но в random модула има и функции, които са по-близо до това, което искаш да направиш. Просто няма да трябва да умножаваш и делиш. Например random.randrange()

+ self.__cards= self.__cards[shuffle_base:number_of_cards] + self.__cards[0:shuffle_base]
+
+ def shuffle(self):
+ random.shuffle(self.__cards)
+
+ def add_cards(self, cards):
+ self.__cards += cards
+
+ def remove_cards(self, cards):
+ for card in cards:
+ self.__cards.remove(card)
+
+
+class Player:
+ def __init__(self):
+ self.__cards = []
+
+ def get_cards(self):
+ return self.__cards
+
+ def add_cards(self, cards):
+ self.__cards += cards
+
+ def collect_cards(self):
+ cards = self.__cards
+ self.__cards = []
+ return 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 get_deck(self):
+ return self.__deck
+
+ def prepare_deck(self):
+ for player in self.__players:
+ cards = player.collect_cards()
+ self.__deck.add_cards(cards)
+ self.__deck.shuffle()
+ self.__deck.cut()
+
+ def deal(self, player):
+ player_index = self.__players.index(player)
+ cards = self.__deck.get_cards()
+
+ for instruction in self.__dealing_instructions:
+ if self.__dealing_direction == 'ltr':
+ for player in self.__players[player_index:]:
+ player.add_cards(cards[:instruction])
+ self.__deck.remove_cards(cards[:instruction])
+
+ for player in self.__players[:player_index]:
+ player.add_cards(cards[:instruction])
+ self.__deck.remove_cards(cards[:instruction])
+
+ elif self.__dealing_direction == 'rtl':
+ first_half = self.__players[:player_index + 1]
+ first_half.reverse()
+ second_half = self.__players[player_index + 1:]
+ second_half.reverse()
+
+ for player in first_half:
+ player.add_cards(cards[:instruction])
+ self.__deck.remove_cards(cards[:instruction])
+
+ for player in second_half:
+ player.add_cards(cards[:instruction])
+ self.__deck.remove_cards(cards[:instruction])
+
+ def _set_deck(self, face_filter):
+ self.__deck = Deck(face_filter)
+
+
+class Belot(Game):
+ def __init__(self):
+ Game.__init__(self, 4, 'ltr', (2, 3, 3))
+ self._set_deck(BELOT_FACES)
+
+
+class Poker(Game):
+ def __init__(self):
+ Game.__init__(self, 9, 'rtl', (1, 1, 1, 1, 1))
+

Айше обнови решението на 15.11.2022 12:24 (преди около 2 години)

import random
ALL_FACES = [
'2', '3', '4', '5',
'6', '7', '8', '9',
'10', 'J', 'Q', 'K', 'A'
]
SUITS = [
'clubs',
'diamonds',
'hearts',
'spades'
]
BELOT_FACES = [
'7', '8', '9',
'10', 'J', 'Q',
'K', 'A'
]
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 = ALL_FACES):
+ def __init__(self, face_filter=ALL_FACES):
self.__cards = []
for face in face_filter:
for suit in SUITS:
self.__cards.append(Card(suit, face))
def get_cards(self):
return self.__cards
def cut(self):
number_of_cards = len(self.__cards)
- shuffle_base = int((random.random() * 100) % number_of_cards)
- self.__cards= self.__cards[shuffle_base:number_of_cards] + self.__cards[0:shuffle_base]
+ shuffle_base = random.randrange(1, number_of_cards)
+ self.__cards= self.__cards[shuffle_base:] + self.__cards[:shuffle_base]
def shuffle(self):
random.shuffle(self.__cards)
def add_cards(self, cards):
self.__cards += cards
def remove_cards(self, cards):
for card in cards:
self.__cards.remove(card)
class Player:
def __init__(self):
self.__cards = []
def get_cards(self):
return self.__cards
def add_cards(self, cards):
self.__cards += cards
def collect_cards(self):
cards = self.__cards
self.__cards = []
return 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 get_deck(self):
return self.__deck
def prepare_deck(self):
for player in self.__players:
cards = player.collect_cards()
self.__deck.add_cards(cards)
self.__deck.shuffle()
self.__deck.cut()
def deal(self, player):
player_index = self.__players.index(player)
cards = self.__deck.get_cards()
for instruction in self.__dealing_instructions:
if self.__dealing_direction == 'ltr':
- for player in self.__players[player_index:]:
+ for player in self.__players[player_index:] + self.__players[:player_index]:
player.add_cards(cards[:instruction])
self.__deck.remove_cards(cards[:instruction])
- for player in self.__players[:player_index]:
- player.add_cards(cards[:instruction])
- self.__deck.remove_cards(cards[:instruction])
-
elif self.__dealing_direction == 'rtl':
first_half = self.__players[:player_index + 1]
first_half.reverse()
second_half = self.__players[player_index + 1:]
second_half.reverse()
- for player in first_half:
+ for player in first_half + second_half:
player.add_cards(cards[:instruction])
self.__deck.remove_cards(cards[:instruction])
-
- for player in second_half:
- player.add_cards(cards[:instruction])
- self.__deck.remove_cards(cards[:instruction])
def _set_deck(self, face_filter):
self.__deck = Deck(face_filter)
class Belot(Game):
def __init__(self):
Game.__init__(self, 4, 'ltr', (2, 3, 3))
self._set_deck(BELOT_FACES)
class Poker(Game):
def __init__(self):
Game.__init__(self, 9, 'rtl', (1, 1, 1, 1, 1))