public void act() 
	{	
		//Initial. Stores the locations which can be reached in the first location.
		//Push it into the stack
		if(init==0)
		{
		  last=this.getLocation();
		  next = last;
		  crossLocation.push(getValid(last));
		  init=1;
		}
		
		if (isEnd == true) 
		{
		//to show step count when reach the goal		
			if (hasShown == false) 
			{
				String msg = stepCount.toString() + " steps";
				JOptionPane.showMessageDialog(null, msg);
				hasShown = true;
			}
		} 
		else  
		{
			if(!crossLocation.isEmpty())
			{	
				//peak's size is greater than 0, needs to move forward from one of the locations.
			    if(crossLocation.peek().size()!=0)
				 {
			    	 
	  				 for(Location loc: crossLocation.peek())
	  				 {
	  					 Actor redrock=getGrid().get(loc);
	  					 //Find the end of the maze.
	    		         if(redrock instanceof Rock && redrock.getColor().getRGB()==Color.RED.getRGB())
	    		         {
	    		        	isEnd=true; 
	    		         	return;
	    		         }  
	  				 }
			     
	  				 //random choose a direction to move.
					 double r = Math.random()*crossLocation.peek().size();
		             next=crossLocation.peek().get((int)Math.floor(r));
		             
		             //After a success move, top of stack directionlist sotres the
		             //direction from the last location to local location.
		          	 directionlist.push(this.getDirection());
		   		     last=getLocation();
		   	         
		   		     //After a success move, top of the stack locationlist stores the
		   		     //last location it just been on.
		   		     locationlist.push(last);

		   	         
		           	 if(canMove())
		           	 {
		       	         move();
				         stepCount++;      		 
		   		         crossLocation.push(getValid(next));
		   		         last=next;
		   		         return;
		             }
                 }
				 
			    //peak's size is 0, need to fall back.
				else
	            {
					
					Location putflower=getLocation();			
		        	this.moveTo(locationlist.pop()); 	   
	           		this.setDirection(directionlist.pop());
	           		Flower flower = new Flower(getColor());
	           		flower.putSelfInGrid(getGrid(), putflower);
	
	           		//a two-time pop and a push means to pop the top 
	           		//and renew the element below the top
		           	crossLocation.pop();
		           	crossLocation.pop();
		           	crossLocation.push(getValid(getLocation()));
		           	    
		            return;
	            }
	           
            
			} 
		}
	}