def testSequenceInit(): print(''' Test sequence construction and validation. ==========================================''') shouldBeTrue = [ ([], []), ([1, 2], [0, 1, 0, 1, 1]), ([3, 4, 1, 2], [1, 1, 1, 0, 0 , 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1]) ] shouldBeFalse = [ ([1], []), ([1, 2], [0, 1, 0, 1, 0]), ([2, 4, 1, 2], [1, 1, 1, 0, 0 , 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1]) ] for test in shouldBeTrue: assert PzlUtil.matches(test[0], test[1]) seq = PzlClass.Sequence(test[0], test[1], log = True) assert seq is not None assert seq.spreadGroups() startingState = PzlUtil.stateForGroups(seq.groups, length = seq.length) print(startingState) assert startingState is not None and len(startingState) == seq.length for test in shouldBeFalse: assert not PzlUtil.matches(test[0], test[1]) assert PzlClass.Sequence(test[0], test[1], log = True) is not None return
def runAllStates(seq): maxStep = seq.length ** (seq.last + 1) step = 0 states = [seq.state] while step < maxStep and (step == 0 or seq.stateIndex): step += 1 seq.nextState() states.append(seq.state) PzlUtil.drawSolution('states-{}-{}'.format(seq.last, seq.length), states) return
def setState(self): ''' Set the new state based on the current group structure. Return True if the new state matches the pattern, else False ''' # Default sequence has some bits filled newState = self.initialState.copy() PuzzleUtil.stateForGroups(self.groups, state = newState) self.state = newState if PuzzleUtil.matches(self.pattern, newState): self.printState("Valid State") return True else: self.invalidStates.add(self.stateIndex) return False
def isSolved(self): grid = self.getSolutionGrid() for column in range(self.height): state = [grid[row][column] for row in range(self.height)] pattern = self.patterns['columns'][column] if not PuzzleUtil.matches(pattern, state): return False return True
def solvePuzzle(name): print(""" Solving puzzle: {} ======================= """.format(name)) puzzle = Pzl.getPuzzle(name) if not puzzle: print("Could not solve puzzle -- No puzzle found for name: '{}'".format(name)) soln = puzzle.solve() if soln: print(soln) PzlUtil.drawSolution(name + "-solution", soln) else: print(""" NO SOLUTION """) print(""" ========================== """)