Esempio n. 1
0
/*----- S o l v e M a z e (  ) ------------------------------

PURPOSE
Attempt to find the shortest path through the maze.

INPUT PARAMETERS
maze           -- the maze object to be traversed
positionQueue  -- the queue of current and future positions

RETURN VALUE
true  -- a path was found.
false -- failed to find a path.
-------------------------------------------------------------*/
bool SolveMaze(Maze &maze, Queue &positionQueue)
{
	maze.Mark(maze.Start(), 0);					// Mark the maze start with distance 0
	positionQueue.Enqueue(maze.Start());		// Add maze start to queue
	CellState distance = 0;						// cell distance from start

	while (!positionQueue.Empty())
	{
		while (((maze.State(positionQueue.Head() + StepEast)) == Open)			// While head position has any unmarked neighbors
			|| ((maze.State(positionQueue.Head() + StepSouth)) == Open) 
			|| ((maze.State(positionQueue.Head() + StepWest)) == Open) 
			|| ((maze.State(positionQueue.Head() + StepNorth)) == Open)) 	
		{
			distance = maze.State(positionQueue.Head());										// Set distance

			if ((maze.State(positionQueue.Head() + StepEast)) == Open)							// Is east cell open?
			{
				maze.Mark(positionQueue.Head() + StepEast, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepEast) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepEast);							// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepSouth)) == Open)					// Is south cell open?
			{
				maze.Mark(positionQueue.Head() + StepSouth, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepSouth) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepSouth);						// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepWest)) == Open)						// Is West cell open?
			{
				maze.Mark(positionQueue.Head() + StepWest, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepWest) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepWest);							// Add it to the queue
			}
			else if ((maze.State(positionQueue.Head() + StepNorth)) == Open)					// Is North cell open?
			{
				maze.Mark(positionQueue.Head() + StepNorth, distance + 1);						// Mark cell with proper distance
				if ((positionQueue.Head() + StepNorth) == maze.Goal())							// Is open cell the goal?
					return true;
				
				positionQueue.Enqueue(positionQueue.Head() + StepNorth);						// Add it to the queue
			}
		}
		positionQueue.Dequeue();
	}
	return false;
}
Esempio n. 2
0
/*----- S o l v e M a z e (  ) -----

PURPOSE
Attempt to find the shortest path through the maze.

INPUT PARAMETERS
maze           -- the maze object to be traversed
positionQueue  -- the queue of current and future positions

RETURN VALUE
true  -- a path was found.
false -- failed to find a path.
*/
bool SolveMaze(Maze &maze, Queue &positionQueue)
{
/*
const int Open     = -1;	// Cell is open
const int Obstacle = -2;	// Cell is an obstacle
const int StartCell= -3;	// Cell is the start cell
const int GoalCell = -4;	// Cell is the goal cell
const int PathCell = -5;	// Cell is on the shortest path
*/ 
	Position curPos = maze.Start();
	Position neighbor;
	positionQueue.Enqueue(curPos);
	maze.Mark(curPos, 0);
	int distance;

	while(!positionQueue.Empty()){
		curPos = positionQueue.Dequeue();
		distance = maze.State(curPos);
		neighbor = openPosition(maze, curPos);
		while(curPos != neighbor){
			maze.Mark(neighbor, distance + 1);
			if(neighbor == maze.Goal())
				return true;
			positionQueue.Enqueue(neighbor);
			neighbor = openPosition(maze, curPos);
		}
	}
	return false;
}
Esempio n. 3
0
/*----- R e t r a c e (  ) ----------------------------------

PURPOSE
Mark the path from the goal to the start cell.

INPUT PARAMETERS
maze           -- the maze object to be marked
-------------------------------------------------------------*/
void Retrace(Maze &maze)
{
 
	Position curPos = maze.Goal();
	int distance = maze.State(maze.Goal());		// Distance from start cell

	while (distance >= 0)
	{
		maze.Mark(curPos, PathCell);
		if ((maze.State(curPos + StepNorth)) == (distance - 1))
			curPos = curPos + StepNorth;
		
		else if ((maze.State(curPos + StepWest)) == (distance - 1))
			curPos = curPos + StepWest;
		
		else if ((maze.State(curPos + StepSouth)) == (distance - 1))
			curPos = curPos + StepSouth;
		
		else if ((maze.State(curPos + StepEast)) == (distance - 1))
			curPos = curPos + StepEast;

		distance -= 1;
	}
}
Esempio n. 4
0
/*----- R e t r a c e P a t h (  ) -----

PURPOSE
Mark the path from the goal to the start cell.

INPUT PARAMETERS
maze           -- the maze object to be marked
*/
void RetracePath(Maze &maze)
{
	Position curPos = maze.Goal();  //This function is used on complete mazes.
	Position neighbor;
	int distance;

	do{
		distance = maze.State(curPos);
		maze.Mark(curPos, PathCell);
		do{
			neighbor = pathCheck(maze, curPos,distance);
			if (maze.State(neighbor) < distance || distance == 0){
				curPos = neighbor;
				break;
			}		
		}while(curPos != neighbor);
	}while(distance > 0);
}