https://github.com/mmichie/cardsharp
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.
https://github.com/mmichie/cardsharp
blackjack blackjack-cli blackjack-python blackjack-simulator blackjack-strategy card-game cards gambling python simulation
Last synced: 3 days ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/mmichie/cardsharp
- Owner: mmichie
- License: mit
- Created: 2023-05-28T22:40:39.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2026-02-14T04:00:29.000Z (about 2 months ago)
- Last Synced: 2026-02-14T10:53:29.744Z (about 2 months ago)
- Topics: blackjack, blackjack-cli, blackjack-python, blackjack-simulator, blackjack-strategy, card-game, cards, gambling, python, simulation
- Language: Python
- Homepage:
- Size: 1.5 MB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Agents: AGENTS.md
Awesome Lists containing this project
README
# 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.
[](https://github.com/mmichie/cardsharp/stargazers)
[](https://opensource.org/licenses/MIT)
[](https://pypi.org/project/cardsharp/)
## ๐ 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
```bash
# Clone the repository
git clone https://github.com/mmichie/cardsharp.git
cd cardsharp
# Create a virtual environment and install dependencies
uv sync
```
## ๐ฎ Usage
CardSharp offers various modes of operation to suit different needs:
### Blackjack Simulation
Run a batch simulation of Blackjack games:
```bash
uv run 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:
```bash
uv run 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:
```bash
uv run python cardsharp/blackjack/blackjack.py --console
```
### Event System Demo
Try the new event system:
```bash
uv run python examples/event_system_demo.py
```
### Platform Adapter Demo
Experience the platform adapter system:
```bash
uv run python examples/adapter_demo.py
```
### Immutable State Demo
See how immutable state transitions work:
```bash
uv run python examples/immutable_state_demo.py
```
### BlackjackEngine Demo
Experience the new engine with immutable state:
```bash
uv run python examples/blackjack_engine_demo.py
```
### Asynchronous API Demo
See the new Phase 3 asynchronous API in action:
```bash
uv run 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:
```bash
uv run python examples/blackjack_game_demo.py
```
### Card Handling Demo
See how different card representations work with the HandState class:
```bash
uv run python examples/card_handling_demo.py
```
### Event Cleanup Demo
Demonstrate proper cleanup of event handlers when games are shut down:
```bash
uv run python examples/event_cleanup_demo.py
```
### Modern UI Demo
Experience the Streamlit-based web UI for Blackjack:
```bash
uv run python examples/modern_blackjack_ui.py
```
### Verification System Demo
See the immutable state verification system in action:
```bash
uv run python examples/verification_demo.py
```
### Full CLI Blackjack Game
Play the complete blackjack game using the new architecture:
```bash
uv run python examples/cli_blackjack.py
```
You can customize your game experience with these options:
```bash
# Play with 3 players
uv run python examples/cli_blackjack.py --players 3
# Start with a custom bankroll
uv run python examples/cli_blackjack.py --bankroll 500
# Play a fixed number of rounds
uv run 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](docs/architecture_modernization.md) for details.
### ๐ Performance Optimization
When optimizing CardSharp simulations, it's critical to maintain accuracy. See our [optimization principles](docs/optimization_principles.md) for guidelines on proper performance improvements without compromising simulation fidelity.
## ๐งช Testing
Run the test suite to ensure everything is working correctly:
```bash
# Run all tests
uv run pytest tests/
# Run tests with coverage report
uv run pytest tests/ --cov=cardsharp
# Run specific test modules
uv run 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](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!
[](https://github.com/mmichie/cardsharp)
## ๐ฌ 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! ๐ฐ๐