Giter Club home page Giter Club logo

dice's Introduction

Dice

Simple dice rolling for Go.

dice offers three basic methods for rolling dice:

dice.Roll()

,

dice.RollP()

, and

dice.RollD6()

The first function receives a single string parameter which describes the dice roll. For example, send 3d6 to roll three 6-sided dice, 1d20+1 to roll one 20-sided die and add 1 to the result, 1.5d6-1 to roll one and a half 6-sided rolls and subtract one from the result. Half-dice are taken to be a single die of half the normal faces. That is, 1.5d6-1 rolls one 6-sided die, one 3-sided die, and subtracts 1 from the sum of both dice.

The second function receives all the parameters of the dice roll separately (think of the P as "parameters", "pieces", or "parts"). The parameters are: number (the number of dice to roll), faces (how many sides, or faces, each die has), adder (any static quantity to add to the roll's total), and half (a boolean value indicating whether a half-die should be rolled in addition to the dice specified in the number parameter). The equivalent to dice.Roll("1.5d6-1") is dice.RollP(1,6,-1,true).

The third function receives a number and rolls that many 6-sided dice. The last rolled die is considered a "wild" die. A wild die coming up 6 causes an additional die to be rolled. This additional die is also a wild die, which means that if it comes up 6, the process continues, until a non-6 die is rolled.

All three functions return a DiceRoll object:

type DiceRoll struct {
    NumberOfDice int
    DieFaces     int
    Adder        int
    Half         bool
    Rolls        []int
    RawTotal     int
    Total        int
}

DiceRoll objects have a method Description() which returns a description like the one used to roll with dice.Roll().

Additionally, a throwdice runnable is included, which can take any number of dice roll descriptions as command-line arguments and prints the result for each. It will use the D6 system if passed the -d6 flag. It will also read from stdin if no command-line arguments are given, stopping on EOF or "exit" or "quit" (case insensitive). Note that in D6 mode, descriptions are merely numbers, specifically the number of 6-sided dice to roll.

(Yes, it's all very simple and not extremely useful... it's mostly an exercise on Go on my part.)

dice's People

Contributors

tonio-ramirez avatar

Watchers

James Cloos avatar Leo Yeung 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.