Giter VIP home page Giter VIP logo

dice's Introduction

Golang dice

This is a simple library for rolling RPG-style dice. The following formats are supported:

  • Standard: xdy[[k|d][h|l]z][+/-c] - rolls and sums x y-sided dice, keeping or dropping the lowest or highest z dice and optionally adding or subtracting c. Example: 4d6kh3+4
  • Fudge: xdf[+/-c] - rolls and sums x fudge dice (Dice that returns numbers between -1 and 1), and optionally adding or subtracting c. Example: 4df+4
  • Versus: xdy[e|r]vt - rolls x y-sided dice, counting the number that roll t or greater.
  • EotE: xc [xc ...] - rolls x dice of color c (b, blk, g, p, r, w, y) and returns the aggregate result.

Adding an e to the Versus rolls above makes dice 'explode' - Dice are rerolled and have the rolled value added to their total when they roll a y. Adding an r makes dice rolling a y add another die to the pool instead.

Installation

The usual go get github.com/justinian/dice.

Usage

The main entrypoint to the library is the dice.Roll function:

func Roll(desc string) (fmt.Stringer, error)

The desc argument takes any string that matches the formats above and rolls correctly. The result is returned as a RollResult (which is a fmt.Stringer for simple printing, but also contains different information based on the type of roll). See the individual roll styles for their result structures.

dice's People

Contributors

eduardostalinho avatar frenata avatar justinian avatar olavmueller avatar silkeh avatar

Stargazers

 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  avatar

dice's Issues

add to RollResult interface: Int() int

I'm not particular about the name of said function, but I tried to find a simple way to retrieve 'just the total' from a StdResult and saw that there was an exported function Total() int, which since it's not a part of the interface requires the client to perform a type assertion along the lines of:

result, _, err := Roll("3d8")
if err!= nil { //handle it }
total := result.(StdResult).Total()

This isn't terrible, but for the purposes of when a client really just wants the simple integer result of a roll, it'd be nice to just call result.Int(). The main problem, though, is that while StdResult.Total() and VsResult.Successes() make sense as the 'simple integer' returns from a Result, I'm clueless as to whether that question even makes sense when it comes to EotE, a system I'm not familiar with.

If there's simply no reasonable simple return from EotE or this is an undesired addition, that's fine; otherwise I'm happy to make the necessary changes and send a PR if given some direction as to what kind of return that implementation should provide.

embedded fmt.Stringer

This isn't necessarily an issue so much as an attempt at understanding the code, but why is the RollResult interface defined with the embedded interface fmt.Stringer. As far as I know this is functionally equivalent to String() string in the interface definition, yes?

Anyway, thanks for the library more generally, I was sitting down thinking of some ideas for CLI helper tools for running some RPGs and realized that a first start was a parser/roller for '3d6' style strings, but figured I better check to make sure someone else hadn't done it first before potentially replicating a lot of work!

Setting Random Generator

Given the structure of dice, it looks like it would be rather trivial to set up the ability to use a custom rand. At least for each individual Roller, given they are all already structs. Setting a base/'dice global' one would be a little more tricky.
I would be willing to submit a PR if it would be accepted.

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.