Last active
February 5, 2018 00:59
-
-
Save dwgill/9746347b143e241123bdbc8d2e9d9157 to your computer and use it in GitHub Desktop.
Automatically roll out a D&D (5e) attribute layout using a method described by Matthew Colville
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Copyright 2017 Daniel Gill | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be included | |
in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
USE OR OTHER DEALINGS IN THE SOFTWARE. | |
Automatically roll out a D&D (5e) attribute layout using the method | |
described by Matthew Colville here: | |
https://youtu.be/0K9mKpAMREU?t=8m53s | |
Put simply, 4d6 drop lowest for each attribute in order (i.e. first | |
roll is str, second is dex, etc.), rerolling the entire layout if | |
at least two of the attributes are not 15 or higher. | |
""" | |
import random | |
d6 = lambda: random.randint(1, 6) | |
def roll_att(): | |
rollout = (d6(), d6(), d6(), d6()) | |
return sum(rollout) - min(rollout) | |
def rollout_attributes(): | |
return { | |
'str': roll_att(), | |
'dex': roll_att(), | |
'con': roll_att(), | |
'int': roll_att(), | |
'wis': roll_att(), | |
'cha': roll_att() | |
} | |
def attributes_are_valid(attributes): | |
over_fifteen = filter(lambda attr: attr >= 15, attributes.values()) | |
return len(list(over_fifteen)) >= 2 | |
def main(): | |
attributes = rollout_attributes() | |
while not attributes_are_valid(attributes): | |
attributes = rollout_attributes() | |
modifiers = {} | |
for attribute, value in attributes.items(): | |
modifiers[attribute + '_mod'] = (value - 10) // 2 | |
attributes.update(modifiers) | |
print('''\ | |
str: {str} ({str_mod:+d}) | |
dex: {dex} ({dex_mod:+d}) | |
con: {con} ({con_mod:+d}) | |
int: {int} ({int_mod:+d}) | |
wis: {wis} ({wis_mod:+d}) | |
cha: {cha} ({cha_mod:+d})\ | |
'''.format(**attributes)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment