wrap your mind around this one

Started by
2 comments, last by Zahlman 17 years, 5 months ago
So I have a program that navigates a leedle mouse around a 2D array. Seeing that I pride myself in being a moderate-level C++ programmer, I figured this was no big deal. My program compiles fine, except the main while loop I am using seems to be "short-circuiting" in a bizarre way. I have been trying to fix this problem for two days without success. Figure this one out and I will clean your gutters for free. My program consists of three files: (1) the driver.cpp which contains the main() function, (2) SolveMaze.h and (3) SolveMaze.cpp. Inside of SolveMaze.h I declare three objects: (1) Mouse, (2) Maze, and (3) MazeStack which is a very simple array-based stack containing the solution to the maze in coordinates. As the mouse goes through the maze, he pushes coordinates onto the MazeStack, and if he determines they are bad coordinates (i.e. he hits a dead end) he pops them off the stack. The mouse object is literally nothing more complicated than a set of coordinate values (rows and columns). Ergo the only mouse data members are mouse.row and mouse.col, which obviously contain the mouse's current location within the maze. The program's maze-solving structure is a while loop inside the main function. The while loop's conditional is to check whether the mouse is at the exit coordinate or not. To do this, the conditional makes two calls, both to Maze object methods, getExitRowVal() and getExitColVal(); it then compares these two values to the mouse's coordinates. If the mouse has arrived at the exit, the loop should break. What is happening is the while loop is breaking prematurely. I have analyzed the program's output, and I now know the last line of code within the while loop before it breaks unexpectedly. The first thing I did was try to get the program to output the mouse's coordinates and the maze object methods (explained above) to the screen. When I do this, it shows that the mouse has a coordinate of (1,6) and that the exit is located at (9,6). Clearly they are not located at the same place, yet the while loop breaks anyways. The next thing I did was try to hardcode the return values for the maze methods to be 9 and 6 respectively....and nothing changed. Below is all the code you should need to understand the set up. I mean it, I will clean your gutters. -hisDudeness

while(mouse.row != maze.getExitRowVal() && mouse.col != maze.getExitColVal())
{
    // Look above you in the maze.
    if(maze.isHall(UP, mouse.row, mouse.col))
    {
        // Reset the mouse to the coordinates above him.
	mouse.row = maze.getRowVal(UP, mouse.row);
	mouse.col = maze.getColVal(UP, mouse.col);

        // Push his new coordinates onto the maze stack.
	solution.push(mouse);

        // Update the maze on the screen.
	maze.print(solution);

        if(mouse.row == maze.getExitRowVal() &&
            mouse.col == maze.getExitColVal())
            cout << "This prints.....hmmmm\n";

        continue;
    }

    // Look to the right...

    // Look down...

    // Look left...
}



So what's happening is the mouse looks above him, sees a hallway, changes his coordinates, pushes them onto the maze stack, prints the maze to the screen, continues, and then triggers the while loop to break. HERE'S THE CRAZY PART: I copied the while loop's conditional and pasted it in as an if-statement right above the continue statement. Which means that if the the conditions to break the while loop are present, then the cout statement printing "This prints..... hmmmm" to the screen should print before the continue executes. IT DOESN'T!! So in other words, the program recognizes that the mouse isn't at the exit before the continue statement, but then says that it is afterwards....bizarre The only clue I have is that (using Dev-C++) when I hover over the mouse object in my source code, the compiler generates a tool tip that says "RAWMOUSE mouse." I have no data structure in my program called RAWMOUSE. I looked it up and it seems to be a Microsoft input device manager belonging to a RAWINPUT family of other classes. Could the fact that I have a class called Mouse in my program be conflicting with this RAWMOUSE, and thus, be causing the while loop to trigger for some strange reason?
Well I believe in God, and the only thing that scares me is Keyser Soze.
Advertisement
your use of the continue keyword is confusing. continue means basically goto the condition check and if it passes run the loop again. "break" means exit the loop.

Your explanation of the problem is also confusing. here's what i would expect your code to do:

loop for a while
print "this prints...hmmmm" one time
exit the loop

is this not what happens?

-me
while(mouse.row != maze.getExitRowVal() || mouse.col != maze.getExitColVal())
Σnigma
0) "Moderate level"? Lol.
1) Like Enigma said. Try reading your while-condition out loud.
2) Why would you have four separate cases like that for each direction? They all do effectively the same thing. Instead, iterate over the possible directions.
3) Try doing it through member functions of the mouse instead; it's better code organization.

// Pass the maze to the mouse by reference. Implementations of those functions// is left as an exercise.while (!mouse.escapedFrom(maze)) { // now we can express the 'escape condition'  // in positive logic, which makes it easier to get correct.  for (Direction d = UP; d <= RIGHT; d = Direction(d + 1)) {    // of course, the limits depend on how you defined the enumeration    if (mouse.canMove(d, maze)) { // calls .isHall() on the passed-in Maze      mouse.move(d); // don't need maze context for this; if we already know      // we can move, then we know exactly what to do to the coordinates.         // Push his new coordinates onto the maze stack.      solution.push(mouse);      // Update the maze on the screen.      maze.print(solution);      break; // from for-loop    }  }}


BTW, you might also want to read this.

This topic is closed to new replies.

Advertisement