Advertisement
Guest User

Sacred Geometry Python Script

a guest
Jul 27th, 2014
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. """
  2. sacredgeometry.py
  3. This script is to be used with the Pathfinder RPG feat "Sacred Geometry," released
  4. in the Occult Mysteries Campaign Setting sourcebook.
  5. http://www.archivesofnethys.com/FeatDisplay.aspx?ItemName=Sacred%20Geometry
  6.  
  7. Inputting the results of a dice roll will determine if it is possible to apply
  8. the Sacred Geometry feat to the results, returning the first result found for a prime
  9. constant as listed by the feat.
  10.  
  11. This script terminates itself as soon as it finds a solution to the given input.
  12. """
  13.  
  14. import itertools
  15. import re
  16.  
  17. OPS = ('+', '-', '*', '/')
  18.  
  19. class Expr:
  20. """An Expr can be built with two different calls:
  21. -Expr(number) to build a literal expression
  22. -Expr(a, op, b) to build a complex expression.
  23. There a and b will be of type Expr,
  24. and op will be one of ('+','-', '*', '/').
  25. """
  26. def __init__(self, *args):
  27. if len(args) == 1:
  28. self.left = self.right = self.op = None
  29. self.value = args[0]
  30. else:
  31. self.left = args[0]
  32. self.right = args[2]
  33. self.op = args[1]
  34. if self.op == '+':
  35. self.value = self.left.value + self.right.value
  36. elif self.op == '-':
  37. self.value = self.left.value - self.right.value
  38. elif self.op == '*':
  39. self.value = self.left.value * self.right.value
  40. elif self.op == '/':
  41. self.value = self.left.value // self.right.value
  42.  
  43. def __str__(self):
  44. if self.op:
  45. return "({0}{1}{2})".format(self.left, self.op, self.right)
  46. else:
  47. return "{0}".format(self.value)
  48.  
  49. #Determines if a solution contains the correct number of dice
  50. def stringChecker(aString):
  51. return len([s for s in re.split(r'[\(\)\+\*-/]+|string', aString) if s])
  52.  
  53. def SearchTrees(current, target, numbersLength):
  54. #current is the current set of expressions.
  55. #target is the target number.
  56. #numbersLength is the number of dice used
  57. for a,b in itertools.combinations(current, 2):
  58. current.remove(a)
  59. current.remove(b)
  60. for o in OPS:
  61. # This checks whether this operation is commutative
  62. if o == '-' or o == '/':
  63. conmut = ((a,b), (b,a))
  64. else:
  65. conmut = ((a,b),)
  66.  
  67. for aa, bb in conmut:
  68. # You do not specify what to do with the division.
  69. # I'm assuming that only integer divisions are allowed.
  70. if o == '/' and (bb.value == 0 or aa.value % bb.value != 0):
  71. continue
  72. e = Expr(aa, o, bb)
  73. # If a solution is found, print it
  74. if (e.value == target and stringChecker(e.__str__()) == numbersLength):
  75. print e.value, '=', e
  76. return 0
  77. current.add(e)
  78. # Recursive call!
  79. SearchTrees(current, target, numbersLength)
  80. # Do not forget to leave the set as it were before
  81. current.remove(e)
  82. # Ditto
  83. current.add(b)
  84. current.add(a)
  85.  
  86. def main():
  87. numbers = input('Dice results as an array here w/syntax [a, b, ..., n]> ')
  88. poolSize = (len(numbers))
  89. spellLevel = input('Effective spell level> ')
  90. initial = set(map(Expr, numbers))
  91.  
  92. #Effective spell level of 1
  93. #try except allows script to terminate from recursive function calls gracefully
  94. #downside: If the user/script screws up, no error message will be given
  95. if(spellLevel == 1):
  96. try:
  97. SearchTrees(initial, 3, poolSize)
  98. except: pass
  99. try:
  100. SearchTrees(initial, 5, poolSize)
  101. except: pass
  102. try:
  103. SearchTrees(initial, 7, poolSize)
  104. except: pass
  105.  
  106. #Effective spell level of 2
  107. elif(spellLevel == 2):
  108. try:
  109. SearchTrees(initial, 11, poolSize)
  110. except: pass
  111. try:
  112. SearchTrees(initial, 13, poolSize)
  113. except: pass
  114. try:
  115. SearchTrees(initial, 17, poolSize)
  116. except: pass
  117.  
  118. #Effective spell level of 3
  119. elif(spellLevel == 3):
  120. try:
  121. SearchTrees(initial, 19, poolSize)
  122. except: pass
  123. try:
  124. SearchTrees(initial, 23, poolSize)
  125. except: pass
  126. try:
  127. SearchTrees(initial, 29, poolSize)
  128. except: pass
  129.  
  130. #Effective spell level of 4
  131. elif(spellLevel == 4):
  132. try:
  133. SearchTrees(initial, 31, poolSize)
  134. except: pass
  135. try:
  136. SearchTrees(initial, 37, poolSize)
  137. except: pass
  138. try:
  139. SearchTrees(initial, 41, poolSize)
  140. except: pass
  141.  
  142. #Effective spell level of 5
  143. elif(spellLevel == 5):
  144. try:
  145. SearchTrees(initial, 43, poolSize)
  146. except: pass
  147. try:
  148. SearchTrees(initial, 47, poolSize)
  149. except: pass
  150. try:
  151. SearchTrees(initial, 53, poolSize)
  152. except: pass
  153.  
  154. #Effective spell level of 6
  155. elif(spellLevel == 6):
  156. try:
  157. SearchTrees(initial, 59, poolSize)
  158. except: pass
  159. try:
  160. SearchTrees(initial, 61, poolSize)
  161. except: pass
  162. try:
  163. SearchTrees(initial, 67, poolSize)
  164. except: pass
  165.  
  166. #Effective spell level of 7
  167. elif(spellLevel == 7):
  168. try:
  169. SearchTrees(initial, 71, poolSize)
  170. except: pass
  171. try:
  172. SearchTrees(initial, 73, poolSize)
  173. except: pass
  174. try:
  175. SearchTrees(initial, 79, poolSize)
  176. except: pass
  177.  
  178. #Effective spell level of 8
  179. elif(spellLevel == 8):
  180. try:
  181. SearchTrees(initial, 83, poolSize)
  182. except: pass
  183. try:
  184. SearchTrees(initial, 89, poolSize)
  185. except: pass
  186. try:
  187. SearchTrees(initial, 97, poolSize)
  188. except: pass
  189.  
  190. #Effective spell level of 9
  191. elif(spellLevel == 9):
  192. try:
  193. SearchTrees(initial, 101, poolSize)
  194. except: pass
  195. try:
  196. SearchTrees(initial, 103, poolSize)
  197. except: pass
  198. try:
  199. SearchTrees(initial, 107, poolSize)
  200. except: pass
  201.  
  202. else:
  203. print "Effective spell level must be between 1 and 9"
  204. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement