Skip to main content

A Python module for playing cards.

Project description

cardpy

A comprehensive Python module for playing cards, providing flexible card and deck management with type hints and modern Python features.

Features

  • Standard 52-card deck management
  • Support for multiple decks (e.g., for Blackjack)
  • Card comparison and sorting
  • Unicode suit symbols (â™ , ♥, ♦, ♣)
  • Rich deck operations (shuffle, cut, deal, etc.)
  • Type hints and thorough error checking
  • Chainable methods for fluid syntax
  • Hand management for card games
  • Custom sorting options
  • Face-up/face-down card tracking

Installation

pip install cardpy

Quick Start

from cardpy import Card, Deck, Hand, Rank, Suit

# Create and shuffle a deck
deck = Deck(init=True)  # Creates standard 52-card deck
deck.shuffle()

# Create a hand and deal cards
hand = Hand()
hand.extend(deck.draw_multiple(5))
print(f"Your hand: {hand}")

# Create specific cards
ace_spades = Card(Rank.ACE, Suit.SPADES)
king_hearts = Card('K', '♥')  # String representations work too

# Compare cards
if ace_spades > king_hearts:
    print("Ace beats King!")

Core Classes

Card

Represents a playing card with rank, suit, color and face-up status:

# Multiple ways to create cards
ace = Card(Rank.ACE, Suit.SPADES)
king = Card('K', 'H')  # Using string shortcuts
ten = Card('10', '♦')  # Using Unicode symbols

# Card properties
print(f"{ace}")        # "ACE of SPADES â™ "
print(f"{ace:rank}")   # "A"
print(f"{ace:suit}")   # "â™ "
print(f"{ace:color}")  # "black"

# Face-up/down handling
ace.face_up = True
ace.flip()  # Flips the card over

Deck

Manages collections of cards with rich operations:

# Different deck types
standard = Deck(init=True)           # 52-card deck
empty = Deck()                       # Empty deck
blackjack = Deck(init=True, deck_count=6)  # 6 decks combined

# Chain operations
deck.shuffle().cut().reverse()

# Card operations
card = deck.draw()
hand = deck.draw_multiple(5)
top_three = deck.peek_multiple(3)

# Combine decks
big_deck = deck1 + deck2
deck1 *= 2  # Double the deck

Hand

Manages player hands with similar operations to Deck:

# Create and manage hands
hand = Hand()
hand.extend(deck.draw_multiple(5))
hand.sort()  # Sort by rank and suit

# Play cards
played_card = hand.play()
played_multiple = hand.play_multiple(2)

# Combine hands
merged_hand = hand1 + hand2

Advanced Features

Custom Sorting

Sort cards using custom rank and suit orders:

# Define custom ordering
ace_low = [Rank.ACE] + Card.RANKS[:-1]  # Ace-low ordering
hearts_high = [Suit.DIAMONDS, Suit.CLUBS, Suit.SPADES, Suit.HEARTS]

# Sort using custom order
deck.sort(ranks=ace_low, suits=hearts_high)
hand.sort(ranks=ace_low, suits=hearts_high)

Multiple Deck Games

# Create a 6-deck shoe for Blackjack
shoe = Deck(init=True, deck_count=6)
shoe.shuffle()

# Deal cards to multiple players
hands = shoe.deal(4, 2)  # 2 cards each to 4 players

# Track remaining cards
remaining = len(shoe)
print(f"Cards remaining: {remaining}")

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Risheet Lenka - GitHub Profile

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

cardpy-2.0.2.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cardpy-2.0.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file cardpy-2.0.2.tar.gz.

File metadata

  • Download URL: cardpy-2.0.2.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for cardpy-2.0.2.tar.gz
Algorithm Hash digest
SHA256 59e9b3477164e5cfdc6542194c8ee0ec6398c58c33556c7cec45e7324eb0b779
MD5 582dfe12e87acf9bf493302729ed382c
BLAKE2b-256 40c4d2a04244ef6e1b6e753a25b5cad1b8f61f518a95e23e383702b0fa45e298

See more details on using hashes here.

File details

Details for the file cardpy-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: cardpy-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for cardpy-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 01b8c8df10887161549484835b11bc6f90990b8c3f33c87a43a26e67c5ce86f9
MD5 53ec5cfa6bf581a324a0a38763c7ede0
BLAKE2b-256 f007697890bacc9bc9692f483636dae1d1ab046bc05422ce25a19ba0ae61c3af

See more details on using hashes here.

Supported by

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