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

I have some issues/problems that I\'m not quit getting it. Please someone check

ID: 3602244 • Letter: I

Question

I have some issues/problems that I'm not quit getting it. Please someone check my code and tell me what's wrong with it? Make sure give an explanation of how and why do I have to change it? I'll thank you so much for your help. Below is the information and my code.

=================================

Information:

input data

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.

=======================================

#include <iostream>

#include <fstream>

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 = { };

   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++)

               Mazes[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 >> Mazes[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));

   }

}

Explanation / Answer

We need to change the 'exit()' function.

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] = '@'; //Modified '==' to '='

       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));

   }

}

I have modified '==' to '='. Because we have to set the rat traverse path with '@' symbol. Then only the rat will not go back into reverse path.