Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following maze: The maze begins at S and ends at E. We represent th

ID: 3759036 • Letter: C

Question

Consider the following maze: The maze begins at S and ends at E. We represent the current position in the maze using two integer variables posX and posY. The starting location (S) is thus represented as posX = 0 and posY = 0. Movement through the maze is defined by the following rules: 1. MoveRight(spaces): posX = posX + spaces 2. MoveLeft(spaces): posX = posX - spaces 3. MoveDown(spaces): posY = posY + spaces 4. MoveUp(spaces): posY = posY - spaces Define functions for each of the above rules such that each function has an integer reference parameter. Use these functions declarations to write a C++ program that defines posX and posY, initialized to 0, and the corresponding calls to your functions that will correctly move your position in the maze from S to E ending at posX = 0, posY = 11. Print out the value of posX and posY after each function call.

Explanation / Answer

Hope this will serve your need. If you have any further queries, just get back to me.

#include <iostream>
using namespace std;
void MoveRight(char Maze[12][16], int posX, int &posY, int spaces)
{  
for(int i = 0; i <= spaces; i++)
if(Maze[posX][posY+i] == 'X')
{
printf("You hit a wall at position %i x %i, and you are dead... ", posX+i, posY);
exit(0);
}
for(int i = 0; i <= spaces; i++)
Maze[posX][posY+i] = 'S';
posY += spaces;
}
void MoveLeft(char Maze[12][16], int posX, int &posY, int spaces)
{
for(int i = 0; i <= spaces; i++)
if(Maze[posX][posY-i] == 'X')
{
printf("You hit a wall, and you are dead... ");
exit(0);
}
for(int i = 0; i <= spaces; i++)
Maze[posX][posY-i] = 'S';
posY -= spaces;
}
void MoveDown(char Maze[12][16], int &posX, int posY, int spaces)
{
for(int i = 0; i <= spaces; i++)
if(Maze[posX+i][posY] == 'X')
{
printf("You hit a wall, and you are dead... ");
exit(0);
}
for(int i = 0; i <= spaces; i++)
Maze[posX+i][posY] = 'S';   
posX += spaces;  
}
void MoveUp(char Maze[12][16], int &posX, int posY, int spaces)
{
for(int i = 0; i <= spaces; i++)
if(Maze[posX-i][posY] == 'X')
{
printf("You hit a wall, and you are dead... ");
exit(0);
}
for(int i = 0; i <= spaces; i++)
Maze[posX-i][posY] = 'S';
posX -= spaces;  
}

void PrintMazeState(char Maze[12][16])
{
printf(" MazeState: ");for(int k = 0; k < 16; k++)
printf("- ");
printf(" ");
for(int i = 0; i < 12; i++)
{
printf("|");
for(int j = 0; j < 16; j++)
printf("%c|",Maze[i][j]);
printf(" ");
for(int k = 0; k < 16; k++)
printf("- ");
printf(" ");
}
}
int main()
{
int posX = 0, posY = 0, spaces;
char Maze[12][16] = {{'S',' ',' ',' ',' ','X',' ',' ',' ','X',' ',' ',' ',' ',' ',' '},
                   {'X','X','X','X',' ','X',' ','X',' ','X',' ','X','X','X','X',' '},
                   {' ',' ',' ',' ',' ','X',' ','X',' ',' ',' ','X',' ','X','X',' '},
                   {' ','X','X','X','X','X',' ','X',' ',' ',' ','X',' ','X','X',' '},
                   {' ',' ',' ',' ',' ',' ',' ','X',' ','X','X','X',' ','X','X',' '},
                   {'X','X','X','X','X','X','X','X',' ','X',' ',' ',' ',' ',' ',' '},
                   {' ',' ',' ',' ',' ',' ',' ',' ',' ','X','X','X','X','X','X',' '},
                   {' ','X','X','X','X','X','X','X',' ',' ',' ','X',' ',' ',' ',' '},
                   {' ',' ',' ',' ',' ',' ',' ','X','X','X',' ','X','X','X','X','X'},
                   {'X','X','X','X','X','X','X','X',' ',' ',' ',' ',' ',' ',' ',' '},
                   {' ',' ',' ',' ',' ',' ',' ','X','X','X','X','X','X','X','X',' '},
                   {'E','X','X','X','X','X',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}};
PrintMazeState(Maze);
spaces = 4;
MoveRight(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 2;
MoveDown(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 4;
MoveLeft(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 2;
MoveDown(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 6;
MoveRight(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 4;
MoveUp(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 2;
MoveRight(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 7;
MoveDown(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 2;
MoveRight(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 2;
MoveDown(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 5;
MoveRight(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 2;
MoveDown(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 9;
MoveLeft(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 1;
MoveUp(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 6;
MoveLeft(Maze, posX, posY, spaces);
PrintMazeState(Maze);
spaces = 1;
MoveDown(Maze, posX, posY, spaces);
PrintMazeState(Maze);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote