Come back again this lesson is actually going to make things a lot more exciting in the sense that we'll
end up randomizing the tiles.
So what we have here which is currently a group of toys that they're in or they're from 007 and again
from 0 7 will end up being randomized all over the place.
So let's go ahead and see how I'm going to do that.
I'm going to go ahead to my viewpoint for that and I'm actually going to go ahead and add a new function
and this function when I call it the randomize function and then randomize function.
Here are the things that we have to do based on the comments I had earlier.
These are taking care of.
We already have all the tiles and centers these two of you still have to do some way to type them in
here.
Let me get myself a little bit more space so we can only focus on one thing at a time.
And this is the randomise function.
So we have to go to all times and that if you remember it would be as simple as doing for any title
in let's call it in tilers array.
So we call that any trial in tiles array.
Here are the things that they want to happen.
I want to find a random center and assign it to any tile.
So here's the first thing I have to do.
I have to say any tile dot center that has to become a random place.
So finding that the random center means we have to find a random ID from day centers.
So I have to essentially go ahead and say let's put this one in a new body but I'm going to call this
one.
Let random center that is going to be centers array and from that I have to find a random location.
So let's go ahead and do that.
I know I'm getting a lot of arrows.
It's just because I'm going one step by the time and I'm kind of reverse engineering going back from
one step at a time.
So I'm going to go ahead and say let's I'm going to call this one random index the random index has
to be a random index from the center array and that's going to be an integer value and that integer
value I have to use an arc for random uniform to get that number that I want and there's going to be
a bit of a longer code.
But bear with me to see how we get there because a lot of it actually depends on ex-coach to be fixed
for me.
So what I need is an arc for Rondeaux uniform and the upper bound that I want to give it is the center
is array dot count.
So I want to produce a random number to a maximum of centers or a count which doesn't include the maximum
itself so it is going to produce between 0 2 that's maximal.
In this case being 15.
So I get zero to 15 which are the numbers that I want now is giving me an error saying that the value
or entering in here is not a U.
Instead if two and I have to actually fix now then I'm going to say well convert that replace it with
you in thirty two and that's all well but now that's going to give me another error saying that well
Orlick Fernando doesn't produce integers by default.
Are you sure you want to use this like that or do you want to convert it to an integer.
And I'm going to say I'm get to this part manually saying eans of that value.
So we get this.
Those are eight counts.
We produce a random number from it.
That's going to be on you in 30 to and from that you entered it to get the integer value we convert
into an integer because there are convertible to each other and then I'm going to say the random index
is the index from centers array that they want to find a new center from data center is the center I
want to apply to my type.
And that's all we have to do.
Of course there is still a few things but they want to show you what happens now and then fix those
things.
So if we call the randomized now in our view a lot after making the tiles we should get at least some
randomization.
Let's go ahead and see if that's actually going to work.
Let me clean the code so we can see everything together just in case you want to catch up with me.
So that's the code they have so far.
And using the randomized interview did a lot.
And if I run my hour right now I loaded it in my simulator we should be able to make all the ties and
go to randomize and randomize them in here.
And it's kind of happening but there are some spots that nothing is shown in them and actually want
you to think for a second why this is happening.
But if you want you can actually pause the video for a site and look at my code and try to think to
yourself why this is happening.
I'm going to pause for just a second.
In case you want to pause the video as well.
So the reason it is happening is this Every time I go to this function I find the random index does
random index is a location somewhere in the center's array and it is very very possible that they get
the same space twice.
It's possible that this location being the index one I've gotten it more than once and there actually
too tired as in years sitting on top of each other.
I just don't see most of them.
I see another one.
So to fix that I'm going to have to make sure that every time they use a center or any of the centers
that they have used I have to take it out of the sensors array.
This way I make sure that nobody can be used more than once.
So let's go ahead and do that.
I'm going to say random index find.
That's where you'll use it in the centers array applied to my tile and then say sensors array dot remove
at random index right index.
And this is going to make sure that we remove every one that we used up from the centers right now.
I want to show you that I'm just going to explain it to you because I know that's going to make sense
to you guys.
The problem with trying to right now is simply this if you decide somebody in the middle of the game
to reset the game using this by then and if you try to rerun this function is going to crash your application
simply because centers are a will not have anything in it anymore centers.
It would actually be zeroed out because once you run this function just once it goes through every member
of the Interior's array and removes them one at a time and as a result you won't have anything left
in here so you can reset your game.
Now there are two ways of thinking about it.
One is features a more common way.
Let's make a temporary Center's array so we don't touch the regional centers OK every time we want to
randomize.
We make a duplicate of it.
So that's one way of thinking about it the other way of thinking about is let's remake the tiles every
time that you want to reset.
Now by the end of the game or by the end of this development I'm going to settle for the second solution.
But for now I want to show you how we would do it if you wanted to make it temporary centers.
All right.
So to do that I would go in here and simply say let's call it Tim sands array and that's going to be
the center's Arri and that's all we have to do.
So we have a temporary version of it that original one is there and I'm going to use the temporary version
for everything in here.
So I use a temporary version to find a random index.
I use the temporary one to locate something and I use the temporary one to remove the random index object
from it.
Once we do this every time that this function is called a new temporary one is created is used up in
here.
Everything in it is going to be finished.
We're going to go through zero to 15.
Or if you're playing the game in a six by six mode 0.35 you're going to go through all of them.
And once they are done that whole object is going to be deallocated.
So that's really going to help me in this situation.
Of course we won't be able to see almost anything happening here visually but that's kind of something
that you can see from the code is happening.
It is randomising it 7 0 4 6 are no pattern between them and now we have a game that can be reset once
we programmed the reset button.
Let's keep it up to here and we'll move on to our next class.