1 00:00:01,300 --> 00:00:06,520 All right, now we're really going to cover the remaining tasks for Part five inside game Java, we're 2 00:00:06,520 --> 00:00:11,290 going to create a method called SIMULATE that returns a string and the string it returns is going to 3 00:00:11,290 --> 00:00:13,720 be a simulation from a play that it receives. 4 00:00:15,360 --> 00:00:20,550 So first from the play we're going to get the placeholder string placeholder is equal to get placeholder 5 00:00:20,550 --> 00:00:21,120 from the play. 6 00:00:26,510 --> 00:00:32,540 And then we need to get a random team team team from the Hashmat for which we're going to assign this 7 00:00:32,540 --> 00:00:32,850 play. 8 00:00:33,620 --> 00:00:36,140 We'll have to create another function that returns a team. 9 00:00:38,120 --> 00:00:40,490 Public team, let's call it get random team. 10 00:00:46,080 --> 00:00:48,510 And our hash map stores, every team has a key. 11 00:00:51,830 --> 00:00:55,550 But from the hash map, we're going to get our collection of keys as a key set. 12 00:00:59,690 --> 00:01:02,190 And now we're going to grab a random key from this key set. 13 00:01:02,630 --> 00:01:09,230 The issue is that indexing sets is kind of annoying, but thankfully, we can call a method called Turei 14 00:01:09,500 --> 00:01:13,790 that returns an array from this set to array returns an array of objects. 15 00:01:13,790 --> 00:01:15,920 It's not specific to the type of object. 16 00:01:15,930 --> 00:01:19,700 So we're just going to have to store our array of teams as an object array. 17 00:01:25,460 --> 00:01:28,610 And now here we want to return a random team from the team's array. 18 00:01:33,990 --> 00:01:37,170 Before we do that, I'll create another function called a random. 19 00:01:43,290 --> 00:01:44,550 It returns an integer. 20 00:01:45,650 --> 00:01:49,220 And accepts a range from which it will get that random number. 21 00:01:51,460 --> 00:01:56,890 Notice that I'm never performing more than one task in a function, every function only does one task, 22 00:01:57,130 --> 00:02:02,230 and this function is going to perform the task of returning a random number between zero and one less. 23 00:02:02,230 --> 00:02:05,590 The range return method, random times range. 24 00:02:07,060 --> 00:02:09,070 And we'll have to typecast that value to. 25 00:02:16,220 --> 00:02:20,960 And back here, we'll say teams and grab a random index inside the length of the team's array. 26 00:02:22,810 --> 00:02:28,840 And our function doesn't know what type of object this is, so we'll have to typecast it to team and 27 00:02:28,840 --> 00:02:29,500 perfect. 28 00:02:31,450 --> 00:02:35,140 That was a lot of steps for the one piece of logic we have to include right here. 29 00:02:35,560 --> 00:02:39,580 Team team is able to get a random team and that looks really nice. 30 00:02:40,270 --> 00:02:41,790 So we have our placeholder. 31 00:02:41,800 --> 00:02:47,620 We have our random team, some in the crowd, and if else, if else statement that compares the placeholder 32 00:02:47,620 --> 00:02:49,360 against three possible cases. 33 00:02:51,630 --> 00:02:53,550 Team get positioned chaser. 34 00:02:59,280 --> 00:03:04,920 And I hope you can see why I'm not playing the writing chaser, because this is an important value that 35 00:03:04,920 --> 00:03:09,220 dictates how this code runs and a value that isn't supposed to change. 36 00:03:09,240 --> 00:03:11,030 We need to ensure that it doesn't. 37 00:03:11,100 --> 00:03:14,970 And that's why we put a constant instead, it's a lot safer anyways. 38 00:03:14,970 --> 00:03:20,520 If this is a play for chasers called Coffle Score for this particular team, there are should update 39 00:03:20,520 --> 00:03:21,040 by 10. 40 00:03:21,690 --> 00:03:23,700 There are three chasers we have to choose from. 41 00:03:23,700 --> 00:03:29,130 So we'll get a random chaser from the team team douget chasers and we'll get a random index from the 42 00:03:29,130 --> 00:03:30,630 length of the chasers array. 43 00:03:47,950 --> 00:03:52,660 And finally, we'll return the value that results from us replacing the placeholder from the simulated 44 00:03:52,660 --> 00:03:52,960 play. 45 00:04:11,810 --> 00:04:13,520 If this is a play for Seeker. 46 00:04:22,089 --> 00:04:24,520 Called Catch Snitch for this particular team. 47 00:04:28,040 --> 00:04:33,740 There are scoresheet update by 150 and will return the value that results from us replacing the placeholder 48 00:04:33,740 --> 00:04:35,120 from this simulated play. 49 00:04:46,170 --> 00:04:47,880 If this is a play for keeper. 50 00:04:58,230 --> 00:05:03,120 Then there are no points associated with them, so we're just going to return the simulated plane. 51 00:05:17,340 --> 00:05:18,990 Otherwise, return nothing. 52 00:05:27,280 --> 00:05:31,990 You know what, I want to clean this code up a little, instead of having so many return keywords, 53 00:05:32,320 --> 00:05:34,120 I'm going to set up a variable outside. 54 00:05:34,120 --> 00:05:38,170 The scope of the AFL's string value is equal to an empty blank string. 55 00:05:40,560 --> 00:05:43,380 I'm going to update the string inside each block. 56 00:06:05,770 --> 00:06:08,080 And I'm going to have one return keyword at the end. 57 00:06:13,100 --> 00:06:14,260 That looks a lot better to me. 58 00:06:15,160 --> 00:06:19,600 That's all four Part five, we're going to test this function in part seven, but in the next video, 59 00:06:19,600 --> 00:06:21,280 we need to apply some quality control.