943,832 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 395
  • C++ RSS
Sep 23rd, 2009
0

Can anyone check my code?

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.

Here is my header file :
C++ Syntax (Toggle Plain Text)
  1. const int ROWS = 15
  2. const int COLS = 25
  3. class MazeMania
  4. {
  5. public:
  6. MazeMania(char maze[ROWS][COLS], int startR, int startC);
  7. void rowCol (int r, int c);
  8. void mazeTraverse( int x, int y);
  9. void printMaze();
  10. void mouseMove();
  11. private:
  12. char mouse[ROWS][COLS];
  13. const static char mouse = 'X'
  14. int row; //starting row
  15. int col; // starting col
  16.  
  17. };
here is my .cpp file
C++ Syntax (Toggle Plain Text)
  1. # include "mazeMania.h"
  2. #include "windows.h"
  3.  
  4. #include <iostream>
  5. using std::cout;
  6. using namespace std;
  7.  
  8.  
  9. void MazeMania::rowCol(int r, int c)
  10. {
  11. COORD pos;
  12. pos.X = c;
  13. pos.Y = r;
  14. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  15. }
  16.  
  17. Mazemania::MazeMania(char mouse[ROWS][COLS] , int row, int col)
  18. {
  19. for ( int k = 0; k< ROWS; k++)
  20. {
  21. for(int l = 0; l < COLS; l++)
  22. {
  23. this->mouse[k][l] = mouse[k][l];
  24. }
  25. }
  26. printMaze();
  27. this->row = row;
  28. this->col = col;
  29. }
  30.  
  31. void MazeMania::mazeTraverse(int x, int y)
  32. {
  33.  
  34. gotoRowCol(x, y);
  35. cout <<" "; // this will delete the mouse (X) previously printed
  36. cout<<mouse;
  37.  
  38. if (maze[x][y] == 'T')
  39. {
  40. cout<<"Mouse is in target"<<endl;
  41. return;
  42. }
  43. else if ((x + 1, y )!= 'A')
  44. {
  45. mazeTraverse( x+1, col);
  46. rowCol(x+1, y);
  47. cout<<mouse;
  48. return;
  49. }
  50. else if ((x, y+ 1)!= 'A')
  51. {
  52. mazeTraverse(x, y+1);
  53. rowCol(x , y+1);
  54. cout<<mouse;
  55. return;
  56. }
  57. else if ((x-1, y) != 'A')
  58. {
  59. mazeTraverse(x - 1, y);
  60. rowCol(x-1 , y);
  61. cout<<mouse;
  62. return;
  63. }
  64. else if ((x, y -1) != 'A')
  65. {
  66. mazeTraverse( x, y - 1);
  67. rowCol(x, y-1);
  68. cout<<mouse;
  69. return;
  70. }
  71. }
  72.  
  73. void MazeMania::printMaze()
  74. {
  75. for ( int i = 0; i < ROWS; i++)
  76. {
  77. for (int j = 0; j <COLS; j++)
  78. {
  79. cout<<maze[i][j];
  80. }
  81. cout<<"\n";
  82. }
  83. }
  84.  
  85. void MazeMania::mouseMove()
  86. {
  87. mazeTraverse(row, col);
  88. }

here is what my driver look like :-
C++ Syntax (Toggle Plain Text)
  1. char puzzle[ROWS][COLS] = (picture of the maze here)
  2. MazeMania move(puzzle, 8,1)
  3. move.mouseMove()
  4. 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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Sep 23rd, 2009
0

Re: Can anyone check my code?

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.
Last edited by NathanOliver; Sep 23rd, 2009 at 7:00 pm.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
Sep 23rd, 2009
0

Re: Can anyone check my code?

ok! got ya!
Last edited by complexcodes; Sep 23rd, 2009 at 7:05 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Sep 23rd, 2009
0

Re: Can anyone check my code?

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!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Sep 23rd, 2009
0

Re: Can anyone check my code?

could you supply me with the maze info so i can run it on my machine and see what its doing?
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
Sep 23rd, 2009
0

Re: Can anyone check my code?

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).
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008

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: Transform the declaration
Next Thread in C++ Forum Timeline: eight queens (not running)





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


Follow us on Twitter


© 2011 DaniWeb® LLC