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: 1.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.5.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.5-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gabrielchaves_blackjack-1.0.5.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.5.tar.gz
Algorithm Hash digest
SHA256 824f60aadf64bd1f6ef5e1e7919cf43eca4fc59079e3709c6504da7d3cc0a42f
MD5 6f3dfb9184871f1de046d5c2a818d3df
BLAKE2b-256 1e566c577e65bad6fdbada3d7b63924aa30329348e2444bce4749ff8aae11590

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gabrielchaves_blackjack-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 19bff1c3b854b488546a171863ad6adfae8e41d4a6ea2a773dc7d6e5896fba71
MD5 76ec2c8f39ff66a900451740e1f7bdd6
BLAKE2b-256 718472d2e98a59a9121463a9b42c56ed6754ab87109a91e69e7b3447f45faf2a

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