Skip to content

Instantly share code, notes, and snippets.

@tphummel
Last active September 1, 2019 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tphummel/264b60b515b235ccdf98224b05f62f79 to your computer and use it in GitHub Desktop.
Save tphummel/264b60b515b235ccdf98224b05f62f79 to your computer and use it in GitHub Desktop.
craps simulator

craps toy/simulator

usage

➜  node craps-toy.js
┌─────────┬──────┬──────┬─────┬─────────────┐
│ (index) │ die1 │ die2 │ sum │   result    │
├─────────┼──────┼──────┼─────┼─────────────┤
│    0    │  1   │  4   │  5  │ 'point set' │
│    1    │  3   │  6   │  9  │  'neutral'  │
│    2    │  1   │  5   │  6  │  'neutral'  │
│    3    │  1   │  1   │  2  │  'neutral'  │
│    4    │  4   │  5   │  9  │  'neutral'  │
│    5    │  2   │  3   │  5  │ 'point win' │
│    6    │  3   │  5   │  8  │ 'point set' │
│    7    │  5   │  5   │ 10  │  'neutral'  │
│    8    │  2   │  4   │  6  │  'neutral'  │
│    9    │  2   │  6   │  8  │ 'point win' │
│   10    │  1   │  3   │  4  │ 'point set' │
│   11    │  2   │  4   │  6  │  'neutral'  │
│   12    │  2   │  5   │  7  │ 'seven out' │
└─────────┴──────┴──────┴─────┴─────────────┘
┌───────────────┬────────┐
│    (index)    │ Values │
├───────────────┼────────┤
│   rollCount   │   13   │
│   pointsSet   │   3    │
│   pointsWon   │   2    │
│  comeOutWins  │   0    │
│ comeOutLosses │   0    │
│   neutrals    │   7    │
└───────────────┴────────┘
'use strict';
function d6 () {
return 1 + Math.floor(Math.random() * 6)
}
function tallyResults (history) {
const resultsTemplate = {
rollCount: 0,
pointsSet: 0,
pointsWon: 0,
comeOutWins: 0,
comeOutLosses: 0,
neutrals: 0
}
return history.reduce((memo, roll) => {
memo.rollCount++
switch (roll.result) {
case 'neutral':
memo.neutrals++;
break;
case 'point set':
memo.pointsSet++;
break;
case 'point win':
memo.pointsWon++;
break;
case 'comeout win':
memo.comeOutWins++;
break;
case 'comeout loss':
memo.comeOutLosses++;
break;
}
return memo
}, resultsTemplate)
}
let history = []
let isComeOut = true
let point
while(true) {
const dice = [d6(), d6()].sort()
let roll = {
die1: dice[0],
die2: dice[1],
sum: dice.reduce((m, r) => { return m + r }, 0)
}
// game logic based on: https://github.com/tphummel/dice-collector/blob/master/PyTom/Dice/logic.py
if (isComeOut) {
if ([2,3,12].indexOf(roll.sum) !== -1) {
roll.result = 'comeout loss'
} else if ([7,11].indexOf(roll.sum) !== -1 ) {
roll.result = 'comeout win'
} else {
point = roll.sum
roll.result = 'point set'
isComeOut = false
}
} else {
if (roll.sum === point) {
roll.result = 'point win'
isComeOut = true
} else if (roll.sum === 7) {
roll.result = 'seven out'
} else {
roll.result = 'neutral'
}
}
history.push(roll)
if (roll.result === 'seven out') break
}
console.table(history)
console.table(tallyResults(history))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment