A BlackJack simulator in Python to simulate thousands or millions of hands using different strategies.

Overview

BlackJack Simulator (in Python)

A BlackJack simulator to play any number of hands using different strategies

The Rules

To keep the code relatively simple, a lot of the rules are hard-coded. This simulator assumes:

  • Dealer hits soft-17
  • Player can double-down any first 2 cards
  • Player can split any number of times
  • Blackjack pays 6:5 (1.2)
  • Surrender is allowed and the loss is 1/2

The Structure

Overview

The way this code is structured is:

  • That the game is played at a Table.
  • A Table has 1 Shoe of cards. The Shoe can have anywhere from 1 to 10 Decks of cards.
  • A Table has 1 Dealer.
  • A Table has 1 or more Players.
  • A Player has 1 or more Hands. Generally, a player has 1 hand unless there are splits, in which case player can have multiple hands.
  • A Dealer has 1 hand.

To simulate a round of BlackJack, you simply:

  1. Create a Table object (which creates a dealer and shoe with 8 decks by default)
  2. Add a player to the table
  3. And then you call Table.playRound() to play 1 round of BlackJack.
  4. The table then needs to reset using the Table.reset() method to play another hand.

The results of each hand is stored in the hand itself.

Code to simulate 1,000 rounds

The code to simulate 1,000 rounds of BlackJack can be as simple as:

table = Table()
table.players.append(Player())
for i in range(0, 1000):        # Simulates 1,000 rounds of BlackJack
    table.playRound()
    table.reset()

In order to see the results, including dealer's FaceCard and the decision the player made, you can also use the Table.printShortResults() or Table.printVerboseResults(). Here is an example of printing condensed results:

table = Table()

table.players.append(Player())
print("Round, Dealer FaceCard, Player Card 1, Card 2, Total, Soft Hand, Is BlackJack, Busted, Action, Result, Value")

for i in range(0, 1000):        # Simulates 1,000 rounds of BlackJack
    table.playRound()
    print(str(i) + ", " + table.printShortResults())
    table.reset()

The results include a "Value" for the hand that ranges from -2.0 to 2.0:

  • -1.0 Means a normal loss for a hand
  • +1.0 Means a normal win for a hand
  • -2.0 Means a Double-Downed round was lost
  • +2.0 Means a Double-Downed round was won
  • +1.2 Means a BlackJack win
  • -0.5 Means a loss by surrender

The Classes

The Shoe Class

The Shoe is designed to be extremely simple. When its initialized (or shuffled), it simply creates an array of 52 x [Num of Decks] elements numbered from 1 to 52. When a card is needed from the Shoe via getCard(), the Shoe randomly picks an array element and removes it from the array and returns it as the card. However, prior to returning the card, it first does a "% 13" operation to simulate a card from 1 (Ace) to 13 (King), and since in BlackJack, a 10, Jack, Queen and King are all 10s, if the card is >= 10, it returns a 10.

The Hand Class

A lot of the logic on how to play a hand is in the Hand class.

The Hand class has 3 main methods on how the hand will be played:

  • basicStrategyPlay - This plays using standard BlackJack basic strategy.
  • randomPlay - This plays using a random allowed action. Results of this could be used to train machine learning alogrithms.
  • dealerPlay - This plays using standard BlackJack dealer play strategy.

The getHandTotal() method is also useful to get the hand total, which takes into consideration soft hands (a hand with an Ace that is counted as an 11). If a hand total is considered to be a soft hand, it also sets the "softHand" variable that's passed in to true.

The Player and Dealer Classes

Since most of the logic for how the hand is played is actually in the Hand class, the Player and Dealer objects are just wrappers to hold a hand. Ideally, it might make sense to refactor the code to put the "play" methods into the Player and Dealer classes rather than the Hand class.

Another potential refactor might make the Dealer a kind of Player rather than its own class.

The Table Class

The Table class is where the bulk of the action is.

When the table "playRound()" is called, the table first deals the first 2 cards as no logic is needed prior to dealing the first 2 cards to all players and the dealer. If the dealer doesn't have a BlackJack, it then continues the play.

The playPlayerHand() is probably the most complicated part of the code because in the event of a Split, it can also be called recursively. The code is written so that the playPlayerHand() uses the basicStrategyPlay() method to play the round. You can also change this to randomPlay() to see what would happen if the player plays the game by taking a random action based on available actions.

Owner
Hamid
Hamid
Audio Steganography is a technique used to transmit hidden information by modifying an audio signal in an imperceptible manner.

Audio Steganography Audio Steganography is a technique used to transmit hidden information by modifying an audio signal in an imperceptible manner. Ab

Karan Yuvraj Singh 1 Oct 17, 2021
Link-tree - Script that iterate over the links found in each page

link-tree Script that iterate over the links found in each page, recursively fin

Rodrigo Stramantinoli 2 Jan 05, 2022
A simple language and reference decompiler/compiler for MHW THK Files

Leviathon A simple language and reference decompiler/compiler for MHW THK Files. Project Goals The project aims to define a language specification for

11 Jan 07, 2023
SmarTool - Smart Util Tool for Python

A set of tools that keep Python sweeter.

Liu Tao 9 Sep 30, 2022
Check the basic quality of any dataset

Data Quality Checker in Python Check the basic quality of any dataset. Sneak Peek Read full tutorial at Medium. Explore the app Requirements python 3.

MalaDeep 8 Feb 23, 2022
Fcpy: A Python package for high performance, fast convergence and high precision numerical fractional calculus computing.

Fcpy: A Python package for high performance, fast convergence and high precision numerical fractional calculus computing.

SciFracX 1 Mar 23, 2022
A hashtag from string extract python module

A hashtag from string extract python module

Fayas Noushad 3 Aug 10, 2022
WindowsDebloat - Windows Debloat with python

Windows Debloat 🗑️ Quickly and easily configure Windows 10. Disclaimer I am NOT

1 Mar 26, 2022
A module for account creation with python

A module for account creation with python

Fayas Noushad 3 Dec 01, 2021
A dictionary that can be flattened and re-inflated

deflatable-dict A dictionary that can be flattened and re-inflated. Particularly useful if you're interacting with yaml, for example. Installation wit

Lucas Sargent 2 Oct 18, 2021
Implicit hierarchical a posteriori error estimates in FEniCSx

FEniCSx Error Estimation (FEniCSx-EE) Description FEniCSx-EE is an open source library showing how various error estimation strategies can be implemen

Jack S. Hale 1 Dec 08, 2021
A Python class for checking the status of an enabled Minecraft server

mcstatus provides an easy way to query Minecraft servers for any information they can expose. It provides three modes of access (query, status and ping), the differences of which are listed below in

Nathan Adams 1.1k Jan 06, 2023
Set of utilities for exporting/controlling your robot in Blender

Blender Robotics Utils This repository contains utilities for exporting/controlling your robot in Blender Maintainers This repository is maintained by

Robotology 33 Nov 30, 2022
Know your customer pipeline in apache air flow

KYC_pipline Know your customer pipeline in apache air flow For a successful pipeline run take these steps: Run you Airflow server Admin - connection

saeed 4 Aug 01, 2022
Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything.

Retrying Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just

Ray Holder 1.9k Dec 29, 2022
A multipurpose python module

pysherlock pysherlock is a Python library for dealing with web scraping using images, it's a Python application of the rendertron headless browser API

Sachit 2 Nov 11, 2021
Random Number Generator

Application for generating a random number.

Michael J Bailey 1 Oct 12, 2021
Run async workflows using pytest-fixtures-style dependency injection

Run async workflows using pytest-fixtures-style dependency injection

Simon Willison 26 Jun 26, 2022
osqueryIR is an artifact collection tool for Linux systems.

osqueryIR osqueryIR is an artifact collection tool for Linux systems. It provides the following capabilities: Execute osquery SQL queries Collect file

AbdulRhman Alfaifi 7 Nov 02, 2022
'ToolBurnt' A Set Of Tools In One Place =}

'ToolBurnt' A Set Of Tools In One Place =}

MasterBurnt 5 Sep 10, 2022