1
00:00:00,150 --> 00:00:04,110
Onwards, the part two in this lesson you're going to get, these are two, three numbers and you're

2
00:00:04,110 --> 00:00:07,380
going to make a function that checks if the user won or lost.

3
00:00:10,040 --> 00:00:16,520
We're going to start using the familiar scanner to get user input, so by virtue of just writing a scanner

4
00:00:17,030 --> 00:00:21,950
and choosing the first option, it's going to auto import scanner and allow me to use it.

5
00:00:22,700 --> 00:00:29,720
Now, I'm going to set a variable scan equal to a new instance of scanner that receives input from the

6
00:00:29,720 --> 00:00:31,430
system system in.

7
00:00:34,230 --> 00:00:38,160
And before we forget, let's add scandal close to the end of the code.

8
00:00:43,800 --> 00:00:47,820
The user needs to enter three numbers to start the game off, so I'm going to start by removing the

9
00:00:47,820 --> 00:00:53,970
last three print line statements and before rolling the dice and ask the user to enter three whole numbers

10
00:00:53,970 --> 00:00:59,250
between one and six print, enter three numbers between one and six.

11
00:01:07,770 --> 00:01:13,800
OK, now we're going to add three, scan the next integer methods to pick up three numbers.

12
00:01:28,260 --> 00:01:29,790
Think we're ready to run our code?

13
00:01:42,110 --> 00:01:48,620
Enter three numbers for two and three, and we're looking good, and if you remember from our previous

14
00:01:48,620 --> 00:01:52,370
discussion, it doesn't matter how much space you put in between the three numbers.

15
00:01:55,070 --> 00:02:01,370
Scandal next and skips the delimiter, it skips the white space and it reads the next input, even if

16
00:02:01,370 --> 00:02:07,400
you put an entire line of space between the three members Scandi next and skips all the white space

17
00:02:07,580 --> 00:02:09,080
and reads the next input.

18
00:02:09,919 --> 00:02:13,670
All of these methods skip the delimiter and read the next input.

19
00:02:13,670 --> 00:02:16,580
Rescanned Eskandar next line just reads the entire line.

20
00:02:17,570 --> 00:02:19,660
In any case, this table is inside your cheek.

21
00:02:19,670 --> 00:02:20,150
It's.

22
00:02:23,110 --> 00:02:28,420
Now, what if the user enters a no outside the range of one to six, if either of the three numbers

23
00:02:28,420 --> 00:02:33,700
then or is not between one and six, we need to exit the game and hope this made you think of the or

24
00:02:33,700 --> 00:02:37,960
operator such that we're going to check if the first number is less than one.

25
00:02:39,740 --> 00:02:42,770
Or if the second number is less than one.

26
00:02:44,510 --> 00:02:47,240
Or if the third number is less than one.

27
00:02:51,270 --> 00:02:57,750
If either of these numbers is less than one, we're going to print numbers cannot be less than one.

28
00:03:01,900 --> 00:03:03,190
Shutting the game down.

29
00:03:08,920 --> 00:03:12,430
And we're going to use system that exit zero to shut the application down.

30
00:03:19,680 --> 00:03:24,930
And now, if none of the numbers are less than the minimum value, we need to check if either one of

31
00:03:24,930 --> 00:03:26,910
them exceeds the maximum value.

32
00:03:28,830 --> 00:03:34,410
So we're going to check if the first number is bigger than six or if the second number is bigger than

33
00:03:34,410 --> 00:03:34,950
six.

34
00:03:37,560 --> 00:03:40,230
Or if the third number is bigger than six.

35
00:03:45,980 --> 00:03:47,030
Then we're going to print.

36
00:03:50,530 --> 00:03:54,370
Numbers cannot be higher than six, shutting the game down.

37
00:04:00,110 --> 00:04:04,250
And once again, we're going to use system that exit zero to shut the application down.

38
00:04:08,380 --> 00:04:10,240
OK, we're ready to run our code.

39
00:04:18,420 --> 00:04:24,450
Let's enter a number outside the range, let's say negative two, and we'll put three and five.

40
00:04:25,620 --> 00:04:29,890
Numbers cannot be less than one shunning the game down, the user entered a bad number.

41
00:04:29,950 --> 00:04:31,470
So we shut the program down.

42
00:04:32,070 --> 00:04:35,880
The first condition is designed to check if any of the numbers are less than one.

43
00:04:36,300 --> 00:04:37,870
The first number is less than one.

44
00:04:37,980 --> 00:04:42,000
So the entire condition is true and that runs the system, that exit code.

45
00:04:42,870 --> 00:04:45,210
What if the user enters a number above six?

46
00:04:45,700 --> 00:04:46,620
Let's try it out.

47
00:04:52,910 --> 00:04:55,310
I'm going to put six, three and seven.

48
00:04:58,990 --> 00:05:04,860
And numbers cannot be higher than six, shutting the game down, the last number is outside the range,

49
00:05:04,870 --> 00:05:06,100
so the program shuts down.

50
00:05:06,700 --> 00:05:11,820
Our trio of numbers is going to get past the first condition because none of them are less than one.

51
00:05:12,430 --> 00:05:17,620
But the second condition is designed to check if any of the numbers are higher than six, since the

52
00:05:17,620 --> 00:05:19,360
third number is higher than six.

53
00:05:19,570 --> 00:05:24,640
The condition is true and a system that exit zero runs shutting the application down.

54
00:05:25,830 --> 00:05:30,300
Now, if the user follows the rules, none of the statements should run and we should get to this part

55
00:05:30,300 --> 00:05:30,840
of the code.

56
00:05:31,320 --> 00:05:34,730
In that case, you can add all the numbers and add all the dice rolls.

57
00:05:35,250 --> 00:05:40,650
So I'm going to say into some of numbers is equal to all three numbers that you chose.

58
00:05:43,660 --> 00:05:47,110
And we'll say into some of roles, some of Dyce roles.

59
00:05:49,310 --> 00:05:52,340
Is going to equal all of the dice rolls.

60
00:05:56,740 --> 00:05:57,700
Then I'm going to print.

61
00:05:59,290 --> 00:06:00,460
The day some.

62
00:06:05,150 --> 00:06:08,480
Is equal to the sum of days, rose.

63
00:06:10,760 --> 00:06:12,050
And the number some.

64
00:06:18,730 --> 00:06:21,820
Is equal to the sum of numbers that you chose.

65
00:06:27,600 --> 00:06:30,260
OK, we can test our code once more.

66
00:06:33,930 --> 00:06:40,080
I'm going to make sure to follow the rules this time and enter three numbers between one and six days,

67
00:06:40,080 --> 00:06:42,450
some 14, no sum equals 10.

68
00:06:42,480 --> 00:06:44,070
All right, we're doing well so far.

69
00:06:44,820 --> 00:06:50,460
And since none of the numbers are smaller than one or bigger than six, Java skips the statements.

70
00:06:50,460 --> 00:06:53,440
Then the code adds all the numbers and stories.

71
00:06:53,470 --> 00:06:59,100
The result in some of numbers, the code adds every dice roll and it stores the result in some of dice

72
00:06:59,100 --> 00:07:02,640
rolls and our print statement runs cool.

73
00:07:05,280 --> 00:07:08,430
OK, so now we're going to wrap up the game by checking if the user won.

74
00:07:11,000 --> 00:07:15,890
I just want to remind you of what the rules are, the these are wins if the sum of dice rolls is smaller

75
00:07:15,890 --> 00:07:21,500
than the sum of numbers that they choose and with an emphasis on the end, the difference between the

76
00:07:21,500 --> 00:07:22,980
two numbers is less than three.

77
00:07:23,660 --> 00:07:30,530
So if the user chooses four, three and five and happens to roll three, four, and for the user would

78
00:07:30,530 --> 00:07:34,390
win because the sum of dice rolls is smaller than the sum of numbers.

79
00:07:35,030 --> 00:07:39,080
And in this case, the difference between the two numbers is less than three.

80
00:07:40,180 --> 00:07:42,610
So I think we're ready to define this task in a function.

81
00:07:44,460 --> 00:07:45,480
It's going to be public.

82
00:07:48,150 --> 00:07:54,270
I'm going to make it void for now, I'm going to address that in a second and I'll name the function,

83
00:07:54,270 --> 00:08:00,300
check one, because it's going to check if the user one and this function is going to take two parameters,

84
00:08:00,300 --> 00:08:02,460
it's going to take the sum of dice rolls.

85
00:08:08,770 --> 00:08:10,420
And the sum of numbers.

86
00:08:13,730 --> 00:08:20,750
And according to the rules, the user wins if the numbers they choose are bigger than the dice rolls.

87
00:08:20,780 --> 00:08:23,720
That is, if no some is bigger than dice some.

88
00:08:31,290 --> 00:08:37,289
And assuming this first comparison is true, such that the numbers you chose are indeed bigger than

89
00:08:37,289 --> 00:08:38,409
the dice rolled.

90
00:08:38,730 --> 00:08:42,960
We need to make sure that the difference between the two sums, some of numbers.

91
00:08:45,580 --> 00:08:47,260
Minus some of dice rolls.

92
00:08:48,810 --> 00:08:49,980
Is less than three.

93
00:08:54,690 --> 00:08:57,810
If both comparisons are true, we're going to print.

94
00:09:00,200 --> 00:09:01,130
Congrats.

95
00:09:04,780 --> 00:09:05,560
You win.

96
00:09:08,630 --> 00:09:09,470
Otherwise.

97
00:09:14,850 --> 00:09:16,680
Sorry, Youlus.

98
00:09:25,120 --> 00:09:29,080
The very last step is to call the function and pass the twosomes as arguments, so we're going to check

99
00:09:29,080 --> 00:09:35,500
if the user one check when by passing in the sum of dice rolls and the sum of numbers that they chose.

100
00:09:37,900 --> 00:09:39,670
I think we're ready to run our code.

101
00:09:43,630 --> 00:09:51,460
I'm going to enter three numbers and dang it, I lost the numbers, I chose add up to 12, but the dice

102
00:09:51,460 --> 00:09:53,730
rolls, whatever they are, they added up to 13.

103
00:09:54,370 --> 00:09:58,690
So the first comparison was false, meaning that the entire condition became false.

104
00:09:58,930 --> 00:10:00,640
And the Elkader runs.

105
00:10:03,480 --> 00:10:06,010
Now, here's another scenario that could have happened as well.

106
00:10:06,570 --> 00:10:11,790
We could have called the check win function and let's assume it passes in a December four and then no,

107
00:10:11,790 --> 00:10:12,660
some of 12.

108
00:10:13,380 --> 00:10:15,750
In this case, we have a higher number, some.

109
00:10:15,930 --> 00:10:19,920
But the if statement is false because the difference between the numbers is higher than three.

110
00:10:20,460 --> 00:10:23,820
So once again, the statement would run and I would lose the game.

111
00:10:24,660 --> 00:10:27,460
But so far I'm really happy with how this game is turning out.

112
00:10:27,700 --> 00:10:28,380
Try your luck.

113
00:10:28,380 --> 00:10:29,700
You might win a few times.

114
00:10:32,840 --> 00:10:35,580
Now, I know it might seem like we're done, but we're not actually done yet.

115
00:10:35,600 --> 00:10:41,480
There's one thing that's really bothering me, Chuck, when it's void, when it shouldn't be a function,

116
00:10:41,480 --> 00:10:44,600
should only perform its intended task and nothing more.

117
00:10:45,220 --> 00:10:48,700
Chuck, when should only check if the user won or not?

118
00:10:49,190 --> 00:10:52,780
It should not bear the responsibility of printing messages to the user.

119
00:10:53,360 --> 00:10:59,000
In that case, I should just rename the function to check when and print random messages.

120
00:11:02,130 --> 00:11:04,030
But we're definitely not doing that.

121
00:11:04,470 --> 00:11:08,590
You might be saying, hey, Erin, it's not calculating anything, so it should be void.

122
00:11:09,090 --> 00:11:09,930
Be careful.

123
00:11:10,050 --> 00:11:12,420
A calculation doesn't have to involve numbers.

124
00:11:12,720 --> 00:11:17,160
I like to think of a calculation as it uses some logic to come up with a value.

125
00:11:17,880 --> 00:11:20,720
This condition, in some ways, it's a calculation.

126
00:11:21,210 --> 00:11:25,410
It uses the logical operator and to perform two comparisons.

127
00:11:25,740 --> 00:11:28,330
And the result is a boolean, true or false.

128
00:11:28,740 --> 00:11:34,260
So it makes sense to just return the boolean that it computed because the Boolean is a result of your

129
00:11:34,260 --> 00:11:35,030
calculation.

130
00:11:35,700 --> 00:11:38,520
So I'm going to change the return type of check into to Boolean.

131
00:11:41,140 --> 00:11:43,810
And then I'm going to return the result of this condition.

132
00:11:55,870 --> 00:12:01,390
In this case, Chuck Wen is performing its intended task and nothing more, it's returning a boolean

133
00:12:01,390 --> 00:12:08,370
that indicates whether the user won or lost and now inside mean the function call is going to hold this

134
00:12:08,380 --> 00:12:09,130
return value.

135
00:12:09,160 --> 00:12:14,980
So we need to make an if statement that checks if that return value is true, if the result from calling

136
00:12:14,980 --> 00:12:15,670
check wen.

137
00:12:18,180 --> 00:12:20,130
What these arguments end up being true.

138
00:12:22,750 --> 00:12:23,680
We're going to print.

139
00:12:27,080 --> 00:12:28,730
Congrats, you win.

140
00:12:37,630 --> 00:12:39,280
Sorry, Youlus.

141
00:12:45,840 --> 00:12:51,810
And with this final line of code, I like to proudly announce that we're all done, let's run our code.

142
00:12:58,190 --> 00:13:01,280
And there you go, I won and this is also much better.

143
00:13:01,490 --> 00:13:06,920
I like that we get the same output and that Szechwan is only performing its intended task this time.

144
00:13:06,920 --> 00:13:09,260
The arguments being passed in are 12 and 13.

145
00:13:09,740 --> 00:13:14,390
Both comparisons are true because my number is bigger than the sum of dice rolls and the difference

146
00:13:14,390 --> 00:13:17,660
is smaller than three, which means the entire condition is true.

147
00:13:17,840 --> 00:13:20,690
And that boolean result is a return to the function.

148
00:13:20,690 --> 00:13:25,490
Call and send out the statement which holds the function call is going to be true.

149
00:13:25,910 --> 00:13:28,070
The code inside runs and I win the game.

150
00:13:31,190 --> 00:13:33,680
I'm going to try and run the code now so that I can lose.

151
00:13:35,660 --> 00:13:36,830
I guess I'm on a roll.

152
00:13:36,920 --> 00:13:37,690
Let's try again.

153
00:13:42,250 --> 00:13:48,160
I guess today's my lucky day, I was going to try to rerun this and finally I lose.

154
00:13:49,680 --> 00:13:55,560
This time around, Chetwyn receives a copy of these values as parameters, it performs the two comparisons,

155
00:13:56,070 --> 00:14:00,720
and the resulting bullion from this condition is going to be false because although my numbers sum is

156
00:14:00,720 --> 00:14:04,070
greater than the Decem, the difference is bigger than three.

157
00:14:04,380 --> 00:14:10,080
So it returns false to the function call, which means the statement which holds my function call is

158
00:14:10,080 --> 00:14:15,000
going to be false and the false code is going to run, in which case I lose once again.

159
00:14:15,530 --> 00:14:16,070
All right.

160
00:14:16,080 --> 00:14:18,490
It seems that we built a pretty robust game.

161
00:14:21,490 --> 00:14:24,010
Congratulations, you built your first Java game.

162
00:14:25,820 --> 00:14:31,400
The first part of this code prompts the user to enter three values, the second part of the code generates

163
00:14:31,400 --> 00:14:32,860
three random values.

164
00:14:33,320 --> 00:14:39,350
Then you took the sum of each and finally check one receives each sum and returns the result in the

165
00:14:39,350 --> 00:14:40,450
form of a boolean.

166
00:14:40,820 --> 00:14:45,980
And we use that boolean to determine if the user won or lost, that's all.

167
00:14:46,040 --> 00:14:50,360
Not only did you learn about functions, but we gave them some pretty complex functionality.

168
00:14:51,240 --> 00:14:56,190
The next challenge is coming up, so I hope you're ready if you ever have any questions, just ask me

169
00:14:56,190 --> 00:14:56,790
on this court.

170
00:14:56,790 --> 00:14:57,570
I'm always there.

