CardSharp is a powerful Python package for simulating, analyzing, and playing card games. While it currently focuses on Blackjack, its flexible architecture allows for easy extension to other card games.
- ๐ Robust Blackjack simulation with multiple strategies
- ๐ Real-time visualization of game statistics
- โ๏ธ Highly configurable game rules and parameters
- ๐งช Extensible framework for implementing new card games
- ๐ฅ๏ธ Support for both CLI and programmatic usage
- ๐งฎ Advanced statistical analysis of game outcomes
- ๐ Event-driven architecture for improved modularity
- ๐ Platform adapters for multi-platform support
# Clone the repository
git clone https://github.com/yourusername/cardsharp.git
cd cardsharp
# Create a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
# Install dependencies
pip install poetry
poetry install
CardSharp offers various modes of operation to suit different needs:
Run a batch simulation of Blackjack games:
python cardsharp/blackjack/blackjack.py --simulate --num_games 10000 --vis
This command simulates 10,000 games of Blackjack and displays a real-time visualization of the results.
Compare different Blackjack strategies:
python cardsharp/blackjack/blackjack.py --analysis --num_games 5000
This runs a comparative analysis of Basic, Counting, Aggressive, and Martingale strategies over 5,000 games.
Play Blackjack interactively in the console:
python cardsharp/blackjack/blackjack.py --console
Try the new event system:
python examples/event_system_demo.py
Experience the platform adapter system:
python examples/adapter_demo.py
See how immutable state transitions work:
python examples/immutable_state_demo.py
Experience the new engine with immutable state:
python examples/blackjack_engine_demo.py
See the new Phase 3 asynchronous API in action:
python examples/async_api_demo.py
This demo showcases:
- High-level BlackjackGame interface
- Event-driven flow control
- Synchronous and asynchronous operation modes
- Auto-play capabilities
Experience the BlackjackGame API with event handling:
python examples/blackjack_game_demo.py
See how different card representations work with the HandState class:
python examples/card_handling_demo.py
Demonstrate proper cleanup of event handlers when games are shut down:
python examples/event_cleanup_test.py
Experience the Streamlit-based web UI for Blackjack:
python examples/modern_blackjack_ui.py
See the immutable state verification system in action:
python examples/verification_demo.py
Play the complete blackjack game using the new architecture:
python examples/cli_blackjack.py
You can customize your game experience with these options:
# Play with 3 players
python examples/cli_blackjack.py --players 3
# Start with a custom bankroll
python examples/cli_blackjack.py --bankroll 500
# Play a fixed number of rounds
python examples/cli_blackjack.py --rounds 5
cardsharp/
blackjack/
: Blackjack-specific implementationscommon/
: Shared utilities and base classesevents/
: Event system for event-driven architectureadapters/
: Platform adapters for different environmentsstate/
: Immutable state models and transition functionsengine/
: Core game engine implementationwar/
: War card game implementationhigh_card/
: High Card game implementationroulette/
: Roulette game implementation (in progress)verification/
: Game verification and event recording
CardSharp has undergone a phased modernization to an event-driven, adapter-based architecture:
- Event System: Core components communicate through a robust event system
- Adapters: Platform-specific code is isolated in adapters
- Immutable State: Game state transitions via pure functions
- Asynchronous API: Clean, platform-agnostic API with rich event-driven flow control
- Dual Mode: Support for both synchronous and asynchronous operation
The architecture modernization plan has been completed:
- โ Phase 1: Event System and Adapters
- โ Phase 2: Immutable State
- โ Phase 3: Asynchronous API
- โ Phase 4: Integration & Component Architecture
Check out the architecture documentation for details.
When optimizing CardSharp simulations, it's critical to maintain accuracy. See our optimization principles for guidelines on proper performance improvements without compromising simulation fidelity.
Run the test suite to ensure everything is working correctly:
# Run all tests
pytest tests/
# Run tests with coverage report
pytest tests/ --cov=cardsharp
# Run specific test modules
pytest tests/api/test_event_cleanup.py -v
The project includes comprehensive test coverage for:
- Core components (cards, decks, etc.)
- Game engines (Blackjack, War, High Card)
- API interfaces
- Resource management (event cleanup)
- Component integration
We welcome contributions! Here's how you can help:
- Fork the repository
- Create a new branch (
git checkout -b feature/amazing-feature
) - Make your changes
- Run tests to ensure no regressions
- Commit your changes (
git commit -am 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you find CardSharp useful, please consider giving it a star on GitHub. It helps us gain visibility and encourages further development!
For questions, suggestions, or discussions, please open an issue on GitHub or contact the maintainers directly.
Happy gaming and may the odds be ever in your favor! ๐ฐ๐