1 00:00:01,130 --> 00:00:06,180 Come back again this lesson is actually going to make things a lot more exciting in the sense that we'll 2 00:00:06,180 --> 00:00:08,520 end up randomizing the tiles. 3 00:00:08,520 --> 00:00:14,250 So what we have here which is currently a group of toys that they're in or they're from 007 and again 4 00:00:14,280 --> 00:00:18,350 from 0 7 will end up being randomized all over the place. 5 00:00:18,360 --> 00:00:20,950 So let's go ahead and see how I'm going to do that. 6 00:00:21,030 --> 00:00:25,620 I'm going to go ahead to my viewpoint for that and I'm actually going to go ahead and add a new function 7 00:00:26,160 --> 00:00:31,860 and this function when I call it the randomize function and then randomize function. 8 00:00:31,860 --> 00:00:36,200 Here are the things that we have to do based on the comments I had earlier. 9 00:00:36,240 --> 00:00:37,820 These are taking care of. 10 00:00:37,840 --> 00:00:43,950 We already have all the tiles and centers these two of you still have to do some way to type them in 11 00:00:43,950 --> 00:00:44,450 here. 12 00:00:44,470 --> 00:00:49,670 Let me get myself a little bit more space so we can only focus on one thing at a time. 13 00:00:49,680 --> 00:00:51,650 And this is the randomise function. 14 00:00:51,780 --> 00:00:58,830 So we have to go to all times and that if you remember it would be as simple as doing for any title 15 00:00:59,790 --> 00:01:02,360 in let's call it in tilers array. 16 00:01:02,400 --> 00:01:05,460 So we call that any trial in tiles array. 17 00:01:05,460 --> 00:01:07,330 Here are the things that they want to happen. 18 00:01:07,380 --> 00:01:12,580 I want to find a random center and assign it to any tile. 19 00:01:12,600 --> 00:01:14,470 So here's the first thing I have to do. 20 00:01:14,580 --> 00:01:20,510 I have to say any tile dot center that has to become a random place. 21 00:01:20,610 --> 00:01:29,700 So finding that the random center means we have to find a random ID from day centers. 22 00:01:29,850 --> 00:01:35,550 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 23 00:01:35,550 --> 00:01:35,790 one. 24 00:01:35,790 --> 00:01:46,540 Let random center that is going to be centers array and from that I have to find a random location. 25 00:01:46,560 --> 00:01:48,060 So let's go ahead and do that. 26 00:01:48,060 --> 00:01:49,590 I know I'm getting a lot of arrows. 27 00:01:49,590 --> 00:01:54,810 It's just because I'm going one step by the time and I'm kind of reverse engineering going back from 28 00:01:54,870 --> 00:01:56,060 one step at a time. 29 00:01:56,160 --> 00:02:03,270 So I'm going to go ahead and say let's I'm going to call this one random index the random index has 30 00:02:03,270 --> 00:02:09,960 to be a random index from the center array and that's going to be an integer value and that integer 31 00:02:09,960 --> 00:02:15,750 value I have to use an arc for random uniform to get that number that I want and there's going to be 32 00:02:15,750 --> 00:02:17,370 a bit of a longer code. 33 00:02:17,430 --> 00:02:23,580 But bear with me to see how we get there because a lot of it actually depends on ex-coach to be fixed 34 00:02:23,580 --> 00:02:24,220 for me. 35 00:02:24,240 --> 00:02:31,640 So what I need is an arc for Rondeaux uniform and the upper bound that I want to give it is the center 36 00:02:31,650 --> 00:02:34,010 is array dot count. 37 00:02:34,230 --> 00:02:41,620 So I want to produce a random number to a maximum of centers or a count which doesn't include the maximum 38 00:02:41,640 --> 00:02:46,180 itself so it is going to produce between 0 2 that's maximal. 39 00:02:46,230 --> 00:02:48,230 In this case being 15. 40 00:02:48,270 --> 00:02:54,480 So I get zero to 15 which are the numbers that I want now is giving me an error saying that the value 41 00:02:54,480 --> 00:02:56,890 or entering in here is not a U. 42 00:02:56,910 --> 00:03:02,820 Instead if two and I have to actually fix now then I'm going to say well convert that replace it with 43 00:03:02,820 --> 00:03:08,280 you in thirty two and that's all well but now that's going to give me another error saying that well 44 00:03:08,650 --> 00:03:13,050 Orlick Fernando doesn't produce integers by default. 45 00:03:13,050 --> 00:03:17,390 Are you sure you want to use this like that or do you want to convert it to an integer. 46 00:03:17,390 --> 00:03:22,570 And I'm going to say I'm get to this part manually saying eans of that value. 47 00:03:22,710 --> 00:03:23,950 So we get this. 48 00:03:24,000 --> 00:03:25,280 Those are eight counts. 49 00:03:25,320 --> 00:03:27,690 We produce a random number from it. 50 00:03:27,690 --> 00:03:33,410 That's going to be on you in 30 to and from that you entered it to get the integer value we convert 51 00:03:33,480 --> 00:03:39,690 into an integer because there are convertible to each other and then I'm going to say the random index 52 00:03:39,750 --> 00:03:47,370 is the index from centers array that they want to find a new center from data center is the center I 53 00:03:47,370 --> 00:03:49,950 want to apply to my type. 54 00:03:50,490 --> 00:03:52,230 And that's all we have to do. 55 00:03:52,230 --> 00:03:57,480 Of course there is still a few things but they want to show you what happens now and then fix those 56 00:03:57,480 --> 00:03:57,990 things. 57 00:03:58,110 --> 00:04:05,340 So if we call the randomized now in our view a lot after making the tiles we should get at least some 58 00:04:05,340 --> 00:04:06,530 randomization. 59 00:04:06,540 --> 00:04:08,760 Let's go ahead and see if that's actually going to work. 60 00:04:08,760 --> 00:04:14,370 Let me clean the code so we can see everything together just in case you want to catch up with me. 61 00:04:15,140 --> 00:04:18,050 So that's the code they have so far. 62 00:04:18,950 --> 00:04:21,980 And using the randomized interview did a lot. 63 00:04:22,040 --> 00:04:28,070 And if I run my hour right now I loaded it in my simulator we should be able to make all the ties and 64 00:04:28,070 --> 00:04:31,110 go to randomize and randomize them in here. 65 00:04:31,490 --> 00:04:38,150 And it's kind of happening but there are some spots that nothing is shown in them and actually want 66 00:04:38,150 --> 00:04:40,560 you to think for a second why this is happening. 67 00:04:40,560 --> 00:04:45,200 But if you want you can actually pause the video for a site and look at my code and try to think to 68 00:04:45,200 --> 00:04:47,670 yourself why this is happening. 69 00:04:47,690 --> 00:04:49,640 I'm going to pause for just a second. 70 00:04:49,640 --> 00:04:52,070 In case you want to pause the video as well. 71 00:04:53,380 --> 00:05:00,520 So the reason it is happening is this Every time I go to this function I find the random index does 72 00:05:00,520 --> 00:05:06,460 random index is a location somewhere in the center's array and it is very very possible that they get 73 00:05:06,460 --> 00:05:08,820 the same space twice. 74 00:05:08,810 --> 00:05:15,790 It's possible that this location being the index one I've gotten it more than once and there actually 75 00:05:15,790 --> 00:05:18,750 too tired as in years sitting on top of each other. 76 00:05:18,880 --> 00:05:20,410 I just don't see most of them. 77 00:05:20,410 --> 00:05:22,060 I see another one. 78 00:05:22,060 --> 00:05:28,510 So to fix that I'm going to have to make sure that every time they use a center or any of the centers 79 00:05:28,510 --> 00:05:32,650 that they have used I have to take it out of the sensors array. 80 00:05:32,650 --> 00:05:36,390 This way I make sure that nobody can be used more than once. 81 00:05:36,580 --> 00:05:38,040 So let's go ahead and do that. 82 00:05:38,050 --> 00:05:40,890 I'm going to say random index find. 83 00:05:40,980 --> 00:05:49,240 That's where you'll use it in the centers array applied to my tile and then say sensors array dot remove 84 00:05:49,330 --> 00:05:54,190 at random index right index. 85 00:05:54,280 --> 00:06:00,550 And this is going to make sure that we remove every one that we used up from the centers right now. 86 00:06:00,790 --> 00:06:04,540 I want to show you that I'm just going to explain it to you because I know that's going to make sense 87 00:06:04,540 --> 00:06:05,410 to you guys. 88 00:06:05,410 --> 00:06:12,100 The problem with trying to right now is simply this if you decide somebody in the middle of the game 89 00:06:12,100 --> 00:06:19,480 to reset the game using this by then and if you try to rerun this function is going to crash your application 90 00:06:19,750 --> 00:06:24,660 simply because centers are a will not have anything in it anymore centers. 91 00:06:24,720 --> 00:06:31,930 It would actually be zeroed out because once you run this function just once it goes through every member 92 00:06:31,930 --> 00:06:37,690 of the Interior's array and removes them one at a time and as a result you won't have anything left 93 00:06:37,690 --> 00:06:40,210 in here so you can reset your game. 94 00:06:40,210 --> 00:06:42,040 Now there are two ways of thinking about it. 95 00:06:42,040 --> 00:06:44,880 One is features a more common way. 96 00:06:45,070 --> 00:06:53,500 Let's make a temporary Center's array so we don't touch the regional centers OK every time we want to 97 00:06:53,500 --> 00:06:54,190 randomize. 98 00:06:54,190 --> 00:06:55,870 We make a duplicate of it. 99 00:06:55,890 --> 00:07:02,790 So that's one way of thinking about it the other way of thinking about is let's remake the tiles every 100 00:07:02,790 --> 00:07:04,560 time that you want to reset. 101 00:07:04,690 --> 00:07:09,960 Now by the end of the game or by the end of this development I'm going to settle for the second solution. 102 00:07:10,060 --> 00:07:14,750 But for now I want to show you how we would do it if you wanted to make it temporary centers. 103 00:07:14,750 --> 00:07:15,390 All right. 104 00:07:15,400 --> 00:07:22,210 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 105 00:07:22,210 --> 00:07:24,990 the center's Arri and that's all we have to do. 106 00:07:25,120 --> 00:07:31,150 So we have a temporary version of it that original one is there and I'm going to use the temporary version 107 00:07:31,150 --> 00:07:32,800 for everything in here. 108 00:07:32,890 --> 00:07:36,460 So I use a temporary version to find a random index. 109 00:07:36,460 --> 00:07:43,270 I use the temporary one to locate something and I use the temporary one to remove the random index object 110 00:07:43,270 --> 00:07:44,140 from it. 111 00:07:44,140 --> 00:07:50,670 Once we do this every time that this function is called a new temporary one is created is used up in 112 00:07:50,680 --> 00:07:51,050 here. 113 00:07:51,070 --> 00:07:52,900 Everything in it is going to be finished. 114 00:07:52,900 --> 00:07:55,160 We're going to go through zero to 15. 115 00:07:55,270 --> 00:08:01,030 Or if you're playing the game in a six by six mode 0.35 you're going to go through all of them. 116 00:08:01,060 --> 00:08:04,880 And once they are done that whole object is going to be deallocated. 117 00:08:05,020 --> 00:08:07,900 So that's really going to help me in this situation. 118 00:08:07,900 --> 00:08:13,420 Of course we won't be able to see almost anything happening here visually but that's kind of something 119 00:08:13,420 --> 00:08:16,050 that you can see from the code is happening. 120 00:08:16,090 --> 00:08:25,030 It is randomising it 7 0 4 6 are no pattern between them and now we have a game that can be reset once 121 00:08:25,030 --> 00:08:27,280 we programmed the reset button. 122 00:08:27,280 --> 00:08:29,970 Let's keep it up to here and we'll move on to our next class.