Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In part one, you're at the logic for starting the game, I get to cards in my total, gets printed
to the console.
The dealer gets two cards as well, but his second card, as well as his total, are hidden.
And now in part two, we're going to start writing the logic for playing the game in this video.
I want to take the time to do just task seven, because I think it's the most meaningful one.
I'm going to start by going through the rules after all the cards have been dealt.
So basically, right after one, it starts with the player's turn.
The player can decide to hit or stay.
Hitting means the player can get another card.
Staying means do nothing.
The player is done and they want to pass up their turn.
And so if the player decides to hit, they're going to get another card and their head value is going
to increase depending on what that card value is.
And if they keep hitting in their head, value exceeds 21, they go bust, they lose.
However, let's assume the player decides to stay.
Then it's the dealers turn.
And the dealer needs to keep hitting, they need to keep drawing another card until their total gets
to at least 17 at 17 points or higher, the dealers turn is going to end.
And if the dealers total ends up being higher than 21, they lose, they go bust and you win.
But in the event that the user and the dealer finished their turns without going bust, then the winner
is determined based on who has the higher hand value in this case, the user wins because their hand
value is higher than the dealer's hand.
OK, now that we know the rules, let's start coding task seven.
So Task seven was to make a function that's going to ask the user if they want to hit or stay.
So, as always, the function is going to be public static.
As indicated, the function returns a string and the name of the function is hit or stay.
The document says nothing about parameters, so we're not going to need them.
OK, inside the function, the first thing we have to do is ask the user if they want to hit or stay.
So print, would you like to hit or stay?
And now we should understand why a defined scanner at the level of the class, not inside me, because
let's imagine that I define scanner inside mean this means that our scanner variable can only exist
inside the main scope and you cannot access a variable beyond the scope of its existence.
So how can I call scan the next line from within my head or state function?
Well, if you think about the lesson on scope, we can define a variable at the level of a function
or at the level of a class, if you declare a variable inside a function and you can only access the
variable inside the scope of that function.
But if we define the variable at the class level, then we can access that variable from anywhere inside
the class.
As I mentioned, don't worry about what static does.
I'm going to explain this to you in Module two object oriented programming.
But now we can also access scanner from every other function.
So here will say string response is equal to the next line to pick up the user's answer.
The next thing we need to do is check if the user entered anything that isn't hit or stay and if the
response they enter is not valid, we need to keep running the code in a loop until they enter a valid
response.
There are so many ways to think about this and approach this problem.
We could do it in one or two ways.
We could make a loop that keeps you running while the user doesn't enter or stay, or we can make a
loop that runs forever and break it.
Once the user enters hit or stay, both approaches are going to lead to the same result.
But I like approach to and it's the one I told you to do because it's a lot easier to implement because
I can easily make a while loop that runs forever.
And if the response is equal to hit or if the response is equal to stay, then break the loop.
But the first approach is a bit harder because it's hard to imagine how we're going to write a condition
that checks if the user doesn't enter or stay.
So for the sake of practice and because I like to live on the edge, I'm going to start with approach
one and then I'll briefly go over approach to.
It's kind of hard to imagine a condition where the user doesn't enter interstate.
How do you write that?
But it's really easy to think of a condition where the user does enter, hit or stay.
So I'm just going to set a boolean variable a valid response.
Is equal to the result from checking if the users answer is equal to hit.
Or if the users answer is equal to stay very simple.
What I'm going to do now is actually change the boolean variable name to invalid response.
And I can never so simply reverse the result by applying the not operator, so at first we were checking
if the user entered Whitter stay, but by wrapping the entire condition with the not operator, we're
checking for the opposite thing if the user didn't enter or stay.
So as you can see, sometimes it's easier to start with a more intuitive condition and then modify it
to suit your needs.
In short, this condition is going to return true if the response doesn't equal, hit or stay.
So now I can make a while loop that keeps running while the users answer is invalid.
And inside the loop, we're going to tell the user, please write Whitter, stay.
And after this prompt, we're going to pick up their answer.
And now I can return the user's response, which at this point has to be hit or stay because the while
loop must have broken.
OK, now inside the main method.
I'm going to set a variable option.
Is equal to the return value from Whitter stay and I think it's finally time to run our code.
At this point, the function better stay is being called, if I write anything that isn't hit or stay,
it keeps prompting me to re-enter my answer until I do so.
Here's an animation of what's going on.
OK, now we could have done this a lot differently.
Approach to suggests using a while loop that runs forever and breaking it once the user enters it,
or that was the instruction I gave you unlearn the parts.
And so I'm not going to change the code.
But if you are interested in approaching it using the second method, here's how the code would look.
The while loop runs forever, but inside of the wire loop, it contains a condition that checks if the
user's answer is hit or stay.
So as soon as the response equals Whitter stay, this condition is going to turn true.
The statement is going to run and break the loop.
And after the while loop, we're sure that the response equals hatred, stay so we can safely return
it.
So I hope you can see that when you're coding an application, there's always more than one way to code
the same thing.
OK, one final thing I want to do before we wrap up this lesson is rerun my code and let's just say
the user writes capital hits instead of lowercase it.
The string is uppercase, so it's not going to work, but in my opinion, this is an acceptable response.
So how do we compare the string value whilst ignoring uppercase and lowercase letters?
Well, this is a perfect time to go on Google and look it up.
So here I would write something to the contents of how to compare strings ignoring case in Java.
Boom.
There is an equal Zeichner case method, so back in my code, instead of checking of the string, values
are equal.
I can check if they're equal while ignoring letter cases.
Rewriting my code, remember, you can press control easy to stop the output.
And now if I write capital letters hit, it works fine.
Does it matter how I decide to play around with the letter cases as long as I write hit or stay, I
should be fine.
So I hope that shows you that even though I've been coding in Java for so many years, I didn't memorize
the built in methods or functions.
When I need them, I can just look them up because a good developer never memorizes code.
A good developer knows how to navigate the Internet to read documentation and find resources.
OK, enough about that.
In the next video, we're going to implement tasks eight to 13 and finalize the blackjack project.
See you in there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.