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

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

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

Резултати

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

Код

import random
faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['clubs', 'diamonds', 'hearts', 'spades']
class Card:
def __init__(self, suit, face):
self._face = face
self._suit = suit
def get_suit(self):
return self._suit
def get_face(self):
return self._face
class Deck:
def __init__(self, face_filter=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):
index = random.randint(1, len(self.cards))
temp_cards = self.cards[0 : index]
self.cards = self.cards[index : ]
self.cards.extend(temp_cards)
return self.cards
def shuffle(self):
return random.shuffle(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.players = []
for i in range(number_of_players):
self.players.append(Player())
self.direction = dealing_direction
self.instructions = dealing_instructions
self.deck = Deck()
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.cards.extend(player.cards)
player.cards = []
self.deck.shuffle()
self.deck.cut()
return self.deck
def deal(self, player):
index = self.players.index(player)
count = 0
while count < len(self.instructions):
if self.direction == 'ltr':
for _ in range(len(self.players)):
self.players[index].cards.extend(self.deck.cards[ : self.instructions[count]])
self.deck.cards = self.deck.cards[self.instructions[count] : ]
index += 1
if index >= len(self.players):
index = 0
if self.direction == 'rtl':
for _ in range(len(self.players)):
self.players[index].cards.extend(self.deck.cards[ : self.instructions[count]])
self.deck.cards = self.deck.cards[self.instructions[count] : ]
index -= 1
count += 1
index = self.players.index(player)
def get_deck(self):
return self.deck
class Poker(Game):
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
class Belot(Game):
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))
self.deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])

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

...............
----------------------------------------------------------------------
Ran 15 tests in 0.165s

OK

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

Клементина обнови решението на 15.11.2022 00:48 (преди над 1 година)

+import random
+
+faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
+suits = ['clubs', 'diamonds', 'hearts', 'spades']
+
+
+
+
+class Card:
+
+ def __int__(self, suit, face):
+ self.face = face
+ self.suit = suit
+
+ def get_suit(self):
+ return self.suit
+
+ def get_face(self):
+ return self.face
+
+
+class Deck:
+
+ cards = []
+
+ def __init__(self, face_filter=faces):
+ 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):
+ index = random.random()
+ while index not in len(self.cards):
+ index = random.random()
+ temp_cards = self.cards[0, index - 1]
+ self.cards=self.cards[index, len(self.cards)]
+ self.cards.append(temp_cards)
+ return self.cards
+
+ def shuffle(self):
+ return random.shuffle(self.cards)
+
+
+class Player:
+ deck_ = []
+ def get_cards(self):
+ return self.deck_
+
+class Game:
+
+ deck = Deck()
+
+ def __init__(self, number_of_players, dealing_direction, dealing_instructions):
+ self.players = number_of_players
+ self.direction = dealing_direction
+ self.instructions = dealing_instructions
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ self.deck.shuffle()
+ self.deck.cut()
+ return self.deck
+
+ def deal(self, player):
+ index = index(player)
+ count = 0
+ while count < len(self.instructions):
+ if self.direction == 'ltr':
+ while index + 1 == index(player) or index - 1 == index(player):
+ self.players[index].deck = self.deck[0, self.instructions[0] - 1]
+ self.deck = self.deck[self.instructions[0] - 1, len(self.deck)]
+ self.players[index].deck = self.deck[0, self.instructions[0] - 1]
+ self.deck = self.deck[self.instructions[count] - 1, len(self.deck)]
+ index += 1
+ if index >= len(self.players):
+ index = 0
+ if self.direction == 'rtl':
+ while index - 1 == index(player):
+ self.players[index].deck = self.deck[0, self.instructions[0] - 1]
+ self.deck = self.deck[self.instructions[count] - 1, len(self.deck)]
+ index -= 1
+ count += 1
+ index = index(player)
+
+ def get_deck(self):
+ return self.deck
+
+
+class Poker(Game):
+
+ deck_ = Deck(faces)
+
+ def __init__(self):
+ super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
+
+
+class Belot(Game):
+
+ deck_ = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
+
+ def __init__(self):
+ super().__init__(4, 'ltr', (2, 3, 3))

Клементина обнови решението на 15.11.2022 00:49 (преди над 1 година)

import random
+
faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['clubs', 'diamonds', 'hearts', 'spades']
-
-
class Card:
def __int__(self, suit, face):
self.face = face
self.suit = suit
def get_suit(self):
return self.suit
def get_face(self):
return self.face
class Deck:
cards = []
def __init__(self, face_filter=faces):
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):
index = random.random()
while index not in len(self.cards):
index = random.random()
temp_cards = self.cards[0, index - 1]
self.cards=self.cards[index, len(self.cards)]
self.cards.append(temp_cards)
return self.cards
def shuffle(self):
return random.shuffle(self.cards)
class Player:
+
deck_ = []
+
def get_cards(self):
return self.deck_
class Game:
deck = Deck()
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.players = number_of_players
self.direction = dealing_direction
self.instructions = dealing_instructions
def get_players(self):
return self.players
def prepare_deck(self):
self.deck.shuffle()
self.deck.cut()
return self.deck
def deal(self, player):
index = index(player)
count = 0
while count < len(self.instructions):
if self.direction == 'ltr':
while index + 1 == index(player) or index - 1 == index(player):
self.players[index].deck = self.deck[0, self.instructions[0] - 1]
self.deck = self.deck[self.instructions[0] - 1, len(self.deck)]
self.players[index].deck = self.deck[0, self.instructions[0] - 1]
self.deck = self.deck[self.instructions[count] - 1, len(self.deck)]
index += 1
if index >= len(self.players):
index = 0
if self.direction == 'rtl':
while index - 1 == index(player):
self.players[index].deck = self.deck[0, self.instructions[0] - 1]
self.deck = self.deck[self.instructions[count] - 1, len(self.deck)]
index -= 1
count += 1
index = index(player)
def get_deck(self):
return self.deck
class Poker(Game):
deck_ = Deck(faces)
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
class Belot(Game):
deck_ = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))

Клементина обнови решението на 15.11.2022 00:50 (преди над 1 година)

import random
faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['clubs', 'diamonds', 'hearts', 'spades']
class Card:
def __int__(self, suit, face):
self.face = face
self.suit = suit
def get_suit(self):
return self.suit
def get_face(self):
return self.face
class Deck:
cards = []

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

Същият доста сериозен проблем имаш и в останалите класове.

def __init__(self, face_filter=faces):
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):
index = random.random()
while index not in len(self.cards):
index = random.random()
temp_cards = self.cards[0, index - 1]
self.cards=self.cards[index, len(self.cards)]
self.cards.append(temp_cards)
return self.cards
def shuffle(self):
return random.shuffle(self.cards)
class Player:
- deck_ = []
+ deck = []
def get_cards(self):
- return self.deck_
+ return self.deck
class Game:
deck = Deck()
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
self.players = number_of_players
self.direction = dealing_direction
self.instructions = dealing_instructions
def get_players(self):
return self.players
def prepare_deck(self):
self.deck.shuffle()
self.deck.cut()
return self.deck
def deal(self, player):
index = index(player)
count = 0
while count < len(self.instructions):
if self.direction == 'ltr':
while index + 1 == index(player) or index - 1 == index(player):
self.players[index].deck = self.deck[0, self.instructions[0] - 1]
self.deck = self.deck[self.instructions[0] - 1, len(self.deck)]
self.players[index].deck = self.deck[0, self.instructions[0] - 1]
self.deck = self.deck[self.instructions[count] - 1, len(self.deck)]
index += 1
if index >= len(self.players):
index = 0
if self.direction == 'rtl':
while index - 1 == index(player):
self.players[index].deck = self.deck[0, self.instructions[0] - 1]
self.deck = self.deck[self.instructions[count] - 1, len(self.deck)]
index -= 1
count += 1
index = index(player)
def get_deck(self):
return self.deck
class Poker(Game):
deck_ = Deck(faces)
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
class Belot(Game):
deck_ = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
def __init__(self):
super().__init__(4, 'ltr', (2, 3, 3))

Клементина обнови решението на 15.11.2022 17:33 (преди над 1 година)

import random
faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['clubs', 'diamonds', 'hearts', 'spades']
class Card:
- def __int__(self, suit, face):
- self.face = face
- self.suit = suit
+ def __init__(self, suit, face):
+ self._face = face
+ self._suit = suit
def get_suit(self):
- return self.suit
+ return self._suit
def get_face(self):
- return self.face
+ return self._face
class Deck:
- cards = []
-
def __init__(self, face_filter=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):
- index = random.random()
- while index not in len(self.cards):
- index = random.random()
- temp_cards = self.cards[0, index - 1]
- self.cards=self.cards[index, len(self.cards)]
- self.cards.append(temp_cards)
+ index = random.randint(1, len(self.cards))
+ temp_cards = self.cards[0 : index]
+ self.cards = self.cards[index : ]
+ self.cards.extend(temp_cards)
return self.cards
def shuffle(self):
return random.shuffle(self.cards)
class Player:
- deck = []
+ def __init__(self):
+ self.cards = []
def get_cards(self):
- return self.deck
+ return self.cards
+
class Game:
- deck = Deck()
-
def __init__(self, number_of_players, dealing_direction, dealing_instructions):
- self.players = number_of_players
+ self.players = []
+ for i in range(number_of_players):
+ self.players.append(Player())
self.direction = dealing_direction
self.instructions = dealing_instructions
+ self.deck = Deck()
def get_players(self):
return self.players
def prepare_deck(self):
+ for player in self.players:
+ self.deck.cards.extend(player.cards)
+ player.cards = []
self.deck.shuffle()
self.deck.cut()
return self.deck
def deal(self, player):
- index = index(player)
+ index = self.players.index(player)
count = 0
while count < len(self.instructions):
if self.direction == 'ltr':
- while index + 1 == index(player) or index - 1 == index(player):
- self.players[index].deck = self.deck[0, self.instructions[0] - 1]
- self.deck = self.deck[self.instructions[0] - 1, len(self.deck)]
- self.players[index].deck = self.deck[0, self.instructions[0] - 1]
- self.deck = self.deck[self.instructions[count] - 1, len(self.deck)]
+ for _ in range(len(self.players)):
+ self.players[index].cards.extend(self.deck.cards[ : self.instructions[count]])
+ self.deck.cards = self.deck.cards[self.instructions[count] : ]
index += 1
if index >= len(self.players):
index = 0
if self.direction == 'rtl':
- while index - 1 == index(player):
- self.players[index].deck = self.deck[0, self.instructions[0] - 1]
- self.deck = self.deck[self.instructions[count] - 1, len(self.deck)]
+ for _ in range(len(self.players)):
+ self.players[index].cards.extend(self.deck.cards[ : self.instructions[count]])
+ self.deck.cards = self.deck.cards[self.instructions[count] : ]
index -= 1
count += 1
- index = index(player)
+ index = self.players.index(player)
def get_deck(self):
return self.deck
class Poker(Game):
- deck_ = Deck(faces)
-
def __init__(self):
super().__init__(9, 'rtl', (1, 1, 1, 1, 1))
class Belot(Game):
- deck_ = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
-
def __init__(self):
- super().__init__(4, 'ltr', (2, 3, 3))
+ super().__init__(4, 'ltr', (2, 3, 3))
+ self.deck = Deck(['7', '8', '9', '10', 'J', 'Q', 'K', 'A'])