The purpose of this lab is to apply selection statements to complete an application.
You have been asked to design and implement a computer program to navigate through a maze. You have been given an application that is complete except for the module that controls the maze navigation. Your job is to implement that module. Initially, the maze might look like this:
![]() |
The red circle represents "your" position in the maze as you attempt to reach the target (the blue square). You may travel only on the white maze cells; the green cells are off limits.
[Note: the colors may not be distinguishable on black-and-white printouts of this page. Run the application or view this web page on-line to see the colors.]
The application program calls your module to determine what move to make next; the possible moves are:
If you navigate the maze correctly, the end result may look something like this:
![]() |
The yellow squares indicate the path that you took to reach the target. Note that the final path may be different, depending on the method you use to navigate. The actual maze is generated "pseudo-randomly" and depends on the "maze number" value and the behavior of the random-number generator on your computer.
The code you write will be part of a function named DoMove. You will learn more about writing your own functions later in this course. For now, there are only a few things you need to know about functions, in order to complete this project.
The DoMove function can be found in the file maze2.cpp; the version you are given to work with looks like this:
MazeMoveType DoMove (int NumMoves, bool OnTarget,
bool OkRight, bool OkUp, bool OkDown)
// Make the next move, based on the state of the maze.
// Parameters:
// NumMoves - number of moves made so far (zero on first move)
// OnTarget - true if current position is target cell
// OkRight - true if it is legal to move right
// OkUp - true if it is legal to move up
// OkDown - true if it is legal to move down
// Returns:
// MoveNone, if no move should be made (maze complete)
// A move code (MoveRight, MoveUp, or MoveDown),
// if a move should be made
{
// This example code does NOT search the maze correctly!
// It is here so you can see something work until you put the
// correct code here.
// Initialize move to "none", in case we decide the maze
// is complete.
MazeMoveType move = MoveNone;
// If the current position is not the target cell,
// decide what move to make.
if (!OnTarget)
{
if (OkRight)
{
move = MoveRight;
}
else
{
move = MoveDown;
}
}
return move;
}
|
The NumMoves, OnTarget, OkRight, OkUp, and OkDown objects are special; they are called formal parameters. For now, you can treat them as regular data objects that are given values each time the DoMove function is called (each time your program code is supposed to decide how to make the next move in a maze).
The move object in the DoMove communicates your instructions on the move you choose to make. This object is of an enumeration (enum) type, with the following values:
// MazeMoveType enumeration
enum MazeMoveType { MoveNone, MoveRight, MoveUp, MoveDown };
// This enumerated type is used to specify one of the possible moves
// in the maze: no move, right, up, or down.
|
If you have difficulties with any part of the lab, consult the instructor for assistance. The basic sequence is:
The maze program is based on an idea developed by Richard Rasala and others at Northeastern University.
This page was last updated on November 29, 1998; send comments to Mark Sebern.