You need to create a map class to represent the game map in the pyramid. Create a Deck class to represent a deck of cards in the pyramid. So I wrote the code, but the teacher says that you need to implement this class using design patterns, please help.

import random class Card(object): def __init__(self): self.list = ['в™*', 'в™Ј', '♥', '♦'] self.cards = [] self.cart = [] for card_num in range(0, 52): r = str(card_num % 13) if r == '0': r = 'K' if r == '1': r = 'A' if r == '12': r = 'Q' if r == '11': r = 'J' index = int((card_num / 13) % 13) self.cards.append((r, self.list[index])) def draw(self): next = self.cards.pop(random.randint(0, len(self.cards) - 1)) return next def deck(self): c = Card() for i in range(0, 52): self.cart.append(c.draw()) print(30*' ',self.cart[0]) print(25*' ',self.cart[1:3]) print(20*' ',self.cart[4:7]) print(15*' ',self.cart[7:11]) print(10*' ',self.cart[11:16]) print(5*' ',self.cart[16:22]) print(self.cart[23:30]) print(30 * "---") print(self.cart[31:]) c = Card() c.deck() 

The deck must be a singleton. Also, the deck has to implement an iterator that produces current maps. Also, as far as I understand, maps have rozpihatsya in the rows of the pyramid, then each row can be created through the class factory. And the output of each line through the decorator.

    1 answer 1

    The deck and card should be made in two separate classes. A singleton in a class can be implemented by overriding the special __new__ method. The iterator can be implemented by defining a special method __iter__

    I didn’t understand this part at all: "maps have rotate through the rows of the pyramid, then each line can be created through the class factory. And the output of each line through the decorator." If you can clearly articulate what you need, I can add the code.

     from random import shuffle CARD_VALUES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'K', 'Q', 'A'] CARD_SUITS = ['\u2665', '\u2666', '\u2663', '\u2660'] class Card: def __init__(self, number): self.suit, self.value = divmod(number, 13) def __str__(self): return CARD_SUITS[self.suit] + CARD_VALUES[self.value] def __repr__(self): return str(self) class Deck: singleton = None def __new__(cls): if cls.singleton: return cls.singleton else: obj = super().__new__(cls) cls.singleton = obj return obj def __init__(self): self.cards = [Card(i) for i in range(52)] self.shuffle() def __iter__(self): return iter(self.cards) def shuffle(self): shuffle(self.cards) def __str__(self): return 'Deck{}'.format(self.cards) # Демнострация поведения синглтона: # видно, что оба экземпляра класса являются одним и тем же объектом deck1 = Deck() deck2 = Deck() print(deck1) print(deck2) print(deck1 is deck2) # Демонстрация работы итератора print([card for card in deck1]) 
    • maps have to diverge along the lines of the pyramid, each line of the pyramid should be an abstract farbrika and the output of each line should be through the decorator - drako08
    • @ drako08, not clearer. Do you have a clearly worded quest text? - Xander
    • create a map class to represent the game map in the pyramid. Create a class Deck to represent a deck of cards in the pyramid - drako08