Question: I have need from you guys, Chegg. I have problems/issues about my code that I know what's wrong with it? I edited it as someone
I have need from you guys, Chegg. I have problems/issues about my code that I know what's wrong with it? I edited it as someone here told me, but it still didn't work. Please somone help me solve this and give explanation of it. I'll thank you so much for your help. Below is the information and my code.
==============================
Console/ data output of this code should look something like this:
# # . # # . # # # # # # . . . . . # . # . . . # . # . # . # . # . # # # . # . # # # . # . # . . . # # . . # . . . # # # # . # . . # . . . X # # # # # # # # # #
PROBLEM: You are to write a recursive program in C++ that mimics a rat trying to navigate a maze in order to find the exit, or if you would rather, when it finds the cheese in the maze. The key term is recursion, which differs from how other functions are called. Instead of creating a loop to solve a problem, a recursive function calls itself until a solution is found, or it terminates without a solution. Having a condition that ends the recursive calls is important, as badly implemented recursion is an easy way to send a computer into an endless loop and possibly crash the system. Recursion sees a lot of heavy use in AI, where researchers and programmers are always looking to fine tune their algorithms in the quest for a smarter program.
IMPLEMENTATION: Your program will read in a simple text file and place the elements in a two-dimensional array. The pound (hash) symbol will represent walls, dots will represent empty spaces the rat can move into, and use either an X to mark the exit, or a C to mark the cheese the rat is seeking. Place the end goal at the opposite end of the maze so that the rat has to traverse the maze using your recursive algorithm. While it will be necessary to output the solution to the screen, this time it is not required that you submit a copy of the text file that shows the path the rat took. I will provide one such file for you, and you will need to create one more yourself using the one I give as a template. Also, test a case where the rat is unable to find the exit or the cheese as it either doesn't exist or is unreachable by modifying one of the files.
=============================================
Here is my code:
#include
#include
using namespace std;
const int SIZE = 10;
typedef char Mazes[SIZE][SIZE];
//This struct allows us to pass the maze by value as opposed to by reference
struct Labs
{
Mazes maze;
};
void readIn();
void clear();
void printMaze(Mazes maze);
bool outOfBounds(int x, int y);
bool exit(Labs lab, int x, int y);
int main()
{
Mazes startingMaze = {{'#', '#', '.', '#', '#', '.', '#', '#', '#', '#'}
{'#', '#', '.', '.', '.', '.', '.', '#', '.', '#'}
{'.', '.', '.', '#', '.', '#', '.', '#', '.', '#'}
{'.', '#', '.', '#', '#', '#', '.', '#', '.', '#'}
{'#', '#', '.', '#', '.', '#', '.', '.', '.', '#'}
{'#', '.', '.', '#', '.', '.', '.', '#', '#', '#'}
{'#', '.', '#', '.', '.', '#', '.', '.', '.', 'X'}
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}};
Labs lab;
//You will instead write a function that reads the file from storage and place it into your project folder
for (int x = 0; x < SIZE; x++)
{
for (int y = 0; y < SIZE; y++)
lab.maze[x][y] = startingMaze[x][y];
}
printMaze(lab.maze);
if (exit(lab, 0, 0))
cout << "The rate found the exit.";
else
cout << "No path found.";
return 0;
}
void clear()
{
for (int x = 0; x < SIZE; x++)
for (int y = 0; y < SIZE; y++)
maze[x][y] = '.';
}// end clear
void readIn()
{
fstream fin;
int size;
fin.open("ratmaze.txt");
for (int x = 0; x < SIZE; x++)
{
for (int y = 0; y < SIZE; y++)
fin >> maze[x][y];
}
}//end readIn
//This function prints out the Maze in question
void printMaze(Mazes maze)
{
for (int x = 0; x < SIZE; x++)
{
for (int y = 0; y < SIZE; y++)
cout << maze[x][y] << ' ';
cout << endl;
}
}//end printMazes
bool outOfBounds(int x, int y)
{
return (x < 0 || y < 0 || x >= SIZE || y >= SIZE);
}
bool exit(Labs lab, int x, int y)
{
if (outOfBounds(x,y))
return false;
else if
(lab.maze[x][y] == 'X')
{
cout << endl << "Solution: " << endl;
printMaze(lab.maze);
return true;
}
else if (lab.maze[x][y] != '.')
return false;
else
{
lab.maze[x][y] = '@';
return (exit(lab, x + 1, y) || exit(lab, x - 1, y) || exit(lab, x, y - 1) || exit(lab, x, y + 1)
|| exit(lab, x - 1 y -1) || exit(lab, x + 1, y - 1) || exit(lab, x - 1, y + 1) || exit(lab, x + 1, y + 1);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
