Click to See Complete Forum and Search --> : need help using random if statement


Sk3letr0n
August 28th, 2009, 12:54 PM
In my code I have an array that holds a bunch of strucs. Those structs have 4 bool values.

What I'm trying to do is have a while loop that runs until the total number has been reached and inside the loop there are 4 if statements that check to make sure all of these bool values are set to true, and if they are it sets one of them to true.

What I'm trying to do is have it do is call these if statements randomly so that they are not always in the same order of bool1, bool2, bool3, bool4. But of course these if statements also have another if statement inside so there is possible that even though one of these is picked at random there is still a chance that it will not go because the if statement inside it doesn't allow the change.

Bascially it's for a game I'm working on. there is squares, what I need to do is pick a random square from the one that I'm currently on.

so:
_ _ _
|+|X|+|
|_|_|X|
|+|_|+|

+ = wall
X = not walkable

so I need to pick either left, or down. How can I do this randomly?

Because if it was just randomly between 4 and it happens to choose up first. it is not walkable to that loop would be wasted. Please help thanks.

dglienna
August 28th, 2009, 08:51 PM
Check them all for status, and load that position from 1 to 9, then chose the upper bound of your RND number to be equal. Then, you'll have a way to find the first OPEN square. If RND=1, then the first index that isn't 0 would be available.

Sk3letr0n
August 30th, 2009, 11:10 AM
Thanks for help but I figured it out. Well it's not a very efficient solution but here is what I did.

I just got a random number from 1 to 4, then just put in the if statement that checks if it is open or not, to also check if the number is their unique number for that if statement.

Like I said a small amount of time could be wasted using this method. But it still works just fine because if it happens to choose a number that the door is open on. it will still be open until the random number can actually be used. So all is well. thanks though. :)