CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Posts
    12

    need help using random if statement

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: need help using random if statement

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2009
    Posts
    12

    Re: need help using random if statement

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured