Hey guys,
I have written a recursive function mazeTraverse which finds the path of the mouse to find its food which is placed inside the maze. I am given a maze of of 15 rows and 25 columns. Mouse's starting position is (8,1)[eighth row and 1st column] and the food is placed in (5, 10).
In my maze, 'A' represents the way is blocked
' ' represnts an opening
'M' represents starting position for the mouse and 'T' represents the target which is food.
I am supposed to print X which will start moving from starting position to the target. X basically represents the mouse movement from the starting position to the target. I know I need to use recursive function so when mouse find dead end it has to do backtracking.

Here is my header file :

const int ROWS = 15
const int COLS = 25
class MazeMania
{
public:
  MazeMania(char maze[ROWS][COLS], int startR, int startC);
  void rowCol (int r, int c);
  void mazeTraverse( int x, int y);
  void printMaze();
  void mouseMove();
private:
  char mouse[ROWS][COLS];
  const static char mouse = 'X'
  int row; //starting row
  int col;  // starting col
 
};

here is my .cpp file

# include "mazeMania.h"
#include "windows.h"

#include <iostream>
using std::cout;
using namespace std;


void MazeMania::rowCol(int r, int c)
{
  COORD pos;
  pos.X = c;
  pos.Y = r;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

Mazemania::MazeMania(char mouse[ROWS][COLS] , int row, int col)
{
  for ( int k = 0; k< ROWS; k++)
  {
    for(int l = 0; l < COLS; l++)
    {
      this->mouse[k][l] = mouse[k][l];
    }
  }
  printMaze();
  this->row = row;
  this->col = col;
}

void MazeMania::mazeTraverse(int x, int y)
{
 
    gotoRowCol(x, y);
    cout <<" "; // this will delete the mouse (X) previously printed
    cout<<mouse;
 
  if (maze[x][y] == 'T')
  {
    cout<<"Mouse is in target"<<endl;
    return;
  }
    else if ((x + 1, y )!= 'A')
    {
      mazeTraverse( x+1, col);
      rowCol(x+1, y);
      cout<<mouse;
      return;
    }
    else if ((x, y+ 1)!= 'A')
    {
      mazeTraverse(x, y+1);
      rowCol(x , y+1);
      cout<<mouse;
      return;
    }
    else if ((x-1, y) != 'A')
    {
      mazeTraverse(x - 1, y);
      rowCol(x-1 , y);
      cout<<mouse;
      return;
    }
    else if ((x, y -1) != 'A')
    {
      mazeTraverse( x, y - 1);
      rowCol(x, y-1);
      cout<<mouse;
      return;
    }
}

void MazeMania::printMaze()
{
  for ( int i = 0; i < ROWS; i++)
  {
    for (int j = 0; j <COLS; j++)
    {
      cout<<maze[i][j];
    }
    cout<<"\n";
  }
}
 
void MazeMania::mouseMove()
{
  mazeTraverse(row, col);
}

here is what my driver look like :-

char puzzle[ROWS][COLS] = (picture of the maze here)
MazeMania move(puzzle, 8,1)
move.mouseMove()
return 0;

compiles fine but X doesn't move instead I get an infinite loop of X where X prints outside the bound of the maze.
I am sure I am messed up in mazeTraverse function. Thank you for your time.

in your if statements in your maze traverse function you have (x+1, y) != 'A' . im not quite sure what this is. did you mean to do if (mouse[x+1][y] != 'A') also on line 38 you have if (maze[x][y] == 'T') but there is no maze variable declared in either the mazemania class or in the traverse function.

Edited 7 Years Ago by NathanOliver: n/a

I fix both errors still I amn't getting any useful output. How can I print X and delete the X that was printed before? In this way, it looks like X is moving inside the maze?

Thanks for pointing out those two errors!

could you supply me with the maze info so i can run it on my machine and see what its doing?

check ur email (private message).

This article has been dead for over six months. Start a new discussion instead.