1
00:00:09,850 --> 00:00:16,240
This is our last lecture in this module
developing a Console Application class.

2
00:00:16,240 --> 00:00:18,835
So next time we will move on to doing XNA
stuff,

3
00:00:18,835 --> 00:00:22,010
but we have a little bit more work to do
here first.

4
00:00:22,010 --> 00:00:25,620
As a reminder here are the module learning
objectives for the entire module.

5
00:00:29,760 --> 00:00:34,370
So you should recall that last time we
added the two constructors to the die

6
00:00:34,370 --> 00:00:38,850
class.
One, that built a die with the standard 6

7
00:00:38,850 --> 00:00:45,650
sides and one that built a die with a
consumer specified to number of sides.

8
00:00:45,650 --> 00:00:49,510
So there was a parameter in the
constructor and there was an argument

9
00:00:49,510 --> 00:00:54,150
when we called the constructor to tell it
how many sides we needed.

10
00:00:54,150 --> 00:00:55,010
This time,

11
00:00:55,010 --> 00:01:00,570
we're going to implement the single roll
method that we need in our die class.

12
00:01:02,940 --> 00:01:05,860
So I have the project available to us.

13
00:01:08,440 --> 00:01:14,390
We already have, recall, fields,
constructors, and properties in our class.

14
00:01:15,550 --> 00:01:17,988
So now we can add methods.

15
00:01:17,988 --> 00:01:22,559
[SOUND].

16
00:01:22,559 --> 00:01:29,340
And we needed a single method.
We need the roll method.

17
00:01:29,340 --> 00:01:32,170
And we're going to learn something new as
we develop

18
00:01:32,170 --> 00:01:35,230
this method and the rest of it we've
already seen before.

19
00:01:35,230 --> 00:01:40,560
So methods have an Access Modifier and
because we want a consumer of the

20
00:01:40,560 --> 00:01:44,495
class to be able to roll the die, we're
going to make this method public

21
00:01:44,495 --> 00:01:46,590
[NOISE].

22
00:01:46,590 --> 00:01:49,920
The next thing that happens, the next
thing that we put

23
00:01:49,920 --> 00:01:55,000
as we build this method, is the return
type for the method.

24
00:01:55,000 --> 00:02:00,310
So, the deck take top card method, had a
return

25
00:02:00,310 --> 00:02:02,340
type, I'm going to type it here, it's not
going to work.

26
00:02:02,340 --> 00:02:05,050
Had a return type of card, because it
returned

27
00:02:05,050 --> 00:02:06,859
a card from the method when you took it.

28
00:02:08,060 --> 00:02:12,010
Other methods in the deck class, had to
return

29
00:02:12,010 --> 00:02:13,470
typo void.

30
00:02:13,470 --> 00:02:15,580
They didn't return anything at all, they
just

31
00:02:15,580 --> 00:02:18,140
did their job and then were done with it.

32
00:02:18,140 --> 00:02:20,850
And so, we need to think about should the
roll

33
00:02:20,850 --> 00:02:27,040
method have a return type, should it
return anything or not.

34
00:02:27,040 --> 00:02:31,510
And this is actually a trade off for this
method because the way I'm implementing

35
00:02:31,510 --> 00:02:36,630
it, it's going to have a void return type
which means it doesn't return anything,

36
00:02:37,670 --> 00:02:42,560
and then it's much more like what actually
do with dice, right.

37
00:02:42,560 --> 00:02:46,720
You roll a die and then you look at what
number's on top.

38
00:02:46,720 --> 00:02:52,560
So standard usage would be somebody rolls
the die by calling the roll method

39
00:02:52,560 --> 00:02:59,020
and then accesses the top side property to
see which side is on top.

40
00:02:59,020 --> 00:03:03,550
There is an alternative certainly we could
return an int from here,

41
00:03:03,550 --> 00:03:08,590
so that we rolled the die and then
generated

42
00:03:08,590 --> 00:03:14,380
the sorry, then accessed the topSide
property an returned that.

43
00:03:14,380 --> 00:03:18,463
We could do that, but we're going to do it
this other way instead.

44
00:03:18,463 --> 00:03:19,143
So,

45
00:03:19,143 --> 00:03:19,993
[NOISE]

46
00:03:19,993 --> 00:03:25,773
Access Modifier, return type, method

47
00:03:25,773 --> 00:03:30,703
name, well, not exactly that way

48
00:03:30,703 --> 00:03:31,930
[NOISE].

49
00:03:34,410 --> 00:03:40,305
Method name parentheses, even if there are
no parameters, parentheses

50
00:03:40,305 --> 00:03:41,590
[NOISE],

51
00:03:41,590 --> 00:03:46,929
open brace, curly brace, and we'll put in
the comment momentarily.

52
00:03:47,950 --> 00:03:53,860
So, the next question is, is there any
information that

53
00:03:53,860 --> 00:04:00,380
we need from the consumer of this method
to say something about the role?

54
00:04:00,380 --> 00:04:03,620
And the answer is no, in this particular
case the answer is no.

55
00:04:03,620 --> 00:04:06,970
They're just going to roll the die.
So

56
00:04:06,970 --> 00:04:11,150
I put the three slashes in, and I say
rolls the die.

57
00:04:13,970 --> 00:04:20,270
So, how do we roll the die?
Well rolling a die means that we are

58
00:04:20,270 --> 00:04:28,570
randomly picking some number, right, from
the set of sides, of the die.

59
00:04:28,570 --> 00:04:32,280
We are randomly picking which side should
be on top.

60
00:04:32,280 --> 00:04:34,100
So we're definetly changing topside.

61
00:04:36,440 --> 00:04:37,850
And we can.

62
00:04:40,660 --> 00:04:45,565
Well, gee, I said we need to have a random
number, so I'm going to create

63
00:04:45,565 --> 00:04:47,280
[NOISE]

64
00:04:47,280 --> 00:04:49,678
a new random number generator

65
00:04:49,678 --> 00:04:52,035
[NOISE]

66
00:04:52,035 --> 00:04:58,670
right here, and this new random number
generator will use, we will

67
00:04:58,670 --> 00:05:03,070
use this new random number generator to
generate a random number for the top side.

68
00:05:04,180 --> 00:05:12,160
And we'll actually use the overload of
rand.Next that lets us specify

69
00:05:12,160 --> 00:05:17,490
both the lower bound and the upper bound,
because we are numbering the sides

70
00:05:17,490 --> 00:05:22,080
on our die starting at 1.
We will put a 1 here as the first one.

71
00:05:23,590 --> 00:05:29,580
And so for a six sided die for example, we
want a random number between 1 and 20.

72
00:05:29,580 --> 00:05:31,750
So, ha, ha, that's crazy.

73
00:05:31,750 --> 00:05:36,460
Here I am thinking DND again.
for a six-sided die, we need a random

74
00:05:36,460 --> 00:05:43,850
number between 1 and 6.
And so we need to say numSides plus

75
00:05:43,850 --> 00:05:50,410
1 because remember the upper bound for
our, for this argument to the next method.

76
00:05:50,410 --> 00:05:54,100
This gives an upper bound, an exclusive
upper bound.

77
00:05:54,100 --> 00:05:58,350
So if we pass in 7, it will give us, for

78
00:05:58,350 --> 00:06:01,900
this call, a number, a random number
between 1 and 6.

79
00:06:01,900 --> 00:06:05,800
So let's actually go back to our, well,
I'll save it.

80
00:06:05,800 --> 00:06:08,544
And we'll go back to our testing code

81
00:06:08,544 --> 00:06:09,044
[NOISE].

82
00:06:12,770 --> 00:06:13,777
And we will roll the die

83
00:06:13,777 --> 00:06:14,277
[NOISE].

84
00:06:23,420 --> 00:06:26,625
We'll roll and print the results.

85
00:06:26,625 --> 00:06:27,550
[NOISE]

86
00:06:27,550 --> 00:06:32,545
So we tell the die, the standard

87
00:06:32,545 --> 00:06:37,910
die, in this case, to roll itself

88
00:06:37,910 --> 00:06:39,205
[NOISE],

89
00:06:39,205 --> 00:06:47,060
and then we will display the top side
again.

90
00:06:47,060 --> 00:06:51,290
So I'll just grab these two lines of code,
and put them here instead.

91
00:06:51,290 --> 00:06:51,790
Put

92
00:06:53,540 --> 00:06:55,230
them here also.

93
00:06:55,230 --> 00:07:01,570
So if I Ctrl F5, we see that we roll and
we get a topside of 3.

94
00:07:01,570 --> 00:07:08,420
And just so that you see that this rolling
also works, for the D20.

95
00:07:08,420 --> 00:07:11,280
We'll do it here as well.

96
00:07:12,930 --> 00:07:15,458
Although of course we'll need to change a
little bit.

97
00:07:15,458 --> 00:07:18,802
We'll need to roll the D, we'll need to
tell

98
00:07:18,802 --> 00:07:23,260
the D20 to roll itself, and we will then
tell the D20.

99
00:07:23,260 --> 00:07:28,120
We'll then access the topSide property of
the D20.

100
00:07:28,120 --> 00:07:29,630
Okay.
So I'll control f5.

101
00:07:30,960 --> 00:07:34,900
So, so far, this looks pretty good, right?

102
00:07:34,900 --> 00:07:37,890
It looks like we're getting a random
number.

103
00:07:37,890 --> 00:07:41,405
And by the way, let me just add the right
line that we're missing here.

104
00:07:41,405 --> 00:07:45,820
[SOUND].

105
00:07:45,820 --> 00:07:52,620
By the way, I had to be careful over here
in the roll method to not just say between

106
00:07:52,620 --> 00:07:54,480
1 and 6 right, because we could have an

107
00:07:54,480 --> 00:07:57,720
arbitrary number of sides on the dice, on
the die.

108
00:07:57,720 --> 00:08:01,470
So that's why I used numSides here to make
sure

109
00:08:01,470 --> 00:08:03,410
that it would still work for the D20 as
well.

110
00:08:04,430 --> 00:08:08,790
Unfortunately, even though there is all
looks great.

111
00:08:08,790 --> 00:08:10,650
Let's roll a few times in a row.

112
00:08:10,650 --> 00:08:12,054
[NOISE]

113
00:08:12,054 --> 00:08:18,606
Ctrll F5, and you will see that for the
standard die

114
00:08:18,606 --> 00:08:24,690
we rolled a 2, and then a 2 and then a 2.
So this is

115
00:08:24,690 --> 00:08:31,086
actually an issue with random number
generators

116
00:08:31,086 --> 00:08:36,870
when we create a new random number
generator,

117
00:08:36,870 --> 00:08:42,390
what happens is, it takes the system clock
time from our computer

118
00:08:42,390 --> 00:08:48,080
in general, and uses it as something
called a Seed, and that seed

119
00:08:48,080 --> 00:08:53,210
is the start of generating a sequence of
numbers

120
00:08:53,210 --> 00:08:59,090
using an algorithm, a set of mathematical
steps to figure out the next number.

121
00:08:59,090 --> 00:09:01,650
So in computers these aren't actually
random.

122
00:09:01,650 --> 00:09:01,960
They're what

123
00:09:01,960 --> 00:09:04,490
are called pseudo-random, because they are
not

124
00:09:04,490 --> 00:09:06,410
actually random, if you know the Seed and

125
00:09:06,410 --> 00:09:09,520
you know the algorithm you can perfectly
predict

126
00:09:09,520 --> 00:09:12,670
the sequence of numbers that would be
generated.

127
00:09:12,670 --> 00:09:16,040
The problem is that if you start with the

128
00:09:16,040 --> 00:09:19,510
same Seed and you are going to be using
the

129
00:09:19,510 --> 00:09:24,880
same algorithm because that's just
expressed in code, then

130
00:09:24,880 --> 00:09:27,400
you're going to get the same sequence of
numbers all

131
00:09:27,400 --> 00:09:32,050
the time.
And so the issue is that because computers

132
00:09:32,050 --> 00:09:37,320
are really, really fast, we end up with

133
00:09:37,320 --> 00:09:42,080
three calls to roll in quick succession
such that the system

134
00:09:42,080 --> 00:09:47,380
clock is close enough to the same that
each random number generator

135
00:09:47,380 --> 00:09:52,450
gets the same seed.
So I am going to take this random out of

136
00:09:52,450 --> 00:09:57,690
there, and I am going to move it up here
as a field instead,

137
00:09:58,850 --> 00:10:03,605
that was crazy.
I will just take it in now

138
00:10:03,605 --> 00:10:04,280
[COUGH],

139
00:10:04,280 --> 00:10:05,555
I guess I just you know,

140
00:10:05,555 --> 00:10:06,210
[UNKNOWN]

141
00:10:06,210 --> 00:10:11,146
instead of seeded.
So new random

142
00:10:11,146 --> 00:10:13,790
[NOISE]

143
00:10:13,790 --> 00:10:16,010
up here in the class.

144
00:10:16,010 --> 00:10:21,210
So, this will get created when the object
is constructed.

145
00:10:22,960 --> 00:10:27,870
And so each call to roll down here isn't
creating a new random number generator

146
00:10:27,870 --> 00:10:33,084
anymore, is in fact just using the one
that we created inside the object.

147
00:10:33,084 --> 00:10:36,630
And I'll Ctrl F5 again, and you can see,
yes, we

148
00:10:36,630 --> 00:10:38,910
did get two 4's in a row, but that was not

149
00:10:38,910 --> 00:10:40,910
the problem I was talking about.

150
00:10:43,800 --> 00:10:48,240
As you can see we are getting essentially,
random rolls.

151
00:10:48,240 --> 00:10:52,050
Now, I've solved part of the problem here.

152
00:10:52,050 --> 00:10:58,058
and the different dice look different
because one is generating numbers between

153
00:10:58,058 --> 00:11:00,809
1 and 6 and one is generating numbers
between 1 and 20.

154
00:11:01,890 --> 00:11:05,690
But, we would have the same exact problem
even

155
00:11:05,690 --> 00:11:08,930
with what I've done so far, if we created

156
00:11:08,930 --> 00:11:13,530
two 6 sided die in a row because we'd

157
00:11:13,530 --> 00:11:17,570
be creating the objects quickly enough
potentially anyway that

158
00:11:17,570 --> 00:11:19,550
they'd end up with the same Seed so both

159
00:11:19,550 --> 00:11:23,550
dice would end up with the same sequence
of numbers.

160
00:11:23,550 --> 00:11:26,500
Even though the numbers changed over the
sequence, the dice would always match.

161
00:11:27,710 --> 00:11:34,300
The optimal robust commercial quality
solution is to have a random

162
00:11:34,300 --> 00:11:38,020
number generator that you create for your
entire game,

163
00:11:38,020 --> 00:11:40,260
and every time anything in the game needs
a

164
00:11:40,260 --> 00:11:43,480
random number, it just goes to that one
single

165
00:11:43,480 --> 00:11:47,660
centralized random number generator and
gets a random number.

166
00:11:47,660 --> 00:11:50,660
We're not going to do that here because
we're focused on class

167
00:11:50,660 --> 00:11:55,510
design and implementation rather than sort
of the overall game design implementation.

168
00:11:55,510 --> 00:11:59,430
But, that you should know that because
this is an important issue

169
00:11:59,430 --> 00:12:00,379
for random numbers.

170
00:12:02,620 --> 00:12:07,610
So we have created everything that we
needed to create today.

171
00:12:07,610 --> 00:12:14,490
And in fact, we have finished off our die
class in implementation.

172
00:12:14,490 --> 00:12:16,360
So, over the course of 3 lectures, we

173
00:12:16,360 --> 00:12:19,750
designed it and we implemented state the
first time,

174
00:12:19,750 --> 00:12:22,610
and then identity last time, and now
behavior

175
00:12:22,610 --> 00:12:25,740
this time, so all done with the die class.

176
00:12:25,740 --> 00:12:27,790
Next time we're going to actually

177
00:12:27,790 --> 00:12:30,010
start developing a class.

178
00:12:30,010 --> 00:12:34,050
We'll design it and implement it for use
within an x and a game.