(fun music)
(typing)
Okay so now that our
loot's been displayed correctly.
It's time to look at how we can deal with
more than one piece of loot.
Now I'm going to give Tim some more loot
in the main function.
So let's kind of have a look at that.
So what we're going to do is add some chest armour as well.
So I'm going to come down here after the add.
Tim inventory dot add I guaranteed sir two more lines.
I'm going to do a val chest armour equals loot.
Then double quotes plus three.
Chest armour.
It's going to be loot type dot armour
and so the value is 80.
Then we're going to do Tim dot
inventory dot add chest armour.
Now as we're dealing with currency here,
the monetary value of the loot,
we could also type 80 point zero zero. Like so.
Which is how--
which is more like how monetary values are usually written.
And that also helps anyone reading the code
to quickly spot that we're dealing with money here.
Kotlin doesn't care in any case.
A single zero after the decimal point
is all that Kotlin needs.
Alright, so let's run the programme.
Now at this point we're still only getting
the one inventory item printed.
So we need to modify our show inventory function
so that it prints all the items in the list.
So to do that we'll go to our player class.
Show inventory function.
And what we need to do
or what we need is something called a for loop.
Now a for loop is a way to repeat a section of code
for a range of items or values.
So in this case we want to loop over
all the items in our list.
So to do that we'll come and add some code.
So we're going to put the code under
the first print of heading.
Under the --
Where its got the name of the person and then inventory.
Below that for parenthesis item
in inventory close parenthesis.
And then we're just going to do a println item.
And what we can now do is
get rid of that inventory dot get zero.
We're not going to be using that anymore.
And we'll leave the equal signs there.
So this code now iterates over
each of the items in the array lists.
Then executes the block of code for each one.
Now in languages such as Java or C,
this form of the for loop is called a for each loop.
Our item starts off being assigned.
You can see item in here below 29.
That starts off being assigned the first item in the list.
And all we do in the block is print it.
So we're printing it out here as you can see
in the code block on line 30.
Now because we have overridden the two string function,
we actually get a neat line of output
that shows the items name and value
as well as the type of loot it is.
We'll run that shortly to see.
Now once the first item's been processed
our item is then assigned the next item in the list.
Then that's printed
and then we go back around to get the third item and so on.
Now we've only got two items
so our loop should terminate after processing the armour.
So lets just run the programme
to make sure we do get both items printed out.
You can see we've got Tim's Inventory,
red potion, plus three chest armour
so it's clearly now working
and doing the right thing which is good.
Now if there were more bits of item inventory
that'd be printed as well.
So what I'm going to do is give Tim some more loot.
This time we're not going to create variables
to store them in.
We're just going to add them directly
to the inventory for Tim.
So to do that we're going to go down
the line after the last add.
Actually, what we'll do is
we'll do it after the show of inventory.
Print down here. We'll put Tim dot inventory dot add
parenthesis. Lets go for ring of protection plus two.
We're going to call this one
it's going to be a loot type dot ring.
No surprises there. The value will go 40.25
and I forgot something there
I needed to add something in here.
Loot. The type that we are actually adding is of type loot.
And that will fix that error up now.
And that's also confirmation that the fact
that when we defined our array list
we mentioned that it was of type loot.
And you can see that I was getting an error until
I actually made sure that I was creating a loot object.
So that's a confirmation that it's working for us.
Alright let's add one more thing we'll add a potion.
So Tim dot inventory dot add. Loot. Invisibility potion.
Invisibility potion. Loot type dot potion and 35 95
I'll go with the monetary value.
And then we'll do another.
Tim dot show inventory.
So let's run that.
If we scroll up and have a look
we can see that we've got the first time
we called show inventory we had the two items.
The second time we called the show inventory
we got a total of five items showing on the screen.
I've four items showing on the screen now with
the ring of protection plus two and
the invisibility potion added the second time.
So for loops are very useful
when you want to process a list of items.
But they're also useful for repeating a section of code
a set number of times. Now for the rest of this video
we're going to leave our classes
and have a look at some more for loops.
And we're going to be coming back to these classes soon
so instead of deleting all this coding, our main function,
I'm going to comment it all out.
Now a comment in Kotlin starts with two slashes
and any commented lines are ignored by the compiler.
Now fortunately we don't have to type two slashes at
the start of every line
because Android Studio provides an easier way.
So I'm going to select all the code
that we want to comment out.
And it's going to be all the code from the last println,
but not including the coding closing code block
we want to leave that in there, through our parenthesis.
the right curly braces go right up to the start.
Just after the definition of the function.
Then once you've done that you can go to the code menu
and you can select comment with line comment.
And you can see it's added the slashes
the two slashes for every line to save us
having to have typed each one individually.
And again make sure you haven't included the curly braces
we need them to remain uncommented.
Curly braces there on line one
and in this case also on line 46.
Alright so let's try another loop now.
So we're going to type for parenthesis I N
one and two periods 10 closing parenthesis
and add left and right curly braces
and do a println I. Let's run that.
Okay we've got the number's one to 10 printed out.
Now that's not very exciting perhaps,
but we can put any code we want inside the for loop.
So to demonstrate that let's go back up and make a change.
So at the moment we've got println I.
Let's change that to println then put two double quotes
dollar I squared is then
parenthesis left and right curly braces I times I.
Closing parenthesis. Let's try running that.
And you can see we've got the value's of
one to 10 squared showing on the screen.
Now of often if you've got 10 objects
they'll be indexed starting at zero.
Now that's easy to do. We just change the one to a zero.
So we go ahead and do that. Let's run that.
Alright so can you spot the problem with doing that though?
The problem is that the loop's gone around
eleven times not 10.
Now this is a very common cause of errors in programmes.
If we start at zero and we want to go around 10 times,
then we need to stop at nine.
Now Kotlin help here by letting us use until
instead of the two dots in the range.
So we can go ahead and change that to
instead of the two dots we can put zero
get rid of the two dots and type in until 10.
If you run that
we correctly get the results from zero through nine now.
When we use until to specify the range
the ending value isn't included in the range.
So that let's us work with values that are more natural.
If there are 10 items to process
its natural to want to put 10 in the range
rather than nine.
So be aware of the differences though.
Using two dots like we had earlier
means that the final value will be included,
But using until which is what we do here
means it wont.
Now if you want it to iterate over the range of numbers
in reverse order we can use down to.
So we can change that to for I N 10.
Instead of until we'll delete that. Down to zero.
Like so. Let's run that.
And so we've got the value starting from 10
down and to and including zero.
Now there isn't a noninclusive version of down to
so the final value as you can see here,
zero in this case, will be included.
So keep that in mind.
Now there's one last thing we can add to our for loop
and that's a step. So let's have a look at that.
So we're going to go back and change this to for I and 10.
Down to zero by the end of it we're going to add step two.
Let's run that.
And you can see we've got a different output now.
Now the loop counts down in steps of two.
Now the step has to be positive
so you can't use a negative step to count backwards.
That's what down to is for.
So that's for loops.
We're going to be using them a lot in the course
and you'll get plenty of practise using them,
but a bit more practise won't hurt though
so I'm going to finish this video with a challenge.
Alright so the challenge is to create a for loop
that prints out the numbers from zero to 100
that are divisible by both three and five.
Now as a hint here you'll need to include
an if inside your loop.
Now I don't think I've mentioned the modulus operator,
the percent. It returns the remainder
after dividing one number into another.
Now if the remainder's zero,
then the second number divides evenly into the first.
So some examples to try as you can see on the screen there.
Println seven modulus three.
So three goes into seven twice with a remainder of one.
Println eight modulus four.
Four goes into eight exactly twice with no remainder.
And the third one println nine modulus five.
Five goes into nine once with a remainder of four.
And you can see the code on the next line.
If sixteen modulus four equals four equals zero.
Four divides exactly into sixteen.
Now another hint for you to solve this challenge is that
Your code will be more efficient if you use a step.
So that's the challenge pause the video
and I'll see you when you get back.
Okay so as usual there are several ways to right this code
It's very rare that there's only one way to do something
as I mentioned before when programming.
Now a simple solution would be to test each number
to see if it's divisible by three.
Then test it again to see if five also divides into it.
Now we can make that more efficient by starting at three
and looping in steps of three.
So let's have a go at doing that.
So were going to leave that first for line in there.
That first for block. You can type for parenthesis value
in three dot dot or period 100 step three.
Then I put a code block. Then I put an if test in there.
If parenthesis value modulus 5 is equal to zero,
then a code block we're going to do println value.
So much of this is looping from three to 100
in steps of three.
Now you can also loop in steps of five instead.
Which type would be more efficient?
Looping in steps of three or in steps of five?
Now if the remainder the modulus is zero,
then five divides exactly into the number.
So we're printing it out.
Now if your solution produces the same values as mine.
Let's just run this to confirm.
Now you can see the examples you should be getting are
15, 30, 45, 60, 75, and 90.
If your solution produces the same results,
then well done you've completed the challenge.
Now looping in steps of five would have been more
efficient. There'd only have been 20 values
to test inside the loop instead of the 33.
So that would be an improvement of over 50 percent.
Alright so in the next video
we're going to go back to our classes
and look at some more functions
we can add to the player class
So see you in the next video.