Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- sacredgeometry.py
- This script is to be used with the Pathfinder RPG feat "Sacred Geometry," released
- in the Occult Mysteries Campaign Setting sourcebook.
- http://www.archivesofnethys.com/FeatDisplay.aspx?ItemName=Sacred%20Geometry
- Inputting the results of a dice roll will determine if it is possible to apply
- the Sacred Geometry feat to the results, returning the first result found for a prime
- constant as listed by the feat.
- This script terminates itself as soon as it finds a solution to the given input.
- """
- import itertools
- import re
- OPS = ('+', '-', '*', '/')
- class Expr:
- """An Expr can be built with two different calls:
- -Expr(number) to build a literal expression
- -Expr(a, op, b) to build a complex expression.
- There a and b will be of type Expr,
- and op will be one of ('+','-', '*', '/').
- """
- def __init__(self, *args):
- if len(args) == 1:
- self.left = self.right = self.op = None
- self.value = args[0]
- else:
- self.left = args[0]
- self.right = args[2]
- self.op = args[1]
- if self.op == '+':
- self.value = self.left.value + self.right.value
- elif self.op == '-':
- self.value = self.left.value - self.right.value
- elif self.op == '*':
- self.value = self.left.value * self.right.value
- elif self.op == '/':
- self.value = self.left.value // self.right.value
- def __str__(self):
- if self.op:
- return "({0}{1}{2})".format(self.left, self.op, self.right)
- else:
- return "{0}".format(self.value)
- #Determines if a solution contains the correct number of dice
- def stringChecker(aString):
- return len([s for s in re.split(r'[\(\)\+\*-/]+|string', aString) if s])
- def SearchTrees(current, target, numbersLength):
- #current is the current set of expressions.
- #target is the target number.
- #numbersLength is the number of dice used
- for a,b in itertools.combinations(current, 2):
- current.remove(a)
- current.remove(b)
- for o in OPS:
- # This checks whether this operation is commutative
- if o == '-' or o == '/':
- conmut = ((a,b), (b,a))
- else:
- conmut = ((a,b),)
- for aa, bb in conmut:
- # You do not specify what to do with the division.
- # I'm assuming that only integer divisions are allowed.
- if o == '/' and (bb.value == 0 or aa.value % bb.value != 0):
- continue
- e = Expr(aa, o, bb)
- # If a solution is found, print it
- if (e.value == target and stringChecker(e.__str__()) == numbersLength):
- print e.value, '=', e
- return 0
- current.add(e)
- # Recursive call!
- SearchTrees(current, target, numbersLength)
- # Do not forget to leave the set as it were before
- current.remove(e)
- # Ditto
- current.add(b)
- current.add(a)
- def main():
- numbers = input('Dice results as an array here w/syntax [a, b, ..., n]> ')
- poolSize = (len(numbers))
- spellLevel = input('Effective spell level> ')
- initial = set(map(Expr, numbers))
- #Effective spell level of 1
- #try except allows script to terminate from recursive function calls gracefully
- #downside: If the user/script screws up, no error message will be given
- if(spellLevel == 1):
- try:
- SearchTrees(initial, 3, poolSize)
- except: pass
- try:
- SearchTrees(initial, 5, poolSize)
- except: pass
- try:
- SearchTrees(initial, 7, poolSize)
- except: pass
- #Effective spell level of 2
- elif(spellLevel == 2):
- try:
- SearchTrees(initial, 11, poolSize)
- except: pass
- try:
- SearchTrees(initial, 13, poolSize)
- except: pass
- try:
- SearchTrees(initial, 17, poolSize)
- except: pass
- #Effective spell level of 3
- elif(spellLevel == 3):
- try:
- SearchTrees(initial, 19, poolSize)
- except: pass
- try:
- SearchTrees(initial, 23, poolSize)
- except: pass
- try:
- SearchTrees(initial, 29, poolSize)
- except: pass
- #Effective spell level of 4
- elif(spellLevel == 4):
- try:
- SearchTrees(initial, 31, poolSize)
- except: pass
- try:
- SearchTrees(initial, 37, poolSize)
- except: pass
- try:
- SearchTrees(initial, 41, poolSize)
- except: pass
- #Effective spell level of 5
- elif(spellLevel == 5):
- try:
- SearchTrees(initial, 43, poolSize)
- except: pass
- try:
- SearchTrees(initial, 47, poolSize)
- except: pass
- try:
- SearchTrees(initial, 53, poolSize)
- except: pass
- #Effective spell level of 6
- elif(spellLevel == 6):
- try:
- SearchTrees(initial, 59, poolSize)
- except: pass
- try:
- SearchTrees(initial, 61, poolSize)
- except: pass
- try:
- SearchTrees(initial, 67, poolSize)
- except: pass
- #Effective spell level of 7
- elif(spellLevel == 7):
- try:
- SearchTrees(initial, 71, poolSize)
- except: pass
- try:
- SearchTrees(initial, 73, poolSize)
- except: pass
- try:
- SearchTrees(initial, 79, poolSize)
- except: pass
- #Effective spell level of 8
- elif(spellLevel == 8):
- try:
- SearchTrees(initial, 83, poolSize)
- except: pass
- try:
- SearchTrees(initial, 89, poolSize)
- except: pass
- try:
- SearchTrees(initial, 97, poolSize)
- except: pass
- #Effective spell level of 9
- elif(spellLevel == 9):
- try:
- SearchTrees(initial, 101, poolSize)
- except: pass
- try:
- SearchTrees(initial, 103, poolSize)
- except: pass
- try:
- SearchTrees(initial, 107, poolSize)
- except: pass
- else:
- print "Effective spell level must be between 1 and 9"
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement