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
Give you a better view of your Docker registry disk usage.

registry-du Give you a better view of your Docker registry disk usage. This small tool will analysis your Docker registry(vanilla or Harbor both work)

Nova Kwok 16 Jan 07, 2023
Stubmaker is an easy-to-use tool for generating python stubs.

Stubmaker is an easy-to-use tool for generating python stubs. Requirements Stubmaker is to be run under Python 3.7.4+ No side effects during

Toloka 24 Aug 28, 2022
Python based tool to extract forensic info from EventTranscript.db (Windows Diagnostic Data)

EventTranscriptParser EventTranscriptParser is python based tool to extract forensically useful details from EventTranscript.db (Windows Diagnostic Da

P. Abhiram Kumar 24 Nov 18, 2022
A simple gpsd client and python library.

gpsdclient A small and simple gpsd client and library Installation Needs Python 3 (no other dependencies). If you want to use the library, use pip: pi

Thomas Feldmann 33 Nov 24, 2022
A script copies movie and TV files to your GD drive, or create Hard Link in a seperate dir, in Emby-happy struct.

torcp A script copies movie and TV files to your GD drive, or create Hard Link in a seperate dir, in Emby-happy struct. Usage: python3 torcp.py -h Exa

ccf2012 105 Dec 22, 2022
Raganarok X: Next Generation Data Dump

Raganarok X Data Dump Raganarok X: Next Generation Data Dump More interesting Files File Name Contains en_langs All the variables you need in English

14 Jul 15, 2022
A color library based on pokemons colors!

pokepalette A simple pokemon color chooser " This repo is based on CDWimmer/PokePalette and was originated from this tweet. If you don't remember your

Thomas Capelle 5 Aug 30, 2021
A tool written in python to generate basic repo files from github

A tool written in python to generate basic repo files from github

Riley 7 Dec 02, 2021
A clock app, which helps you with routine tasks.

Clock This app helps you with routine tasks. Alarm Clock Timer Stop Watch World Time (Which city you want) About me Full name: Matin Ardestani Age: 14

Matin Ardestani 13 Jul 30, 2022
PyGMT - A Python interface for the Generic Mapping Tools

PyGMT A Python interface for the Generic Mapping Tools Documentation (development version) | Contact | Try Online Why PyGMT? A beautiful map is worth

The Generic Mapping Tools (GMT) 564 Dec 28, 2022
Data Utilities e.g. for importing files to onetask

Use this repository to easily convert your source files (csv, txt, excel, json, html) into record-oriented JSON files that can be uploaded into onetask.

onetask.ai 1 Jul 18, 2022
Plone Interface contracts, plus basic features and utilities

plone.base This package is the base package of the CMS Plone https://plone.org. It contains only interface contracts and basic features and utilitie

Plone Foundation 1 Oct 03, 2022
JavaScript to Python Translator & JavaScript interpreter written in 100% pure Python🚀

Pure Python JavaScript Translator/Interpreter Everything is done in 100% pure Python so it's extremely easy to install and use. Supports Python 2 & 3.

Piotr Dabkowski 2.1k Dec 30, 2022
Python based utilities for interacting with digital multimeters that are built on the FS9721-LP3 chipset.

Python based utilities for interacting with digital multimeters that are built on the FS9721-LP3 chipset.

Fergus 1 Feb 02, 2022
A way to write regex with objects instead of strings.

Py Idiomatic Regex (AKA iregex) Documentation Available Here An easier way to write regex in Python using OOP instead of strings. Makes the code much

Ryan Peach 18 Nov 15, 2021
Dill_tils is a package that has my commonly used functions inside it for ease of use.

DilllonB07 Utilities Dill_tils is a package that has my commonly used functions inside it for ease of use. Installation Anyone can use this package by

Dillon Barnes 2 Dec 05, 2021
kawadi is a versatile tool that used as a form of weapon and is used to cut, shape and split wood.

kawadi kawadi (કવાડિ in Gujarati) (Axe in English) is a versatile tool that used as a form of weapon and is used to cut, shape and split wood. kawadi

Jay Vala 2 Jan 10, 2022
EVE-NG tools, A Utility to make operations with EVE-NG more friendly.

EVE-NG tools, A Utility to make operations with EVE-NG more friendly. Also it support different snapshot operations with same style as Libvirt/KVM

Bassem Aly 8 Jan 05, 2023
Python module and its web equivalent, to hide text within text by manipulating bits

cacherdutexte.github.io This project contains : Python modules (binary and decimal system 6) with a dedicated tkinter program to use it. A web version

2 Sep 04, 2022
✨ Un pierre feuille ciseaux totalement fait en Python par moi, et en français.

Pierre Feuille Ciseaux ❗ Un pierre feuille ciseaux totalement fait en Python par moi. 🔮 Avec l'utilisation du module "random", j'ai pu faire un choix

MrGabin 3 Jun 06, 2021