Giter Club home page Giter Club logo

dice's Introduction

Dice

NPM version License GitHub Build Status Code coverage Written in TypeScript

This is a TypeScript library designed to facilitate the computation of dice rolls and random number generation. With support for various types and quantities of dice, it allows users to easily perform calculations for use in games, simulations, and other applications. Its RPG dice notation parsing capabilities allow users to roll based on specific game rules (e.g., 1d4, d6, 2d6+5, 1d20-1). Whether building a character generator for a tabletop RPG or developing a gaming application, this utility library is an essential tool for any developer.

Install

npm install @2bad/dice

Warning: This package is native ESM and no longer provides a CommonJS export. If your project uses CommonJS, you will have to convert to ESM or use the dynamic import() function. Please don't open issues for questions regarding CommonJS / ESM.

Usage

import { roll } from '@2bad/dice'

// Roll a six-sided die with lowercase 'd'
roll('d6')
// same as
roll('D6')

// Roll two eight-sided dice
roll('2d8')

// Roll two twenty-sided dice
roll('2d20').

Dice-Rolling with Modifier

In addition to rolling a single die or multiple dice, this dice-rolling program can also handle modifiers. A modifier is a number added to or subtracted from the total value obtained from the dice rolls. For instance, in DnD, instead of just rolling a 20-sided die (d20), you might also add a modifier to reflect a character's skill or level.

To roll a dice with a modifier, simply include a plus (+) or minus (-) sign and another integer at the end of your dice-roll string. Here are some examples:

  • 2d6+3: Roll two six-sided dice and add 3 to the total value.
  • d20-1: Roll a single 20-sided die and subtract 1 from the total value.
  • 3d8+12: Roll three eight-sided dice and add 12 to the total value.

Note that the modifier applies to the entire result of the dice roll, not each individual die roll. When using a modifier, it's important to check the rules of your game system to determine which calculations are appropriate.

Backus–Naur form grammar for dice roll string syntax

<dice-notation>   ::= <non-zero-number>? "d" <non-zero-number> <modifier>?
<modifier>        ::= ("+" | "-") <non-zero-number>
<non-zero-number> ::= <non-zero-digit> | <number>
<number>          ::= <non-zero-digit> <digit>*
<digit>           ::= "0" | <non-zero-digit>
<non-zero-digit>  ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

This BNF grammar defines the syntax for a dice notation, which consists of a optional non-zero number of dice to be rolled and a number of sides each die has, optionally followed by a modifier that can be added to or subtracted from the result of the roll.

Contributing

We welcome contributions! If you find a bug or want to request a new feature, please open an issue. If you want to submit a bug fix or new feature, please open a pull request.

dice's People

Watchers

 avatar

dice's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency @2bad/tsconfig to v3
  • Update dependency eslint to v9
  • Update dependency eslint-config-standard-with-typescript to v43
  • Update dependency eslint-plugin-jsdoc to v48
  • Update dependency eslint-plugin-n to v17
  • Update dependency vitest to v1
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v3
  • actions/setup-node v3
  • codecov/codecov-action v3
.github/workflows/publish.yml
  • actions/checkout v3
  • actions/setup-node v3
npm
package.json
  • pure-rand 6.0.2
  • @2bad/tsconfig 1.5.1
  • @types/node 20.4.5
  • @typescript-eslint/eslint-plugin 5.62.0
  • @typescript-eslint/parser 5.62.0
  • @vitest/coverage-c8 0.33.0
  • eslint 8.46.0
  • eslint-config-prettier 8.9.0
  • eslint-config-standard-with-typescript 37.0.0
  • eslint-import-resolver-typescript 3.5.5
  • eslint-plugin-import 2.28.0
  • eslint-plugin-jsdoc 46.4.5
  • eslint-plugin-n 15.7.0
  • eslint-plugin-prettier 4.2.1
  • eslint-plugin-promise 6.1.1
  • eslint-plugin-vitest 0.2.8
  • npm-run-all 4.1.5
  • prettier 2.8.8
  • rimraf 5.0.1
  • tsc-alias 1.8.7
  • typescript 5.1.6
  • vitest 0.33.0
  • node >=18
  • node 19.9.0

  • Check this box to trigger a request for Renovate to run again on this repository

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.