npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@blackjacktrainer/blackjack-simulator

v0.33.4

Published

Core game logic for a Blackjack basic strategy and card counting trainer

Downloads

84

Readme

Blackjack Simulator

Practice card counting using Hi-Lo and calculate EV for any table conditions

Features

  • Simulator mode for computing EV given some table conditions (10M hands / second)
  • Game mode for practicing basic strategy and card counting with hints
  • No package dependencies
  • Runs in any JS environment (CLI, browser, React Native app etc)
  • Multi-core support in Node

Usage

Simulator mode

npm install -g @blackjacktrainer/blackjack-simulator
blackjack-simulator simulate --help

Override the number of CPU cores used:

CORES=1 blackjack-simulator simulate

Game mode

blackjack-simulator game --help

See it live as a web app here.

Build and run locally

npm install
nvm use --install
NODE_ENV=development npm run build
./bin/cli.js

Use as a library (simulator mode)

import { Simulator } from '@blackjacktrainer/blackjack-simulator';

// Default settings:
const settings = {
  debug: false,

  // Simulator-only settings:
  hands: 10 ** 7,
  // Can be one of:
  // 'basic-strategy': play perfect basic strategy
  // 'basic-strategy-i18': play perfect basic strategy plus illustrious 18
  // 'basic-strategy-i18-fab4': play perfect basic strategy plus illustrious 18 + fab 4
  playerStrategy: 'basic-strategy-i18-fab4',

  playerBetSpread: [1000, 2000, 4000, 8000, 16000],
  playerSpots: [1, 1, 1, 1, 1],
  playerTablePosition: 1,
  playerBankroll: 1000 * 10 ** 7,
  playerWongOutTrueCount: null,

  // Table rules
  allowDoubleAfterSplit: true,
  allowLateSurrender: true,
  allowResplitAces: false,
  blackjackPayout: '3:2',
  deckCount: 2,
  hitSoft17: true,
  maxHandsAllowed: 4,
  maximumBet: 1000 * 100,
  minimumBet: 1000,
  playerCount: 1,
  penetration: 0.75
};

const simulator = new Simulator(settings);
const result = simulator.run();

Result contains the following data:

{
  amountEarned: number;
  amountWagered: number;
  bankrollMean: number;
  bankrollRqd: number;
  bankrollVariance: number;
  handsLost: number;
  handsPlayed: number;
  handsPushed: number;
  handsWon: number;
  hoursPlayed: number;
  riskOfRuin: number;
  timeElapsed: number;
}

Use as a library (game mode)

import {
  Event,
  Game,
  GameStep,
  PlayerInputReader
} from '@blackjacktrainer/blackjack-simulator';

// Default settings:
const settings = {
  autoDeclineInsurance: false,
  disableEvents: false,
  checkDeviations: false,

  // Can be one of 'default', 'pairs', 'uncommon', 'deviations'. If the mode is set to 'deviations', `checkDeviations`
  // will be forced to true.
  mode: 'default',
  debug: false,

  playerBankroll: 1000 * 10 ** 7,
  playerTablePosition: 1,
  playerStrategyOverride: {},

  // Table rules
  allowDoubleAfterSplit: true,
  allowLateSurrender: false,
  allowResplitAces: false,
  blackjackPayout: '3:2',
  deckCount: 2,
  hitSoft17: true,
  maxHandsAllowed: 4,
  maximumBet: 1000 * 100,
  minimumBet: 1000,
  playerCount: 1,
  penetration: 0.75
};

const game = new Game(settings);

// In a real app, this will likely be a React-redux store or a Vuex store.
const state = {};

// Called when any game state changes. `name` will be one of the following:
//
// - focusedHand
// - sessionMovesCorrect
// - sessionMovesTotal
// - playCorrection
// - step
// - shoe
// - discardTray
// - dealer
// - player
// - handWinner
game.on(Event.Change, (name, value) => {
  state[name] = value;
});

game.on(Event.Shuffle, () => {
  console.log('End of shoe, cards shuffled!');
});

// Emitted when the game wants to save optional game statistics.
// `entityName` can be one of `hand-result` or `move`.
// `data` is a plain object with values to save to the backend.
game.on(Event.CreateRecord, (entityName, data) => {
  fetch(`/api/v1/${entityName}`, {
    method: 'POST',
    body: JSON.serialize(data),
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json'
    }
  });
});

function stepGame(game, playerInputReader, input) {
  const step = game.step(input);

  if (
    ![
      GameStep.WaitingForPlayInput,
      GameStep.WaitingForInsuranceInput,
      GameStep.WaitingForNewGameInput
    ].includes(step)
  ) {
    return Promise.resolve();
  }

  return new Promise((resolve) => playerInputReader.readInput(resolve));
}

async function runGame(game) {
  game.betAmount = 10 * 100;

  const playerInputReader = new PlayerInputReader();
  let input;

  while (true) {
    input = await stepGame(game, playerInputReader, input);
  }
}

runGame(game);

PlayerInputReader.readInput listens for a click or keypress event on document.body. Your DOM just has to declare the following buttons somewhere for user interaction:

{
  game.state.step === GameStep.WaitingForPlayInput && (
    <>
      <button data-action="s">Stand (S)</button>
      <button data-action="h">Hit (H)</button>
      <button data-action="d">Double (D)</button>
      <button data-action="r">Surrender (R)</button>
      <button data-action="p">Split (P)</button>
    </>
  );
}
{
  game.state.step === GameStep.WaitingForInsuranceInput && (
    <>
      <button data-action="n">No (N)</button>
      <button data-action="y">Yes (Y)</button>
    </>
  );
}
{
  game.state.step === GameStep.WaitingForNewGameInput && (
    <>
      <button data-action="d">Deal (press any key)</button>
    </>
  );
}