All right, now we're really going to cover the remaining tasks for Part five inside game Java, we're
going to create a method called SIMULATE that returns a string and the string it returns is going to
be a simulation from a play that it receives.
So first from the play we're going to get the placeholder string placeholder is equal to get placeholder
from the play.
And then we need to get a random team team team from the Hashmat for which we're going to assign this
play.
We'll have to create another function that returns a team.
Public team, let's call it get random team.
And our hash map stores, every team has a key.
But from the hash map, we're going to get our collection of keys as a key set.
And now we're going to grab a random key from this key set.
The issue is that indexing sets is kind of annoying, but thankfully, we can call a method called Turei
that returns an array from this set to array returns an array of objects.
It's not specific to the type of object.
So we're just going to have to store our array of teams as an object array.
And now here we want to return a random team from the team's array.
Before we do that, I'll create another function called a random.
It returns an integer.
And accepts a range from which it will get that random number.
Notice that I'm never performing more than one task in a function, every function only does one task,
and this function is going to perform the task of returning a random number between zero and one less.
The range return method, random times range.
And we'll have to typecast that value to.
And back here, we'll say teams and grab a random index inside the length of the team's array.
And our function doesn't know what type of object this is, so we'll have to typecast it to team and
perfect.
That was a lot of steps for the one piece of logic we have to include right here.
Team team is able to get a random team and that looks really nice.
So we have our placeholder.
We have our random team, some in the crowd, and if else, if else statement that compares the placeholder
against three possible cases.
Team get positioned chaser.
And I hope you can see why I'm not playing the writing chaser, because this is an important value that
dictates how this code runs and a value that isn't supposed to change.
We need to ensure that it doesn't.
And that's why we put a constant instead, it's a lot safer anyways.
If this is a play for chasers called Coffle Score for this particular team, there are should update
by 10.
There are three chasers we have to choose from.
So we'll get a random chaser from the team team douget chasers and we'll get a random index from the
length of the chasers array.
And finally, we'll return the value that results from us replacing the placeholder from the simulated
play.
If this is a play for Seeker.
Called Catch Snitch for this particular team.
There are scoresheet update by 150 and will return the value that results from us replacing the placeholder
from this simulated play.
If this is a play for keeper.
Then there are no points associated with them, so we're just going to return the simulated plane.
Otherwise, return nothing.
You know what, I want to clean this code up a little, instead of having so many return keywords,
I'm going to set up a variable outside.
The scope of the AFL's string value is equal to an empty blank string.
I'm going to update the string inside each block.
And I'm going to have one return keyword at the end.
That looks a lot better to me.
That's all four Part five, we're going to test this function in part seven, but in the next video,
we need to apply some quality control.