Guest User

Untitled

a guest
Oct 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: latin-1 -*-
  3. # import normal division from python 3 to make producing percentages more
  4. # straightforward
  5. from __future__ import division
  6. from collections import Counter
  7.  
  8. faces = {
  9. "b": ["", "", "s", "sa", "aa", "a"], # Boost die faces
  10. "s": ["", "", "f", "f", "d", "d"], # Setback die faces
  11. "a": ["", "s", "s", "ss", "a", "a", "sa", "aa"], # Ability die faces
  12. "d": ["", "f", "ff", "d", "d", "d", "dd", "fd"], # Difficulty die faces
  13. "p": ["", "s", "s", "ss", "ss", "a", "sa", "sa", "sa", "aa", "aa", "T"], # Proficiency die faces
  14. "c": ["", "f", "f", "ff", "ff", "d", "d", "fd", "fd", "dd", "dd", "D"] # Challenge die faces
  15. }
  16.  
  17. simplified_faces = {
  18. "b": [0, 0, 1, 1, 0, 0], # Boost die faces
  19. "s": [0, 0, -1, -1, 0, 0], # Setback die faces
  20. "a": [0, 1, 1, 2, 0, 0, 1, 0], # Ability die faces
  21. "d": [0, -1, -2, 0, 0, 0, 0, -1], # Difficulty die faces
  22. "p": [0, 1, 1, 2, 2, 0, 1, 1, 1, 0, 0, 1], # Proficiency die faces
  23. "c": [0, -1, -1, -2, -2, 0, 0, -1, -1, 0, 0, 0] # Challenge die faces
  24. }
  25.  
  26. def single_bar(percent):
  27. bar_string = ""
  28. bar_string += int(percent//0.01)*"|"
  29. # do some fancy box drawing bar graphs
  30. """
  31. modulo = percent % 0.04
  32. eighths = int(modulo//0.005)
  33. characters =
  34. """
  35. return bar_string
  36.  
  37.  
  38.  
  39. def possible_totals(dice_string):
  40. """
  41. Output all the possible totals from rolling a given dice string
  42. """
  43.  
  44. # check if we're done
  45. if dice_string == "":
  46. # print "received empty dice string!"
  47. return [0]
  48.  
  49. # otherwise,
  50. # slice the dice_string into the die being rolled and the rest of the string
  51. dice_to_roll = dice_string[0]
  52. remaining_dice = dice_string[1:]
  53.  
  54. """
  55. print "rolling {} with a remainder of '{}'".format(dice_to_roll,
  56. remaining_dice)
  57. """
  58.  
  59. #prepare a container for a list of possible totals to pass back
  60. totals = []
  61.  
  62. #for each potential result of the rest of the string,
  63. for remaining_total in possible_totals(remaining_dice):
  64. #take each possible roll of the die at hand
  65. for result in simplified_faces[dice_to_roll]:
  66. #sum the die at hand and the total of the remainder
  67. total = result+remaining_total
  68. totals.append(total)
  69.  
  70. """
  71. print "from dicestring of {} returning totals \n{}".format(dice_string,
  72. totals)
  73. """
  74. return totals
  75.  
  76. def print_chart(prob_dict):
  77. for entry in sorted(prob_dict.items()):
  78. key = entry[0]
  79. # produce the bar
  80. bar = single_bar(prob_dict[key])
  81. # produce the suffix
  82. suffix = "{} - {}%".format(key,round(prob_dict[key]*100,2))
  83. #print them
  84. print "{} {}".format(bar,suffix)
  85.  
  86.  
  87. def probabilities(totals):
  88. # count the total number of outputs
  89. length = len(totals)
  90. # put them into a Counter
  91. c = Counter(totals)
  92. #convert the counter to a dict so it is sorted
  93. c = dict(sorted(c.items()))
  94.  
  95. # produce a probability dictionary
  96. probabilities = {}
  97. for entry in c:
  98. probabilities[entry] = c[entry]/length
  99.  
  100. return probabilities
  101.  
  102. def overall_success_prob(totals):
  103. # put them into a Counter
  104. c = Counter(totals)
  105. #convert the counter to a dict so it is sorted
  106. c = dict(sorted(c.items()))
  107.  
  108. total_success = 0
  109. total_failure = 0
  110.  
  111. for value in c:
  112. if value > 0:
  113. total_success+=c[value]
  114. elif value <=0:
  115. total_failure+=c[value]
  116.  
  117. return total_success/(total_success+total_failure)
  118.  
  119. return 0
  120.  
  121. def f(string):
  122. pt = possible_totals(string)
  123. p = probabilities(pt)
  124. print_chart(p)
  125. overall_p = overall_success_prob(pt)
  126. print "Overall chance of success: {}%".format(round(overall_p*100,2))
Add Comment
Please, Sign In to add comment