Skip to content

CardSharp is a Python package for simulating and testing card games. The package currently supports Blackjack but can be extended to support more card games.

License

Notifications You must be signed in to change notification settings

mmichie/cardsharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

CardSharp: Advanced Card Game Simulation Framework

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.

GitHub stars License: MIT Python Versions

๐Ÿš€ Key Features

  • ๐Ÿƒ 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

๐Ÿ› ๏ธ Installation

# 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

๐ŸŽฎ Usage

CardSharp offers various modes of operation to suit different needs:

Blackjack Simulation

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.

Strategy Analysis

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.

Interactive Console Mode

Play Blackjack interactively in the console:

python cardsharp/blackjack/blackjack.py --console

Event System Demo

Try the new event system:

python examples/event_system_demo.py

Platform Adapter Demo

Experience the platform adapter system:

python examples/adapter_demo.py

Immutable State Demo

See how immutable state transitions work:

python examples/immutable_state_demo.py

BlackjackEngine Demo

Experience the new engine with immutable state:

python examples/blackjack_engine_demo.py

Asynchronous API Demo

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

BlackjackGame Demo

Experience the BlackjackGame API with event handling:

python examples/blackjack_game_demo.py

Card Handling Demo

See how different card representations work with the HandState class:

python examples/card_handling_demo.py

Event Cleanup Demo

Demonstrate proper cleanup of event handlers when games are shut down:

python examples/event_cleanup_test.py

Modern UI Demo

Experience the Streamlit-based web UI for Blackjack:

python examples/modern_blackjack_ui.py

Verification System Demo

See the immutable state verification system in action:

python examples/verification_demo.py

Full CLI Blackjack Game

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

๐Ÿ“ Project Structure

  • cardsharp/
    • blackjack/: Blackjack-specific implementations
    • common/: Shared utilities and base classes
    • events/: Event system for event-driven architecture
    • adapters/: Platform adapters for different environments
    • state/: Immutable state models and transition functions
    • engine/: Core game engine implementation
    • war/: War card game implementation
    • high_card/: High Card game implementation
    • roulette/: Roulette game implementation (in progress)
    • verification/: Game verification and event recording

๐Ÿ—๏ธ Architecture

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:

  1. โœ… Phase 1: Event System and Adapters
  2. โœ… Phase 2: Immutable State
  3. โœ… Phase 3: Asynchronous API
  4. โœ… Phase 4: Integration & Component Architecture

Check out the architecture documentation for details.

๐Ÿ“Š Performance Optimization

When optimizing CardSharp simulations, it's critical to maintain accuracy. See our optimization principles for guidelines on proper performance improvements without compromising simulation fidelity.

๐Ÿงช Testing

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

๐Ÿค Contributing

We welcome contributions! Here's how you can help:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests to ensure no regressions
  5. Commit your changes (git commit -am 'Add some amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

๐Ÿ“„ License

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

๐ŸŒŸ Star Us!

If you find CardSharp useful, please consider giving it a star on GitHub. It helps us gain visibility and encourages further development!

GitHub stars

๐Ÿ“ฌ Contact

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! ๐ŸŽฐ๐Ÿƒ

About

CardSharp is a Python package for simulating and testing card games. The package currently supports Blackjack but can be extended to support more card games.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •