Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is a prompt for a C++ program and what you have to do: Here is the given code with it: #include #include #include #include

Here is a prompt for a C++ program and what you have to do:

image text in transcribed

Here is the given code with it:

#include

#include

#include

#include

using namespace std;

#define BYTE unsigned char

// wall and visited flag values

const BYTE CELL_TOP = 1;

const BYTE CELL_BOTTOM = 2;

const BYTE CELL_LEFT = 4;

const BYTE CELL_RIGHT = 8;

const BYTE CELL_VISITED = 16;

// maze dimensions

const int MAZE_HEIGHT = 6;

const int MAZE_WIDTH = 7;

//starting cell

const int START_CELL_HEIGHT = 0;

const int START_CELL_WIDTH = 0;

const int START_WALL = CELL_LEFT;

//ending cell

const int END_CELL_HEIGHT = 2;

const int END_CELL_WIDTH = 2;

const int END_WALL = CELL_RIGHT;

// location indexs'

const int CELL_HEIGHT_INDEX = 0;

const int CELL_WIDTH_INDEX = 1;

// maze print characters

const char OUT_TOP_LEFT = '-';

const char OUT_TOP_MID = '-';

const char OUT_TOP_RIGHT = '-';

const char OUT_SIDE_LEFT = '|';

const char OUT_SIDE_RIGHT = '|';

const char OUT_BOT_LEFT = '-';

const char OUT_BOT_MID = '-';

const char OUT_BOT_RIGHT = '-';

const char INSIDE_MIDDLE = '+';

const char CELL_TOP_BOT = '-';

const char CELL_LEFT_RIGHT = '|';

const char CELL_OPEN_HORZ = ' ';

const char CELL_OPEN_VERT = ' ';

const char CELL_VISITED_ON = '.';

const char CELL_VISITED_OFF = ' ';

// menu definition

enum MENU { EXIT = 0, GENERATE_MAZE = 1, MOUSE_NAVIGATION = 2 };

// function declerations

bool hasUnvisitedCells(BYTE cells[][MAZE_WIDTH]);

bool hasAvailableNeighbors(BYTE cells[][MAZE_WIDTH], int location[]);

void chooseRandomNeighbor(BYTE cells[][MAZE_WIDTH], int current[], int neighbor[]);

void removeWalls(BYTE cells[][MAZE_WIDTH], int current[], int neighbor[]);

void initMaze(BYTE cells[][MAZE_WIDTH]);

int pushStack(int stack [][2], int location[], int stackPoint);

void popStack(int stack[][2], int location[], int& stackPoint);

void copyOneLocTwo(int locOne[], int locTwo[]);

void printMaze(BYTE cells[][MAZE_WIDTH]);

void printMazeDebug(BYTE cells[][MAZE_WIDTH]);

int main() {

MENU choice;

do {

cout

";

int ask;

cin >> ask;

choice = MENU(ask);

// storage for the maze cells

BYTE maze[MAZE_HEIGHT][MAZE_WIDTH] = { 0 };

// storage for our stack of visited cells

int stack[MAZE_HEIGHT * MAZE_WIDTH][2] = { 0 };

int stackPointer = -1; // empty stack value

initMaze(maze);

//set starting cell

int startCell[2];

startCell[CELL_HEIGHT_INDEX] = START_CELL_HEIGHT;

startCell[CELL_WIDTH_INDEX] = START_CELL_WIDTH;

switch (choice) {

case GENERATE_MAZE:

// init random generator

srand(time(NULL)); // requires cstdlib; time.h

// turn off starting wall

maze[startCell[CELL_HEIGHT_INDEX]][startCell[CELL_WIDTH_INDEX]] ^= START_WALL;

// turn off ending wall

maze[END_CELL_HEIGHT][END_CELL_WIDTH] ^= END_WALL;

// print maze

printMaze(maze);

cout

printMazeDebug(maze);

cout

// pause program

char tmp;

cin >> tmp;

// 1. Make the initial cell the current cell and mark it as visited

int currentCell[2];

copyOneLocTwo(startCell, currentCell);

// mark visited flag

maze[currentCell[CELL_HEIGHT_INDEX]][currentCell[CELL_WIDTH_INDEX]] ^= CELL_VISITED;

// 2. While there are unvisited cells

while (hasUnvisitedCells(maze)) {

// i. if the current cell has any neighbors which have not been visited

if (hasAvailableNeighbors(maze, currentCell)) {

// 1. Choose randomly one of the unvisited neighbors

int neighborCell[2] = { 0 };

chooseRandomNeighbor(maze, currentCell, neighborCell);

// 2. Push the current cell to the stack

stackPointer = pushStack(stack, currentCell, stackPointer);

// 3. Remove the wall between current cell and the chosen cell

removeWalls(maze, currentCell, neighborCell);

// 4. Make the chosen cell the current cell and mark it as visited

copyOneLocTwo(neighborCell, currentCell);

// mark visited flag

maze[currentCell[CELL_HEIGHT_INDEX]][currentCell[CELL_WIDTH_INDEX]] ^= CELL_VISITED;

// 5. Print the maze

printMaze(maze);

cout

printMazeDebug(maze);

cout

} // end has any neighbors

// ii. Else if the stack is not empty

else if (stackPointer > -1) {

// 1. pop last cell from the stack into current

popStack(stack, currentCell, stackPointer);

}

} // end unvisited cells

// 3. Print the maze

printMaze(maze);

cout

printMazeDebug(maze);

cout

break;

case MOUSE_NAVIGATION:

break;

case EXIT:

break;

default:

cout

}

cout

} // ends main

while (choice != EXIT);

return 0;

}

/**

* Check to see if the maze has any unvisited cells

* @param isVisited - boolean to see if a cell is visited

* @param height - set height location to 0

* @return bool - is the cell visited

*/

bool hasUnvisitedCells(BYTE cells[][MAZE_WIDTH]) {

bool isVisited = true;

int height = 0;

while (isVisited && height

int width = 0;

while (isVisited && width

isVisited = cells[height][width] & CELL_VISITED;

width++;

}

height++;

}

return !isVisited;

} // end hasUnvisited

/**

* Check to see if the current cell has any neighbors

* @param cells - The grid of cells in the maze

* @param location - location in the maze

* @param isAvailable - boolean to see if the cell has neighbors

* @return bool - does the cell have neighbors

*/

bool hasAvailableNeighbors(BYTE cells[][MAZE_WIDTH], int location[]) {

bool isAvailable = true;

// check if has neighbor above

if (location[CELL_HEIGHT_INDEX] > 0) {

// check if not visited

if (!(cells[location[CELL_HEIGHT_INDEX] - 1][location[CELL_WIDTH_INDEX]] & CELL_VISITED)) {

return true;

}

}

// check if has neighbor below

if (location[CELL_HEIGHT_INDEX]

// check if not visited

if (!(cells[location[CELL_HEIGHT_INDEX] + 1][location[CELL_WIDTH_INDEX]] & CELL_VISITED)) {

return true;

}

}

// check if has left neighbor

if (location[CELL_WIDTH_INDEX] > 0) {

// check if not visited

if (!(cells[location[CELL_HEIGHT_INDEX]][location[CELL_WIDTH_INDEX] - 1] & CELL_VISITED)) {

return true;

}

}

// check if has right neighbor

if (location[CELL_WIDTH_INDEX]

// check if not visited

if (!(cells[location[CELL_HEIGHT_INDEX]][location[CELL_WIDTH_INDEX] + 1] & CELL_VISITED)) {

return true;

}

}

return false;

} // end hasAvailableNeighbors

/**

* Pick a random neighboring cell that is unvisited

* @param cells - the grid of cells in the maze

* @param current - current cell location

* @param neighbor - neighbor of the current cell

* @param done - boolean to see if a neighbor is empty and choose it

*/

void chooseRandomNeighbor(BYTE cells[][MAZE_WIDTH], int current[], int neighbor[]) {

bool done = false;

while (!done) {

int randNeighbor = rand() % 4; // random 0 through 3

switch (randNeighbor) {

case 0: // TOP

if (current[CELL_HEIGHT_INDEX] > 0) {

if (!(cells[current[CELL_HEIGHT_INDEX] - 1][current[CELL_WIDTH_INDEX]] & CELL_VISITED)) {

neighbor[CELL_HEIGHT_INDEX] = current[CELL_HEIGHT_INDEX] - 1;

neighbor[CELL_WIDTH_INDEX] = current[CELL_WIDTH_INDEX];

done = true;

}

}

break;

case 1: // BOTTOM

if (current[CELL_HEIGHT_INDEX]

if (!(cells[current[CELL_HEIGHT_INDEX] + 1][current[CELL_WIDTH_INDEX]] & CELL_VISITED)) {

neighbor[CELL_HEIGHT_INDEX] = current[CELL_HEIGHT_INDEX] + 1;

neighbor[CELL_WIDTH_INDEX] = current[CELL_WIDTH_INDEX];

done = true;

}

}

break;

case 2: // LEFT

if (current[CELL_WIDTH_INDEX] > 0) {

if (!(cells[current[CELL_HEIGHT_INDEX]][current[CELL_WIDTH_INDEX] - 1] & CELL_VISITED)) {

neighbor[CELL_HEIGHT_INDEX] = current[CELL_HEIGHT_INDEX];

neighbor[CELL_WIDTH_INDEX] = current[CELL_WIDTH_INDEX] - 1;

done = true;

}

}

break;

case 3: // RIGHT

if (current[CELL_WIDTH_INDEX]

if (!(cells[current[CELL_HEIGHT_INDEX]][current[CELL_WIDTH_INDEX] + 1] & CELL_VISITED)) {

neighbor[CELL_HEIGHT_INDEX] = current[CELL_HEIGHT_INDEX];

neighbor[CELL_WIDTH_INDEX] = current[CELL_WIDTH_INDEX] + 1;

done = true;

}

}

break;

} // end switch

}

} // ends chooseRandomNeighbor

/**

* 3. Remove the wall between the current cell and the chose cell

* @param cells - the grid of cells in the maze

* @param current - current location of the cell

* @param neighbor - neighbors of the current cell

*/

void removeWalls(BYTE cells[][MAZE_WIDTH], int current[], int neighbor[]) {

// test for location of current and neighbor

// test for neighbor above

if (neighbor[CELL_HEIGHT_INDEX]

cells[neighbor[CELL_HEIGHT_INDEX]][neighbor[CELL_WIDTH_INDEX]] ^= CELL_BOTTOM; // toggle cell bottom off

cells[current[CELL_HEIGHT_INDEX]][current[CELL_WIDTH_INDEX]] ^= CELL_TOP; // toggle wall off

}

// test for neighbor below

else if (neighbor[CELL_HEIGHT_INDEX] > current[CELL_HEIGHT_INDEX]) {

cells[neighbor[CELL_HEIGHT_INDEX]][neighbor[CELL_WIDTH_INDEX]] ^= CELL_TOP; // toggle wall off

cells[current[CELL_HEIGHT_INDEX]][current[CELL_WIDTH_INDEX]] ^= CELL_BOTTOM; // toggle cell bottom off

}

// test for neighbor left

else if (neighbor[CELL_WIDTH_INDEX]

cells[neighbor[CELL_HEIGHT_INDEX]][neighbor[CELL_WIDTH_INDEX]] ^= CELL_RIGHT; // toggle wall off

cells[current[CELL_HEIGHT_INDEX]][current[CELL_WIDTH_INDEX]] ^= CELL_LEFT; // toggle cell wall off

}

// neighbor must be right

else {

cells[neighbor[CELL_HEIGHT_INDEX]][neighbor[CELL_WIDTH_INDEX]] ^= CELL_LEFT; // toggle wall off

cells[current[CELL_HEIGHT_INDEX]][current[CELL_WIDTH_INDEX]] ^= CELL_RIGHT; // toggle cell wall off

}

} // end removeWalls

/**

* Initialize maze array elements to turn all

* walls on and visited off

* @param cells - the grid of cells in the maze

*/

void initMaze(BYTE cells[][MAZE_WIDTH]) {

for (int height = 0; height

for (int width = 0; width

cells[height][width] = CELL_TOP | CELL_BOTTOM | CELL_LEFT | CELL_RIGHT;

}

}

} // end initMaze

/**

* Adds a cell location to the stack

* @param stack - the location stack

* @param location - height & width of a maze cell

* @param stackPoint - last location added to stack

* @return int - new stack pointer

*/

int pushStack(int stack[][2], int location[], int stackPoint) {

int spNew = stackPoint;

spNew++; // move sp up the stack

stack[spNew][CELL_HEIGHT_INDEX] = location[CELL_HEIGHT_INDEX];

stack[spNew][CELL_WIDTH_INDEX] = location[CELL_WIDTH_INDEX];

return spNew;

}

/**

* Pull a cell location to the stack

* @param stack - the location stack

* @param location - height & width of a maze cell

* @param stackPoint IN/OUT - last location added to stack and return new

*/

void popStack(int stack[][2], int location[], int& stackPoint) {

location[CELL_HEIGHT_INDEX] = stack[stackPoint][CELL_HEIGHT_INDEX];

location[CELL_WIDTH_INDEX] = stack[stackPoint][CELL_WIDTH_INDEX];

stackPoint--;

}

/**

*/

void copyOneLocTwo(int locOne[], int locTwo[]) {

locTwo[CELL_HEIGHT_INDEX] = locOne[CELL_HEIGHT_INDEX];

locTwo[CELL_WIDTH_INDEX] = locOne[CELL_WIDTH_INDEX];

}

/**

* Printing maze to console

* @param cells - the grid of cells in the maze

*/

void printMaze(BYTE cells[][MAZE_WIDTH]) {

// print the top row of the cells

for (int height = 0; height

// print top row of cells

for (int width = 0; width

/*** print left spacer ***/

// are we on the top row

if (height == 0) {

// are we on the left wall

if (width == 0) {

cout

}

else {

cout

}

}

else { // not on top row

// are we on the left wall

if (width == 0) {

cout

}

else {

cout

}

}// print top left spacer

/*** print cell top ***/

if (cells[height][width] & CELL_TOP) {

cout

}

else {

cout

}

// print last right spacer

if (width == MAZE_WIDTH - 1) {

// top row

if (height == 0) {

cout

}

else { // not top row

cout

}

}

} //print top row

// move down to start printing side walls

cout

// print side walls of the cells

for (int width = 0; width

// prints cell left side wall

if (cells[height][width] & CELL_LEFT) {

cout

}

else {

cout

}

// prints cell visited

if (cells[height][width] & CELL_VISITED) {

cout

}

else {

cout

}

// print right wall

if (width == MAZE_WIDTH - 1) {

// prints cell right side wall

if (cells[height][width] & CELL_RIGHT) {

cout

}

else {

cout

}

}

} // print side walls

// move down to start printing next top walls

cout

// print bottom row

if (height == MAZE_HEIGHT - 1) {

// print the bottom row of the cell walls

for (int width = 0; width

// print spacer

if (width == 0) {

cout

}

else {

cout

}

// print cell bottom wall

if (cells[height][width] & CELL_BOTTOM) {

cout

}

else {

cout

}

// print right corner

if (width == MAZE_WIDTH - 1) {

cout

}

} // bottom walls

// end the maze

cout

} // bottom row

} // ends height loop

}

/**

* print the maze

* @param cells - the grid of cells in the maze

*/

void printMazeDebug(BYTE cells[][MAZE_WIDTH]) {

for (int height = 0; height

for (int width = 0; width

cout

}

cout

}

}

You will implement a Maze Generator using the code we have developed in class adding functionality to show a mouse navigating the maze after it is generated You will implement a simple menu in your main method with an Enum that ask the user to first generate a maze, and then watch a mouse navigate the maze. The user will select 1 to generate a new maze, 2 to watch a mouse navigate the curent maze, or 0 to exit. elcome to the random maze generator elect from the following menu 1-Generate a new random maze 2 Watch a mouse navigate the maze -exit The mouse should be depicted as an Asterix in each cell that it navigates to, and your program must have the ability to either pause after each print of the maze or print each step to a console or file (we will cover writing to external files in class) The Mouse must start at the starting cell and is finished as soon as it reaches the designated ending cell The mouse does not need to visit every cell of the maze The program must be configurable for the number of rows and columns, and the location of the starting and ending cells, along with what wall is open for each. Only valid values will be used for these You may use the multi-dimensional array implementation of the maze that we have done already in class, or you may use the user dinedclasses version we will be working on over the next two class periods and hybrid labs The maze initialization and generation code must be moved out of the main() method into local functions to be called from main(). You must create a function to initiate running the mouse through the maze to be called from main(), and you may need more than one function to make the logic efficient and modular Hints: You will need to select a flag value to use in the maze cell flags to represent that the mouse is currently in that cell. You will need to be able to turn this flag on and off as the mouse transverses the maze You will need to add logic to the printing routine to show the mouse character for the appropriate cell The user may select to run the mouse through the same maze once a maze is generated or generate a new maze and have the mouse run through that. Your menu must allow the user to run multiple iterations of the program without having to exit and rerun. You will implement a Maze Generator using the code we have developed in class adding functionality to show a mouse navigating the maze after it is generated You will implement a simple menu in your main method with an Enum that ask the user to first generate a maze, and then watch a mouse navigate the maze. The user will select 1 to generate a new maze, 2 to watch a mouse navigate the curent maze, or 0 to exit. elcome to the random maze generator elect from the following menu 1-Generate a new random maze 2 Watch a mouse navigate the maze -exit The mouse should be depicted as an Asterix in each cell that it navigates to, and your program must have the ability to either pause after each print of the maze or print each step to a console or file (we will cover writing to external files in class) The Mouse must start at the starting cell and is finished as soon as it reaches the designated ending cell The mouse does not need to visit every cell of the maze The program must be configurable for the number of rows and columns, and the location of the starting and ending cells, along with what wall is open for each. Only valid values will be used for these You may use the multi-dimensional array implementation of the maze that we have done already in class, or you may use the user dinedclasses version we will be working on over the next two class periods and hybrid labs The maze initialization and generation code must be moved out of the main() method into local functions to be called from main(). You must create a function to initiate running the mouse through the maze to be called from main(), and you may need more than one function to make the logic efficient and modular Hints: You will need to select a flag value to use in the maze cell flags to represent that the mouse is currently in that cell. You will need to be able to turn this flag on and off as the mouse transverses the maze You will need to add logic to the printing routine to show the mouse character for the appropriate cell The user may select to run the mouse through the same maze once a maze is generated or generate a new maze and have the mouse run through that. Your menu must allow the user to run multiple iterations of the program without having to exit and rerun

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Differentiate 3sin(9x+2x)

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago