Skip to main content

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

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

playingcards.py-1.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

playingcards.py-1.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file playingcards.py-1.1.1.tar.gz.

File metadata

  • Download URL: playingcards.py-1.1.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for playingcards.py-1.1.1.tar.gz
Algorithm Hash digest
SHA256 53c975acb8c5280a3668db6d6d8c254e6db79edcd0daf74f0cd83f00770831ac
MD5 a03f7b49b8c9cd685e6213124f90d69a
BLAKE2b-256 013a05f9f911e968f97a0e47ae0eae6525232a957ed8f64ca8668aabaa2fe33d

See more details on using hashes here.

File details

Details for the file playingcards.py-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: playingcards.py-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for playingcards.py-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6db47f2ed132ab5f611acd4fce002235e11eb4225567bfb8d147c02589897d4f
MD5 45a1c22e5ade424846f1f48529dac11d
BLAKE2b-256 41de9987567abe83e164b7282ebb8894d50064d49b6644487d4c6b10dfe9dce6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page