| | |
Can anyone check my code?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 57
Reputation:
Solved Threads: 1
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 :
here is my .cpp file
here is what my driver look like :-
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.
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)
const int ROWS = 15 const int COLS = 25 class MazeMania { public: MazeMania(char maze[ROWS][COLS], int startR, int startC); void rowCol (int r, int c); void mazeTraverse( int x, int y); void printMaze(); void mouseMove(); private: char mouse[ROWS][COLS]; const static char mouse = 'X' int row; //starting row int col; // starting col };
C++ Syntax (Toggle Plain Text)
# include "mazeMania.h" #include "windows.h" #include <iostream> using std::cout; using namespace std; void MazeMania::rowCol(int r, int c) { COORD pos; pos.X = c; pos.Y = r; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } Mazemania::MazeMania(char mouse[ROWS][COLS] , int row, int col) { for ( int k = 0; k< ROWS; k++) { for(int l = 0; l < COLS; l++) { this->mouse[k][l] = mouse[k][l]; } } printMaze(); this->row = row; this->col = col; } void MazeMania::mazeTraverse(int x, int y) { gotoRowCol(x, y); cout <<" "; // this will delete the mouse (X) previously printed cout<<mouse; if (maze[x][y] == 'T') { cout<<"Mouse is in target"<<endl; return; } else if ((x + 1, y )!= 'A') { mazeTraverse( x+1, col); rowCol(x+1, y); cout<<mouse; return; } else if ((x, y+ 1)!= 'A') { mazeTraverse(x, y+1); rowCol(x , y+1); cout<<mouse; return; } else if ((x-1, y) != 'A') { mazeTraverse(x - 1, y); rowCol(x-1 , y); cout<<mouse; return; } else if ((x, y -1) != 'A') { mazeTraverse( x, y - 1); rowCol(x, y-1); cout<<mouse; return; } } void MazeMania::printMaze() { for ( int i = 0; i < ROWS; i++) { for (int j = 0; j <COLS; j++) { cout<<maze[i][j]; } cout<<"\n"; } } void MazeMania::mouseMove() { mazeTraverse(row, col); }
here is what my driver look like :-
C++ Syntax (Toggle Plain Text)
char puzzle[ROWS][COLS] = (picture of the maze here) MazeMania move(puzzle, 8,1) move.mouseMove() 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.
in your if statements in your maze traverse function you have
also on line 38 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
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
![]() |
Similar Threads
- Check my code: Get ten random unique numbers then store in array. (C)
- hey guys could u check my code PLEASE its a code with a "text file" (C++)
- help me (C)
- CALENDAR in TASM!!(pls check my code!!:) (Assembly)
- could you check my code ? (C)
- check this code pleeeeeeeeeease (C++)
- Again please someone check my code (C++)
- Check the code!! (C++)
Other Threads in the C++ Forum
- Previous Thread: Transform the declaration
- Next Thread: eight queens (not running)
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





