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

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

Към профила на Йоанна Кръстева

Резултати

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

Код

import random
import os
SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
DIRECTIONS = ['ltr', 'rtl']
class Card:
def __init__(self, suit, face):
if face not in FACES:
raise ValueError(f'Invalid card face: {face}')
if suit not in SUITS:
raise ValueError(f'Invalid card suit: {suit}')
self._suit = suit
self._face = face
def get_suit(self):
return self._suit
def get_face(self):
return self._face
def __repr__(self):
return f"<Card {self._face} of {self._suit}>"
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is not None:
self.deck_initializer(face_filter)
else:
self.deck_initializer()
def deck_initializer(self, _filter=FACES):
for suit in SUITS:
for face in _filter:
self.cards.append(Card(suit, face))
def __repr__(self):
return os.linesep.join(map(str, self.cards))
def cut(self):
random_point = random.randrange(1, len(self.cards))
self.cards = self.cards[random_point:] + self.cards[:random_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def remove(self):
if self.cards:
self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game:
def __init__(self, number_of_players: int, dealing_direction: str,
dealing_instructions: tuple):
if dealing_direction not in DIRECTIONS:
raise Exception
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
for item in player.hand:
self.deck.cards.append(item)
player.hand.clear()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player):
players_in_direction = []
for person in self.players:
if person == player:
if self.dealing_direction == 'ltr':
position = self.players.index(player)
players_in_direction = self.players[position:] + self.players[0:position]
else:
position = self.players.index(player)
players_in_direction = [*(self.players[position::-1]), *(self.players[:position:-1])]
for instruction in self.dealing_instructions:
for person in players_in_direction:
for _ in range(instruction):
if self.deck:
person.hand.append(self.deck.cards[-1])
self.deck.remove()
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.158s

OK

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

Йоанна обнови решението на 11.11.2022 01:36 (преди около 2 години)

+import random
+
+SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
+FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+DIRECTIONS = ['ltr', 'rtl']
+
+
+class Card:
+
+ def __init__(self, suit, face):
+ if face not in FACES:
+ raise Exception
+ if suit not in SUITS:
+ raise Exception
+ self.suit = suit
+ self.face = face
+
+ def get_suit(self):
+ return self.suit
+
+ def get_face(self):
+ return self.face
+
+ def __repr__(self):
+ return f"<Card {self.face} of {self.suit}>"
+
+
+class Deck:
+ def __init__(self, face_filter=None):
+ self.cards = []
+ if face_filter is not None:
+ for suit in SUITS:
+ for face in face_filter:
+ self.cards.append(Card(suit, face))
+ else:
+ for suit in SUITS:
+ for face in FACES:
+ self.cards.append(Card(suit, face))
+
+ def __repr__(self):
+ output = [f"{c}\n" for c in self.cards]
+ return "".join(output)
+
+ def cut(self):
+ random_point = random.randrange(1, len(self.cards))
+ self.cards = self.cards[random_point:] + self.cards[0:random_point]
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def remove(self):
+ if self.cards:
+ self.cards.pop()
+
+
+class Player:
+ def __init__(self):
+ self.hand = []
+
+ def get_cards(self):
+ return self.hand
+
+
+class Game:
+ def __init__(self, number_of_players: int, dealing_direction: str,
+ dealing_instructions: tuple):
+ if dealing_direction not in DIRECTIONS:
+ raise Exception
+ self.number_of_players = number_of_players
+ self.dealing_direction = dealing_direction
+ self.dealing_instructions = dealing_instructions
+ self.deck = Deck()
+ self.players = []
+ for _ in range(number_of_players):
+ self.players.append(Player())
+
+ # Check what ltr actually means?
+ def get_players(self):
+ return self.players
+
+ # Check here for problems?
+ def prepare_deck(self):
+ for _player in self.players:

Долната черта преди player е излишна. Ако това е резервирана дума, чертата се слага отзад. Ако е атрибут на клас, слага се отпред. Тук не е нито едно от двете.

+ for item in _player.hand:
+ self.deck.cards.append(item)
+ _player.hand.clear()
+ self.deck.shuffle()
+ self.deck.cut()
+
+ # Check this method!
+ def deal(self, player: Player):
+ for person in self.players:
+ if person == player:
+ if self.dealing_direction == 'rtl':
+ _index = self.players.index(person)
+ self.players = self.players[_index:0:-1] + self.players[0:_index:-1]

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

+ else:
+ _index = self.players.index(person)
+ self.players = self.players[_index:] + self.players[0:_index]
+
+ for instruction in self.dealing_instructions:
+ for person in self.players:
+ for i in range(instruction):
+ if self.deck:
+ person.hand.append(self.deck.cards[-1])
+ self.deck.remove()
+
+ 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))
+

Йоанна обнови решението на 11.11.2022 22:45 (преди около 2 години)

import random
+import os
SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
DIRECTIONS = ['ltr', 'rtl']
class Card:
def __init__(self, suit, face):
if face not in FACES:
- raise Exception
+ raise ValueError(f'Invalid card face: {face}')
if suit not in SUITS:
- raise Exception
- self.suit = suit
- self.face = face
+ raise ValueError(f'Invalid card suit: {suit}')
+ 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
def __repr__(self):
- return f"<Card {self.face} of {self.suit}>"
+ return f"<Card {self._face} of {self._suit}>"
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is not None:
- for suit in SUITS:
- for face in face_filter:
- self.cards.append(Card(suit, face))
+ self.deck_initializer(face_filter)
else:
- for suit in SUITS:
- for face in FACES:
- self.cards.append(Card(suit, face))
+ self.deck_initializer()
+ def deck_initializer(self, _filter=FACES):
+ for suit in SUITS:
+ for face in _filter:
+ self.cards.append(Card(suit, face))
+
def __repr__(self):
- output = [f"{c}\n" for c in self.cards]
- return "".join(output)
+ return os.linesep.join(map(str, self.cards))
def cut(self):
random_point = random.randrange(1, len(self.cards))
self.cards = self.cards[random_point:] + self.cards[0:random_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def remove(self):
if self.cards:
self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game:
def __init__(self, number_of_players: int, dealing_direction: str,
dealing_instructions: tuple):
if dealing_direction not in DIRECTIONS:
raise Exception
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
- # Check what ltr actually means?
def get_players(self):
return self.players
- # Check here for problems?
def prepare_deck(self):
- for _player in self.players:
- for item in _player.hand:
+ for player in self.players:
+ for item in player.hand:
self.deck.cards.append(item)
- _player.hand.clear()
+ player.hand.clear()
self.deck.shuffle()
self.deck.cut()
- # Check this method!
+# Check if it works in the right way when ltr rtl!
def deal(self, player: Player):
+ players_in_direction = []
for person in self.players:
if person == player:
if self.dealing_direction == 'rtl':
- _index = self.players.index(person)
- self.players = self.players[_index:0:-1] + self.players[0:_index:-1]
+ position = self.players.index(player)
+ players_in_direction = self.players[position:] + self.players[0:position]
else:
- _index = self.players.index(person)
- self.players = self.players[_index:] + self.players[0:_index]
+ position = self.players.index(player)
+ players_in_direction = [*(self.players[position:0:-1]), *(self.players[:position:-1])]
for instruction in self.dealing_instructions:
- for person in self.players:
- for i in range(instruction):
+ for person in players_in_direction:
+ for _ in range(instruction):
if self.deck:
person.hand.append(self.deck.cards[-1])
self.deck.remove()
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))

Йоанна обнови решението на 11.11.2022 23:12 (преди около 2 години)

import random
import os
SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
DIRECTIONS = ['ltr', 'rtl']
class Card:
def __init__(self, suit, face):
if face not in FACES:
raise ValueError(f'Invalid card face: {face}')
if suit not in SUITS:
raise ValueError(f'Invalid card suit: {suit}')
self._suit = suit
self._face = face
def get_suit(self):
return self._suit
def get_face(self):
return self._face
def __repr__(self):
return f"<Card {self._face} of {self._suit}>"
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is not None:
self.deck_initializer(face_filter)
else:
self.deck_initializer()
def deck_initializer(self, _filter=FACES):
for suit in SUITS:
for face in _filter:
self.cards.append(Card(suit, face))
def __repr__(self):
return os.linesep.join(map(str, self.cards))
def cut(self):
random_point = random.randrange(1, len(self.cards))
self.cards = self.cards[random_point:] + self.cards[0:random_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def remove(self):
if self.cards:
self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game:
def __init__(self, number_of_players: int, dealing_direction: str,
dealing_instructions: tuple):
if dealing_direction not in DIRECTIONS:
raise Exception
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
for item in player.hand:
self.deck.cards.append(item)
player.hand.clear()
self.deck.shuffle()
self.deck.cut()
# Check if it works in the right way when ltr rtl!
def deal(self, player: Player):
players_in_direction = []
for person in self.players:
if person == player:
if self.dealing_direction == 'rtl':
position = self.players.index(player)
players_in_direction = self.players[position:] + self.players[0:position]
else:
position = self.players.index(player)
- players_in_direction = [*(self.players[position:0:-1]), *(self.players[:position:-1])]
+ players_in_direction = [*(self.players[position::-1]), *(self.players[:position:-1])]
for instruction in self.dealing_instructions:
for person in players_in_direction:
for _ in range(instruction):
if self.deck:
person.hand.append(self.deck.cards[-1])
self.deck.remove()
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))

Йоанна обнови решението на 11.11.2022 23:52 (преди около 2 години)

import random
import os
SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
DIRECTIONS = ['ltr', 'rtl']
class Card:
def __init__(self, suit, face):
if face not in FACES:
raise ValueError(f'Invalid card face: {face}')
if suit not in SUITS:
raise ValueError(f'Invalid card suit: {suit}')
self._suit = suit
self._face = face
def get_suit(self):
return self._suit
def get_face(self):
return self._face
def __repr__(self):
return f"<Card {self._face} of {self._suit}>"
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is not None:
self.deck_initializer(face_filter)
else:
self.deck_initializer()
def deck_initializer(self, _filter=FACES):
for suit in SUITS:
for face in _filter:
self.cards.append(Card(suit, face))
def __repr__(self):
return os.linesep.join(map(str, self.cards))
def cut(self):
- random_point = random.randrange(1, len(self.cards))
+ random_point = random.randrange(2, (len(self.cards) - 1))
self.cards = self.cards[random_point:] + self.cards[0:random_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def remove(self):
if self.cards:
self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game:
def __init__(self, number_of_players: int, dealing_direction: str,
dealing_instructions: tuple):
if dealing_direction not in DIRECTIONS:
raise Exception
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
for item in player.hand:
self.deck.cards.append(item)
player.hand.clear()
self.deck.shuffle()
self.deck.cut()
-# Check if it works in the right way when ltr rtl!
def deal(self, player: Player):
players_in_direction = []
for person in self.players:
if person == player:
if self.dealing_direction == 'rtl':
position = self.players.index(player)
players_in_direction = self.players[position:] + self.players[0:position]
else:
position = self.players.index(player)
players_in_direction = [*(self.players[position::-1]), *(self.players[:position:-1])]
for instruction in self.dealing_instructions:
for person in players_in_direction:
for _ in range(instruction):
if self.deck:
person.hand.append(self.deck.cards[-1])
self.deck.remove()
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))

Йоанна обнови решението на 12.11.2022 00:10 (преди около 2 години)

import random
import os
SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
DIRECTIONS = ['ltr', 'rtl']
class Card:
def __init__(self, suit, face):
if face not in FACES:
raise ValueError(f'Invalid card face: {face}')
if suit not in SUITS:
raise ValueError(f'Invalid card suit: {suit}')
self._suit = suit
self._face = face
def get_suit(self):
return self._suit
def get_face(self):
return self._face
def __repr__(self):
return f"<Card {self._face} of {self._suit}>"
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is not None:
self.deck_initializer(face_filter)
else:
self.deck_initializer()
def deck_initializer(self, _filter=FACES):
for suit in SUITS:
for face in _filter:
self.cards.append(Card(suit, face))
def __repr__(self):
return os.linesep.join(map(str, self.cards))
def cut(self):
- random_point = random.randrange(2, (len(self.cards) - 1))
+ random_point = random.randrange(1, len(self.cards))
self.cards = self.cards[random_point:] + self.cards[0:random_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def remove(self):
if self.cards:
self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game:
def __init__(self, number_of_players: int, dealing_direction: str,
dealing_instructions: tuple):
if dealing_direction not in DIRECTIONS:
raise Exception
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
for item in player.hand:
self.deck.cards.append(item)
player.hand.clear()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player):
players_in_direction = []
for person in self.players:
if person == player:
if self.dealing_direction == 'rtl':
position = self.players.index(player)
players_in_direction = self.players[position:] + self.players[0:position]
else:
position = self.players.index(player)
players_in_direction = [*(self.players[position::-1]), *(self.players[:position:-1])]
for instruction in self.dealing_instructions:
for person in players_in_direction:
for _ in range(instruction):
if self.deck:
person.hand.append(self.deck.cards[-1])
self.deck.remove()
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 13:41 (преди около 2 години)

import random
import os
SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
DIRECTIONS = ['ltr', 'rtl']
class Card:
def __init__(self, suit, face):
if face not in FACES:
raise ValueError(f'Invalid card face: {face}')
if suit not in SUITS:
raise ValueError(f'Invalid card suit: {suit}')
self._suit = suit
self._face = face
def get_suit(self):
return self._suit
def get_face(self):
return self._face
def __repr__(self):
return f"<Card {self._face} of {self._suit}>"
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is not None:
self.deck_initializer(face_filter)
else:
self.deck_initializer()
def deck_initializer(self, _filter=FACES):
for suit in SUITS:
for face in _filter:
self.cards.append(Card(suit, face))
def __repr__(self):
return os.linesep.join(map(str, self.cards))
def cut(self):
random_point = random.randrange(1, len(self.cards))
- self.cards = self.cards[random_point:] + self.cards[0:random_point]
+ self.cards = self.cards[random_point:] + self.cards[:random_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def remove(self):
if self.cards:
self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game:
def __init__(self, number_of_players: int, dealing_direction: str,
dealing_instructions: tuple):
if dealing_direction not in DIRECTIONS:
raise Exception
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
for item in player.hand:
self.deck.cards.append(item)
player.hand.clear()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player):
players_in_direction = []
for person in self.players:
if person == player:
- if self.dealing_direction == 'rtl':
+ if self.dealing_direction == 'ltr':
position = self.players.index(player)
players_in_direction = self.players[position:] + self.players[0:position]
else:
position = self.players.index(player)
players_in_direction = [*(self.players[position::-1]), *(self.players[:position:-1])]
for instruction in self.dealing_instructions:
for person in players_in_direction:
for _ in range(instruction):
if self.deck:
person.hand.append(self.deck.cards[-1])
self.deck.remove()
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 13:55 (преди около 2 години)

import random
import os
SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
DIRECTIONS = ['ltr', 'rtl']
class Card:
def __init__(self, suit, face):
if face not in FACES:
raise ValueError(f'Invalid card face: {face}')
if suit not in SUITS:
raise ValueError(f'Invalid card suit: {suit}')
self._suit = suit
self._face = face
def get_suit(self):
return self._suit
def get_face(self):
return self._face
def __repr__(self):
return f"<Card {self._face} of {self._suit}>"
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is not None:
self.deck_initializer(face_filter)
else:
self.deck_initializer()
def deck_initializer(self, _filter=FACES):
for suit in SUITS:
for face in _filter:
self.cards.append(Card(suit, face))
def __repr__(self):
return os.linesep.join(map(str, self.cards))
def cut(self):
random_point = random.randrange(1, len(self.cards))
self.cards = self.cards[random_point:] + self.cards[:random_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def remove(self):
if self.cards:
self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game:
def __init__(self, number_of_players: int, dealing_direction: str,
dealing_instructions: tuple):
if dealing_direction not in DIRECTIONS:
raise Exception
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
for item in player.hand:
self.deck.cards.append(item)
player.hand.clear()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player):
players_in_direction = []
for person in self.players:
if person == player:
if self.dealing_direction == 'ltr':
position = self.players.index(player)
players_in_direction = self.players[position:] + self.players[0:position]
else:
position = self.players.index(player)
players_in_direction = [*(self.players[position::-1]), *(self.players[:position:-1])]
for instruction in self.dealing_instructions:
for person in players_in_direction:
for _ in range(instruction):
if self.deck:
person.hand.append(self.deck.cards[-1])
self.deck.remove()
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 13:55 (преди около 2 години)

import random
import os
SUITS = ['clubs', 'diamonds', 'hearts', 'spades']
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
DIRECTIONS = ['ltr', 'rtl']
class Card:
def __init__(self, suit, face):
if face not in FACES:
raise ValueError(f'Invalid card face: {face}')
if suit not in SUITS:
raise ValueError(f'Invalid card suit: {suit}')
self._suit = suit
self._face = face
def get_suit(self):
return self._suit
def get_face(self):
return self._face
def __repr__(self):
return f"<Card {self._face} of {self._suit}>"
class Deck:
def __init__(self, face_filter=None):
self.cards = []
if face_filter is not None:
self.deck_initializer(face_filter)
else:
self.deck_initializer()
def deck_initializer(self, _filter=FACES):
for suit in SUITS:
for face in _filter:
self.cards.append(Card(suit, face))
def __repr__(self):
return os.linesep.join(map(str, self.cards))
def cut(self):
random_point = random.randrange(1, len(self.cards))
self.cards = self.cards[random_point:] + self.cards[:random_point]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def remove(self):
if self.cards:
self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def get_cards(self):
return self.hand
class Game:
def __init__(self, number_of_players: int, dealing_direction: str,
dealing_instructions: tuple):
if dealing_direction not in DIRECTIONS:
raise Exception
self.number_of_players = number_of_players
self.dealing_direction = dealing_direction
self.dealing_instructions = dealing_instructions
self.deck = Deck()
self.players = []
for _ in range(number_of_players):
self.players.append(Player())
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
for item in player.hand:
self.deck.cards.append(item)
player.hand.clear()
self.deck.shuffle()
self.deck.cut()
def deal(self, player: Player):
players_in_direction = []
for person in self.players:
if person == player:
if self.dealing_direction == 'ltr':
position = self.players.index(player)
players_in_direction = self.players[position:] + self.players[0:position]
else:
position = self.players.index(player)
players_in_direction = [*(self.players[position::-1]), *(self.players[:position:-1])]
for instruction in self.dealing_instructions:
for person in players_in_direction:
for _ in range(instruction):
if self.deck:
person.hand.append(self.deck.cards[-1])
self.deck.remove()
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))
+