An open API service indexing awesome lists of open source software.

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.

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.

[![GitHub stars](https://img.shields.io/github/stars/mmichie/cardsharp.svg)](https://github.com/mmichie/cardsharp/stargazers)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python Versions](https://img.shields.io/pypi/pyversions/cardsharp.svg)](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!

[![GitHub stars](https://img.shields.io/github/stars/mmichie/cardsharp.svg?style=social&label=Star)](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! ๐ŸŽฐ๐Ÿƒ