So far, we implement the tasks one to seven of our blackjack project, and this is the output we have
so far.
I think by this point, we're finished all of the tricky tasks, and in this last video, we're going
to raise three tasks, eight to 13.
And so Task Eight tells us to make a wide loop that runs forever, just that it keeps asking the user
to hit or stay.
This is pretty simple.
A while loop keeps running as long as the condition you specify is true.
So if I create a while loop and put while true, it's going to keep running forever.
And during each run I'm going to keep calling the header state function.
Let's rerun the code.
And I get an error because job is telling me, hey, your while loop is running forever.
This is never going to get called.
So I'm going to add comments in front of this code for now.
And rerun.
And here, I expect the while loop to keep running my function forever.
So it's time to add a little bit of logic every time the player hits, I'm going to draw a new card
and calculate their new total.
So I'll set a variable int new card is equal to draw a random card.
And I'll update their head value once again by adding to it the minimum value between ten and the value
of our new card, because remember this new card, it could be a king queen or a Jay.
And in such cases, we need to reduce their values to 10.
All right, then I'll print new line.
You get a new line.
You know, connect the string representation of our new card.
Then I'll print the updated total.
Your total is the updated total value.
Now I can rerun the code.
I can use control, see to interrupt the output and now I can run.
OK.
The while loop keeps getting me to hit day, and every time I hit it draws a new card and it updates
my total.
I can do this forever, and obviously if I write anything other than Whitter, stay the while loop from
the head state function is going to yell at us and say, please write, hit or stay.
And so far our code seems bulletproof and I can keep hitting as many times as I want.
There is just one more thing we need to do.
When the user and we stay, it should break the wire loop.
So here I'll say if option that equal stay.
Break in before I do anything else in my code, I can safely put back the scanned close code and it
should give me any more errors because this code is now reachable.
The while loop doesn't run forever because it will break once, which you stay.
So I'm going to press control, see to interrupt the output, we're on my code.
First, I'll keep hitting a few times.
I'm going to mess things up and everything is working just like I wanted to.
OK, then I'll stay and it breaks the loop perfect.
OK, Task Nine tells us that while the player is heading, if their total exceeds twenty one percent
of best player loses and we can do just that.
After putting the years total, if their total happens to exceed 21.
Then print best player loses.
And terminate the application with system that exit zero.
OK, time to run the code.
I start with an ace and a queen, so my total hand is 11.
And if I hit, I get a 10 woo, I got a 10, that's amazing.
If I go anything above that, I'll go bust because three plus 21 is 24, which is above 21, obviously,
and I lose.
OK, task 10, if the player chooses to stay, it becomes the dealer's third and first, we have to
reveal the dealer's hidden cards.
So after this, while loop breaks, it means the user's turn is done.
So I'll print.
Dealers turn.
The dealers, car dealer.
The string representation of his first card.
The string representation of his second card.
And I'm pretty sure that was a then I'm going to rewrite my code.
I have a four and a two, and I'll hit just enough to get a high score.
Sex was forced, I'll go one more time.
Woops, it should be hitting on her, and you know what, I'm happy with 14, I'm not going to get greedy.
Then I'll stay breaking the while loop, and now it's the dealer's turn and they reveal their cards.
Now, initially, the coach shows you a hidden card.
And that hidden card ended up being a four.
Now, I'm going to rerun the code.
In the event that I keep hitting and go bust, I noticed that the code for the dealers turn doesn't
run because in this case system that exit zero got called, which shuts the entire application down.
OK, time for TASC 11 in this task, the dealer must keep hitting until the total gets to 17.
Once again, I hope this made you think of the while loop.
While the dealers total is less than 17.
We'll say Inch Mukada is equal to draw a random card.
And we'll update the dealers total by setting an equal to the minimum value between 10 and your card
value.
This ensures that any face card like Jack Queen or King or reduce the 10 before being added up.
All right, I think it's time to rerun the code.
Once.
Twenty, I'm happy with that, I'll just stay this time.
And after I states the dealers turn.
And the dealer keeps hitting until their hand, the value exceeds 17.
In this case, the dealer gets 10.
So their new total is 23, which is higher than 17.
And the loop breaks, their turn ends.
Good, we can keep running this to make sure everything is working as it should.
The dealer gets a seven and a three hits, gets a queen, so their new score is 20, which is bigger
than 17.
So the dealer decides to end their turn.
In other words, the loop breaks.
But there's something wrong here.
An extra space is being added to the first line.
I'm just going to fix that.
There you go, the space is being added to the first line of the card were good.
So on to task 12, if the dealers total is higher than 21, we need to make sure the dealer loses.
So at this point in our code, the dealers turn is done, and before we can compare your hand value
against the dealers, we need to make sure that dealer didn't go bust.
So if the dealers total is bigger than 21.
Will print bust dealer loses.
And we will terminate the application with system that exit zero.
OK, I'm going to test the code until we can simulate a scenario where the dealer goes bust and loses.
I'll stay the dealer's cards are king and kings.
That's 20, which is higher than 17.
So the dealer doesn't hit at all.
We'll try again.
How the dealer is so lucky.
This is crazy.
And finally, the dealer loses, the dealer started with a three and a nine, the dealer hit got a J,
which is worth ten and the dealer goes bust because their head value is bigger than twenty one.
Good.
Although something was bothering me as I was running the code, I just want to make sure that says Card's
not card.
And the dealers total should print after they revealed their initial cards.
Right now, we don't have that.
Something to fix this first.
You're running my code.
Good, everything is good so far here, the dealer gets a five and a six, their initial total, a line
of code we just added is 11.
Then the dealer gets a king and they stop at 21.
All right, onto our final task task 13, if at this point the program didn't terminate, then it means
both the user and the dealer finished their turns without going bust.
So we can determine the winner by comparing the dealer's hand against the players hand values.
It will say if my total is bigger than the dealer's total.
Player wins.
Otherwise, the dealer wins.
All right, I run my code.
I'm going to hit and I'm happy with 17, I'm not going to get greedy, I'll stay.
And the dealer wins.
The dealers initial cards are two and three, so their initial total is five.
The dealer hits get a two, that's a total of seven.
They hit once more, get it to once again, their total is nine.
Either gets a king, their total is 19, the dealer stops here and they win the game.
Now, I'm going to try and win because I always seem to lose these Jova games.
I'll hit.
And you know what, I guess I'm destined to lose every job again that I play, I'll try one more time.
Ilet wow, 21, there's no way I'm losing one, and indeed I won finally.
All right.
And on another positive note, our application seems to be working perfectly.
This is definitely by far the hardest and the funnest project you've built so far.
I really hope you've been able to follow along.
And if you have been able to, then congratulations.
There are so many more fun projects we're going to build.
But before we do that, we're going to add one last scale to our Java fundamentals toolbox, and that
is the ability to work with a race.
I will see you in the next section.