Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: latin-1 -*-
- # import normal division from python 3 to make producing percentages more
- # straightforward
- from __future__ import division
- from collections import Counter
- faces = {
- "b": ["", "", "s", "sa", "aa", "a"], # Boost die faces
- "s": ["", "", "f", "f", "d", "d"], # Setback die faces
- "a": ["", "s", "s", "ss", "a", "a", "sa", "aa"], # Ability die faces
- "d": ["", "f", "ff", "d", "d", "d", "dd", "fd"], # Difficulty die faces
- "p": ["", "s", "s", "ss", "ss", "a", "sa", "sa", "sa", "aa", "aa", "T"], # Proficiency die faces
- "c": ["", "f", "f", "ff", "ff", "d", "d", "fd", "fd", "dd", "dd", "D"] # Challenge die faces
- }
- simplified_faces = {
- "b": [0, 0, 1, 1, 0, 0], # Boost die faces
- "s": [0, 0, -1, -1, 0, 0], # Setback die faces
- "a": [0, 1, 1, 2, 0, 0, 1, 0], # Ability die faces
- "d": [0, -1, -2, 0, 0, 0, 0, -1], # Difficulty die faces
- "p": [0, 1, 1, 2, 2, 0, 1, 1, 1, 0, 0, 1], # Proficiency die faces
- "c": [0, -1, -1, -2, -2, 0, 0, -1, -1, 0, 0, 0] # Challenge die faces
- }
- def single_bar(percent):
- bar_string = ""
- bar_string += int(percent//0.01)*"|"
- # do some fancy box drawing bar graphs
- """
- modulo = percent % 0.04
- eighths = int(modulo//0.005)
- characters =
- """
- return bar_string
- def possible_totals(dice_string):
- """
- Output all the possible totals from rolling a given dice string
- """
- # check if we're done
- if dice_string == "":
- # print "received empty dice string!"
- return [0]
- # otherwise,
- # slice the dice_string into the die being rolled and the rest of the string
- dice_to_roll = dice_string[0]
- remaining_dice = dice_string[1:]
- """
- print "rolling {} with a remainder of '{}'".format(dice_to_roll,
- remaining_dice)
- """
- #prepare a container for a list of possible totals to pass back
- totals = []
- #for each potential result of the rest of the string,
- for remaining_total in possible_totals(remaining_dice):
- #take each possible roll of the die at hand
- for result in simplified_faces[dice_to_roll]:
- #sum the die at hand and the total of the remainder
- total = result+remaining_total
- totals.append(total)
- """
- print "from dicestring of {} returning totals \n{}".format(dice_string,
- totals)
- """
- return totals
- def print_chart(prob_dict):
- for entry in sorted(prob_dict.items()):
- key = entry[0]
- # produce the bar
- bar = single_bar(prob_dict[key])
- # produce the suffix
- suffix = "{} - {}%".format(key,round(prob_dict[key]*100,2))
- #print them
- print "{} {}".format(bar,suffix)
- def probabilities(totals):
- # count the total number of outputs
- length = len(totals)
- # put them into a Counter
- c = Counter(totals)
- #convert the counter to a dict so it is sorted
- c = dict(sorted(c.items()))
- # produce a probability dictionary
- probabilities = {}
- for entry in c:
- probabilities[entry] = c[entry]/length
- return probabilities
- def overall_success_prob(totals):
- # put them into a Counter
- c = Counter(totals)
- #convert the counter to a dict so it is sorted
- c = dict(sorted(c.items()))
- total_success = 0
- total_failure = 0
- for value in c:
- if value > 0:
- total_success+=c[value]
- elif value <=0:
- total_failure+=c[value]
- return total_success/(total_success+total_failure)
- return 0
- def f(string):
- pt = possible_totals(string)
- p = probabilities(pt)
- print_chart(p)
- overall_p = overall_success_prob(pt)
- print "Overall chance of success: {}%".format(round(overall_p*100,2))
Add Comment
Please, Sign In to add comment