Beispiel #1
0
StackLinked<DataType>::StackLinked(const StackLinked& other){
	if(this != other){
		while(!other->isEmpty()){
			this->push(other->pop());
		}
	}
}
Beispiel #2
0
bool Maze::traverse()
{
   //DO THIS
   //complete several sections in this method

   bool done = false; //assume we are not done unless proven otherwise
   StackLinked<Cell> stack;

   maze->setElement(1, 1, TRIED);
   gui->update();

   Cell* start_cell = new Cell(1, 1);
   stack.push(start_cell);  //start from the top left corner

   while(!stack.isEmpty())
   {
      Cell* top_cell = processBackTrack(&stack);
      if (top_cell == NULL) break;  //no solution (back tracked all the way to the beginning)

      //call a method in the Cell class to give you a new Cell in a new direction relative to top_cell (initially, DOWN)
      //DO THIS
      Cell* curr_cell = top_cell -> nextCell();

      //does this new Cell solve the maze?
      done = isSolved(curr_cell, &stack);
      if (done) break;

      //DO THIS
      //get the row and col from curr_cell
      int row = curr_cell -> getRow();
      int col = curr_cell -> getCol();
      double element = maze -> getElement(row, col);

      //check that the current cell corresponds to SPACE, otherwise delete it
      if (element == SPACE)
      {
         //update the cell to TRIED
         //put the cell on the stack (move forward through the maze)
         maze -> setElement(row, col, TRIED);
         stack.push(curr_cell);

         Sleep(75);  //slow down the maze traversal
         gui->update();
      }
      else //look for a different route 
      {
         //DO THIS
         //delete the cell
         delete curr_cell;
      }
   }

   //did we make it to the bottom right?
   if (done)
   {
      processSolution(&stack);
   }
   else
   {
      cout << "No solution." << endl;
   }

   return done;
}