15,997,284 members
1.00/5 (1 vote)
See more:
this is my code for solution path, i used random fn but every time it produces the same result. and 2,3 paths are shown but correct maze contains one path for solution and so many false paths, how can i do this? kindly someone tell me
void SolutionPath()
{
int x=0,y=0,tempX,tempY,currentX,currentY,legSize=3,direction=RIGHT;
while(x<mazeheight)>
{
while(legSize-- && x <mazeheight)>
{
currentX = x;
currentY = y;
switch(direction)
{
case LEFT:
mazeArray[x][y--]= SOLUTION_PATH;
break;
case RIGHT:
mazeArray[x][y++]= SOLUTION_PATH;
break;
case TOP:
mazeArray[x--][y]= SOLUTION_PATH;
break;
case BOTTOM:
mazeArray[x++][y]= SOLUTION_PATH;
break;
}
if(x<0||y<0||y>mazeWidth)
{
x= currentX;
y= currentY;
break;
}

}
tempX = x;
tempY = y;
int flag=0;
do
{
flag++;
legSize = (rand()%5 )+1;
direction = (rand()%3)+1;
switch(direction)
{
case LEFT:
tempY-=legSize;
break;
case RIGHT:
tempY+=legSize;
break;
case TOP:
tempX-=legSize;
break;
case BOTTOM:
tempX+=legSize;
break;
}

}
while((tempX<0||tempY<0||tempY>=mazeWidth||tempX>=mazeHeight)&& flag<3);
if(flag>=3)
direction = (rand()%3)+1;
}
}
Posted
Updated 26-Sep-11 8:18am
v8
Sergey Alexandrovich Kryukov 26-Sep-11 2:19am
Probably you have a weird notion of "error"...
--SA
Sweety Khan 26-Sep-11 2:21am
lOl no i m just stating tht i dont get any error + fruitful result
Sweety Khan 26-Sep-11 6:42am
plz someone respond to my question :( i have updated it.
Sweety Khan 26-Sep-11 7:33am
i m not asking for code, just tell me theortically wht is needed

## Solution 1

Take a look at the values of your variables before the final do loop in your code. You are adding to tempX continually but it's quite possible that one of the other variables in your while() expression is preventing the loop from terminating. It is always useful to step through problems like this with your debugger to try and see where one of your values or tests may be inconsistent.

On second thoughts you probably need to do this in your first loop also.

Sweety Khan 26-Sep-11 7:06am
yeh it stucks in do while loop. i think i should recode all :(
Sweety Khan 26-Sep-11 7:26am
i corrected it. now plz have a lOok n tell me wht should be done for the correct maze
Sweety Khan 26-Sep-11 7:33am
i m not asking for code, just tell me theortically wht is needed
Richard MacCutchan 26-Sep-11 12:12pm
What have you corrected? And, speaking theoretically, I have no idea since you have not explained what is going wrong. Use your debugger to step through the code and figure out why your values are not correct.
Sweety Khan 26-Sep-11 14:16pm
i am trying since morning n now it is night here, plz have a look, u r my only hope. this code is for creating a solution path in maze. the problem is tht it is not generating random path. every time i run it creates the same path n more over the path is not one, it should be one bc it is solution path

## Solution 2

Hi Sweety,

I guess You have a problem with random number generation.might be your program is not able to generate the random numbers.

C++
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main()
{
int i, r;

srand(time(0));

for(i = 0; i <= 2000; i++)
{
r = rand();
cout << "Number is " << r << ". It was generated on try number " << i << ".\n";
}

return 0;
}

v2
Sweety Khan 27-Sep-11 5:19am
yeh right my first problem is this which is solved by u. thanX but wht is the function of srand(time(0)). i read from somewhere but still its concept is not much clear
TRPatil 4-Oct-11 12:59pm
hmmm even I cant explain you the exact mechanism of this function..
but the problem with rand function is it will generate the same sequence whenever u execute it... and we need different nuber as the execution goes on... so we need to give some seed value for the random generator so that it will generate different no each time.
for that reason srand() is used ... we need to initialize the srand () function before we use it...
as we need different no we have to provide different seed value every time and time is the best option for this as it will never be same as the execution proceeds :) hence srand() is generally itialize with time(NULL) ..
u can find more help on srand() here
http://msdn.microsoft.com/en-us/library/f0d4wb4t%28v=vs.71%29.aspx