Need help in finding correct method for my program!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 11
Reputation: Nyaato is an unknown quantity at this point 
Solved Threads: 0
Nyaato Nyaato is offline Offline
Newbie Poster

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

 
0
  #11
Dec 2nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #12
Dec 3rd, 2008
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.

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,672
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #13
Dec 3rd, 2008
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC