Skip to main content

A dice rolling package focused on flexibility

Project description

Overview

multi_dice.py provides functions for rolling dice and calculating results for tabletop RPGs and other dice games. It handles standard dice rolls, advantage/disadvantage, modifiers, and selecting highest/lowest rolls.

Usage

To use the multi_dice module, first import it:

import multi_dice

To roll dice, call the roll function and pass the dice string:

result = multi_dice.roll("2d20+5")

To roll with advantage, add an 'a' before the dice string:

result = multi_dice.roll("a2d20+5")

To roll with disadvantage, add a 'd' before the dice string:

result = multi_dice.roll("d2d20+5")

Examples

  • "2d20" - Roll 2d20
  • "a2d20" - Roll advantage on 2d20
  • "d2d20" - Roll disadvantage on 2d20
  • "2d20+5" - Roll 2d20 and add 5
  • "4d6k3" - Roll 4d6 keep highest 3
  • "2d20l1-5" - Roll 2d20 keep lowest 1 and subtract 5
  • "2d8+1d6" - Roll 1d6 and add it to a roll of 2d8

Dice String Rules

Here are the rules for constructing a valid dice string:

  • "a" xor "d" (optional)
  • int
  • "d"
  • int
  • "k" xor "l" (optional)
    • int (requried if k xor l)
  • opcode (+, -, /, //, *) (optional)
    • int or another dice string (required if opcode)

OR

  • int

"a" = advantage
"d" = disadvantage
"k" = keep highest int rolls
"l" = keep lowest int rolls

Limits

The only checks on whether a roll is performed is if either the number of rolls or if the sides of the dice are over 1000, and if k and l are <= to the number of sides.

Functions

roll(dice, crit=20)

Rolls dice according to the provided dice string and returns the result.

  • dice (str): The dice rolling string
  • crit (int): The value that causes a critical (default 20)
  • Returns: (int) Result of the dice roll

advantage(*func,*args, *kwargs)

Calls a function twice and returns the higher result. Used for rolling with advantage.

  • func (function): The function to call
  • *args: Positional args to pass to the function
  • **kwargs: Keyword args to pass to the function
  • Returns: (int) The higher of the two function calls

disadvantage(*func,*args,*kwargs)

Calls a function twice and returns the lower result. Used for rolling with disadvantage.

  • func (function): The function to call
  • *args: Positional args to pass to the function
  • **kwargs: Keyword args to pass to the function
  • Returns: (int) The lower of the two function calls

Classes

RollDice

Class for rolling dice and calculating results. Handles parsing dice strings, rolling, modifiers, advantage/disadvantage, etc.

_init_(dice, crit=20)

  • dice (str): Dice rolling string

  • crit (int): Value that causes critical hit

.advantage(): Reroll with advantage, keeping higher value

.disadvantage(): Reroll with disadvantage, keeping lower value

.value: Result of the roll

.rolls: List of each individual dice roll

.average: Average possible roll result

.lowrange: Minimum possible roll result

.highrange: Maximum possible roll result

.crit: True if a critical hit was rolled

Class Examples

Regular usage

dice = multi_dice.RollDice("3d6")
print(dice.value)

Roll with dis/advantage:

dice = multi_dice.RollDice("a2d20")
print(dice.value)

dice2 = multi_dice.RollDice("d2d20")
print(dice2.value)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multi_dice-0.0.4.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

multi_dice-0.0.4-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file multi_dice-0.0.4.tar.gz.

File metadata

  • Download URL: multi_dice-0.0.4.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for multi_dice-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c9b6dfe0343bcf0720d6308fc47cdc84709aa05289b99183ebe3674ad78059ee
MD5 839ae746e38292192505b27a2c4f7bea
BLAKE2b-256 13ed82eeed5d738bae624975e59d35e4b72f7c48f7b59e4265c5e44869b1a886

See more details on using hashes here.

File details

Details for the file multi_dice-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: multi_dice-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for multi_dice-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 eec66768470318c728f010424978f74feb8468dcd0c1438a934aa03ccbe93804
MD5 522f805007423fdde5d0cd01355241ec
BLAKE2b-256 ccbdd636b3cb9124d0c9d4915cd9b87a76ae9d6425aab2cb6bdbce0925a2a9ae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page