No project description provided
Project description
CardKit
A Python library for cards and decks with context-dependent rules, supporting various card games like Poker, Blackjack, Baccarat, and more.
Features
- Flexible Card Representation: Cards with ranks, suits, and game-specific values.
- Context-Dependent Rules: Easily switch between different card game rules using predefined contexts.
- Deck Management: Shuffle, draw cards from top or bottom, reset, and compare cards.
- Game-Specific Contexts: Predefined rank values for Poker, Blackjack, Baccarat, Rummy, Bridge, War, and Crazy Eights.
Installation
Install CardKit via pip:
pip install pycardkit
Usage
Basic Usage
Import the necessary classes and contexts:
from pycardkit import Card, Deck, card_context
Creating Cards
Create a card with a specific context:
# Using Poker context
card = Card("A", "Hearts", card_context.POKER_RANK_VALUES)
print(card) # Output: A of Hearts
print(card.value) # Output: 14
Creating and Managing Decks
Create a deck with a context:
deck = Deck(card_context.BLACKJACK_RANK_VALUES)
deck.shuffle()
# Draw cards
hand = deck.grab_cards_top(5)
for card in hand:
print(card)
Comparing Cards
Compare two cards based on the deck's rules:
card1 = Card("K", "Spades", card_context.POKER_RANK_VALUES)
card2 = Card("Q", "Hearts", card_context.POKER_RANK_VALUES)
diff = deck.compare(card1, card2)
if diff > 0:
print("Card1 is higher")
elif diff < 0:
print("Card2 is higher")
else:
print("Cards are equal")
Ordering Hands
Order a hand of cards:
hand = [Card("5", "Clubs", card_context.POKER_RANK_VALUES),
Card("A", "Hearts", card_context.POKER_RANK_VALUES),
Card("K", "Diamonds", card_context.POKER_RANK_VALUES)]
ordered_hand = deck.order_cards(hand)
for card in ordered_hand:
print(card)
Card Properties
Check various properties of a card:
card = Card("K", "Hearts", card_context.POKER_RANK_VALUES)
print(f"Is face card: {card.is_face()}") # Output: True
print(f"Is ace: {card.is_ace()}") # Output: False
print(f"Color: {card.get_color()}") # Output: Red
Direct Card Comparisons
Use comparison operators for cards:
card1 = Card("A", "Spades", card_context.POKER_RANK_VALUES)
card2 = Card("K", "Hearts", card_context.POKER_RANK_VALUES)
print(card1 > card2) # Output: True (Ace is higher in Poker)
print(card1 == card2) # Output: False
Drawing from Bottom and Resetting
Draw cards from the bottom and reset the deck:
deck = Deck(card_context.BLACKJACK_RANK_VALUES)
deck.shuffle()
bottom_cards = deck.grab_cards_bottom(3)
print("Bottom cards:")
for card in bottom_cards:
print(card)
deck.reset() # Reset to ordered deck
print(f"Deck size after reset: {len(deck.deck)}")
Game-Specific Examples
Poker
from pypycardkit import Deck, card_context
deck = Deck(card_context.POKER_RANK_VALUES)
deck.shuffle()
hand = deck.grab_cards_top(5)
print("Poker Hand:")
for card in hand:
print(f"{card} - Value: {card.value}")
Blackjack
from pycardkit import Deck, card_context
deck = Deck(card_context.BLACKJACK_RANK_VALUES)
deck.shuffle()
player_hand = deck.grab_cards_top(2)
dealer_hand = deck.grab_cards_top(2)
print("Player Hand:")
for card in player_hand:
print(f"{card} - Value: {card.value}")
print("Dealer Hand:")
for card in dealer_hand:
print(f"{card} - Value: {card.value}")
Baccarat
from pycardkit import Deck, card_context
deck = Deck(card_context.BACCARAT_RANK_VALUES)
deck.shuffle()
player_card = deck.grab_cards_top(1)[0]
banker_card = deck.grab_cards_top(1)[0]
print(f"Player Card: {player_card} - Value: {player_card.value}")
print(f"Banker Card: {banker_card} - Value: {banker_card.value}")
API Reference
Card Class
__init__(rank: str, suit: str, context: dict): Initialize a card. Raises ValueError for invalid rank or suit.__str__(): String representation.__repr__(): Representation for debugging.__eq__(other): Check equality based on rank and suit.__gt__(other): Greater than comparison using deck rules.__lt__(other): Less than comparison using deck rules.is_face() -> bool: Check if the card is a face card (J, Q, K).is_ace() -> bool: Check if the card is an Ace.get_color() -> str: Get the color of the card ('Red' or 'Black').
Deck Class
__init__(context: dict): Initialize a deck with a context.shuffle(): Shuffle the deck.grab_cards_top(amount): Draw cards from the top.grab_cards_bottom(amount): Draw cards from the bottom.reset(): Reset the deck to ordered state.compare(card1, card2): Compare two cards.order_cards(hand): Order a list of cards.
Contexts
Available contexts in pycardkit.card_context:
POKER_RANK_VALUESPOKER_ACE_LOW_RANK_VALUESPOKER_ACE_FLEXIBLE_RANK_VALUESBLACKJACK_RANK_VALUESBACCARAT_RANK_VALUESRUMMY_RANK_VALUESRUMMY_ACE_HIGH_RANK_VALUESBRIDGE_RANK_VALUESWAR_RANK_VALUESCRAZY_EIGHTS_RANK_VALUES
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycardkit-1.3.tar.gz.
File metadata
- Download URL: pycardkit-1.3.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2cc28c193985f4076c133df6617e9e7995245e3b2c4e4f328f59b3b25dd1925
|
|
| MD5 |
8fd82cab66d74cab979333fe3935b3e0
|
|
| BLAKE2b-256 |
7dc7ee674198f214c10026158f44a6ab44c0044e621eaf2b5936efbc2129f894
|
File details
Details for the file pycardkit-1.3-py3-none-any.whl.
File metadata
- Download URL: pycardkit-1.3-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ab7b306e17e8e039300460f11821a6f78e3d478c75f3e50230f17f483a88a22
|
|
| MD5 |
685d7b34b3854a40fa1a41e34afbdfa5
|
|
| BLAKE2b-256 |
a09ac2bfbf6281b65ca7548be9accf26d9aee884569087d1af7f6cd20817c9da
|