Char roller script

Post/Author/DateTimePost
#1

Eraslin

Jul 07, 2014 19:19:10

I cooked up a quick and dirty little Python script to roll up a stat array. Figured I'd share it in case others might find it useful. To run the script, just copy the code below into any online Python interpreter (ex: Compile Online) Enjoy.

Show
#!/usr/local/bin/python2.7

'''
 A simple little script to roll up a 4d6 drop lowest D&D stat array.
'''

import random

def rollStat():
  '''
  Roll 4d6, drop lowest
  '''
  rolls = [ random.randint(1,6) for i in range(0,4) ]
  return sum(rolls)-min(rolls), rolls

def rollStatBlock():
  '''
  Roll 6 stats, and return them in a list sorted by stat
  '''
  L = [ (rollStat()) for i in range(0,6) ]
  L.sort(key=lambda item: item[0])
  return L

def calcPointValue(rollsList):
  '''
  Calculate the point value of the stat
  Point values for stats > 15 are extrapolated
  from the "D&D Basic Rules" point-buy table
  '''
  value = 0
  for roll in rollsList:
    stat = roll[0]
    if stat <= 8:  value += 0
    elif stat <= 13: value += stat - 8
    elif stat <= 15: value += 5 + 2*(stat-13)
    elif stat <= 17: value += 7 + 3*(stat-15)
    else: value += 17
  return value


def printStatBlock(rolls):
  '''
  Print out a stat block
  '''
  for roll in rolls:
    stat,dice = roll
    print "Roll:", dice, " -> ", stat
  print "Point value: ", calcPointValue(rolls)


random.seed()
'''
 To reproduce a past roll:
  1) Add a # to the start of the line:
     seed = random.randint(...)
     ( This will comment-out [i.e. remove] that line from the program )
  2) Remove the # from the start of the line:
     #seed = 1234
  3) Change the 1234 on that line to your seed
'''

seed = random.randint(0,999999999999)
#seed = 1234
random.seed(seed)
print "Seed = ", seed

printStatBlock(rollStatBlock())
#2

Jordan175

Jul 07, 2014 22:58:31

Can you do one for the point-buy system? With or without racial modifiers.

#3

Itaav

Jul 09, 2014 7:03:05

I can't get it working, I keep getting errors. Would anyone care to help me? 

(Reply to #3)

Eraslin

I just tried actually copy/pasting the script code into the online interpreter, and saw the error that you got.

It looks like this is a case of the web browser trying to be too smart for its own good. After you have copied the script text into the interpreter window, you can scroll down a bit and you will see a series of lines that look like:

if stat &lt;= 8: value += 0

You need to change all of those "<" into <; (there are 4 in total to change). So, the line should read:

if stat <= 8: value += 0

.... and so on

 

Edit: I've fixed the code in the original post. The text box was converting my text into html code on me...

(Reply to #2)

Eraslin

Jordan175 wrote: