Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Static fields, like static methods, are called directly on the class name - unless you're already in the class.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we have a random
number generator.
0:00
Let's use it to make it possible for the
towers to occasionally miss their targets.
0:02
Let's start by deciding how accurate
we want our towers to be and
0:07
then make that a constant.
0:11
We'll set the accuracy to .75.
0:13
Meaning on average 75% of the time
the tower will hit its target.
0:17
No let's create a method
that when called returns
0:23
true when the tower
successfully hits the target.
0:26
We'll call it IsSuccessfulShot.
0:29
Now here's where we can use
our random number generator.
0:32
We can ask for a number between zero and
one by calling NextDouble, and
0:35
return true if the number generated
is less than the accuracy.
0:39
The .NET random number
generator is uniformly random,
0:44
meaning every value between zero and one
has an equal chance of being generated.
0:48
So 75% of the time it will generate
a number between zero and 0.75.
0:53
So if the number it
generates is less than 0.75,
0:59
we'll count that as hitting
the target otherwise it's a miss.
1:02
Notice that here,
1:06
when using the random field,
we're referencing the Tower class name.
1:07
This is how we use static
members of a class.
1:12
Actually, this is only necessary
when accessing the number from
1:16
outside the class in order to
state which class is being used.
1:20
However, since we're already in
the Tower class, we don't need this.
1:24
We could type it here but
we don't need to.
1:29
We can just use it like any
other field in the class.
1:31
What we can't do is type this,
because this
1:35
refers to the current object and the
object doesn't have the random variable.
1:40
Only the Tower class itself has one.
1:46
Now we can check for successful shots
in the fire on invaders method.
1:49
We only want to decrease the health of the
invader the tower attempts to shoot at,
1:53
if it's a successful shot.
1:57
So I'll wrap this call to decrease
health by this if statement like so.
1:59
To see what's happening
while the game runs,
2:07
let's print the results
to the console here.
2:09
Let's add the system namespace
to the top of the file.
2:13
And we can remove references to it here.
2:19
Now, let's print something
if it's a successful shot.
2:26
So I'll say, shot at and hit an invader.
2:31
Let's print something else if they miss.
2:37
We'll say, shot at and missed an invader.
2:44
Don't forget to type else here.
2:52
Let's also print something if
the invader has been neutralized.
2:55
So I’ll say if invader.IsNeutralized.
2:58
Then we'll print Neutralized an invader.
3:10
Now let's compile and run the game
to see how this changes things.
3:19
As you can see, the player wins
sometimes and loses sometimes.
3:30
Adding a little randomness increases
the challenge of playing the game.
3:35
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up