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 three optional parameters:

  • 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.

[!NOTE] The BlackJack object only uses one deck to give as much advantage to the player as possible.

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.0.1.tar.gz (10.0 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.0.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: blackjack_api-1.0.1.tar.gz
  • Upload date:
  • Size: 10.0 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.0.1.tar.gz
Algorithm Hash digest
SHA256 8ee43278e3523300adac296f576a46cf60d06c5e91a662deb1933870ac09e145
MD5 426856f173dfb7b4133432d73a443965
BLAKE2b-256 d24f92ca78f9fac8b4a200b1b0de1f1604b7ecf44d6d773b172b182c888cc6a9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blackjack_api-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 95de8f84f659a8c95554b14b05e89da106d0ca9ecaaa4af416217a854f3abda0
MD5 9058e847a132367837d02e0d6c489396
BLAKE2b-256 c4dfd24dec91da9bc1a61926f81ed1fa63565719147af1fe092b8613e950241e

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