Code Monkey home page Code Monkey logo

d20's Introduction

d20

Crates.io docs.rs

D20 is a simple crate designed to evaluate roll expressions. A roll expression is an english-language string that reflects the intent of a dungeon or game master to perform a particular roll.

For example, in a tabletop game you may frequently hear phrases like "roll 2d10", or "roll 3d6 and add 5". These are roll expressions, and the components within them are what we call die roll terms. A die roll term is either a term that calls for the rolling of an n-sided die x times (e.g. 3d6) or a modifier that simply adds or subtracts a constant value from the larger expression.

Examples of valid roll expressions include:

  • 3d6
  • 2d10 + 5
  • 1d20-3
  • +6
  • -2
  • 3d10+5d100-21+7

Roll expressions can have arbitrary length and complexity, and it is perfectly legal for the final result of a roll expression to be negative after applying modifiers.

Usage

extern crate d20;

fn main() {
    let r = d20::roll_dice("3d6 + 4").unwrap();
    assert!(r.total > 6);
    let r = d20::roll_dice("1d1-3").unwrap();
    assert_eq!(r.total, -2);

    let r = d20::roll_dice("roll four chickens and add six ferrets");
    match r {
       Ok(_) => assert!(false), // this should NOT be ok, fail
       Err(_) => assert!(true), // bad expressions produce errors
   }
}

Iterating Roll

A valid Roll can be converted into an open ended iterator via its into_iter() method, providing successive rolls of the given die roll expression.

Note that it will be necessary to constrain the iterator via take(n).

extern crate d20;
use d20::*;

fn main() {
    let raw_stats: Vec<Roll> = d20::roll_dice("3d6").unwrap().into_iter().take(6).collect();

    println!("\nCHARACTER STATS:");
    println!("  STR: {}", raw_stats[0].total);
    println!("  INT: {}", raw_stats[1].total);
    println!("  WIS: {}", raw_stats[2].total);
    println!("  DEX: {}", raw_stats[3].total);
    println!("  CON: {}", raw_stats[4].total);
    println!("  CHA: {}", raw_stats[5].total);
}

Range Rolls

If you are less concerned about dice rolls and require only a random number within a given range, roll_range() will do just that.

extern crate d20;
fn main() {
    let rg = d20::roll_range(1,100).unwrap();
    assert!(rg >= 1 && rg <= 100);
}

d20's People

Contributors

autodidaddict avatar ddn4 avatar dnem avatar

Watchers

 avatar

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.