943,589 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 696
  • C++ RSS
Sep 19th, 2009
0

Maze game help.

Expand Post »
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. I have written a function but it is not correct. Can anyone look at my code and guide me. I am very interested to learn something new.

Here is my code :
C++ Syntax (Toggle Plain Text)
  1. class MazeMania
  2. {
  3. public:
  4. MazeMania(const int maze[][], int start, int end);
  5. int traverse( int x, int y);
  6. private:
  7. const int ROWS = 15;
  8. const int COLS = 25;
  9. char mouse = 'X';
  10. };
  11.  
  12. # include "mazeMania.h"
  13.  
  14. #define FALSE 0
  15. #define TRUE 1
  16.  
  17.  
  18. // initializes the constructor
  19. MazeMania::MazeMania(const int maze[][], int start, int end)
  20. {
  21. int start = 8;
  22. int end = 1;
  23. }
  24.  
  25. int MazeMania::mazeTraverse(int x, int y)
  26. {
  27. if ( x < 0 || x > COLS -1 || y < 0 || y > ROWS -1)
  28. {
  29. return FALSE;
  30. }
  31.  
  32. if (maze[y][x] == 'A')
  33. {
  34. return FALSE;
  35. }
  36. cout <<" "<<mouse;
  37. // if x, y is the goal, return true
  38. if (maze[y][x] == 'T')
  39. {
  40. return TRUE;
  41. }
  42.  
  43. if (mazeTraserse(x-1, y) == TRUE)
  44. {
  45. return TRUE;
  46. }
  47. else if (mazeTraverse (x, y-1) == TRUE)
  48. {
  49. return TRUE;
  50. }
  51. else if (mazeTraverse (x + 1, y) == TRUE)
  52. {
  53. return TRUE;
  54. }
  55. else if (mazeTraverse (x, y + 1) == TRUE)
  56. {
  57. return TRUE;
  58. }

I am confused now. Is my algorithm correct? How can I print 'X' now? and how can I display like X is moving from starting position to find the target? Please guide me through. I am willing to learn. Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Sep 19th, 2009
0

Re: Maze game help.

There's no ending bracket to your function, so is there more to the function? I see no change in the maze[][] array and no other variables that keep track of where you've been. You have cout statements that display an 'X', but what's the point of this? I get a return value of true or false, but assuming the maze is solvable, I'd better get true, right? But I get "true" returned, I still have no idea what path I took. As far as "backtracking" goes, there's no direction, there's just a coordinate or something, so how do I know how to reverse directions?

So if the goal is to decide WHETHER a maze is solvable, it might possibly work. If the goal is to determine the correct path, I don't see anything that has anything to do with a path here. I also think that possibly, you're going to have a massive call stack, though I'm not 100% positive. I don't understand the algorithm and I don't understand the purpose of the function.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 19th, 2009
0

Re: Maze game help.

What does your maze look like? You just give the start and finish points.
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Binary Search fails sometimes
Next Thread in C++ Forum Timeline: Generating Pythagorean Numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC