| | |
Need help in finding correct method for my program!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 11
Reputation:
Solved Threads: 0
Ah yes. I'm sorry for leaving out these information.
Basically, this is a "game" of the mouse and cheese. The mouse is represented with M, which is the starting point of the maze itself. C is the cheese, which is the ending point of the maze.
Thus, the objective of the whole program is to reach from M to C!
The highlighted text from main function:
The red highlighted text is the "infinite loop", which will keep prompting the user for an action after a particular action has been performed. I'm really sorry if I've confused you...
The for loop in the main function is just to show the maze, it has no relation in the pathfinding/insert the maze into array.
Once again, I'm sorry if I didn't provide sufficient information.
Here's the results I've gotten so far...
http://i37.tinypic.com/2rpblzm.jpg
Basically, this is a "game" of the mouse and cheese. The mouse is represented with M, which is the starting point of the maze itself. C is the cheese, which is the ending point of the maze.
Thus, the objective of the whole program is to reach from M to C!
The highlighted text from main function:
for (int start_loop = 1; start_loop>0; start_loop++)
{
if (status_mode != "NONE")
{
for (int indexrow=0; indexrow<8; indexrow++)
{
for (int indexcol=0; indexcol<16;indexcol++)
{
cout << gameArray[indexrow][indexcol];
}
}
cout << endl;
}The red highlighted text is the "infinite loop", which will keep prompting the user for an action after a particular action has been performed. I'm really sorry if I've confused you...
The for loop in the main function is just to show the maze, it has no relation in the pathfinding/insert the maze into array.
Once again, I'm sorry if I didn't provide sufficient information.
Here's the results I've gotten so far...
http://i37.tinypic.com/2rpblzm.jpg
Last edited by Nyaato; Dec 2nd, 2008 at 10:04 pm. Reason: Addition of link for image
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
For debugging purposes, you need to display each iteration of your maze solving function to see what's happening. Take your display code and make a function out of it.
At the top of the findPath () function, add these two lines:
C++ Syntax (Toggle Plain Text)
void Display () { for (int indexrow=0; indexrow<8; indexrow++) { for (int indexcol=0; indexcol<16;indexcol++) { cout << gameArray[indexrow][indexcol]; } } cout << endl; }
At the top of the findPath () function, add these two lines:
C++ Syntax (Toggle Plain Text)
Display (); cin.get ();
cin.get () pauses. Press the enter key to see the next iteration. It will give you a much better feel for what is going on step by step. These two lines are debugging code, so you need to remember to eventually take them out. •
•
Join Date: Jul 2005
Posts: 1,672
Reputation:
Solved Threads: 261
Backtracking can be made more efficient if you can keep track of where you've been. As I said in my earlier post a stack is commonly used but you could use an array or list almost as readily. Since you are already using an array for the maze I assume you are reasonably familiar with their use. So, I'd declare a one dimensional array of type struct called path or solution or whatever with the same number of total objects as in the maze. I'd als declare an int to keep track of how many cells of the maze you actually have in the path. As you go to each new cell of the maze add it to the end of the array. When you can't go any further with a given cell then you can effectively remove that cell from the path by decreasing the value of the variable keeping track of the number of cells actually in the path at the same time you place the '+' in the cell of the maze that is the dead end. Then it's a simple task print out the path when you find a solution to the maze.
You will probably need to do bounds check sooner or later, too. That means if the Mouse is in the top row it can't go up (South?) any further, if it's in the far right row it go right (East) any further, etc.
Next moves can be checked in same order every time----North, East, South, West, etc. Valid next moves for the Mouse would be elements of the maze that have values C or space. First valid move found in that sequence put 0 in current cell, move m (the actual mouse, which is separate from M which is the starting position of the Mouse) in the next valid move cell and repeat. If the move puts the Mouse out of bounds and if the next element of the maze is *, +, 0, or M then that move is invalid. If no valid move is found after looking in all four directions, then put a + in the current cell and decrease the length of the current path by one and put m in that cell and retry. If the length of the path ever gets to minus one (or zero or other value meaning it is empty) then there is no solution.
The process stops when there is a solution or the path is empty.
If there is a solution then print it out.
You will probably need to do bounds check sooner or later, too. That means if the Mouse is in the top row it can't go up (South?) any further, if it's in the far right row it go right (East) any further, etc.
Next moves can be checked in same order every time----North, East, South, West, etc. Valid next moves for the Mouse would be elements of the maze that have values C or space. First valid move found in that sequence put 0 in current cell, move m (the actual mouse, which is separate from M which is the starting position of the Mouse) in the next valid move cell and repeat. If the move puts the Mouse out of bounds and if the next element of the maze is *, +, 0, or M then that move is invalid. If no valid move is found after looking in all four directions, then put a + in the current cell and decrease the length of the current path by one and put m in that cell and retry. If the length of the path ever gets to minus one (or zero or other value meaning it is empty) then there is no solution.
The process stops when there is a solution or the path is empty.
If there is a solution then print it out.
Klatu Barada Nikto
![]() |
Similar Threads
- Please help me!! Newbie in C++ (C++)
- For loop problem (Java)
- Giving code to posters rather than helping them (IT Professionals' Lounge)
- Help with Java program writing (Java)
- Very New to java, skeleton of program given (Java)
- Syntax Analyser and General Java Help (Java)
Other Threads in the C++ Forum
- Previous Thread: Windows Console Mouse operations
- Next Thread: Segmentation Fault error
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






