void RandomMazeRunner::solveMaze (Maze *maze,ostream& solutionOutput) { MazeNode *currNode; currNode=maze->getStartMazeNode(); while (!currNode->isExitNode()) { /* for debugging currNode->print(cerr); cerr << "\n"; */ // retrieve (or create) the extra info for this node ExtraNodeInfo *extraInfo; extraInfo=getExtraNodeInfo(currNode); currNode->setVisitationState(MazeNode::VisitInProgress); // pick a next node MazeNode *nextNode; nextNode=pickNextNode(currNode); currNode->setVisitationState(MazeNode::Visited); // and go to that node extraInfo->nextInSolution=nextNode; currNode=nextNode; } // yaay!!! we solved it! // print out our solution solutionOutput << "RANDOM\n"; // print out the type of solution currNode=maze->getStartMazeNode(); while (!currNode->isExitNode()) { // print this node in the solution currNode->print(solutionOutput); solutionOutput << " "; // separate by spaces // get to the next node ExtraNodeInfo *extraInfo; extraInfo=getExtraNodeInfo(currNode); currNode=extraInfo->nextInSolution; } // and the exit node currNode->print(solutionOutput); solutionOutput << " "; // separate by spaces solutionOutput << "\n"; // it's on one line // delete all ExtraNodeInfo's deleteExtraNodeInfo(maze); }
void NearestNeighbourHeuristic::appendNextNode() { m_road.append(pickNextNode()); }
void RandomMazeRunner::solveMaze (Maze *maze,ostream& solutionOutput) { MazeNode *currNode; currNode=maze->getStartMazeNode(); while (!currNode->isExitNode()) { // for debugging // currNode->print(cerr); // cerr << "\n"; // make a note that we have started the visit currNode->setVisitationState(MazeNode::VisitInProgress); // pick a next node MazeNode *nextNode; nextNode=pickNextNode(currNode); // make a note that this node is the parent of the next node (so // that we can later reconstruct the path we followed to the // solution). // // ONLY change the parent if it's not already set, however. // (Otherwise, we can end up with a parent loop!) if (nextNode->getPathParent() == NULL) nextNode->setPathParent(currNode); // make a node that we have completed the visit currNode->setVisitationState(MazeNode::Visited); // and go to the next node currNode=nextNode; } // yaay!!! we solved it! // print out our solution // Use a stack to reverse the order of output // Note that currNode is already the exit node (at the point we left // the loop above). Stack* myStack = new Stack(); while (currNode != maze->getStartMazeNode()) { myStack->push(currNode); currNode = currNode->getPathParent(); } // print out the startnode as part of the solution path currNode->print(solutionOutput); currNode = myStack->pop(); while(!currNode->isExitNode()){ // print this node in the solution currNode->print(solutionOutput); solutionOutput << " "; // separate by spaces // get to the next node currNode = myStack->pop(); } // and the entry node currNode->print(solutionOutput); solutionOutput << "\n"; delete myStack; // cout << " testing creation of mySTack" << endl; // Stack* myStack = new Stack(); // cout << myStack->size() << endl; // cout << "done with myStack"<< endl; }