How do I code picking random variables to then compare it to an array?

0 favourites
  • 8 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I'm trying to create a random order for my levels to be picked from in sets of ten. I have an array that has 1,1 to 10,10 all marked with ones. (I avoided starting at 0,0 so the numbers match up better.) If a level is completed, the spot will be marked/changed to a 0.

    The Y part of the array is the tier difficulty and is preset until a player defeats 10 levels at which time the games adds +1 to it thus moving the player to the second tier of levels.

    I've tried using int or floor(random(1,10)) and choose(1,2,3,4,5,6,7,8,9,10) to pick the X. I need it to check if (LevelSelected, LevelTier)=0 in my array because if it does it needs to pick again. Even with manipulating the array to only have some of them be 1's the game still cycles through all of ten options.

    Any thoughts?

  • Wait, you want a random row and a random column from an array and not have it = 0?

    Use a while statement.

    While found = false
     x=floor(random(1,10))
     y=floor(random(1,10))
     array.at(x,y)) <> 0
     else
     found = true
     stop loop
    

    then, x & y hold the answer you seek

    I know Ashley will correct my method here but I'm old school so this makes sense to me.

    I believe this is the proper method

    While 
     x=floor(random(1,10))
     y=floor(random(1,10))
     array.at(x,y)) <> 0
     else
     stop loop
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you very much for the response! I've tried to implement that while loop, but no matter how I put it in, it tends to freeze the game on load-up or when trying to execute the while loop itself.

    The first is the code by itself, the others are the code attached to the button you're meant to press to then send you to the selected level.

    Am I just doing it wrong?

    *EDIT* The first image isn't showing for some reason for me. Sorry.

  • I believe your else is in the wrong spot. Put it under the value at check. If you look at the lines, it's connected to the tap gesture and the value at check does nothing if it's true or false.

  • Here's an example of one of mine.

    Notice the boolean check is directly under the for loop, not the while.

    This one runs a bunch of for loops. If I need to keep repeating those loops, I set the found variable to true inside that for loop. That way, it keeps looping and looping until it doesn't find things I need to change and then, with found = false, exits the while.

    Again, Ashley says I'm doing it wrong, but hey, it works and I understand it!

  • So this is what the answer eventually was.

    I'm not sure what was going on with Random, but it wouldn't select (10,1) in the array. Also, other variations of this code let the button just continue to either pick more letters, pick more letters and then stop or just crash. Choose seems to nip that in the bud and should carry on because 1-10 are the same X coordinates in the array with me moving it down the Y axis +1.

    Thank you very much again for the help. Now I've just got to make it take me to a level!

    *EDIT* I believe you were also correct about the Else being in the wrong spot. It's a small detail to miss and so I missed it completely. lol (Also coding the way you understand it sticking with it because it works makes sense to me too.)

    *EDIT2* I also realized that I needed to change the =/= 0 to = 0 when checking my array (line 25) because when it is 0, it needed to keep going. Because: 0 = bad. It took some time to work the kinks out, but this is the process. I'm just glad there are people that can help.

  • Arrays (and a lot of other things) are typically 0 based. If you were doing 1 to 10 and it was a 10 wide array, the 10 didn't exist so it couldn't go there. I'm so used to 0 based stuff, I just put things like 0 to array.width-1 as a habit.

  • Yup, I wish that was the issue though. I usually make my arrays up to 11 or 21. This way I have some that number similarly, but the I have a few rows with spaces for the randoms.

    Thanks again man.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)