Can anyone check my code?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

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

Can anyone check my code?

 
0
  #1
Sep 23rd, 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.

Here is my header file :
  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
  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 :-
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Can anyone check my code?

 
0
  #2
Sep 23rd, 2009
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.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Can anyone check my code?

 
0
  #3
Sep 23rd, 2009
ok! got ya!
Last edited by complexcodes; Sep 23rd, 2009 at 7:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Can anyone check my code?

 
0
  #4
Sep 23rd, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Can anyone check my code?

 
0
  #5
Sep 23rd, 2009
could you supply me with the maze info so i can run it on my machine and see what its doing?
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Can anyone check my code?

 
0
  #6
Sep 23rd, 2009
Originally Posted by NathanOliver View Post
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).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 236 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC