Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:05,220
In part one, you're at the logic for starting the game, I get to cards in my total, gets printed
2
00:00:05,220 --> 00:00:05,910
to the console.
3
00:00:08,710 --> 00:00:13,080
The dealer gets two cards as well, but his second card, as well as his total, are hidden.
4
00:00:14,020 --> 00:00:18,450
And now in part two, we're going to start writing the logic for playing the game in this video.
5
00:00:18,460 --> 00:00:22,840
I want to take the time to do just task seven, because I think it's the most meaningful one.
6
00:00:23,290 --> 00:00:26,790
I'm going to start by going through the rules after all the cards have been dealt.
7
00:00:26,800 --> 00:00:30,250
So basically, right after one, it starts with the player's turn.
8
00:00:30,790 --> 00:00:32,860
The player can decide to hit or stay.
9
00:00:33,500 --> 00:00:35,470
Hitting means the player can get another card.
10
00:00:35,950 --> 00:00:37,750
Staying means do nothing.
11
00:00:37,780 --> 00:00:40,020
The player is done and they want to pass up their turn.
12
00:00:40,450 --> 00:00:45,010
And so if the player decides to hit, they're going to get another card and their head value is going
13
00:00:45,010 --> 00:00:47,560
to increase depending on what that card value is.
14
00:00:53,930 --> 00:00:58,580
And if they keep hitting in their head, value exceeds 21, they go bust, they lose.
15
00:01:00,540 --> 00:01:02,880
However, let's assume the player decides to stay.
16
00:01:08,510 --> 00:01:09,740
Then it's the dealers turn.
17
00:01:15,290 --> 00:01:20,090
And the dealer needs to keep hitting, they need to keep drawing another card until their total gets
18
00:01:20,090 --> 00:01:25,330
to at least 17 at 17 points or higher, the dealers turn is going to end.
19
00:01:25,700 --> 00:01:30,950
And if the dealers total ends up being higher than 21, they lose, they go bust and you win.
20
00:01:36,760 --> 00:01:42,370
But in the event that the user and the dealer finished their turns without going bust, then the winner
21
00:01:42,370 --> 00:01:47,830
is determined based on who has the higher hand value in this case, the user wins because their hand
22
00:01:47,830 --> 00:01:49,750
value is higher than the dealer's hand.
23
00:01:50,920 --> 00:01:54,310
OK, now that we know the rules, let's start coding task seven.
24
00:01:57,300 --> 00:02:01,090
So Task seven was to make a function that's going to ask the user if they want to hit or stay.
25
00:02:01,290 --> 00:02:03,840
So, as always, the function is going to be public static.
26
00:02:07,700 --> 00:02:12,560
As indicated, the function returns a string and the name of the function is hit or stay.
27
00:02:14,960 --> 00:02:18,560
The document says nothing about parameters, so we're not going to need them.
28
00:02:19,460 --> 00:02:24,050
OK, inside the function, the first thing we have to do is ask the user if they want to hit or stay.
29
00:02:24,740 --> 00:02:27,050
So print, would you like to hit or stay?
30
00:02:31,440 --> 00:02:36,720
And now we should understand why a defined scanner at the level of the class, not inside me, because
31
00:02:36,720 --> 00:02:42,270
let's imagine that I define scanner inside mean this means that our scanner variable can only exist
32
00:02:42,270 --> 00:02:47,470
inside the main scope and you cannot access a variable beyond the scope of its existence.
33
00:02:48,180 --> 00:02:51,840
So how can I call scan the next line from within my head or state function?
34
00:02:53,790 --> 00:02:58,380
Well, if you think about the lesson on scope, we can define a variable at the level of a function
35
00:02:58,680 --> 00:03:03,810
or at the level of a class, if you declare a variable inside a function and you can only access the
36
00:03:03,810 --> 00:03:06,110
variable inside the scope of that function.
37
00:03:06,630 --> 00:03:11,730
But if we define the variable at the class level, then we can access that variable from anywhere inside
38
00:03:11,730 --> 00:03:12,360
the class.
39
00:03:13,050 --> 00:03:15,810
As I mentioned, don't worry about what static does.
40
00:03:16,050 --> 00:03:19,680
I'm going to explain this to you in Module two object oriented programming.
41
00:03:19,770 --> 00:03:22,440
But now we can also access scanner from every other function.
42
00:03:22,470 --> 00:03:27,900
So here will say string response is equal to the next line to pick up the user's answer.
43
00:03:30,160 --> 00:03:34,990
The next thing we need to do is check if the user entered anything that isn't hit or stay and if the
44
00:03:34,990 --> 00:03:39,880
response they enter is not valid, we need to keep running the code in a loop until they enter a valid
45
00:03:39,880 --> 00:03:40,750
response.
46
00:03:41,470 --> 00:03:44,490
There are so many ways to think about this and approach this problem.
47
00:03:44,950 --> 00:03:46,720
We could do it in one or two ways.
48
00:03:47,140 --> 00:03:52,900
We could make a loop that keeps you running while the user doesn't enter or stay, or we can make a
49
00:03:52,900 --> 00:03:55,210
loop that runs forever and break it.
50
00:03:55,210 --> 00:04:00,040
Once the user enters hit or stay, both approaches are going to lead to the same result.
51
00:04:00,040 --> 00:04:05,860
But I like approach to and it's the one I told you to do because it's a lot easier to implement because
52
00:04:05,860 --> 00:04:07,930
I can easily make a while loop that runs forever.
53
00:04:08,080 --> 00:04:12,880
And if the response is equal to hit or if the response is equal to stay, then break the loop.
54
00:04:13,570 --> 00:04:18,579
But the first approach is a bit harder because it's hard to imagine how we're going to write a condition
55
00:04:18,579 --> 00:04:21,160
that checks if the user doesn't enter or stay.
56
00:04:21,910 --> 00:04:25,960
So for the sake of practice and because I like to live on the edge, I'm going to start with approach
57
00:04:25,960 --> 00:04:28,840
one and then I'll briefly go over approach to.
58
00:04:32,150 --> 00:04:35,940
It's kind of hard to imagine a condition where the user doesn't enter interstate.
59
00:04:35,960 --> 00:04:36,820
How do you write that?
60
00:04:37,250 --> 00:04:41,640
But it's really easy to think of a condition where the user does enter, hit or stay.
61
00:04:42,350 --> 00:04:45,890
So I'm just going to set a boolean variable a valid response.
62
00:04:50,690 --> 00:04:55,550
Is equal to the result from checking if the users answer is equal to hit.
63
00:04:57,960 --> 00:05:02,220
Or if the users answer is equal to stay very simple.
64
00:05:09,330 --> 00:05:13,890
What I'm going to do now is actually change the boolean variable name to invalid response.
65
00:05:16,800 --> 00:05:22,950
And I can never so simply reverse the result by applying the not operator, so at first we were checking
66
00:05:22,950 --> 00:05:28,500
if the user entered Whitter stay, but by wrapping the entire condition with the not operator, we're
67
00:05:28,500 --> 00:05:32,250
checking for the opposite thing if the user didn't enter or stay.
68
00:05:32,880 --> 00:05:37,830
So as you can see, sometimes it's easier to start with a more intuitive condition and then modify it
69
00:05:37,830 --> 00:05:38,970
to suit your needs.
70
00:05:39,450 --> 00:05:44,250
In short, this condition is going to return true if the response doesn't equal, hit or stay.
71
00:05:45,770 --> 00:05:49,520
So now I can make a while loop that keeps running while the users answer is invalid.
72
00:06:03,850 --> 00:06:07,960
And inside the loop, we're going to tell the user, please write Whitter, stay.
73
00:06:16,120 --> 00:06:18,640
And after this prompt, we're going to pick up their answer.
74
00:06:31,320 --> 00:06:36,300
And now I can return the user's response, which at this point has to be hit or stay because the while
75
00:06:36,300 --> 00:06:37,440
loop must have broken.
76
00:06:41,630 --> 00:06:43,550
OK, now inside the main method.
77
00:06:46,370 --> 00:06:48,020
I'm going to set a variable option.
78
00:06:50,840 --> 00:06:56,810
Is equal to the return value from Whitter stay and I think it's finally time to run our code.
79
00:07:10,800 --> 00:07:16,710
At this point, the function better stay is being called, if I write anything that isn't hit or stay,
80
00:07:16,980 --> 00:07:20,340
it keeps prompting me to re-enter my answer until I do so.
81
00:07:37,500 --> 00:07:39,290
Here's an animation of what's going on.
82
00:08:21,650 --> 00:08:24,070
OK, now we could have done this a lot differently.
83
00:08:25,010 --> 00:08:30,500
Approach to suggests using a while loop that runs forever and breaking it once the user enters it,
84
00:08:30,500 --> 00:08:33,559
or that was the instruction I gave you unlearn the parts.
85
00:08:33,740 --> 00:08:36,020
And so I'm not going to change the code.
86
00:08:36,679 --> 00:08:41,750
But if you are interested in approaching it using the second method, here's how the code would look.
87
00:08:50,980 --> 00:08:55,540
The while loop runs forever, but inside of the wire loop, it contains a condition that checks if the
88
00:08:55,540 --> 00:08:56,980
user's answer is hit or stay.
89
00:08:57,160 --> 00:09:01,540
So as soon as the response equals Whitter stay, this condition is going to turn true.
90
00:09:01,760 --> 00:09:03,940
The statement is going to run and break the loop.
91
00:09:31,670 --> 00:09:36,320
And after the while loop, we're sure that the response equals hatred, stay so we can safely return
92
00:09:36,320 --> 00:09:36,610
it.
93
00:09:45,770 --> 00:09:50,060
So I hope you can see that when you're coding an application, there's always more than one way to code
94
00:09:50,060 --> 00:09:50,810
the same thing.
95
00:09:56,930 --> 00:10:02,840
OK, one final thing I want to do before we wrap up this lesson is rerun my code and let's just say
96
00:10:02,840 --> 00:10:06,110
the user writes capital hits instead of lowercase it.
97
00:10:07,080 --> 00:10:12,420
The string is uppercase, so it's not going to work, but in my opinion, this is an acceptable response.
98
00:10:13,090 --> 00:10:17,410
So how do we compare the string value whilst ignoring uppercase and lowercase letters?
99
00:10:18,060 --> 00:10:21,420
Well, this is a perfect time to go on Google and look it up.
100
00:10:22,140 --> 00:10:28,710
So here I would write something to the contents of how to compare strings ignoring case in Java.
101
00:10:30,540 --> 00:10:30,990
Boom.
102
00:10:31,020 --> 00:10:36,450
There is an equal Zeichner case method, so back in my code, instead of checking of the string, values
103
00:10:36,450 --> 00:10:36,930
are equal.
104
00:10:40,550 --> 00:10:43,280
I can check if they're equal while ignoring letter cases.
105
00:10:52,040 --> 00:10:56,030
Rewriting my code, remember, you can press control easy to stop the output.
106
00:11:02,870 --> 00:11:06,010
And now if I write capital letters hit, it works fine.
107
00:11:10,310 --> 00:11:15,410
Does it matter how I decide to play around with the letter cases as long as I write hit or stay, I
108
00:11:15,410 --> 00:11:16,280
should be fine.
109
00:11:16,910 --> 00:11:21,770
So I hope that shows you that even though I've been coding in Java for so many years, I didn't memorize
110
00:11:21,770 --> 00:11:23,570
the built in methods or functions.
111
00:11:23,960 --> 00:11:28,520
When I need them, I can just look them up because a good developer never memorizes code.
112
00:11:29,000 --> 00:11:34,070
A good developer knows how to navigate the Internet to read documentation and find resources.
113
00:11:35,060 --> 00:11:36,530
OK, enough about that.
114
00:11:36,530 --> 00:11:42,280
In the next video, we're going to implement tasks eight to 13 and finalize the blackjack project.
115
00:11:42,590 --> 00:11:43,430
See you in there.
12409
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.