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 allowedany
any allowed9or10
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:
- the default state is loaded into game
- 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.