Python Forum

Full Version: Implementation Pyramid Solitaire
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
You must create a class map to represent a playing card in the pyramid.
Create a class deck for submission deck of cards in the pyramid.
Here's the code I wrote, but the teacher said that we should implement this class using design patterns, help please.

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 should be sinhletonom to have accidentally got 5 aces when you do something. There is a deck implement iterator that give the current map. Also, as I understand it, the cards have rozpyhatys pyramid in rows, each row can be created churez factory classes. And the output of each line by designer says teacher but I have not quite mastered the design patterns.
I update the tags in your post for you. Please copy plain text code, so that there are no formatting tags in the post. Otherwise we can't copy and run your code to find any problems with it.

There are several ways to implement an iterator. The easiest way to do this with an object like you've got is to override __iter__ to return an iterator over the list of cards. However, you've got two lists of cards, which is confusing. I would just shuffle the first list of cards (using random.shuffle), and not create a second list of cards. Also, that would just give you an iterator over the cards. Is the iterator supposed to yield the rows, not the individual cards?

I'm not sure about design patterns, I don't use those.
(Feb-27-2017, 11:47 AM)ichabod801 Wrote: [ -> ]I update the tags in your post for you. Please copy plain text code, so that there are no formatting tags in the post. Otherwise we can't copy and run your code to find any problems with it.

There are several ways to implement an iterator. The easiest way to do this with an object like you've got is to override __iter__ to return an iterator over the list of cards. However, you've got two lists of cards, which is confusing. I would just shuffle the first list of cards (using random.shuffle), and not create a second list of cards. Also, that would just give you an iterator over the cards. Is the iterator supposed to yield the rows, not the individual cards?

I'm not sure about design patterns, I don't use those.

I also say that the patterns are not very suitable for this task, but the condition of laboratory work to do with the implementation of design patterns