Skip to main content

A text-based Blackjack game with ASCII art and smart betting system

Project description

๐Ÿƒ Blackjack Game

A Python implementation of Blackjack (21) featuring ASCII card art, a chip betting system, and Object-Oriented Programming principles.

Features

  • Complete Blackjack game logic
  • Beautiful ASCII card visualization
  • Chip-based betting system (start with 200 chips)
  • Automatic screen clearing for better user experience
  • Input filtering to prevent paste attempts and ensure clean gameplay
  • Blackjack Basic Strategy implemented for Dealer
  • Player vs Dealer gameplay
  • Clean, modular OOP design
  • Fully installable package via pip
  • Command-line interface

Installation

Using a virtual environment (recommended)

# Create a virtual environment
python -m venv venv

# Activate the environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate

# Install the game from PyPI
pip install gabrielchaves-blackjack

Updating the game

# Upgrade to the latest version
pip install --upgrade gabrielchaves-blackjack

From source (optional)

# Clone or download the repository
cd blackjack

# Install in development mode
pip install -e .

# Or install normally
pip install .

After installation

# Run the game
blackjack

How to Play

  1. Run the blackjack command after installation
  2. Place your bet - Choose from 1, 5, 10, 25, or 50 chips
  3. You'll receive 2 cards, the dealer receives 2 (one hidden)
  4. Choose Hit (take a card) or Stand (keep your current hand)
  5. Try to get as close to 21 as possible without going over
  6. The dealer plays automatically (follows Basic Strategy)
  7. The highest hand wins!
  8. Win: Get your bet back doubled
  9. Lose: Lose your bet
  10. Push: Bet is returned on a tie
  11. Game continues until you run out of chips or choose to quit

Betting System

  • Start with 200 chips
  • Betting options: 1, 5, 10, 25, or 50 chips
  • Win your bet amount on a win
  • Lose your bet amount on a loss
  • Bet returned on a push (tie)
  • Game ends when you run out of chips
  • Option to start a new game with 200 chips

ASCII Visualization

Cards are displayed in beautiful ASCII art:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚A        โ”‚  โ”‚K        โ”‚  โ”‚Q        โ”‚
โ”‚         โ”‚  โ”‚         โ”‚  โ”‚         โ”‚
โ”‚    โ™     โ”‚  โ”‚    โ™ฅ    โ”‚  โ”‚    โ™ฆ    โ”‚
โ”‚         โ”‚  โ”‚         โ”‚  โ”‚         โ”‚
โ”‚        Aโ”‚  โ”‚        Kโ”‚  โ”‚        Qโ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The dealer's hidden card appears as:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ”‚
โ”‚โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ”‚
โ”‚โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ”‚
โ”‚โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ”‚
โ”‚โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Usage as Module

from blackjack import BlackJackGame

# Create and play a game
game = BlackJackGame()
game.play_round()

Card Art Demo

To see cards in ASCII art:

python demo_ascii_cards.py

Architecture

Design Principles

This implementation follows solid OOP principles:

  • Encapsulation: Each class manages its own data and behavior
  • Single Responsibility: Each class has a clear, focused purpose
  • Composition: Game uses Deck, Players use Hands
  • Inheritance: Dealer extends Player with specific rules
  • Separation of Concerns: Input handling, game logic, and display are separated

Class Structure

  • Card: Represents a single playing card (with ASCII art)
  • Deck: Manages a 52-card deck (creation, shuffling, dealing)
  • Hand: Manages a collection of cards and calculates values
  • Player: Represents a player with a hand and chips
  • Dealer: Extends Player with specific dealer rules and Basic Strategy
  • BlackJackGame: Orchestrates game flow and logic
  • CLI: Command-line interface with betting flow
  • InputHandler: Filtered input system to prevent paste attempts

Key Implementation Details

Card Values

  • Number cards (2-10): Face value
  • Face cards (J, Q, K): 10 points
  • Aces: 11 points (no soft/hard handling in current version)

Dealer Strategy

The dealer follows Blackjack Basic Strategy:

  • Hard hands:
    • Hits on 16 or less
    • Stands on 17 or more
  • Soft hands (with Ace):
    • Complex strategy based on up card
    • Optimized for realistic gameplay

Input Security

  • Paste prevention: All paste attempts (Ctrl+V, Cmd+V, right-click) are blocked
  • Character filtering: Only valid single-character inputs accepted
  • Clean experience: Invalid inputs are silently ignored

Improvements Implemented

โœ… ASCII card art: Beautiful, immersive visualization โœ… Chip betting system: Realistic casino-style betting โœ… Screen clearing: Clean, professional interface โœ… Smooth animations: Strategic pauses for better experience โœ… Emojis: Fun visual feedback โœ… Input filtering: Prevents cheating and ensures fair play โœ… Cross-platform compatibility: Works on Windows, Linux, and macOS โœ… UTF-8 encoding handling: Proper character display across systems

Testing

# Run tests
python -m pytest tests/

๐Ÿ“‹ Requirements

  • Python 3.8+
  • prompt-toolkit for enhanced input handling

Game Rules

  1. Get as close to 21 as possible without going over
  2. Face cards (J, Q, K) are worth 10 points
  3. Aces are worth 11 points
  4. Number cards are worth their face value
  5. Going over 21 is a "bust" - you lose automatically
  6. Dealer must hit on 16 or less
  7. If both player and dealer have the same value, it's a "push" (tie)

License

MIT License

Author

Gabriel Chaves


Version: 0.0.5

Enjoy the game! ๐Ÿƒ

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gabrielchaves_blackjack-1.0.4.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gabrielchaves_blackjack-1.0.4-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file gabrielchaves_blackjack-1.0.4.tar.gz.

File metadata

  • Download URL: gabrielchaves_blackjack-1.0.4.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for gabrielchaves_blackjack-1.0.4.tar.gz
Algorithm Hash digest
SHA256 9d5528bd46945007da53ba1d0009b0852f34e09cbb80b4166a303baf16e14c98
MD5 fae11990d94cc1b6683e607efc023028
BLAKE2b-256 902b1c1e3ab73dd68fdcd6eda30c84617a75726449126cec7d9cf165eea1b88b

See more details on using hashes here.

File details

Details for the file gabrielchaves_blackjack-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for gabrielchaves_blackjack-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 aba84e29a34c34a6e783329f81cde52eeb2e3267a627024b1aad215b8cd51809
MD5 e2ceecda939ad8294779687a9ca68f46
BLAKE2b-256 a37e8d855284533d34ecd27505fe0efdf7ccbaaa2b6027a5d4e58a18a45d70a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page