This short video is a challenge to give
you a chance to practice some of what
we've done so far. So the first part of
the challenge is to modify the show
function in the Player class, so that it
also prints the details for the weapon
that the players have. Now as well as
printing the Players name, lives, level
and score, it should also print the name
of their weapon, as well as the damage
that that weapon inflicts. Now use the
modified show function in main, to print
out tim's details, including the weapon,
instead of just the weapon. So in other
words, instead of just printing tim dot
weapon.name, use the modified show
function. Now as a hint, review the
video on String concatenation and
interpolation, to remind yourself how to
include more complex expressions in a
string. Now for the second part of the
challenge, modify the code in the main
function, so that louise gets the sword
before tim replaces it with a spear. So
the point here is to give Louise the
same sword that Tim had, not to create a
new one. And use the show function to
display the details for louise, to check
the your code's working. So that's it -
that's the challenge. Pause the video
here and see how you go at the challenge,
and I'll see you when you get back.
So there's very rarely one way to write
code, so your solution may well be
different to mine. The real test is that
the weapon details are displayed when
you call the show function. So for my
solution, I'm going to include the two
properties of the weapon in the output
string. So I'm gonna go back to the
Player class. I'm gonna come down here
below score. I'm going to type
weapon : then the dollar sign, left and
right curly braces, weapon.name. Then
on the next line, it's going to be
damaged, so damage :
dollar sign left and right curly braces,
weapon.damageInflicted. Now remember
that we need to enclose more complex
expressions in curly braces, as I've done
here. So if you try that without the
curly braces, it'll actually print out
the dot name as a literal string. So the
code, for example, on line 14, without
the curly braces - you'll get a dot with
the characters n a m e printed out
instead. Alright, so now that we've done
that, in our main function - let's go back
to that - and what we want to do is
replace the println statements with a
call to tim's show function. So let's do
that first.
So this is the one down here now, as you can
see on line 25. So I'm going to comment
that line 25 out, then on the next
line, I'm going to do a tim.dot show.
So let's run the program to make sure
that it's actually working. Now if you
get this situation pop up - well you can see
what's happened here and it's not actually
printing out the full values, and it seems
to be not taking the changes that we've
made - make sure that you do go back and
do a Clean Project, and go back and make
the module again. And then go back and
right-click here and select Run Main
kt. That should fix it generally, and you
can see it has done that now - we're
seeing the new code output. So it appears to
have been cached - or the previous version
of running the code was cached - and by
doing it this way, we're clearing it out
and making sure that we're running the
latest version, and you can see the
output is showing there on the screen.
We've got the revised show function
which is also working at the top, if we
scroll up as well, the weapon and the
damage, and that clearly wasn't working
before we made the change.
But the change we're looking at here, is
tim has got weapon, sword and damage
showing. Alright, so that's the first part
of the challenge. The second part of the
challenge; what we can do is we can
assign, or we should assign, tim's
weapon to louise, but before tim picks up
the spear. So the code for that, we're
going to come down here, after the tim dot
show line. We're going to type louise dot
weapon is equal to tim.weapon, and
on the next line, louise.show.
I'll just make a bit of space there.
Alright, so you can think of this as
louise taking the sword from tim. So
we're assigning tim's weapon to louise's
weapon property on line 28, and then
obviously, line 29 is showing it. Now it's
important to do that before assigning
the spear to tim. It won't work if we try
to do it the other way around, because
there'll be no reference to the sword,
once tim gets the spear. So let's just
try running this to make sure that it
works. And again, you can see that we've
got these funny caching issues, which may
or may not come up for you. We can't see
any output showing from louise, so we
can actually just try running it here
first. That doesn't do any good. We can
just try doing a clean again. We can
make the module again. It's frustrating
having to do this - I hope Google will fix
this soon. We'll try running it again, and
this time we're getting the right output -
you can see we're getting louise's
output showing on the screen there, now.
And again, you can see in this case, that
we've got tim showing the sword. Louise
is now seeing, we're seeing the sword
for louise, then we're getting the
output of the spear only and that's
because, at the moment, we're just
printing the weapon.name on line 32. So
we could fix that as well. Let's do that -
let's comment that out - and we'll put tim
show. We'll run that and see whether
that works for us, and unfortunately it
doesn't work again. We're not really having much
fun with his are we? So we'll actually,
clean again, we'll make the module. We'll
try running it again, and that's better. Now
you can see we've got the output, and
this time we've got the first one - the
first line, which was this output line
here, on line 26. We're seeing tim's got
the sword. Then the output on line 29 is
louise output, and then finally, the
output on line 33, we've seen the weapon
and the damage as well. Alright, so
that's the challenge solved. Now there
are other ways you could have solved
this; you may have created a new sword
variable, for example, and assigned
tim's weapon to that first. That would
also work, and as long as louise has
the sword when you run the program, then
Congratulations. Alrighty, so that's
the challenge completed. So I'm going to
stop this video here. In the next video,
we're going to let our players have more
than one instance of an object - some loot
that they'll be picking up. So I'll see
you in the next video.