Giter VIP home page Giter VIP logo

node-roll's Introduction

roll

Roll is a node.js package for rolling dice and adding modifiers (such as "2d6+1").

NPM

build status

How To Use (From Shell)

Installation (via npm)

$ npm install -g roll

Usage

$ roll 2d6+3
9
$ roll d20
15
$ roll d%
99
$ roll -d 2d20
Dice: 13, 8
Total: 21
$ roll --detail 5d%
Dice: 45, 86, 86, 4, 72
Total: 293

How To Use (As Library)

Installation (via npm)

$ npm install roll

Usage

Get an instance of the library:

var Roll = require('roll'),
  roll = new Roll();

Rolling a single die:

var oneDie = roll.roll('d6');
console.log(oneDie.result); //random number between 1 and 6 (inclusive)

Rolling multiple dice:

var twoTwenties = roll.roll('2d20');
console.log(twoTwenties.result); //random number between 2 and 40 (inclusive)

Rolling multiple sets of dice:

var bunchOfDice = roll.roll('2d20+1d12');
console.log(bunchOfDice.result); //random number between 3 and 52 (inclusive)

Rolling a percentage:

var chance = roll.roll('d%'); //same as '1d100', 'd100', or '1d%'
console.log(chance.result); //random number between 1 and 100 (inclusive)

Simple calculation (+, -, *, /):

var attack = roll.roll('2d6+2');
console.log(attack.result); //random number between 3 and 8 (inclusive)

Seeing what was rolled, rather than the sum:

var yahtzee = roll.roll('5d6');
console.log(yahtzee.rolled); //yahtzee.rolled will return something like [5, 2, 4, 6, 1] rather than the sum

var blessedSneaker = roll.roll('2d20b1+1d4+5');
console.log(blessedSneaker.rolled); // blessedSneaker.rolled will return an array containing an array for each component that is a roll of the dice, in the order in which they occurred, e.g. [[19,3],[1]]

Getting the highest two dice of the set:

var pickBestTwo = roll.roll('6d20b2'); //roll 6 dice and give me the 2 highest
console.log(pickBestTwo.calculations[1]); //pickBestTwo.calculations[0] is the same as .result, .calculations[1] is prior to the sum operation

Processing rolls without parsing a string:

var attack = roll.roll({
    quantity: 2,
    sides: 6,
    transformations: [ //can list n-number of pipeline operations to perform on the result
      'sum', //take the array of rolled dice and sum them together
      ['add', 2] //add 2 to the sum
    ]
});
console.log(attack.result); //random number between 3 and 8 (inclusive)

Using custom transformations:

var dropOnes = function(results){
  return results.filter(function (result) {
    return result !== 1;
  });
};
var noOnes = roll.roll({
  quantity: 5,
  sides: 4,
  transformations: [
    dropOnes, // remove any 1s because we have teh lucky bootz
    'sum'
  ]
});

Using a custom seed:

var srand = require('srand'); //https://github.com/isaacs/node-srand (npm install srand)
srand.seed(1000);

roll = new Roll(function () {
  return srand.random();
});

console.log(roll.roll('2d6+5').result);

Validating user input:

var userInput = 'this isn\'t a valid roll',
  valid = roll.validate(userInput);

if (!valid) {
  console.error('"%s" is not a valid input string for node-roll!', userInput);
}

Credits

Inspired by Phillip Newton's Games::Dice.

License

MIT License

Author

Troy Goode ([email protected])

node-roll's People

Contributors

cajahnke avatar koyamie avatar mfunkie avatar troygoode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-roll's Issues

Error on Worst-Of

if i do 2d20w1 will get the worst fine, but it i put 2d20w1+5
it will add the two data, the best-of work's fine

Explanation on input.transformations

Hey there.

I'm having trouble understanding everything on input.transformations haha. I was wondering if you could do a bit of an explanation on how it lays it out (god i dont know how to say things. Hopefully you understand, if not, let me know and I'll try explain a little better)

Calling .rolled does not result in expected output.

According to the documentation:
var yahtzee = roll.roll('5d6');
console.log(yahtzee.rolled); //yahtzee.rolled will return something like [5, 2, 4, 6, 1] rather than the sum

But doing something like:
var myRoll = roll.roll("2d10+5");
console.log(myRoll.rolled); // returns "6"

It returns the sum of the rolled die, not the individual die. I'm presuming that this is because the adding the modifier overwrites the list of rolled die.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.