def ReadPuzzle(filename): puzFile = open(filename, 'r') initialPuzzle = None # Read the header line headerLine = puzFile.readline() # try to split the header line and assure three tokens headerTokens = headerLine.split() if len(headerTokens) == 3: try: # initialie the puzzle return value initialPuzzle = Puzzle() # first number is column count initialPuzzle.numCols = int(headerTokens[0]) # second number is row count initialPuzzle.numRows = int(headerTokens[1]) # final number is wriggler count initialPuzzle.numWrigglers = int(headerTokens[2]) except Exception as e: # If any number parsing fails, just carry on. # We'll return the None puzzle indicating failure print "FAILED to parse puzzle in " + filename print "Exception says " + e.message print "Confirm formatting." return None # Now that we have the number of rows, we can start reading # in puzzle lines currPuzzleLine = 1 for nextLine in puzFile: if currPuzzleLine <= initialPuzzle.numRows: # split the line into tile tokens tiles = nextLine.split() # and append the tiles onto the initial puzzle initialPuzzle.AddLine(tiles) #initialPuzzle.puzzle += tiles else: print "FAILED to parse puzzle in " + filename print "Tried to parse " + str (currPuzzleLine) \ + " but there are only " + str(initialPuzzle.numRows) \ + " according to the header!" del initialPuzzle initialPuzzle = None return initialPuzzle
print str(newSearchNode.state.puzzle) print "====" print str(newSearchNode) if __name__ == "__main__": from Puzzle import Puzzle from Wriggler import Wriggler # Test goal state determination goalWriggler = Wriggler() goalWriggler.head.pos = (1, 2) goalWriggler.tail.idNumber = 0 goalWriggler.tail.pos = (2,2) puzz = Puzzle() puzz.numCols = 3 puzz.numRows = 3 state = State(puzz, [goalWriggler]) agent = Agent(SearchNode(state, None, None, 0)) if not agent.currentSearchNode.ContainsGoalState(): print "FAILED to detect goal state" print "TESTING SEARCH NODE GEN:" TestSearchNodeGen() print "" print "TESTING THE FRONTIER" print "" TestFrontierExpand() print ""