Skip to main content

playingcards.py

MIT License PyPi Python Versions

An Advanced Python Playing Card Module that makes creating playing card games simple and easy!

Features

  • Easy to Understand Class Objects
  • ASCII Card Images
  • Card Comparisons
  • Duplicate Prevention within Decks

Installation

Requires Python 3.6 and above

You can install the module from PyPI or by using pip.

# Linux/MacOS
pip3 install playingcards.py

# Windows
pip install playingcards.py

How To Use

This module introduces three class objects: CardCollection, Deck and Card.

Differences Between Classes

Card is a class that contains properties that are found on your everyday playing card, i.e. rank and suit.

CardCollection is a class used to construct various types of card collections. For example, you could use it to construct a class for a "Hand" of cards.

Deck is class constructed from the CardCollection class. It contains 52 upon its initialization and has member functions related to drawing cards and attributes referring to the numeric amounts in the deck.

Drawing Cards

The Deck class can be called with the draw_card() function to draw a card object. Additionally, draw_n() can be used to draw multiple cards at once and be returned as a CardCollection object.

Generating a card by only using the Card class happpens when you instantiate it.

from playingcards import Deck, Card

# Card Generated Using Deck
player_deck = Deck()
player_card = player_deck.draw_card()
player_hand = player_deck.draw_n(5)

# Card Generated From Instantiation
player_card_2 = Card()

Class Methods & Attributes

  • Card
    • Attributes
      • deck Deck - Returns a Deck object if the card was drawn from a deck. Default: None.
      • suit int - Returns an integer that corresponds with the card's suit.
      • suit_name str - Returns a string containing the converted suit name.
      • value int - Returns an integer of the card's face value.
      • rank Union[str,int] - Returns a string if the value can be converted into a word value (Ex. 11 -> Jack). Defaults to returning an integer if its not applicable (Ex. 2 -> 2).
      • name str - Returns a string containing the full name of the card. This prints out the rank and the suit of the card. (Ex. Ace of Spades, 3 of Hearts)
      • img str - Returns a string that contains an ASCII image of the card with the corresponding suit symbol.
  • CardCollection
    • Attributes
      • cards List[Card] - Returns a list of Card objects.
      • maximum int - Returns the maximum number of cards that can be held in the collection.
      • ordered bool - Returns a boolean value that indicates whether the collection is ordered or not.
    • Methods
      • add_cards(cards, position, random ) - Adds cards to the collection.
        • Parameters
          • cards (List[Card]) - A list of Card objects to be added to the collection.
          • position (int) - The position in the collection to add the cards.
          • random (bool) - A boolean value that indicates whether the cards should be added in a random position.
        • Returns
          • None
      • remove_card(cards, position) - Removes a card from the collection.
        • Parameters
          • cards (List[Card]) - A list of Card objects to be removed from the collection.
          • position (int) - The position in the collection to remove the card.
        • Returns
          • None
      • order_cards() - Orders the cards in the collection.
        • Parameters
          • None
        • Returns
          • None
      • shuffle() - Shuffles the cards in the collection.
        • Parameters
          • None
        • Returns
          • None
  • Deck (inherits CardCollection methods & attributes)
    • Attributes
      • cards list - Returns a list containing the class objects of each drawn card.
      • drawn int - Returns an integer of the amount of cards that have been drawn.
      • remaining int - Returns an integer amount of the remaining cards that can be drawn.
  • Methods
    • draw_card() - Draws a card from the top of the deck.
      • Parameters
        • None
      • Returns
        • Card
    • draw_n(n) - Draws a number of cards from the top of the deck.
      • Parameters
        • n (int) - The number of cards to draw.
      • Returns
        • CardCollection

Object Initialization Arguments

A card object can be instantiated with preconceived values instead of using a random number generator.

  • Suits are ordered numerically from 0-3.

    • 0: Spades

    • 1: Clubs

    • 2: Hearts

    • 3: Diamonds

  • Values are ordered numerically from 1-13.

    • 1: Ace

    • 2-10: Face Value

    • 11: Jack

    • 12: Queen

    • 13: King

player_card = Card(value=11, suit=1)


print(player_card)
>> Jack of Clubs

print(player_card.value)
>> 11

print(player_card.suit_name)
>> Clubs

print(player_card.rank)
>> Jack

print(player_card.img)
>> *- - -*
   | ♣   |
   |  J  |
   |   ♣ |
   *- - -*

Card Comparisons

The card objects feature comparison features which allows their values to be compared.

When checking for equivalency, it only checks the value of the card, not the suits.

card_1 = Card(value=8, suit=0)
card_2 = Card(value=12, suit=2)
card_3 = Card(value=8, suit=3)

print(card_1 < card_2)
>> True

print(card_3 == card_1) # Returns True even if the suit is different
>> True

Contributing

See CONTRIBUTING.md

Acknoledgements

The CardCollection object was inspired by @mitchr1598 whose playingcards project utilized.

Release files for playingcards.py 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for playingcards.py 1.1.1
File Size Uploaded
playingcards.py-1.1.1.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for playingcards.py 1.1.1
File Interpreter ABI Platform
playingcards.py-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / playingcards.py-1.1.1.tar.gz

Download URL playingcards.py-1.1.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
53c975acb8c5280a3668db6d6d8c254e6db79edcd0daf74f0cd83f00770831ac
BLAKE2b-256 checksum
How to use checksums
013a05f9f911e968f97a0e47ae0eae6525232a957ed8f64ca8668aabaa2fe33d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

Release files / playingcards.py-1.1.1-py3-none-any.whl

Download URL playingcards.py-1.1.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6db47f2ed132ab5f611acd4fce002235e11eb4225567bfb8d147c02589897d4f
BLAKE2b-256 checksum
How to use checksums
41de9987567abe83e164b7282ebb8894d50064d49b6644487d4c6b10dfe9dce6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page