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

blackjack-engine

v0.1.2

Published

NodeJs, multi-player, blackjack engine

Downloads

8

Readme

Available game parameters

There are many possible configuration. We are implementing Standard and Custom options so that you can easily combine flags to create games according with your skill/needs.

Standard variations

  • number of decks, default is 1
  • standOnSoft17, turn On/Off the "Soft 17" rule, default true
  • double, ability to double after deal, default any
    • none not allowed
    • any any allowed
    • 9or10
    • 9or10or11
    • 9thru15
  • split, On/Off the possibility to split after deal, default true
  • doubleAfterSplit, On/Off the possibility to double after split (split and double must be "on"), default true
  • surrender, on/off the ability to surrender after deal, default true
  • insurance, on/off the ability of ensuring a hand, default true

Install

If you are using npm, to get the last version:

  • yarn add blackjack-engine
  • npm install blackjack-engine

Quick Start

Once obtained the library just require Game and actions.

const blackjack = require('blackjack-engine')
const actions = blackjack.actions
const Game = blackjack.Game

At this point you can initialize a new game by calling the Game constructor.

Creating a new game

const game = new Game()

In this cases, no state is passed to the constructor:

  1. the default state is loaded into game
  2. game is ready to dispatch actions to alter the state

Getting current state

At any moment we can require the current state of the game by calling the getState().

console.dir(game.getState())

The content of the state and its schema depends on the stage of the game. In this case we initialized the game without any precedent state, so we will receive something like this:

For the moment the only thing we should note is that the field stage tells us "game is ready".

Dispatching actions

The only way to mutate the state of the game is to dispatch actions. Some actions are required by the "user", some other actions are dispatched by the engine to "complete" the game.

NOTE: In a real game, players and dealer are allowed to "do actions". The engine will "impersonate the dealer" at some point, depending on the last action and the state.

// stage is "ready"
console.log(game.getState().stage)

// call an action to mutate the state
game.dispatch(actions.deal())

// stage has changed
console.log(game.getState().stage)

Project Structure

Based on Marco Casula's project.

Actions

see the /src/actions.js

Engine exposes actions, once invoked, the state of the game is changed. The following list represent the actions that can be dispatched by from the public API.

  • bet
  • insurance
  • double
  • split
  • hit
  • stand
  • surrender

And, those are actions that are internally called in determinate stages by the engine itself.

  • deal-cards
  • showdown
  • dealerHit
  • invalid

Stages

See the /src/game.ts

The stage represent a moment in the game. The stage is directly related with the action allowed in that particular moment.

Current available stages for players are:

  • ready
  • insurance
  • players-turn
  • showdown
  • dealer-turn
  • done

Logic

The game logic is implemented into /src/engine.ts. There is a specific design limitation currently in the code.

NOTE: If you are interested in the random components, check out the shuffle() function.

Test

Run tests by calling yarn test or npm test

Jest will care about the following tasks:

  • create a new game
  • initialize it by injecting ♠10 ♦1 ♥5 ♣6 ♠11 ♦10 at the and of the deck
  • run the desired restore, deal, split, insurance and finally stand
  • return the current state
  • compare if stage is 'done' at the end

If you specify the finalWin the test will compare the final winning.