943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1101
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 2nd, 2008
0

Re: Need help in finding correct method for my program!

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:
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nyaato is offline Offline
11 posts
since Nov 2008
Dec 3rd, 2008
0

Re: Need help in finding correct method for my program!

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.

C++ Syntax (Toggle Plain Text)
  1. void Display ()
  2. {
  3. for (int indexrow=0; indexrow<8; indexrow++)
  4. {
  5. for (int indexcol=0; indexcol<16;indexcol++)
  6. {
  7. cout << gameArray[indexrow][indexcol];
  8. }
  9. }
  10. cout << endl;
  11. }

At the top of the findPath () function, add these two lines:

C++ Syntax (Toggle Plain Text)
  1. Display ();
  2. 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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Dec 3rd, 2008
0

Re: Need help in finding correct method for my program!

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

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: Windows Console Mouse operations
Next Thread in C++ Forum Timeline: Segmentation Fault error





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


Follow us on Twitter


© 2011 DaniWeb® LLC