https://github.com/bilbottom/playing-cards
Playing cards from the French-suited, standard 52-card pack.
https://github.com/bilbottom/playing-cards
python
Last synced: about 1 year ago
JSON representation
Playing cards from the French-suited, standard 52-card pack.
- Host: GitHub
- URL: https://github.com/bilbottom/playing-cards
- Owner: Bilbottom
- Created: 2024-08-31T14:39:08.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-09T08:05:40.000Z (about 1 year ago)
- Last Synced: 2025-04-24T02:05:21.198Z (about 1 year ago)
- Topics: python
- Language: Python
- Homepage:
- Size: 65.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[](https://www.python.org/downloads/)
[](https://github.com/astral-sh/uv)
[](https://github.com/Bilbottom/playing-cards/actions/workflows/tests.yaml)
[](https://github.com/dbrgn/coverage-badge)
[](https://shields.io/badges/git-hub-last-commit)
[](https://github.com/prettier/prettier)
[](https://github.com/astral-sh/ruff)
[](https://results.pre-commit.ci/latest/github/Bilbottom/playing-cards/main)
---
# Playing Cards 🃏
Playing cards from the French-suited, standard 52-card pack.
## Installation ⬇️
This package is currently only available from GitHub:
```
pip install git+https://github.com/Bilbottom/playing-cards@v0.0.3
```
## Usage 📖
This library provides classes to represent playing cards and decks of cards.
Import the package and start using the classes. Here's an example of a simple game of _Red or Black_:
```python
import playing_cards
def ask(choices: list[str]) -> str:
"""Ask a question and return the user's choice."""
question = f"{', '.join(choices[:-1])} or {choices[-1]}? "
while (choice := input(question).lower()) not in choices:
pass
return choice
def print_cards(cards: list[playing_cards.Card]) -> None:
"""Print the faces of the cards in the list."""
print(f"[{', '.join(card.face for card in cards)}]")
def red_or_black() -> None:
"""Play a game of Red or Black."""
deck = playing_cards.Deck()
cards = []
### red or black
colour_choice = ask(["red", "black"])
cards.append(deck.take_card()), print_cards(cards)
if cards[-1].colour != colour_choice:
print("Wrong colour. You lose!")
return
### higher or lower (ties lose)
hl_choice = ask(["higher", "lower"])
cards.append(deck.take_card()), print_cards(cards)
is_higher = cards[-1].rank > cards[-2].rank
is_lower = cards[-1].rank < cards[-2].rank
if not (
(hl_choice == "higher" and is_higher)
or (hl_choice == "lower" and is_lower)
):
print("Wrong direction. You lose!")
return
### inside or outside (only inside is inclusive)
io_choice = ask(["inside", "outside"])
cards.append(deck.take_card()), print_cards(cards)
rank_min, rank_max = sorted([cards[-3].rank, cards[-2].rank])
is_inside = rank_min <= cards[-1].rank <= rank_max
if not (
(io_choice == "inside" and is_inside)
or (io_choice == "outside" and not is_inside)
):
print("Wrong range. You lose!")
return
### suit
suit_choice = ask(list(playing_cards.Suit))
cards.append(deck.take_card()), print_cards(cards)
if cards[-1].suit != suit_choice:
print("Wrong suit. You lose!")
return
print("You win!")
if __name__ == "__main__":
red_or_black()
```
## Contributing
Install [uv](https://docs.astral.sh/uv/getting-started/installation/) and then enable [pre-commit](https://pre-commit.com/):
```bash
uv sync --all-groups
pre-commit install --install-hooks
```