0

I am asked to make a maze game in c++ (using codeblocks). I figured out most of it, but stuck in one method of Maze class. I have this function to say that travel in anyone direction (up, down, left, right) where you dont get the wall.

int Maze::mazeTraversal(int a, int b)
{
    // If a,b is outside maze, return false.
    if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) return FALSE;

    // If a,b is the goal, return true.
    if ( maze[b][a] == 'G' ) return TRUE;

    // If a,b is not open, return false.
    if ( maze[b][a] != '0' && maze[b][a] != 'S' ) return FALSE;

    // Mark a,b part of solution path.
    maze[b][a] = 'x';

    // If find_path North of a,b is true, return true.
    if ( mazeTraversal(a, b - 1) == TRUE ) return TRUE;

    // If find_path East of a,b is true, return true.
    if ( mazeTraversal(a + 1, b) == TRUE ) return TRUE;

    // If find_path South of a,b is true, return true.
    if ( mazeTraversal(a, b + 1) == TRUE ) return TRUE;

    // If find_path West of a,b is true, return true.
    if ( mazeTraversal(a - 1, b) == TRUE ) return TRUE;

    // Unmark a,b as part of solution path.
    maze[b][a] = '0';

    return FALSE;
}

I am calling this function as:

Maze mo(maze,12); //creating maze game with 12/12 array
mo. mazeTraversal(0,2) // because the entry point is in 0,2 position of the game.

I just realised that I am asked to have this mazeTraversal as void. No any return. My mind is blowing up. Excepting some creative ideas please.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
userabc55478
  • 61
  • 1
  • 8
  • 2
    it returns the result which you discards. is it what confused you? – varnie Sep 09 '13 at 16:33
  • Was there a complete signature specified for mazeTraversal(), or just the fact that it must return void? What is the spec for mazeTraversal()? – Adam Burry Sep 09 '13 at 16:36
  • @Varnie and Adam, I have made the code compiling well. Just the requirement of my assignment is "mazeTraversal() must be void. So I couldnt figure out how do i pass the way or call this function to travel in maze.?? my mind couldnt be any creative. – userabc55478 Sep 09 '13 at 16:43

4 Answers4

1

Use:

void Maze::mazeTraversal(int a, int b, bool& Status)

and then instead of return inside function use :-

Status = false; // or true

or use a data member bool Status inside class and update its value

class Maze{

public :
   bool Status;
   //..

   void Maze::mazeTraversal(int a, int b);
   //...
};
P0W
  • 46,614
  • 9
  • 72
  • 119
0

If your request states that the signature of the function must not be modified, the signature does not include the return type, therefore, you can change the return type. If the assignment does not tell anything about the signature there are the solutions already posted upstream:

  • add a by reference param (not a pointer, that is cumbersome and really not needed)
  • add an internal state that you maintain and return in some "Status" function.

These are the solution I would investigate. Either way you'd have to adapt the implementation of the traverse function. As it seems that your Maze object is stateful (i.e.: does maintain some internal state as to what position it arrived to), the second makes more sense.

CristiArg
  • 103
  • 2
  • 7
0

Here is another solution that does not change your function signature yet it does not return anything at all:

bool g_Status = true;
void Maze::mazeTraversal(int a, int b)
{
    if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) // If a,b is outside maze, return false.
        g_Status=false;
    else  if ( maze[b][a] == 'G' )    // If a,b is the goal, return true.
        g_Status=true; 
    else if ( maze[b][a] != '0' && maze[b][a] != 'S' ) // If a,b is not open, return false.
        g_Status=false;
    else{
        maze[b][a] = 'x'; // Mark a,b part of solution path.
        mazeTraversal(a, b - 1);
        if (!g_Status)  // If find_path North of a,b is true, return true.
            mazeTraversal(a + 1, b); // If find_path East of a,b is true, return true.
        if (!g_Status) 
            mazeTraversal(a, b + 1); // If find_path South of a,b is true, return true.
        if (!g_Status)
            mazeTraversal(a - 1, b); // If find_path West of a,b is true, return true.
        if (!g_Status)
            maze[b][a] = '0'; // Unmark a,b as part of solution path.
    }
}
Simple Fellow
  • 4,315
  • 2
  • 31
  • 44
-1

You may write your code like this:

void Maze::mazeTraversal(int a, int b,bool* retValue)
{
    // If a,b is outside maze, *retValue =  false.
    if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) {
          *retValue= FALSE;
           return;
    }

    // If a,b is the goal, return true.
    if ( maze[b][a] == 'G' ) {
        *retValue= TRUE;
         return;
    }

    // If a,b is not open, return false.
    if ( maze[b][a] != '0' && maze[b][a] != 'S' ) {
         *retValue= FALSE;
          return;
    }

    // Mark a,b part of solution path.
    maze[b][a] = 'x';

    // If find_path North of a,b is true, return true.
    bool* ret1;
    mazeTraversal(a + 1, b,ret1)
    if ( *ret1 == TRUE ) {
       *retValue= TRUE;
        return;
    }

    // If find_path East of a,b is true, return true.
    bool* ret2;
    mazeTraversal(a + 1, b,ret2);
    if ( *ret2 == TRUE ) {
        *retValue= TRUE;
         return;
    }

    // If find_path South of a,b is true, return true.
    bool* ret3;
    mazeTraversal(a, b + 1,ret3);
    if ( *ret3 == TRUE ) {
        *retValue= TRUE;
         return;
   }

    // If find_path West of a,b is true, *retValue= true.
    if ( mazeTraversal(a - 1, b) == TRUE ) {
         *retValue= TRUE;
          return;
    }

    // Unmark a,b as part of solution path.
    maze[b][a] = '0';

    *retValue= FALSE;
}

Now create a global variable:

bool* retvalue;

This variable will always hold the return value, you dont need to return from function

deeiip
  • 3,319
  • 2
  • 22
  • 33
  • 1
    Then fix all the `if (mazeTraversal(...))` that stop this compiling. Then (maybe now, maybe in a few months) fix the reentrancy problems caused by using a global variable. Then think "why is this code using a pointer when a return value would be so much simpler?" and put it back how it was. – Mike Seymour Sep 09 '13 at 16:48
  • shouldn't there be `return;` after each successful check? i.e. for example `if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) { *retValue= FALSE; return; }` – varnie Sep 09 '13 at 16:49
  • 1
    @Mike, your sattire understood. I would be yelling my coordinator for this all. And deeiip, thanks dear. I will finish it in 10 mins (may be multiplied by 6) and sleep. Its 2.26 am here. – userabc55478 Sep 09 '13 at 16:56