1
00:00:08,940 --> 00:00:12,332
Well, we're moving onto the next module,
where we're going

2
00:00:12,332 --> 00:00:15,110
to learn how we can make decisions in our
programs.

3
00:00:16,170 --> 00:00:21,405
Here are the learning objectives for the
module.

4
00:00:21,405 --> 00:00:30,021
So I said we're going to learn about how
we can make decisions in our programs.

5
00:00:30,021 --> 00:00:33,254
So what kinds of decisions do we need to
make?

6
00:00:33,254 --> 00:00:34,104
So, here are some examples.

7
00:00:34,104 --> 00:00:35,271
What does our program have to decide to
do?

8
00:00:35,271 --> 00:00:36,276
We might decide, based on user input.

9
00:00:36,276 --> 00:00:39,830
Which direction we should move the
player's avatar.

10
00:00:39,830 --> 00:00:43,780
So, for example, if they're using wasd,
then you want to

11
00:00:43,780 --> 00:00:46,940
move up with w and left with a and so on.

12
00:00:46,940 --> 00:00:52,350
So movement based on user is one of the
things we decide about.

13
00:00:53,900 --> 00:00:59,700
Another thing we decide about

14
00:00:59,700 --> 00:01:05,036
is Is the selection control

15
00:01:05,036 --> 00:01:09,214
structure.
That's

16
00:01:09,214 --> 00:01:14,164
what it's called because

17
00:01:14,164 --> 00:01:19,114
we're selecting between

18
00:01:19,114 --> 00:01:24,739
multiple courses of action in

19
00:01:24,739 --> 00:01:28,520
our program.
Okay.

20
00:01:28,520 --> 00:01:32,949
So we use the selection control structure
to detect

21
00:01:32,949 --> 00:01:37,620
collisions and also we use it to resolve
collisions.

22
00:01:38,810 --> 00:01:44,440
So the resolution of a collision may be
based on a variety of different factors.

23
00:01:44,440 --> 00:01:47,140
Are we wearing enough armor that the
rocket

24
00:01:47,140 --> 00:01:51,370
propelled grenade bounces off when the
collision occurs?

25
00:01:51,370 --> 00:01:53,581
Or do we not have the level of protection

26
00:01:53,581 --> 00:01:57,039
so that something else happens when that
collision occurs.

27
00:01:58,170 --> 00:01:59,164
Okay.

28
00:01:59,164 --> 00:02:01,030
players do stuff in games right?

29
00:02:01,030 --> 00:02:04,072
That's why they are fun to play and based
on the

30
00:02:04,072 --> 00:02:08,752
players performance, say whether they are
playing a mini game and they

31
00:02:08,752 --> 00:02:12,106
have to have a sort of a minimal bar of
performance,

32
00:02:12,106 --> 00:02:16,960
we can use selection to decide whether
they get, loot or not.

33
00:02:16,960 --> 00:02:18,310
So we might need to decide that.

34
00:02:20,360 --> 00:02:25,789
Players navigate menus in most games, and
so based on their selection of

35
00:02:25,789 --> 00:02:30,640
which menu they want to go to, we might do
that as well.

36
00:02:30,640 --> 00:02:32,800
By the way, menus?

37
00:02:33,820 --> 00:02:37,370
People do not get at all excited about
writing menus.

38
00:02:37,370 --> 00:02:41,160
And virtually every single game you'll
ever play has 'em.

39
00:02:41,160 --> 00:02:45,448
So it's one of the things that you'll end
up doing as a game developer that is

40
00:02:45,448 --> 00:02:49,210
not nearly as exciting as some of the
other things you do.

41
00:02:49,210 --> 00:02:54,130
You can actually build some interesting
menu systems if you choose to do so.

42
00:02:54,130 --> 00:02:55,390
But.

43
00:02:55,390 --> 00:02:57,648
You know, you're going to have to build
them.

44
00:02:57,648 --> 00:02:59,968
Just like tutorials, which has nothing to
with

45
00:02:59,968 --> 00:03:02,868
the selection control structure but
tutorials are another

46
00:03:02,868 --> 00:03:04,608
thing that people tend to not get too

47
00:03:04,608 --> 00:03:08,280
excited about but everyone builds into
their games.

48
00:03:08,280 --> 00:03:09,384
And lots more.

49
00:03:09,384 --> 00:03:10,500
There's lots

50
00:03:10,500 --> 00:03:16,800
more stuff that you might need to do in
terms of deciding how your game performs.

51
00:03:19,150 --> 00:03:22,842
So you should go take an in-lecture quiz,
so that you can tell

52
00:03:22,842 --> 00:03:27,750
me about the statement we're going to look
at today for doing that decision making.

53
00:03:31,860 --> 00:03:38,370
Okay, so this a graphical representation
of the if statement.

54
00:03:38,370 --> 00:03:42,716
And this graphical representation is
what's called a control

55
00:03:42,716 --> 00:03:46,734
flow graph because it graphs how the
control flows through

56
00:03:46,734 --> 00:03:50,342
the program, eh, looks something like a
flow chart

57
00:03:50,342 --> 00:03:53,603
for those of you who have heard of such
things.

58
00:03:53,603 --> 00:03:57,375
We don't tend to use these things as
design mechanisms

59
00:03:57,375 --> 00:04:01,130
anymore, but we do use them to help
visualize.

60
00:04:01,130 --> 00:04:03,370
What the program will do.

61
00:04:03,370 --> 00:04:07,374
And so, let's pretend, because we're going
to do it soon,

62
00:04:07,374 --> 00:04:12,380
let's pretend that we're going to ask the
player for some input.

63
00:04:12,380 --> 00:04:17,310
We're going to ask them a yes or no
question, and if they answer yes, we

64
00:04:17,310 --> 00:04:22,428
want to print an appropriate yes message.
And so, this is sort

65
00:04:22,428 --> 00:04:27,340
of, a picture of how that flow through the
program would work.

66
00:04:27,340 --> 00:04:29,400
We'll ask them the question.

67
00:04:29,400 --> 00:04:33,890
And then we'll check their response.
Let's put it in a variable called answer.

68
00:04:33,890 --> 00:04:38,610
And we'll check their response to see if
it's equal to Y for yes.

69
00:04:38,610 --> 00:04:41,200
And if it is we'll print that message.

70
00:04:41,200 --> 00:04:42,490
And if it's not we won't.

71
00:04:45,900 --> 00:04:47,880
So those things.

72
00:04:47,880 --> 00:04:54,090
Those edges, if you will, that come out of
that top node are called branches.

73
00:04:54,090 --> 00:04:56,826
And so you should take and in-lecture quiz

74
00:04:56,826 --> 00:05:00,250
to tell me about what you understand about
branches.

75
00:05:01,450 --> 00:05:06,310
And now you should go do another
in-lecture quiz to tell me what you

76
00:05:06,310 --> 00:05:11,390
know about that answer equal equal yes
part that we labeled the node with.

77
00:05:15,390 --> 00:05:22,900
So that expression the answer equal equal
yes Is called a Boolean expression.

78
00:05:22,900 --> 00:05:27,112
Remember, Boolean variables can have
values of true or false

79
00:05:27,112 --> 00:05:31,648
and a Boolean expression is merely an
expression that evaluates to

80
00:05:31,648 --> 00:05:34,969
true or false and we use that result, the
value it

81
00:05:34,969 --> 00:05:39,091
evaluates to decide whether or not we
should do the thing.

82
00:05:39,091 --> 00:05:41,072
The branch, on the true side.

83
00:05:41,072 --> 00:05:44,830
Let's fire up the IDE, I already have a
template built.

84
00:05:46,470 --> 00:05:52,710
Notice I remembered to zoom in this time,
so we're going to look at some forms of

85
00:05:52,710 --> 00:05:59,620
the if statement and so, the first thing
we're going to do is ask for and get.

86
00:05:59,620 --> 00:06:03,280
An input from the user to a yes or no
question.

87
00:06:03,280 --> 00:06:05,255
So we know how to ask for, we do a prompt.

88
00:06:05,255 --> 00:06:05,820
[NOISE]

89
00:06:05,820 --> 00:06:11,710
And let's give them something interesting
to answer about.

90
00:06:11,710 --> 00:06:15,518
Pick up the shiny thing, and we'll tell

91
00:06:15,518 --> 00:06:19,730
them, they should tell us either yes or
no.

92
00:06:21,820 --> 00:06:22,884
So there's our prompt

93
00:06:22,884 --> 00:06:23,164
[NOISE]

94
00:06:23,164 --> 00:06:25,510
and then we're going to read this in.

95
00:06:25,510 --> 00:06:28,749
Now, this is a single character we're
trying to

96
00:06:28,749 --> 00:06:32,067
read in so let's call it answer and I've
declared

97
00:06:32,067 --> 00:06:37,170
it to be the char data type and of course,
I try to do a console dot read line.

98
00:06:40,890 --> 00:06:43,100
And, I get a compilation error that says

99
00:06:43,100 --> 00:06:45,730
I can't implicitly convert a string to a
char.

100
00:06:46,840 --> 00:06:51,030
I've specifically chosen to put this into
a char for two reasons.

101
00:06:51,030 --> 00:06:52,944
First of all, the character is the

102
00:06:52,944 --> 00:06:56,112
right choice, right, they're picking
something with

103
00:06:56,112 --> 00:07:01,560
the single character so we should in fact
store it in the single character variable.

104
00:07:01,560 --> 00:07:06,074
The other reason I've decided to do it
this way is because I wanted to show

105
00:07:06,074 --> 00:07:08,516
you that at this point after this call to

106
00:07:08,516 --> 00:07:13,580
Console.ReadLine we know we're holding a
string of characters.

107
00:07:13,580 --> 00:07:14,810
A string in fact.

108
00:07:14,810 --> 00:07:16,720
A string object.

109
00:07:16,720 --> 00:07:19,494
And we also know that we can access
particular

110
00:07:19,494 --> 00:07:24,080
characters in that string by indexing
using the square brackets.

111
00:07:24,080 --> 00:07:26,950
So I wanted to show you that we can take
this

112
00:07:26,950 --> 00:07:31,214
call to the method, and then add at the
end, square bracket,

113
00:07:31,214 --> 00:07:36,544
zero, square bracket, and that will take
that string object that the method

114
00:07:36,544 --> 00:07:42,670
returns, and index into it to extract the
0th character in the string.

115
00:07:42,670 --> 00:07:46,130
So now we're holding the answer, let's add
that.

116
00:07:46,130 --> 00:07:47,995
Extra blank line that I like so much.

117
00:07:47,995 --> 00:07:48,475
[NOISE]

118
00:07:48,475 --> 00:07:51,163
And let's run it now before we add

119
00:07:51,163 --> 00:07:56,000
the if statement to make sure that's
working properly.

120
00:07:56,000 --> 00:07:58,020
Pick up the shiny thing, yes or no.

121
00:07:58,020 --> 00:08:01,330
You'll notice that I can type a whole long
thing here.

122
00:08:01,330 --> 00:08:06,020
And, it's just going to extract the d from
the very beginning.

123
00:08:06,020 --> 00:08:09,540
We'll learn later that there are other
ways that we could take care of this,

124
00:08:09,540 --> 00:08:11,190
but for now we're going to focus on the

125
00:08:11,190 --> 00:08:13,380
if statement so we're going to let that
happen.

126
00:08:15,470 --> 00:08:19,940
And that's the read line, press any key to
continue so I have.

127
00:08:19,940 --> 00:08:24,080
So, let's print an appropriate message.
Let's print a message for the yes.

128
00:08:24,080 --> 00:08:29,705
So, this looks much like the control flow
graph where

129
00:08:29,705 --> 00:08:35,260
Boolean expression between the parentheses
here.

130
00:08:37,190 --> 00:08:40,702
So, if in fact the.
Character that they,

131
00:08:40,702 --> 00:08:43,354
the first character of the string they
enter is

132
00:08:43,354 --> 00:08:46,485
in fact a lower case y, then we'll print
something.

133
00:08:46,485 --> 00:08:47,945
[NOISE]

134
00:08:49,113 --> 00:08:55,860
you have the shiny object.

135
00:08:59,780 --> 00:09:02,040
So, now let's test our code.

136
00:09:02,040 --> 00:09:05,510
So we're going to try it with, we want to
go down the true branch.

137
00:09:05,510 --> 00:09:09,114
So we'll make sure we enter a lowercase y,
and we want to go down

138
00:09:09,114 --> 00:09:11,698
the false branch from that picture which
will

139
00:09:11,698 --> 00:09:14,160
imprint any message at all at this point.

140
00:09:16,000 --> 00:09:19,724
Okay, so let's run it, and I'll just enter

141
00:09:19,724 --> 00:09:24,260
y, and I get that you have the shiny
object.

142
00:09:24,260 --> 00:09:24,892
I'll run

143
00:09:24,892 --> 00:09:28,369
it again, and enter anything other than y,
like h.

144
00:09:29,750 --> 00:09:31,080
And I don't get that message.

145
00:09:31,080 --> 00:09:33,520
So that was the false branch.

146
00:09:33,520 --> 00:09:36,636
The only thing you might notice is that if
I run

147
00:09:36,636 --> 00:09:40,100
this and I enter capital Y instead of
lower case y.

148
00:09:40,100 --> 00:09:41,430
Because I've got caps lock on.

149
00:09:41,430 --> 00:09:45,790
Because cruise control for cool.
Oh I don't get a message.

150
00:09:45,790 --> 00:09:47,740
And it feels like I should.

151
00:09:47,740 --> 00:09:50,260
Right, so there are a variety of ways that
we can do this.

152
00:09:50,260 --> 00:09:52,816
We can make sure that the time we get to
the if

153
00:09:52,816 --> 00:09:55,656
statement, we've capitalized or lower
cased

154
00:09:55,656 --> 00:09:58,283
whatever character they entered, but I'd

155
00:09:58,283 --> 00:10:03,040
rather actually build a more complicated
Boolean expression here, to show you

156
00:10:03,040 --> 00:10:08,070
how that works, so we're checking if
answer equals lower case y here.

157
00:10:08,070 --> 00:10:15,792
And we learned that we have this or,
Boolean operator, so we can check if it's

158
00:10:15,792 --> 00:10:18,860
lowercase y or, uppercase y.

159
00:10:18,860 --> 00:10:22,657
And so that way when I Ctrl+F5, we'll try
all three again.

160
00:10:22,657 --> 00:10:25,080
Y still works the way it should've.

161
00:10:26,340 --> 00:10:30,276
H still works the way it should've, but
now when

162
00:10:30,276 --> 00:10:34,590
I enter a capital I, I get that message
again.

163
00:10:34,590 --> 00:10:37,983
So, we've looked at the first former
variant

164
00:10:37,983 --> 00:10:40,854
of an if statement where we just have the

165
00:10:40,854 --> 00:10:44,640
if clause, or the if block, this part
right here.

166
00:10:45,810 --> 00:10:49,050
And we've built a, not really complicated
but

167
00:10:49,050 --> 00:10:53,870
more complicated than just testing one
thing, Boolean expression.

168
00:10:54,980 --> 00:10:59,944
So let's go back to the slides and look at

169
00:10:59,944 --> 00:11:05,359
another form where in fact, if they say
no.

170
00:11:06,990 --> 00:11:09,770
We can do something else.

171
00:11:09,770 --> 00:11:16,080
So at this point we have both a Print yes
message along the true branch.

172
00:11:16,080 --> 00:11:19,020
And a Print no message along the false
branch.

173
00:11:19,020 --> 00:11:21,396
So this is a pictorial representation of
an

174
00:11:21,396 --> 00:11:24,320
If statement that has an Else clause in
it.

175
00:11:24,320 --> 00:11:26,500
So now we add the code for that.

176
00:11:28,040 --> 00:11:30,590
And we add the code, by saying else.

177
00:11:39,080 --> 00:11:42,248
And we'll write now, whoops, sorry

178
00:11:42,248 --> 00:11:42,958
[LAUGH].

179
00:11:42,958 --> 00:11:48,590
Something really compelling, you don't
have the shiny object.

180
00:11:52,350 --> 00:11:56,460
So now we have an if statement that will
do this part.

181
00:11:57,540 --> 00:12:00,350
If the Boolean expression of that leads to
true.

182
00:12:00,350 --> 00:12:04,390
And if the Boolean expression of that
leads to false it will go do this part.

183
00:12:04,390 --> 00:12:09,726
So when we Ctrl+F5, I'll test y again.
And

184
00:12:09,726 --> 00:12:16,840
I'll test n.
Oh, good that's what it should have done.

185
00:12:16,840 --> 00:12:20,530
However, you might also realize that if we
enter

186
00:12:20,530 --> 00:12:25,140
H, we still get, you don't have the shiny
object.

187
00:12:25,140 --> 00:12:28,936
And that maybe acceptable for what we're
writing right now,

188
00:12:28,936 --> 00:12:32,878
however, lets actually be, we're tired,
we're tired of the

189
00:12:32,878 --> 00:12:34,922
use of being a dork and so we want to

190
00:12:34,922 --> 00:12:38,320
be a little abusive if they don't pick yes
or no.

191
00:12:38,320 --> 00:12:41,946
other than being mean to the you, player,
there's a good

192
00:12:41,946 --> 00:12:46,780
reason for this because it lets me show
you one more piece of the if statement.

193
00:12:48,230 --> 00:12:52,390
So let's actually say that we want the
else to be

194
00:12:52,390 --> 00:12:56,980
something mean.
You are a noob.

195
00:12:59,030 --> 00:13:05,498
And here we want to print the no message.
What

196
00:13:05,498 --> 00:13:11,897
we can do is we can add an else if,

197
00:13:11,897 --> 00:13:13,082
[SOUND]

198
00:13:13,082 --> 00:13:23,036
and I will check both lowercase n, and
uppercase N

199
00:13:23,036 --> 00:13:24,221
[NOISE]

200
00:13:24,221 --> 00:13:26,201
like so.

201
00:13:26,201 --> 00:13:27,196
[NOISE]

202
00:13:27,196 --> 00:13:34,559
And here's where we'll write, you don't

203
00:13:34,559 --> 00:13:42,430
have the shiny object.
So thinking about how this will work.

204
00:13:42,430 --> 00:13:47,650
What happens is when we're running this
code we get to this point right here.

205
00:13:47,650 --> 00:13:52,607
And we check the Boolean expression.
If this Boolean expression

206
00:13:52,607 --> 00:13:58,869
evaluates to true it will print this line.
And then it goes all the way down here.

207
00:13:59,970 --> 00:14:04,760
So it goes all the way past this very last
curly brace.

208
00:14:04,760 --> 00:14:11,000
If this evaluates to a false, it goes down
and checks at this one.

209
00:14:11,000 --> 00:14:14,192
And if this one evaluates to a true, it
does this,

210
00:14:14,192 --> 00:14:17,729
and then comes all the way past the whole
if statement.

211
00:14:19,120 --> 00:14:22,927
If neither this nor this evaluated to
true, it'll come

212
00:14:22,927 --> 00:14:25,970
to the else and tell the user they are a
noob.

213
00:14:27,800 --> 00:14:28,550
Let's try it.

214
00:14:30,050 --> 00:14:33,506
I pressed F5 and I crashed it because I
just

215
00:14:33,506 --> 00:14:37,226
hit Enter because the user is in fact a
moron.

216
00:14:37,226 --> 00:14:42,455
So let me do Ctrl+F5, we'll check yes.
Yes, works fine.

217
00:14:42,455 --> 00:14:43,590
[SOUND]

218
00:14:43,590 --> 00:14:49,038
We'll check both lowercase

219
00:14:49,038 --> 00:14:53,980
n, oh, dear.
It says, we're a noob.

220
00:14:56,130 --> 00:14:59,580
So now we have this debugging thing.
Right?

221
00:14:59,580 --> 00:15:03,009
We have to go look at this Boolean
expression and say, what's going wrong?

222
00:15:04,200 --> 00:15:08,432
And if we look at this Boolean expression
we can see that

223
00:15:08,432 --> 00:15:13,308
in my frenzy of typing, I said for this
Boolean expression, if

224
00:15:13,308 --> 00:15:16,804
the answer is, this is an and not an or, I
said

225
00:15:16,804 --> 00:15:21,220
if the answer is both lower case and
uppercase n, and that

226
00:15:21,220 --> 00:15:26,389
is, in fact, impossible.
So, this should have been an or.

227
00:15:27,490 --> 00:15:30,946
See, even in a well-polished well-prepared
lecture, there

228
00:15:30,946 --> 00:15:34,330
can be mistakes, just like programming
happens in general.

229
00:15:35,390 --> 00:15:36,410
That's why we're testing it.

230
00:15:37,440 --> 00:15:41,598
Okay, so, I'll try the lower case n, you
don't have the shiny

231
00:15:41,598 --> 00:15:46,295
object, I'll try the upper case N, you
don't have the shiny object and

232
00:15:46,295 --> 00:15:48,759
finally I'll try my, my favorite not

233
00:15:48,759 --> 00:15:52,040
search for something, I'll try my favorite
h.

234
00:15:53,100 --> 00:15:55,490
And, I am, in fact, a noob.

235
00:15:58,470 --> 00:16:02,089
So, that's it for the sort of different
variance of the

236
00:16:02,089 --> 00:16:06,890
if statement, we looked at three different
ways we could use this.

237
00:16:06,890 --> 00:16:09,463
We looked at and if that just has a true

238
00:16:09,463 --> 00:16:12,949
branch and nothing on the false branch We
looked at

239
00:16:12,949 --> 00:16:16,684
the if else, which has both true and false
branches,

240
00:16:16,684 --> 00:16:20,370
and then we looked at adding else ifs, as
well.

241
00:16:20,370 --> 00:16:23,475
In case we have a more complicated
sequence of Boolean

242
00:16:23,475 --> 00:16:25,420
expressions that we want to test.

243
00:16:26,610 --> 00:16:30,106
You should know, once we start adding else
ifs, we can

244
00:16:30,106 --> 00:16:33,720
add as many else ifs if we want, as we
want.

245
00:16:33,720 --> 00:16:38,208
And, the else part is in fact, option, we
can just have if,

246
00:16:38,208 --> 00:16:43,910
else, if and that's it and it will be
exactly as you'd expect.

247
00:16:43,910 --> 00:16:45,950
Next time, we're going to look.

248
00:16:45,950 --> 00:16:48,710
So that's it for today, next time we're
going

249
00:16:48,710 --> 00:16:53,690
to look at another statement in C# that we
can use to make decisions in our programs.