Skip to content

Instantly share code, notes, and snippets.

@bag-man
Created May 31, 2021 16:19
Show Gist options
  • Save bag-man/16c0c3cb5e8a3ea2191249b311f166d5 to your computer and use it in GitHub Desktop.
Save bag-man/16c0c3cb5e8a3ea2191249b311f166d5 to your computer and use it in GitHub Desktop.
Blackjack card counter
#!/bin/python3
import curses
import sys
import os
from math import floor
decks = 6
bet_unit = 10
def main(win, decks):
total_cards = decks * 52
running_count = 0
true_count = 0
bet = bet_unit
menu = "Low (z), Neutral (x), High (c), Shuffle (f)\n"
key=""
curses.init_pair(1, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.curs_set(0)
win.bkgd(' ', curses.color_pair(1) | curses.A_BOLD)
win.clear()
win.addstr(menu)
while 1:
try:
key = win.getkey()
total_cards -= 1
if total_cards == 0 or key == 'f':
total_cards = decks * 52
running_count = 0
true_count = 0
bet = bet_unit
else:
if key == 'c':
running_count -= 1
if key == 'z':
running_count += 1
decks_remaining = (total_cards / 52)
true_count = floor((running_count / decks_remaining) * 10) / 10
if true_count < 2:
bet = bet_unit
if true_count >= 2:
bet = bet_unit * 2
if true_count >= 3:
bet = bet_unit * 4
if true_count >= 4:
bet = bet_unit * 8
win.clear()
win.addstr(menu)
win.addstr("-------------------\n")
win.addstr(f"Cards remaining: {str(total_cards)}\n")
win.addstr(f"True count: {str(true_count)}\n")
win.addstr(f"Bet: {str(bet)}\n")
win.addstr("-------------------\n")
# Illustrious 18 Deviations
if true_count >= 3:
win.addstr("Insurance: Take\n")
if true_count >= 5:
win.addstr("T,T vs 5: Split\n")
if true_count >= 4:
win.addstr("T,T vs 6: Split\n")
if true_count > 0:
win.addstr("16 vs 10: Stand\n")
if true_count >= 5:
win.addstr("16 vs 9: Stand\n")
if true_count >= 4:
win.addstr("15 vs 10: Stand\n")
if true_count <= -2:
win.addstr("13 vs 3: Hit\n")
if true_count <= -1:
win.addstr("13 vs 2: Hit\n")
if true_count < 0:
win.addstr("12 vs 4: Hit\n")
if true_count <= -1:
win.addstr("12 vs 5: Hit\n")
if true_count <= -1:
win.addstr("12 vs 6: Hit\n")
if true_count >= 2:
win.addstr("12 vs 3: Stand\n")
if true_count >= 3:
win.addstr("12 vs 2: Stand\n")
if true_count >= 1:
win.addstr("11 vs A: Double\n")
if true_count >= 4:
win.addstr("10 vs T: Double\n")
if true_count >= 4:
win.addstr("10 vs A: Double\n")
if true_count >= 1:
win.addstr("9 vs 2: Double\n")
if true_count >= 3:
win.addstr("9 vs 7: Double\n")
if key == os.linesep:
break
except Exception as e:
pass
try:
curses.wrapper(main, decks)
except KeyboardInterrupt:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment