import Menu import BoardSetup import BoardScreen import sys, pickle sys.path.append('../model') from Predicate import * from SpeciesDatabase import * from LocalWorld import LocalWorld def menuTest(): Menu.run() def screenTest(): BoardScreen.run(10, 10, None, "White", None, None) def setupTest(): BoardSetup.run() def test(): #from LocalWorld import LocalWorld import Direction from Species import Species from Instruction import Instruction from ConflictResolutionPolicy import ConflictResolutionPolicy from Rule import Rule #make species dead = Species("dead","grey") squirrel = Species("squirrel","red") bear = Species("bear","blue") sam = Species("sam","purple") ravi = Species("ravi","yellow") tree = Species("tree", "green") #make world testWorld = LocalWorld(dead, 6,6) #make predicate testPred = DirectionalTestPredicate(dead,Direction.W) #make instruction testInstruc1 = Instruction(Direction.E,squirrel) testInstruc2 = Instruction(Direction.C,dead) testInstrucList = [testInstruc1, testInstruc2] #make and add rule ##if dead to left, then move left testRule = Rule(testPred,testInstrucList) squirrel.addRule(testRule,0) #make strength dictionary testWorld.conflictResolver.addPolicy(squirrel,dead,1) #add species testWorld.setSpecies(4,2,squirrel) sd = SpeciesDatabase.getInstance() sd.addSpecies(squirrel.kind, squirrel) sd.addSpecies(dead.kind, dead) sd.addSpecies(bear.kind, bear) sd.addSpecies(sam.kind, sam) sd.addSpecies(ravi.kind, ravi) sd.addSpecies(tree.kind, tree) BoardSetup.run(sd=sd) def test2(debug=None, threebug = None): '''threebug is a function that test2 will execute.''' #from LocalWorld import LocalWorld import Direction from Species import Species from Instruction import Instruction from ConflictResolutionPolicy import ConflictResolutionPolicy from Rule import Rule #make species ConwaySpec = Species("Conway", "blue") DeadSpec = Species("Dead","grey") #make predicates two = QuantityPredicate(ConwaySpec, 2) three = QuantityPredicate(ConwaySpec, 3) comeAlive1 = QuantityPredicate(ConwaySpec, 2) comeAlive2 = QuantityPredicate(ConwaySpec, 3) comeAlive = OrPredicate([comeAlive1,comeAlive2]) becomeDead = NotPredicate(comeAlive) #make instructions live = Instruction(Direction.C,ConwaySpec) die = Instruction(Direction.C,DeadSpec) comeToLife = Rule(three,[live]) liveRule = Rule(OrPredicate([two,three]), [live]) dieRule = Rule(NotPredicate(OrPredicate([two, three])), [die]) #make rules ConwaySpec.addRule(liveRule, 1) ConwaySpec.addRule(dieRule, 0) DeadSpec.addRule(comeToLife, 1) sd = SpeciesDatabase.getInstance() sd.addSpecies(DeadSpec) sd.addSpecies(ConwaySpec) if debug: print("DEBUG ON: Try to make a pickle of a species") print(sd.getSpecies("Conway")) s = sd.getSpecies("Conway") pickle.dumps(s.kind) pickle.dumps(s.color) pickle.dumps(s.id_count) #pickle.dumps(s.rules) rule = s.rules[0] pickle.dumps(rule.predicate) pickle.dumps(rule.instructions) pickle.dumps(s.alive) # sd.addSpecies(ConwaySpec, 1, [DeadSpec]) if threebug == None: Menu.run() else: threebug() def samr(): """SamR's test. Intended to play with counting of live neighbors, and other such fun.""" #from LocalWorld import LocalWorld import Direction from Species import Species from Instruction import Instruction from ConflictResolutionPolicy import ConflictResolutionPolicy from Rule import Rule #make species Conway0 = Species("Conway0", "purple") Conway1 = Species("Conway1", "green") Conway2 = Species("Conway2", "blue") ConwayDead = Species("Conway Dead","grey") JustPlainDead = Species("Just Plain Dead", "black") SamR = Species("SamR", "white") #make predicates two = LiveNeighbors(2, int.__eq__) three = LiveNeighbors(3, int.__eq__) #make instructions con0 = Instruction(Direction.C, Conway0) con1 = Instruction(Direction.C, Conway1) con2 = Instruction(Direction.C, Conway2) die = Instruction(Direction.C, ConwayDead) # Make Rules ConwayDead.addRule(Rule(three, [con0]), 0) Conway0.addRule(Rule(two, [con1]), 0) Conway0.addRule(Rule(three, [con2]), 1) Conway0.addRule(Rule(TruePredicate(), [die]), 2) Conway1.addRule(Rule(two, [con1]), 0) Conway1.addRule(Rule(three, [con2]), 1) Conway1.addRule(Rule(TruePredicate(), [die]), 2) Conway2.addRule(Rule(two, [con1]), 0) Conway2.addRule(Rule(three, [con2]), 1) Conway2.addRule(Rule(TruePredicate(), [die]), 2) SamR.addRule(Rule(LiveNeighbors(2, int.__ge__), \ [Instruction(Direction.C, ConwayDead), \ Instruction(Direction.E, SamR)]), \ 0) SamR.addRule(Rule(LiveNeighbors(5, int.__ge__), \ [Instruction(Direction.C, ConwayDead), \ Instruction(Direction.W, SamR)]), \ 1) JustPlainDead.addRule(Rule(LiveNeighbors(0, int.__ge__), \ [Instruction(Direction.C, ConwayDead)]), \ 0) # Build the database sd = SpeciesDatabase.getInstance() sd.addSpecies(JustPlainDead,0) sd.addSpecies(ConwayDead) sd.addSpecies(Conway0) sd.addSpecies(Conway1) sd.addSpecies(Conway2) sd.addSpecies(SamR,1) # And go! Menu.run() def test3(): '''tests loading from a database''' '''test currently fails. Suspected problem is that when Predicates are loaded, the species contained in the predicate is a name, not a species object. Can someone who has worked on database confirm?''' from LocalWorld import LocalWorld import Direction from Species import Species from Instruction import Instruction from ConflictResolutionPolicy import ConflictResolutionPolicy from Rule import Rule sd = SpeciesDatabase.getInstance() sd.loadFile('../model/conway-rule') print(sd.allSpeciesNames()) BoardSetup.run(sd=sd) def explode(): from LocalWorld import LocalWorld import Direction from Species import Species from Instruction import Instruction from ConflictResolutionPolicy import ConflictResolutionPolicy from Rule import Rule #make species ExplodingSpec = Species("Explode", "blue") DeadSpec = Species("Dead","grey") #make predicates me = DirectionalTestPredicate(ExplodingSpec, Direction.C) #make instructions live1 = Instruction(Direction.C,ExplodingSpec) live2 = Instruction(Direction.W,ExplodingSpec) live3 = Instruction(Direction.NW,ExplodingSpec) live4 = Instruction(Direction.SW,ExplodingSpec) live5 = Instruction(Direction.N,ExplodingSpec) live6 = Instruction(Direction.S,ExplodingSpec) live7 = Instruction(Direction.SE,ExplodingSpec) live8 = Instruction(Direction.E,ExplodingSpec) live9 = Instruction(Direction.NE,ExplodingSpec) live=[live1,live2,live3,live4,live5,live6,live7,live8,live9] #make rules liveRule = Rule(me,live) ExplodingSpec.addRule(liveRule, 0) sd = SpeciesDatabase.getInstance() sd.addSpecies(DeadSpec) sd.addSpecies(ExplodingSpec, 1, [DeadSpec]) Menu.run()