Question: Write a maze traversal program in C++. To understand what this program is supposed to do, you should run the maze program that's on the

Write a maze traversal program in C++. To understand what this program is supposed to do, you should run the maze

program that's on the homework section of the course website. The program is an executable file. Since

firewalls block executable files, I've saved the file with a .docx extension to fool the firewall into thinking that

it's a Word document. To run the program, save it to your local hard drive or flash drive. Then from within

Windows Explorer, 1) rename the file such that the files extension is .exe, and 2) double click on the file.

Since this is a non-trivial program (five total pages of code for my non-extra-credit program), you are

required to break it up into separate files in the standard manner. Use these files:

mazeGame.h: for named constants and the

MazeGame

class definition

mazeGame.cpp: for member function definitions

mazeGameDriver.cpp: for driving the

MazeGame

class

Use this mazeGameDriver.cpp driver file:

#include "mazeGame.h"

int main()

{

// One extra column is needed for the null character.

char maze[MAZE_ROWS][MAZE_COLS+1] =

{" ---------------------------- ",

"|XXXXXXXXXXXXXXXXXXXXXXXXXXXX|",

"|XXX |",

"|XXX XXXXX XXXXXXXXXXXXXXXXXX|",

"|XXX XXXXXXXXXXXXXXXXXX|",

"|XXX XXXXX XXXXXXXXXXXXXXXXXX|",

"|XXX XXXXX X|",

"|S XXXXXXXXXXXXXXXXXXXXXX X|",

"| XX XXXXXXXXXXXXXXXXXXXXXX X|",

"| XXXXXXXXXXXXX FXXXXXXX X|",

"| XXXXXXXXXXXXXX XXXXXXXXX|",

"|XXX XXXXXXXXXXXXXX XXXXXXXXX|",

"|XXX XXXXXX XXXXXXX|",

"|XXX XXXXXXX XXXXXX XXXXXXX|",

"|XXX XXXXXXX XXXXXXXX XXXXXXX|",

"|XXX XXX XXXXXXXX X|",

"|XXX XXX XXX XXXXXXXX XXXXXXX|",

"|XXX XXX XXXXXXX|",

"|XXXXXXXXXXXXXXXXXXXXXXXXXXXX|",

" ------------------------------------------------- "};

MazeGame mazeGame(maze, 7, 1);

mazeGame.play();

} // end main

The MazeGame

constructor receives three parameters the maze array, the row value for the S cell, and the

column value for the S cell. Note in the above code that the S cell is at row 7 and column 1.

Normally, you should not hardcode special constant values like 7 and 1 in your program. However, I'll allow

it for the

MazeGame

constructor call because I'm saving the more elegant implementation for the extra

credit. On the other hand, don't hardcode any other special constant values in your program. Instead, declare

them using named constants in your header file and use the named constants in your program. For example,

insert these named constant declarations above your

MazeGame

class definition in MazeGame.h:

const int MAZE_ROWS = 20;

const int MAZE_COLS = 30;

Note the characters in the above array declaration. Here's what they mean:

'X'

: an impeding wall

' '

: an opening (the mouse can go through this)

'S'

: starting position for the mouse

'F'

: finish position (the mouse tries to find this)

Represent the mouse with an uppercase O. Before the first mouse move, print the maze and then print the

mouse at its starting position. After each move, print the mouse at the mouse's current position within the

maze. The cell that was previously occupied by the mouse should display its original contents (a ' ' or an 'S').

If your mouse reaches a dead-end, it will have to retrace its steps (backtrack) until it finds a new opening.

In normal console-based programs, you are limited to printing something at the spot just to the right of where

you previously printed something. But for this program, you need to print at specified positions. How can that

be done? Add the following

gotoRowCol

member function to your

MazeGame

class and call it right

before you want to print something at a particular position.

// This member function causes the cursor to jump to the given row and

// column coordinate positions within the console window.

void MazeGame::gotoRowCol(int row, int col)

{

COORD pos;

pos.X=col;

pos.Y=row;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

} // end gotoRowCol

When calling

gotoRowCol

, pass in the row and column coordinate positions on the console window where

you want to start printing. Note that the console window's top left position is specified by row = 0, col = 0.

Since

gotoRowCol

's

COORD

and

SetConsoleCursorPosition

are declared in windows.h, include

the windows.h file in your program.

In order to properly display maze traversals, your program should pause briefly after each mouse move. With

Microsoft C++ compilers, use the

Sleep(x)

function where

x

should be replaced by the number of

milliseconds that are to be delayed. Remember to spell

Sleep

with an uppercase S. The winbase.h header

file contains the prototype for the

Sleep

function. Since the windows.h file, which youll include in your

program, includes winbase.h, you dont have to include winbase.h. explicitly.

When moving the mouse to a new position, your program will need to remember the mouse's current position

(i.e., current row and current column within the array) so that the mouse can backtrack if necessary. To do

this, your program must call a recursive function named

search

that checks the new position and then acts

accordingly. Due to the magic of recursion, the original row and column values are automatically restored to

the calling module when the recursive function call has completed its execution (so you do not need to

explicitly save the old row and old column values in separate variables in order for backtracking to work).

Here are two different strategies for what to do when the

search

function finds the F cell:

Return true and then have the calling module handle the case of the F being found.

Call exit(0).

I prefer the first strategy since it provides more flexibility from the driver module's point of view. But I'll

allow the second strategy as well since it provides a more straightforward solution.

Since the maze array initialization (shown above) does contain a path from 'S' to 'F,' your mouse should be

able to successfully traverse the maze. As soon as your mouse reaches the 'F,' your program should print an

appropriate message of joy. To test your mouse on an impossible maze, add an 'X' character in front of the 'F'

in the maze array initialization code. Then, in traversing the maze, your mouse should exhaust all possible

paths (thus backtracking to the original starting position at cell S), and your program should print an

appropriate message of sorrow. After verifying that your program does this properly, change the maze array

initialization back to its original state.

As always, you must use appropriate member functions and you must declare them with the appropriate

access modifier

public

or

private

.

Turn in two copies of pasted-in output one after running your program with the original array initialization

and one after running your program with the updated array initialization where the path to the 'F' is blocked.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!