Maze game help.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Maze game help.

 
0
  #1
Sep 19th, 2009
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 :
  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,815
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Maze game help.

 
0
  #2
Sep 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training

Re: Maze game help.

 
0
  #3
Sep 19th, 2009
What does your maze look like? You just give the start and finish points.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC