Skip to main content

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

Project description

py-jack

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: blackjack_api-1.0.0.tar.gz
  • Upload date:
  • Size: 9.9 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.0.tar.gz
Algorithm Hash digest
SHA256 830178f4bbc118c3d06aeebfb063901b6b30acc5ba6c6f077e27e49ff37ae805
MD5 96bc4b0e7f99e2e7e87a14e64ec5580a
BLAKE2b-256 889ce4daf4cf2ecf98989a11f88215194636c0a99f6a6a19380a1f11cca13de7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blackjack_api-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15a8e3ecb9ad5c13a56a75a964dc4037d72f0bac11891643ab2ed416cb46c216
MD5 08d1a9a8b966c68a08f5fcf7551af7f6
BLAKE2b-256 5e45a5540021b19a09f199f93491d351510520e7bbfc617e891b661db1921194

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