Решение на От ливадите до Лас Вегас (и назад) от Таня Сейкова

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

Към профила на Таня Сейкова

Резултати

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

Код

import random
class Card:
def __init__(self, suit, face):
self.__suit,self.__face = suit, face
def get_suit(self) -> str:
return self.__suit
def get_face(self) -> str:
return self.__face
def print_card(self):
print(self.__suit, self.__face)
class Deck:
def __init__(self, face_filter=None):
faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
suits = ["clubs", "diamonds", "hearts", "spades"]
cards = []
if face_filter != None:
faces = face_filter
for face in faces:
for suit in suits:
card = Card(suit, face)
cards.append(card)
self.cards = cards
def add_cards(self, cards):
self.cards += cards
def pop_cards(self, num_cards):
to_return = self.cards[:num_cards]
self.cards = self.cards[num_cards:]
return to_return
def cut(self):
random_cut = random.randint(1, len(self.cards)) # decide how many card to cut
self.cards = self.cards[random_cut:] + self.cards[:random_cut]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def print_deck(self):
for card in self.cards:
print("{", card.get_suit(), card.get_face(), "}", end=""),
print("\n")
class Player:
def __init__(self):
self.owned_cards = []
def get_cards(self):
return self.owned_cards
def remove_cards(self):
self.owned_cards = []
def deal_cards(self, cards):
self.owned_cards += cards
class Game:
def __init__(self, number_of_players, dealing_directions, dealing_instructions):
self.number_of_players = number_of_players
self.dealing_directions = dealing_directions
self.dealing_instructions = dealing_instructions
self.faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
self.deck = Deck(self.faces)
self.players = []
for i in range(self.number_of_players):
player = Player()
self.players.append(player)
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.add_cards(player.get_cards())
player.remove_cards()
self.deck.cut()
self.deck.shuffle()
def deal(self, player):
ind = self.players.index(player)
order = []
if self.dealing_directions == "ltr":
order += range(ind, len(self.players))
order += range(0, ind - 1)
else:
order += range(ind , -1, -1)
order += range(len(self.players) - 1, ind, -1)
for cards_to_deal in self.dealing_instructions:
if len(self.deck.get_cards()) // self.number_of_players == 0:
break # if there are not enough cards to give to all players at least 1, stop
for i in order:
self.players[i].deal_cards(self.deck.pop_cards(cards_to_deal))
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
self.faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
self.deck = Deck(self.faces)
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))

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

.........F.....
======================================================================
FAIL: test_dealing_ltr (test.TestGame)
Test dealing the cards left to right.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: {('hearts', '2'), ('clubs', '3'), ('clubs', '2')} not found in ({('spades', '2'), ('hearts', '3'), ('clubs', '2')}, {('spades', 'A'), ('diamonds', 'K'), ('clubs', 'A')})

----------------------------------------------------------------------
Ran 15 tests in 0.157s

FAILED (failures=1)

История (3 версии и 7 коментара)

Таня обнови решението на 13.11.2022 19:58 (преди около 2 години)

+import random
+
+class Card:
+ def __init__(self, suit, face):
+ self.suit, self.face = suit, face
+
+ def get_suit(self) -> str:
+ return self.suit
+
+ def get_face(self) -> str:
+ return self.face
+
+ def print_card(self):
+ print(self.suit, self.face)
+
+class Deck:
+ def __init__(self, face_filter = None):
+ faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
+ suits = ["clubs", "diamonds", "hearts", "spades"]
+ cards = []
+
+ if face_filter != None:
+ faces = face_filter
+
+ for face in faces:
+ for suit in suits:
+ card = Card(face, suit)
+ cards.append(card)
+
+ self.cards = cards
+
+ def add_cards(self, cards):
+ self.cards += cards
+
+ def pop_cards(self, num_cards):
+ to_ret = []
+ for i in range(num_cards):
+ to_ret.append(self.cards.pop(0))
+ return to_ret
+
+ def cut(self):
+ random_cut = random.randint(1, len(self.cards)) # decide how many card to cut
+ self.cards = self.cards[random_cut:] + self.cards[:random_cut]
+
+ def shuffle(self):
+ random.shuffle(self.cards)
+
+ def get_cards(self):
+ return self.cards
+
+ def print_deck(self):
+ for card in self.cards:
+ print("{", card.get_suit(), card.get_face(), "}", end=""),
+ print("\n")
+
+class Player:
+ def __init__(self):
+ self.owned_cards = []
+
+ def get_cards(self):
+ return self.owned_cards
+
+ def remove_cards(self):
+ self.owned_cards = []
+
+ def deal_cards(self, cards):
+ self.owned_cards += cards
+
+class Game:
+ def __init__(self, number_of_players, dealing_directions, dealing_instructions):
+ self.number_of_players = number_of_players
+ self.dealing_directions = dealing_directions
+ self.dealing_instructions = dealing_instructions
+ self.faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
+ self.deck = Deck(self.faces)
+ self.players = []
+ for i in range(self.number_of_players):
+ player = Player()
+ self.players.append(player)
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ self.deck.add_cards(player.get_cards())
+ player.remove_cards()
+
+ self.deck.cut()
+ self.deck.shuffle()
+
+ def deal(self, player):
+ ind = self.players.index(player)
+ order = []
+ if self.dealing_directions == "ltr":
+ order += range(ind, len(self.players))
+ order += range(0, ind - 1)
+ else:
+ order += range(ind , -1, -1)
+ order += range(len(self.players) - 1, ind, -1)
+
+
+ for cards_to_deal in self.dealing_instructions:
+ if len(self.deck.get_cards()) // self.number_of_players == 0:
+ break # if there are not enough cards to give to all players at least 1, stop
+ for i in order:
+ self.players[i].deal_cards(self.deck.pop_cards(cards_to_deal))
+
+ def get_deck(self):
+ return self.deck
+
+
+class Belot(Game):
+ def __init__(self):
+ super().__init__(4, "ltr", (2, 3, 3))
+ self.faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
+ self.deck = Deck(self.faces)
+
+
+class Poker(Game):
+ def __init__(self):
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))
+
+
+def main():
+ belot = Poker()
+ players = belot.get_players()
+ belot.prepare_deck()
+ belot.deal(players[0])
+ # Play the game, argue about the rules, curse, fight, argue about who should deal the cards during second round...
+ belot.prepare_deck()
+ belot.deal(players[1])
+if __name__ == "__main__":
+ main()

Таня обнови решението на 13.11.2022 19:58 (преди около 2 години)

import random
class Card:
def __init__(self, suit, face):
self.suit, self.face = suit, face
def get_suit(self) -> str:
return self.suit
def get_face(self) -> str:
return self.face
def print_card(self):
print(self.suit, self.face)
class Deck:
def __init__(self, face_filter = None):
faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
suits = ["clubs", "diamonds", "hearts", "spades"]
cards = []
if face_filter != None:
faces = face_filter
for face in faces:
for suit in suits:
card = Card(face, suit)
cards.append(card)
self.cards = cards
def add_cards(self, cards):
self.cards += cards
def pop_cards(self, num_cards):
to_ret = []
for i in range(num_cards):

Ако няма да иползваш някаква променлива, имнувай я _, както е по конвенция.

Реално можеш да напишеш същото и по-кратко:

to_return = self.cards[:num_cards]
self.cards = self.cards[num_cards:]
return to_return

Не съм сигурен, че индексите ми са правилни. не съм го тествал. Може да се наложи +-1 някъде, но това е идеята.

to_ret.append(self.cards.pop(0))
return to_ret
def cut(self):
random_cut = random.randint(1, len(self.cards)) # decide how many card to cut
self.cards = self.cards[random_cut:] + self.cards[:random_cut]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def print_deck(self):
for card in self.cards:
print("{", card.get_suit(), card.get_face(), "}", end=""),
print("\n")
class Player:
def __init__(self):
self.owned_cards = []
def get_cards(self):
return self.owned_cards
def remove_cards(self):
self.owned_cards = []
def deal_cards(self, cards):
self.owned_cards += cards
class Game:
def __init__(self, number_of_players, dealing_directions, dealing_instructions):
self.number_of_players = number_of_players
self.dealing_directions = dealing_directions
self.dealing_instructions = dealing_instructions
self.faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
self.deck = Deck(self.faces)
self.players = []
for i in range(self.number_of_players):
player = Player()
self.players.append(player)
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.add_cards(player.get_cards())
player.remove_cards()
self.deck.cut()
self.deck.shuffle()
def deal(self, player):
ind = self.players.index(player)
order = []
if self.dealing_directions == "ltr":
order += range(ind, len(self.players))
order += range(0, ind - 1)
else:
order += range(ind , -1, -1)
order += range(len(self.players) - 1, ind, -1)
for cards_to_deal in self.dealing_instructions:
if len(self.deck.get_cards()) // self.number_of_players == 0:
break # if there are not enough cards to give to all players at least 1, stop
for i in order:
self.players[i].deal_cards(self.deck.pop_cards(cards_to_deal))
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
self.faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
self.deck = Deck(self.faces)
class Poker(Game):
def __init__(self):
- super().__init__(9, "rtl", (1, 1, 1, 1, 1))
-
+ super().__init__(9, "rtl", (1, 1, 1, 1, 1))
-
-def main():
- belot = Poker()
- players = belot.get_players()
- belot.prepare_deck()
- belot.deal(players[0])
- # Play the game, argue about the rules, curse, fight, argue about who should deal the cards during second round...
- belot.prepare_deck()
- belot.deal(players[1])
-if __name__ == "__main__":
- main()

Таня обнови решението на 14.11.2022 20:25 (преди около 2 години)

import random
class Card:
def __init__(self, suit, face):
- self.suit, self.face = suit, face
+ self.__suit,self.__face = suit, face
def get_suit(self) -> str:
- return self.suit
+ return self.__suit
def get_face(self) -> str:
- return self.face
+ return self.__face
def print_card(self):
- print(self.suit, self.face)
+ print(self.__suit, self.__face)
class Deck:
- def __init__(self, face_filter = None):
+ def __init__(self, face_filter=None):
faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
suits = ["clubs", "diamonds", "hearts", "spades"]
cards = []
if face_filter != None:
faces = face_filter
for face in faces:
for suit in suits:
- card = Card(face, suit)
+ card = Card(suit, face)
cards.append(card)
self.cards = cards
def add_cards(self, cards):
self.cards += cards
def pop_cards(self, num_cards):
- to_ret = []
- for i in range(num_cards):
- to_ret.append(self.cards.pop(0))
- return to_ret
+ to_return = self.cards[:num_cards]
+ self.cards = self.cards[num_cards:]
+ return to_return
def cut(self):
random_cut = random.randint(1, len(self.cards)) # decide how many card to cut
self.cards = self.cards[random_cut:] + self.cards[:random_cut]
def shuffle(self):
random.shuffle(self.cards)
def get_cards(self):
return self.cards
def print_deck(self):
for card in self.cards:
print("{", card.get_suit(), card.get_face(), "}", end=""),
print("\n")
class Player:
def __init__(self):
self.owned_cards = []
def get_cards(self):
return self.owned_cards
def remove_cards(self):
self.owned_cards = []
def deal_cards(self, cards):
self.owned_cards += cards
class Game:
def __init__(self, number_of_players, dealing_directions, dealing_instructions):
self.number_of_players = number_of_players
self.dealing_directions = dealing_directions
self.dealing_instructions = dealing_instructions
self.faces = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
self.deck = Deck(self.faces)
self.players = []
for i in range(self.number_of_players):
player = Player()
self.players.append(player)
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
self.deck.add_cards(player.get_cards())
player.remove_cards()
self.deck.cut()
self.deck.shuffle()
def deal(self, player):
ind = self.players.index(player)
order = []
if self.dealing_directions == "ltr":
order += range(ind, len(self.players))
order += range(0, ind - 1)
else:
order += range(ind , -1, -1)
order += range(len(self.players) - 1, ind, -1)
for cards_to_deal in self.dealing_instructions:
if len(self.deck.get_cards()) // self.number_of_players == 0:
break # if there are not enough cards to give to all players at least 1, stop
for i in order:
self.players[i].deal_cards(self.deck.pop_cards(cards_to_deal))
def get_deck(self):
return self.deck
class Belot(Game):
def __init__(self):
super().__init__(4, "ltr", (2, 3, 3))
self.faces = ["7", "8", "9", "10", "J", "Q", "K", "A"]
self.deck = Deck(self.faces)
class Poker(Game):
def __init__(self):
super().__init__(9, "rtl", (1, 1, 1, 1, 1))