Example #1
0
def build_cards(jokers=False, num_jokers=0):
    """
    Builds a list containing a full French deck of 52 Card instances. The
    cards are sorted according to ``DEFAULT_RANKS``.

    .. note:
        Adding jokers may break some functions & methods at the moment.

    :arg bool jokers:
        Whether or not to include jokers in the deck.
    :arg int num_jokers:
        The number of jokers to include.

    :returns:
        A list containing a full French deck of 52 Card instances.

    """
    new_deck = []

    if jokers:
        new_deck += [Card("Joker", None) for i in xrange(num_jokers)]

    new_deck += [Card(value, suit) for value in VALUES for suit in SUITS]

    return new_deck
Example #2
0
    def build(self, faces=None, suits=None):
        """
        Builds a list containing a full French deck of 52 Card instances.
    
        :arg int jokers:
            The number of jokers to include.
    
        :returns:
            A list containing a full French deck of 52 Card instances, plus
            Jokers, if applicable.
        """

        faces = faces or self.faces
        suits = suits or self.suits

        if not isinstance(faces, list) or not isinstance(suits, list):
            raise ValueError(
                'Cannot build a deck with a list of faces and suits')

        self.cards = [
            Card(face, suit, ranks=self.ranks) for face in faces
            for suit in suits
        ]

        if self.jokers:
            self.cards += [
                Card(Faces.JOKER, None, ranks=self.ranks)
                for i in range(int(self.jokers))
            ]

        self.cards = deque(self.cards)
        return self.cards
Example #3
0
def open_cards(filename=None):
    """
    Open cards from a txt file.

    :arg str filename:
        The filename of the deck file to open. If no filename given,
        defaults to "cards-YYYYMMDD.txt", where "YYYYMMDD" is the year, month,
        and day. For example, "cards-20140711.txt".

    :returns:
        The opened cards, as a list.

    """
    filename = filename or "cards-%s.txt" % (time.strftime("%Y%m%d"))

    with open(filename, "r") as deck_file:
        card_data = [line.rstrip("\n") for line in deck_file.readlines()]

    cards = [None] * len(card_data)

    for i, card in enumerate(card_data):
        card = card.split()
        cards[i] = Card(card[0], card[1])

    return cards