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

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

Към профила на Александра Радева

Резултати

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

Код

import random
suits = ['clubs', 'diamonds', 'hearts', 'spades']
faces = ['2','3','4','5','6','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 = faces):
self.face_filter = face_filter
self.cards = []
self.create_deck()
def create_deck(self):
for suit in suits:
for face in faces:
if face in self.face_filter:
self.cards.append(Card(suit,face))
def add_card(self, card):
self.cards.append(card)
def get_face_filter(self):
return self.face_filter
def cut(self):
cut_card = self.cards[-1]
self.cards.pop()
self.cards.insert(0, cut_card)
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def draw_card(self):
self.cards.pop()
class Player:
def __init__(self):
self.owned_cards = []
def get_cards(self):
return self.owned_cards
def add_card_to_hand(self, card):
self.owned_cards.append(card)
def clear_hand(self):
self.owned_cards.clear()
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 = []
self.add_players()
def add_players(self):
for player in range(0,self. number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
player_cards = player.get_cards()
for player_card in player_cards:
self.deck.add_card(player_card)
player.clear_hand()
self.deck.cut()
self.deck.shuffle()
def deal(self, player):
for instruction in self.dealing_instructions:
player_counter = 0
index = self.players.index(player)
while player_counter < len(self.players):
card_counter = 0
while instruction > card_counter:
self.players[index].add_card_to_hand(self.deck.get_cards()[-1])
self.deck.draw_card()
card_counter += 1
if self.dealing_direction == "ltr":
if index == len(self.players) - 1 :
index = -1
index += 1
else: index -= 1
player_counter += 1
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.166s

OK

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

Александра обнови решението на 15.11.2022 16:03 (преди около 2 години)

+import random
+
+suits = ['clubs', 'diamonds', 'hearts', 'spades']
+faces = ['2','3','4','5','6','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
+
+ #def print_card(self):
+ # print("{},{}".format(self.suit,self.face))
+
+class Deck:
+ def __init__(self, _face_filter=faces):
+ self.face_filter = _face_filter

Не е нужно да слагаш долна черта. Едното е пропърти на иснтанцията на класа, а другото аргумент на инициализатора. Няма да има конфликт на имената.

+ self.cards = []
+ self.create_deck()
+
+ def create_deck(self):
+ for suit in suits:
+ for face in faces:
+ if face in self.face_filter:
+ self.cards.append(Card(suit,face))
+
+ def add_card(self, card):
+ self.cards.append(card)
+
+ def get_face_filter(self):
+ return self.face_filter
+
+ def cut(self):
+ cut_card=self.cards[-1]
+ self.cards.pop()
+ self.cards.insert(0,cut_card)
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def draw_card(self):
+ self.cards.pop()
+
+ def print_deck(self):
+ for card in self.cards:
+ card.print_card()
+
+class Player:
+ def __init__(self):
+ self.owned_cards = []
+
+ def get_cards(self):
+ return self.owned_cards
+
+ def add_card_to_hand(self, card):
+ self.owned_cards.append(card)
+
+ def clear_hand(self):
+ self.owned_cards.clear()
+
+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 = []
+ self.add_players()
+
+ def add_players(self):
+ counter = 0
+ while counter < self.number_of_players:
+ self.players.append(Player())
+ counter+=1
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ player_cards = player.get_cards()
+ for player_card in player_cards:
+ self.deck.add_card(player_card)
+ player.clear_hand()
+ self.deck.cut()
+ self.deck.shuffle()
+
+ def deal(self, player):
+ for instruction in self.dealing_instructions:
+ player_counter = 0
+ index = self.players.index(player)
+ while player_counter < len(self.players):
+ card_counter = 0
+ while instruction > card_counter:
+ self.players[index].add_card_to_hand(self.deck.get_cards()[-1])
+ self.deck.draw_card()
+ card_counter+=1
+ if self.dealing_direction == "ltr":
+ if index == len(self.players) - 1 :
+ index = -1
+ index+=1
+ else: index-=1
+ player_counter+=1
+
+ 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 16:46 (преди около 2 години)

import random
suits = ['clubs', 'diamonds', 'hearts', 'spades']
faces = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
class Card:
- def __init__(self, _suit, _face):
- self.suit = _suit
- self.face = _face
+ 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
- #def print_card(self):
- # print("{},{}".format(self.suit,self.face))
-
class Deck:
- def __init__(self, _face_filter=faces):
- self.face_filter = _face_filter
+ def __init__(self, face_filter = faces):
+ self.face_filter = face_filter
self.cards = []
self.create_deck()
def create_deck(self):
for suit in suits:
for face in faces:
if face in self.face_filter:
self.cards.append(Card(suit,face))
def add_card(self, card):
self.cards.append(card)
def get_face_filter(self):
return self.face_filter
def cut(self):
- cut_card=self.cards[-1]
+ cut_card = self.cards[-1]
self.cards.pop()
- self.cards.insert(0,cut_card)
+ self.cards.insert(0, cut_card)
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def draw_card(self):
self.cards.pop()
- def print_deck(self):
- for card in self.cards:
- card.print_card()
-
class Player:
def __init__(self):
self.owned_cards = []
def get_cards(self):
return self.owned_cards
def add_card_to_hand(self, card):
self.owned_cards.append(card)
def clear_hand(self):
self.owned_cards.clear()
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
+ 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 = []
self.add_players()
def add_players(self):
- counter = 0
- while counter < self.number_of_players:
+ for player in range(0,self. number_of_players):
self.players.append(Player())
- counter+=1
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
player_cards = player.get_cards()
for player_card in player_cards:
self.deck.add_card(player_card)
player.clear_hand()
self.deck.cut()
self.deck.shuffle()
def deal(self, player):
for instruction in self.dealing_instructions:
player_counter = 0
index = self.players.index(player)
while player_counter < len(self.players):
card_counter = 0
while instruction > card_counter:
self.players[index].add_card_to_hand(self.deck.get_cards()[-1])
self.deck.draw_card()
- card_counter+=1
+ card_counter += 1
if self.dealing_direction == "ltr":
if index == len(self.players) - 1 :
index = -1
- index+=1
- else: index-=1
- player_counter+=1
+ index += 1
+ else: index -= 1
+ player_counter += 1
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))
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))