1
00:00:00,458 --> 00:00:02,207
(fun music)

2
00:00:02,207 --> 00:00:04,290
(typing)

3
00:00:05,207 --> 00:00:06,562
Okay so now that our

4
00:00:06,562 --> 00:00:08,515
loot's been displayed correctly.

5
00:00:08,515 --> 00:00:10,047
It's time to look at how we can deal with

6
00:00:10,047 --> 00:00:11,834
more than one piece of loot.

7
00:00:11,834 --> 00:00:13,447
Now I'm going to give Tim some more loot

8
00:00:13,447 --> 00:00:14,570
in the main function.

9
00:00:14,570 --> 00:00:15,672
So let's kind of have a look at that.

10
00:00:15,672 --> 00:00:20,439
So what we're going to do is add some chest armour as well.

11
00:00:20,439 --> 00:00:22,892
So I'm going to come down here after the add.

12
00:00:22,892 --> 00:00:26,312
Tim inventory dot add I guaranteed sir two more lines.

13
00:00:26,312 --> 00:00:30,145
I'm going to do a val chest armour equals loot.

14
00:00:33,320 --> 00:00:36,276
Then double quotes plus three.

15
00:00:36,276 --> 00:00:37,276
Chest armour.

16
00:00:39,694 --> 00:00:42,694
It's going to be loot type dot armour

17
00:00:43,598 --> 00:00:45,515
and so the value is 80.

18
00:00:47,322 --> 00:00:49,696
Then we're going to do Tim dot

19
00:00:49,696 --> 00:00:52,196
inventory dot add chest armour.

20
00:00:54,475 --> 00:00:56,173
Now as we're dealing with currency here,

21
00:00:56,173 --> 00:00:57,856
the monetary value of the loot,

22
00:00:57,856 --> 00:01:01,645
we could also type 80 point zero zero. Like so.

23
00:01:01,645 --> 00:01:02,928
Which is how--

24
00:01:02,928 --> 00:01:06,026
which is more like how monetary values are usually written.

25
00:01:06,026 --> 00:01:08,514
And that also helps anyone reading the code

26
00:01:08,514 --> 00:01:11,078
to quickly spot that we're dealing with money here.

27
00:01:11,078 --> 00:01:12,808
Kotlin doesn't care in any case.

28
00:01:12,808 --> 00:01:14,470
A single zero after the decimal point

29
00:01:14,470 --> 00:01:16,298
is all that Kotlin needs.

30
00:01:16,298 --> 00:01:19,131
Alright, so let's run the programme.

31
00:01:25,611 --> 00:01:27,830
Now at this point we're still only getting

32
00:01:27,830 --> 00:01:30,064
the one inventory item printed.

33
00:01:30,064 --> 00:01:33,122
So we need to modify our show inventory function

34
00:01:33,122 --> 00:01:36,162
so that it prints all the items in the list.

35
00:01:36,162 --> 00:01:39,676
So to do that we'll go to our player class.

36
00:01:39,676 --> 00:01:41,188
Show inventory function.

37
00:01:41,188 --> 00:01:42,784
And what we need to do

38
00:01:42,784 --> 00:01:45,758
or what we need is something called a for loop.

39
00:01:45,758 --> 00:01:48,760
Now a for loop is a way to repeat a section of code

40
00:01:48,760 --> 00:01:51,400
for a range of items or values.

41
00:01:51,400 --> 00:01:53,547
So in this case we want to loop over

42
00:01:53,547 --> 00:01:55,486
all the items in our list.

43
00:01:55,486 --> 00:01:57,758
So to do that we'll come and add some code.

44
00:01:57,758 --> 00:01:58,952
So we're going to put the code under

45
00:01:58,952 --> 00:02:01,536
the first print of heading.

46
00:02:01,536 --> 00:02:02,486
Under the --

47
00:02:02,486 --> 00:02:06,618
Where its got the name of the person and then inventory.

48
00:02:06,618 --> 00:02:09,961
Below that for parenthesis item

49
00:02:09,961 --> 00:02:12,545
in inventory close parenthesis.

50
00:02:14,007 --> 00:02:17,924
And then we're just going to do a println item.

51
00:02:21,941 --> 00:02:23,263
And what we can now do is

52
00:02:23,263 --> 00:02:24,740
get rid of that inventory dot get zero.

53
00:02:24,740 --> 00:02:26,174
We're not going to be using that anymore.

54
00:02:26,174 --> 00:02:28,216
And we'll leave the equal signs there.

55
00:02:28,216 --> 00:02:29,641
So this code now iterates over

56
00:02:29,641 --> 00:02:32,406
each of the items in the array lists.

57
00:02:32,406 --> 00:02:34,975
Then executes the block of code for each one.

58
00:02:34,975 --> 00:02:37,808
Now in languages such as Java or C,

59
00:02:37,808 --> 00:02:41,551
this form of the for loop is called a for each loop.

60
00:02:41,551 --> 00:02:43,766
Our item starts off being assigned.

61
00:02:43,766 --> 00:02:46,236
You can see item in here below 29.

62
00:02:46,236 --> 00:02:49,303
That starts off being assigned the first item in the list.

63
00:02:49,303 --> 00:02:51,233
And all we do in the block is print it.

64
00:02:51,233 --> 00:02:52,934
So we're printing it out here as you can see

65
00:02:52,934 --> 00:02:55,916
in the code block on line 30.

66
00:02:55,916 --> 00:02:59,374
Now because we have overridden the two string function,

67
00:02:59,374 --> 00:03:01,236
we actually get a neat line of output

68
00:03:01,236 --> 00:03:03,667
that shows the items name and value

69
00:03:03,667 --> 00:03:05,112
as well as the type of loot it is.

70
00:03:05,112 --> 00:03:06,754
We'll run that shortly to see.

71
00:03:06,754 --> 00:03:09,112
Now once the first item's been processed

72
00:03:09,112 --> 00:03:12,104
our item is then assigned the next item in the list.

73
00:03:12,104 --> 00:03:13,282
Then that's printed

74
00:03:13,282 --> 00:03:15,853
and then we go back around to get the third item and so on.

75
00:03:15,853 --> 00:03:17,684
Now we've only got two items

76
00:03:17,684 --> 00:03:20,723
so our loop should terminate after processing the armour.

77
00:03:20,723 --> 00:03:22,130
So lets just run the programme

78
00:03:22,130 --> 00:03:25,963
to make sure we do get both items printed out.

79
00:03:34,378 --> 00:03:36,666
You can see we've got Tim's Inventory,

80
00:03:36,666 --> 00:03:39,143
red potion, plus three chest armour

81
00:03:39,143 --> 00:03:40,722
so it's clearly now working

82
00:03:40,722 --> 00:03:43,039
and doing the right thing which is good.

83
00:03:43,039 --> 00:03:45,216
Now if there were more bits of item inventory

84
00:03:45,216 --> 00:03:46,801
that'd be printed as well.

85
00:03:46,801 --> 00:03:49,346
So what I'm going to do is give Tim some more loot.

86
00:03:49,346 --> 00:03:51,323
This time we're not going to create variables

87
00:03:51,323 --> 00:03:52,294
to store them in.

88
00:03:52,294 --> 00:03:53,472
We're just going to add them directly

89
00:03:53,472 --> 00:03:55,524
to the inventory for Tim.

90
00:03:55,524 --> 00:03:56,826
So to do that we're going to go down

91
00:03:56,826 --> 00:03:59,008
the line after the last add.

92
00:03:59,008 --> 00:04:00,058
Actually, what we'll do is

93
00:04:00,058 --> 00:04:01,651
we'll do it after the show of inventory.

94
00:04:01,651 --> 00:04:05,984
Print down here. We'll put Tim dot inventory dot add

95
00:04:07,017 --> 00:04:11,434
parenthesis. Lets go for ring of protection plus two.

96
00:04:13,287 --> 00:04:14,447
We're going to call this one

97
00:04:14,447 --> 00:04:17,613
it's going to be a loot type dot ring.

98
00:04:18,817 --> 00:04:22,400
No surprises there. The value will go 40.25

99
00:04:23,567 --> 00:04:24,869
and I forgot something there

100
00:04:24,869 --> 00:04:25,702
I needed to add something in here.

101
00:04:25,702 --> 00:04:30,452
Loot. The type that we are actually adding is of type loot.

102
00:04:30,452 --> 00:04:32,256
And that will fix that error up now.

103
00:04:32,256 --> 00:04:34,688
And that's also confirmation that the fact

104
00:04:34,688 --> 00:04:37,261
that when we defined our array list

105
00:04:37,261 --> 00:04:39,648
we mentioned that it was of type loot.

106
00:04:39,648 --> 00:04:41,707
And you can see that I was getting an error until

107
00:04:41,707 --> 00:04:44,710
I actually made sure that I was creating a loot object.

108
00:04:44,710 --> 00:04:47,170
So that's a confirmation that it's working for us.

109
00:04:47,170 --> 00:04:48,850
Alright let's add one more thing we'll add a potion.

110
00:04:48,850 --> 00:04:53,517
So Tim dot inventory dot add. Loot. Invisibility potion.

111
00:04:57,060 --> 00:05:01,310
Invisibility potion. Loot type dot potion and 35 95

112
00:05:03,297 --> 00:05:05,937
I'll go with the monetary value.

113
00:05:05,937 --> 00:05:07,239
And then we'll do another.

114
00:05:07,239 --> 00:05:09,156
Tim dot show inventory.

115
00:05:11,229 --> 00:05:12,729
So let's run that.

116
00:05:19,644 --> 00:05:20,821
If we scroll up and have a look

117
00:05:20,821 --> 00:05:22,818
we can see that we've got the first time

118
00:05:22,818 --> 00:05:25,189
we called show inventory we had the two items.

119
00:05:25,189 --> 00:05:27,318
The second time we called the show inventory

120
00:05:27,318 --> 00:05:29,902
we got a total of five items showing on the screen.

121
00:05:29,902 --> 00:05:32,154
I've four items showing on the screen now with

122
00:05:32,154 --> 00:05:33,160
the ring of protection plus two and

123
00:05:33,160 --> 00:05:36,928
the invisibility potion added the second time.

124
00:05:36,928 --> 00:05:38,860
So for loops are very useful

125
00:05:38,860 --> 00:05:41,044
when you want to process a list of items.

126
00:05:41,044 --> 00:05:44,046
But they're also useful for repeating a section of code

127
00:05:44,046 --> 00:05:47,314
a set number of times. Now for the rest of this video

128
00:05:47,314 --> 00:05:49,454
we're going to leave our classes

129
00:05:49,454 --> 00:05:51,858
and have a look at some more for loops.

130
00:05:51,858 --> 00:05:53,672
And we're going to be coming back to these classes soon

131
00:05:53,672 --> 00:05:56,238
so instead of deleting all this coding, our main function,

132
00:05:56,238 --> 00:05:58,252
I'm going to comment it all out.

133
00:05:58,252 --> 00:06:01,016
Now a comment in Kotlin starts with two slashes

134
00:06:01,016 --> 00:06:04,266
and any commented lines are ignored by the compiler.

135
00:06:04,266 --> 00:06:06,640
Now fortunately we don't have to type two slashes at

136
00:06:06,640 --> 00:06:08,226
the start of every line

137
00:06:08,226 --> 00:06:11,038
because Android Studio provides an easier way.

138
00:06:11,038 --> 00:06:13,090
So I'm going to select all the code

139
00:06:13,090 --> 00:06:14,678
that we want to comment out.

140
00:06:14,678 --> 00:06:18,050
And it's going to be all the code from the last println,

141
00:06:18,050 --> 00:06:21,802
but not including the coding closing code block

142
00:06:21,802 --> 00:06:26,096
we want to leave that in there, through our parenthesis.

143
00:06:26,096 --> 00:06:28,850
the right curly braces go right up to the start.

144
00:06:28,850 --> 00:06:31,786
Just after the definition of the function.

145
00:06:31,786 --> 00:06:35,434
Then once you've done that you can go to the code menu

146
00:06:35,434 --> 00:06:38,978
and you can select comment with line comment.

147
00:06:38,978 --> 00:06:41,856
And you can see it's added the slashes

148
00:06:41,856 --> 00:06:44,080
the two slashes for every line to save us

149
00:06:44,080 --> 00:06:46,434
having to have typed each one individually.

150
00:06:46,434 --> 00:06:49,095
And again make sure you haven't included the curly braces

151
00:06:49,095 --> 00:06:51,072
we need them to remain uncommented.

152
00:06:51,072 --> 00:06:52,516
Curly braces there on line one

153
00:06:52,516 --> 00:06:55,345
and in this case also on line 46.

154
00:06:55,345 --> 00:06:57,018
Alright so let's try another loop now.

155
00:06:57,018 --> 00:07:00,518
So we're going to type for parenthesis I N

156
00:07:01,740 --> 00:07:05,240
one and two periods 10 closing parenthesis

157
00:07:08,110 --> 00:07:10,132
and add left and right curly braces

158
00:07:10,132 --> 00:07:13,049
and do a println I. Let's run that.

159
00:07:23,213 --> 00:07:26,358
Okay we've got the number's one to 10 printed out.

160
00:07:26,358 --> 00:07:28,002
Now that's not very exciting perhaps,

161
00:07:28,002 --> 00:07:32,676
but we can put any code we want inside the for loop.

162
00:07:32,676 --> 00:07:35,546
So to demonstrate that let's go back up and make a change.

163
00:07:35,546 --> 00:07:37,066
So at the moment we've got println I.

164
00:07:37,066 --> 00:07:42,024
Let's change that to println then put two double quotes

165
00:07:42,024 --> 00:07:44,024
dollar I squared is then

166
00:07:46,869 --> 00:07:51,036
parenthesis left and right curly braces I times I.

167
00:07:53,255 --> 00:07:56,922
Closing parenthesis. Let's try running that.

168
00:08:05,298 --> 00:08:06,902
And you can see we've got the value's of

169
00:08:06,902 --> 00:08:09,306
one to 10 squared showing on the screen.

170
00:08:09,306 --> 00:08:11,420
Now of often if you've got 10 objects

171
00:08:11,420 --> 00:08:13,339
they'll be indexed starting at zero.

172
00:08:13,339 --> 00:08:16,829
Now that's easy to do. We just change the one to a zero.

173
00:08:16,829 --> 00:08:20,412
So we go ahead and do that. Let's run that.

174
00:08:31,041 --> 00:08:33,278
Alright so can you spot the problem with doing that though?

175
00:08:33,278 --> 00:08:35,962
The problem is that the loop's gone around

176
00:08:35,962 --> 00:08:37,977
eleven times not 10.

177
00:08:37,977 --> 00:08:40,576
Now this is a very common cause of errors in programmes.

178
00:08:40,576 --> 00:08:44,205
If we start at zero and we want to go around 10 times,

179
00:08:44,205 --> 00:08:46,404
then we need to stop at nine.

180
00:08:46,404 --> 00:08:48,441
Now Kotlin help here by letting us use until

181
00:08:48,441 --> 00:08:51,665
instead of the two dots in the range.

182
00:08:51,665 --> 00:08:53,915
So we can go ahead and change that to

183
00:08:53,915 --> 00:08:56,191
instead of the two dots we can put zero

184
00:08:56,191 --> 00:08:59,941
get rid of the two dots and type in until 10.

185
00:09:01,328 --> 00:09:02,578
If you run that

186
00:09:11,091 --> 00:09:14,702
we correctly get the results from zero through nine now.

187
00:09:14,702 --> 00:09:16,890
When we use until to specify the range

188
00:09:16,890 --> 00:09:19,928
the ending value isn't included in the range.

189
00:09:19,928 --> 00:09:22,265
So that let's us work with values that are more natural.

190
00:09:22,265 --> 00:09:24,078
If there are 10 items to process

191
00:09:24,078 --> 00:09:27,065
its natural to want to put 10 in the range

192
00:09:27,065 --> 00:09:28,378
rather than nine.

193
00:09:28,378 --> 00:09:30,005
So be aware of the differences though.

194
00:09:30,005 --> 00:09:31,915
Using two dots like we had earlier

195
00:09:31,915 --> 00:09:34,278
means that the final value will be included,

196
00:09:34,278 --> 00:09:36,391
But using until which is what we do here

197
00:09:36,391 --> 00:09:37,558
means it wont.

198
00:09:38,515 --> 00:09:40,628
Now if you want it to iterate over the range of numbers

199
00:09:40,628 --> 00:09:43,754
in reverse order we can use down to.

200
00:09:43,754 --> 00:09:46,837
So we can change that to for I N 10.

201
00:09:49,264 --> 00:09:53,347
Instead of until we'll delete that. Down to zero.

202
00:09:55,890 --> 00:09:57,890
Like so. Let's run that.

203
00:10:04,862 --> 00:10:06,325
And so we've got the value starting from 10

204
00:10:06,325 --> 00:10:08,954
down and to and including zero.

205
00:10:08,954 --> 00:10:11,667
Now there isn't a noninclusive version of down to

206
00:10:11,667 --> 00:10:14,017
so the final value as you can see here,

207
00:10:14,017 --> 00:10:15,851
zero in this case, will be included.

208
00:10:15,851 --> 00:10:17,094
So keep that in mind.

209
00:10:17,094 --> 00:10:20,031
Now there's one last thing we can add to our for loop

210
00:10:20,031 --> 00:10:22,784
and that's a step. So let's have a look at that.

211
00:10:22,784 --> 00:10:26,445
So we're going to go back and change this to for I and 10.

212
00:10:26,445 --> 00:10:31,445
Down to zero by the end of it we're going to add step two.

213
00:10:31,745 --> 00:10:32,995
Let's run that.

214
00:10:38,956 --> 00:10:41,220
And you can see we've got a different output now.

215
00:10:41,220 --> 00:10:43,783
Now the loop counts down in steps of two.

216
00:10:43,783 --> 00:10:45,593
Now the step has to be positive

217
00:10:45,593 --> 00:10:47,969
so you can't use a negative step to count backwards.

218
00:10:47,969 --> 00:10:50,193
That's what down to is for.

219
00:10:50,193 --> 00:10:52,116
So that's for loops.

220
00:10:52,116 --> 00:10:54,430
We're going to be using them a lot in the course

221
00:10:54,430 --> 00:10:55,953
and you'll get plenty of practise using them,

222
00:10:55,953 --> 00:10:57,753
but a bit more practise won't hurt though

223
00:10:57,753 --> 00:11:01,764
so I'm going to finish this video with a challenge.

224
00:11:01,764 --> 00:11:03,930
Alright so the challenge is to create a for loop

225
00:11:03,930 --> 00:11:06,516
that prints out the numbers from zero to 100

226
00:11:06,516 --> 00:11:10,614
that are divisible by both three and five.

227
00:11:10,614 --> 00:11:12,680
Now as a hint here you'll need to include

228
00:11:12,680 --> 00:11:14,880
an if inside your loop.

229
00:11:14,880 --> 00:11:17,243
Now I don't think I've mentioned the modulus operator,

230
00:11:17,243 --> 00:11:19,403
the percent. It returns the remainder

231
00:11:19,403 --> 00:11:21,943
after dividing one number into another.

232
00:11:21,943 --> 00:11:24,253
Now if the remainder's zero,

233
00:11:24,253 --> 00:11:27,643
then the second number divides evenly into the first.

234
00:11:27,643 --> 00:11:32,643
So some examples to try as you can see on the screen there.

235
00:11:32,923 --> 00:11:34,754
Println seven modulus three.

236
00:11:34,754 --> 00:11:36,404
So three goes into seven twice with a remainder of one.

237
00:11:36,404 --> 00:11:38,380
Println eight modulus four.

238
00:11:38,380 --> 00:11:41,389
Four goes into eight exactly twice with no remainder.

239
00:11:41,389 --> 00:11:43,433
And the third one println nine modulus five.

240
00:11:43,433 --> 00:11:46,763
Five goes into nine once with a remainder of four.

241
00:11:46,763 --> 00:11:48,990
And you can see the code on the next line.

242
00:11:48,990 --> 00:11:50,429
If sixteen modulus four equals four equals zero.

243
00:11:50,429 --> 00:11:53,262
Four divides exactly into sixteen.

244
00:11:54,732 --> 00:11:57,372
Now another hint for you to solve this challenge is that

245
00:11:57,372 --> 00:12:00,509
Your code will be more efficient if you use a step.

246
00:12:00,509 --> 00:12:02,541
So that's the challenge pause the video

247
00:12:02,541 --> 00:12:05,458
and I'll see you when you get back.

248
00:12:06,504 --> 00:12:09,736
Okay so as usual there are several ways to right this code

249
00:12:09,736 --> 00:12:13,038
It's very rare that there's only one way to do something

250
00:12:13,038 --> 00:12:16,170
as I mentioned before when programming.

251
00:12:16,170 --> 00:12:17,592
Now a simple solution would be to test each number

252
00:12:17,592 --> 00:12:19,537
to see if it's divisible by three.

253
00:12:19,537 --> 00:12:23,678
Then test it again to see if five also divides into it.

254
00:12:23,678 --> 00:12:26,533
Now we can make that more efficient by starting at three

255
00:12:26,533 --> 00:12:28,530
and looping in steps of three.

256
00:12:28,530 --> 00:12:30,330
So let's have a go at doing that.

257
00:12:30,330 --> 00:12:32,469
So were going to leave that first for line in there.

258
00:12:32,469 --> 00:12:36,826
That first for block. You can type for parenthesis value

259
00:12:36,826 --> 00:12:39,851
in three dot dot or period 100 step three.

260
00:12:39,851 --> 00:12:44,518
Then I put a code block. Then I put an if test in there.

261
00:12:51,745 --> 00:12:52,920
If parenthesis value modulus 5 is equal to zero,

262
00:12:52,920 --> 00:12:57,087
then a code block we're going to do println value.

263
00:12:58,543 --> 00:13:02,210
So much of this is looping from three to 100

264
00:13:04,848 --> 00:13:06,780
in steps of three.

265
00:13:06,780 --> 00:13:09,318
Now you can also loop in steps of five instead.

266
00:13:09,318 --> 00:13:11,279
Which type would be more efficient?

267
00:13:11,279 --> 00:13:12,984
Looping in steps of three or in steps of five?

268
00:13:12,984 --> 00:13:16,401
Now if the remainder the modulus is zero,

269
00:13:17,253 --> 00:13:21,181
then five divides exactly into the number.

270
00:13:21,181 --> 00:13:23,264
So we're printing it out.

271
00:13:26,131 --> 00:13:28,522
Now if your solution produces the same values as mine.

272
00:13:28,522 --> 00:13:31,398
Let's just run this to confirm.

273
00:13:31,398 --> 00:13:35,898
Now you can see the examples you should be getting are

274
00:13:37,087 --> 00:13:39,337
15, 30, 45, 60, 75, and 90.

275
00:13:42,252 --> 00:13:46,164
If your solution produces the same results,

276
00:13:46,164 --> 00:13:49,706
then well done you've completed the challenge.

277
00:13:49,706 --> 00:13:52,914
Now looping in steps of five would have been more

278
00:13:52,914 --> 00:13:54,828
efficient. There'd only have been 20 values

279
00:13:54,828 --> 00:13:58,009
to test inside the loop instead of the 33.

280
00:13:58,009 --> 00:13:59,989
So that would be an improvement of over 50 percent.

281
00:13:59,989 --> 00:14:02,748
Alright so in the next video

282
00:14:02,748 --> 00:14:03,672
we're going to go back to our classes

283
00:14:03,672 --> 00:14:04,794
and look at some more functions

284
00:14:04,794 --> 00:14:06,259
we can add to the player class

285
00:14:06,259 --> 00:14:07,428
So see you in the next video.

