Skip to main content

A 1-on-1 blackjack game library for simulating games, usable for any blackjack needs.

Project description

blackjack-api

A 1-on-1 blackjack game library for simulating games, usable for any blackjack needs.

View here on PyPI.

Actual Library

blackjack.py

The BlackJack class constructor takes several optional parameters:

  • The number of decks to use
  • The win ratio
  • Whether the dealer hits on a soft 17
  • Whether to use the 5-Card Charlie rule

The class automatically starts a round and hand. However, after completing a hand, the driver program must call BlackJack.start_hand() to start a new hand. The class boolean BlackJack.hand_running specifies whether a hand is running. As such, a while loop may be helpful for running multiple hands programatically. When the last hand terminates (Ex. when a player stands after being dealt their first hand), the BlackJack class will automatically terminate the round as well. New rounds can be started with BlackJack.start_round() and can be monitored with BlackJack.round_running.

When both a round and hand are running, the player may be able to play the following:

  • BlackJack.hit()
  • BlackJack.stand()
  • BlackJack.double()
  • BlackJack.surrender()
  • BlackJack.insurance(bet = .5)

Of course, a player cannot surrender if they have already hit or split during the round. However, all of these actions (with the exception of insurance) can potentially terminate the hand. For example, standing will guarantee the hand terminates, but hitting will only terminate the hand if the hand busts.

[!WARNING] A hand does not automatically terminate when the player reaches a score of 21. This means the driver program still has to make a decision in this situation.

When a round does terminate, the dealer will play until their hand terminates. The BlackJack class then computes the rewards for the player. Since a player can have multiple hands if they split, the BlackJack.rewards are stored as a variable, where each reward correspond to the player's hand at the same index.

BlackJack.hilo tracks the running count of the deck and can be referenced for advanced decision making in your driver programs.

cli.py

Wraps the BlackJack class with a console interface. To see how to use the CLI class, simply call CLI.loop().

Class Architecture

BlackJack stores the dealer as a Hand class and the player as a Player class. The Player class is merely a list of Hand objects. However, each Hand class is a list of Card objects.

The main highlights of the Hand class are the Hand.count() and Hand.is_soft() functions. The first gets the highest legal value of the hand—a return value of over 21 means the hand has busted! The latter function returns whether the highest legal value of the hand counts an ace as an 11 instead of a 1.

The Card class is much simpler and has three values:

  • The card name
  • The value of the card (an ace's value is stored as tuple([1, 11]))
  • The suit of the card (Ex. a spade has a suit of 'S')

The BlackJack class also uses a couple Deck objects to keep track of the deck and discard pile. Whenever the deck empties, the discard pile will be used to refill the deck.

Tests

There are five different test files which demonstrate the library:

File Description
basic_debug.py Run an individual game using basic strategy
basic.py Run many games using basic strategy and plot the results
double.py Run many games where the player only doubles down
loop.py Run the premade console interface
stand.py Run many games where the player only stand

Notes to myself

To build the project yourself, simply run the following command:

python -m build

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

blackjack_api-1.1.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

blackjack_api-1.1.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file blackjack_api-1.1.0.tar.gz.

File metadata

  • Download URL: blackjack_api-1.1.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for blackjack_api-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8585d7ce1024a436d85cbcbc082e5f2f1bdd1f6ac52f6bcb5cc4e76e01d22739
MD5 494939abb586f720fec554ef55640b81
BLAKE2b-256 9195b8ea62be90b59bedbb0a087c2dd17d8e865186c7d9cbe86a668c03e3ff75

See more details on using hashes here.

File details

Details for the file blackjack_api-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: blackjack_api-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for blackjack_api-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e17c30bdf8ca8b5b11d6c14ede71b5e07a3714984727b887c13589f4fffc41bb
MD5 96bf12eb7d257504949672a06d7fa633
BLAKE2b-256 1f00b598b2c745203ab19967d90ced2d21026e8d083ab465d73711e080b204ca

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