1 00:00:00,210 --> 00:00:04,950 So far, we implement the tasks one to seven of our blackjack project, and this is the output we have 2 00:00:04,950 --> 00:00:05,610 so far. 3 00:00:07,820 --> 00:00:12,440 I think by this point, we're finished all of the tricky tasks, and in this last video, we're going 4 00:00:12,440 --> 00:00:14,450 to raise three tasks, eight to 13. 5 00:00:15,320 --> 00:00:20,180 And so Task Eight tells us to make a wide loop that runs forever, just that it keeps asking the user 6 00:00:20,180 --> 00:00:20,980 to hit or stay. 7 00:00:21,680 --> 00:00:22,900 This is pretty simple. 8 00:00:22,910 --> 00:00:26,390 A while loop keeps running as long as the condition you specify is true. 9 00:00:26,930 --> 00:00:31,790 So if I create a while loop and put while true, it's going to keep running forever. 10 00:00:32,150 --> 00:00:35,660 And during each run I'm going to keep calling the header state function. 11 00:00:38,550 --> 00:00:39,810 Let's rerun the code. 12 00:00:46,510 --> 00:00:50,890 And I get an error because job is telling me, hey, your while loop is running forever. 13 00:00:50,930 --> 00:00:52,450 This is never going to get called. 14 00:00:52,810 --> 00:00:55,450 So I'm going to add comments in front of this code for now. 15 00:00:58,340 --> 00:00:59,240 And rerun. 16 00:01:14,190 --> 00:01:17,640 And here, I expect the while loop to keep running my function forever. 17 00:01:22,980 --> 00:01:28,290 So it's time to add a little bit of logic every time the player hits, I'm going to draw a new card 18 00:01:28,290 --> 00:01:29,760 and calculate their new total. 19 00:01:30,330 --> 00:01:35,580 So I'll set a variable int new card is equal to draw a random card. 20 00:01:39,880 --> 00:01:45,520 And I'll update their head value once again by adding to it the minimum value between ten and the value 21 00:01:45,520 --> 00:01:49,860 of our new card, because remember this new card, it could be a king queen or a Jay. 22 00:01:49,870 --> 00:01:52,450 And in such cases, we need to reduce their values to 10. 23 00:01:58,250 --> 00:02:00,200 All right, then I'll print new line. 24 00:02:02,850 --> 00:02:04,170 You get a new line. 25 00:02:14,060 --> 00:02:16,670 You know, connect the string representation of our new card. 26 00:02:23,500 --> 00:02:25,120 Then I'll print the updated total. 27 00:02:28,350 --> 00:02:30,990 Your total is the updated total value. 28 00:02:40,020 --> 00:02:41,580 Now I can rerun the code. 29 00:02:42,810 --> 00:02:46,010 I can use control, see to interrupt the output and now I can run. 30 00:02:54,430 --> 00:02:54,850 OK. 31 00:02:54,880 --> 00:03:00,310 The while loop keeps getting me to hit day, and every time I hit it draws a new card and it updates 32 00:03:00,310 --> 00:03:00,910 my total. 33 00:03:06,160 --> 00:03:11,140 I can do this forever, and obviously if I write anything other than Whitter, stay the while loop from 34 00:03:11,140 --> 00:03:14,680 the head state function is going to yell at us and say, please write, hit or stay. 35 00:03:15,250 --> 00:03:20,400 And so far our code seems bulletproof and I can keep hitting as many times as I want. 36 00:03:20,710 --> 00:03:22,900 There is just one more thing we need to do. 37 00:03:23,050 --> 00:03:26,040 When the user and we stay, it should break the wire loop. 38 00:03:26,590 --> 00:03:29,680 So here I'll say if option that equal stay. 39 00:03:32,410 --> 00:03:39,370 Break in before I do anything else in my code, I can safely put back the scanned close code and it 40 00:03:39,370 --> 00:03:42,510 should give me any more errors because this code is now reachable. 41 00:03:42,850 --> 00:03:46,330 The while loop doesn't run forever because it will break once, which you stay. 42 00:03:48,530 --> 00:03:52,130 So I'm going to press control, see to interrupt the output, we're on my code. 43 00:03:56,410 --> 00:03:58,330 First, I'll keep hitting a few times. 44 00:04:03,210 --> 00:04:07,260 I'm going to mess things up and everything is working just like I wanted to. 45 00:04:08,660 --> 00:04:12,620 OK, then I'll stay and it breaks the loop perfect. 46 00:04:24,620 --> 00:04:29,480 OK, Task Nine tells us that while the player is heading, if their total exceeds twenty one percent 47 00:04:29,480 --> 00:04:32,900 of best player loses and we can do just that. 48 00:04:36,510 --> 00:04:40,380 After putting the years total, if their total happens to exceed 21. 49 00:04:48,690 --> 00:04:51,780 Then print best player loses. 50 00:04:54,370 --> 00:04:57,850 And terminate the application with system that exit zero. 51 00:05:02,760 --> 00:05:04,110 OK, time to run the code. 52 00:05:19,740 --> 00:05:23,190 I start with an ace and a queen, so my total hand is 11. 53 00:05:27,780 --> 00:05:31,520 And if I hit, I get a 10 woo, I got a 10, that's amazing. 54 00:05:32,040 --> 00:05:39,090 If I go anything above that, I'll go bust because three plus 21 is 24, which is above 21, obviously, 55 00:05:39,090 --> 00:05:39,930 and I lose. 56 00:05:43,470 --> 00:05:49,440 OK, task 10, if the player chooses to stay, it becomes the dealer's third and first, we have to 57 00:05:49,440 --> 00:05:51,100 reveal the dealer's hidden cards. 58 00:05:51,630 --> 00:05:55,020 So after this, while loop breaks, it means the user's turn is done. 59 00:05:55,380 --> 00:05:56,370 So I'll print. 60 00:05:57,900 --> 00:05:58,860 Dealers turn. 61 00:06:09,940 --> 00:06:11,350 The dealers, car dealer. 62 00:06:16,520 --> 00:06:18,950 The string representation of his first card. 63 00:06:39,180 --> 00:06:41,550 The string representation of his second card. 64 00:06:45,630 --> 00:06:48,750 And I'm pretty sure that was a then I'm going to rewrite my code. 65 00:06:55,710 --> 00:06:59,910 I have a four and a two, and I'll hit just enough to get a high score. 66 00:07:00,900 --> 00:07:03,170 Sex was forced, I'll go one more time. 67 00:07:06,190 --> 00:07:12,010 Woops, it should be hitting on her, and you know what, I'm happy with 14, I'm not going to get greedy. 68 00:07:13,970 --> 00:07:20,140 Then I'll stay breaking the while loop, and now it's the dealer's turn and they reveal their cards. 69 00:07:23,810 --> 00:07:26,420 Now, initially, the coach shows you a hidden card. 70 00:07:28,270 --> 00:07:30,730 And that hidden card ended up being a four. 71 00:07:34,720 --> 00:07:36,190 Now, I'm going to rerun the code. 72 00:07:46,870 --> 00:07:52,210 In the event that I keep hitting and go bust, I noticed that the code for the dealers turn doesn't 73 00:07:52,210 --> 00:07:58,060 run because in this case system that exit zero got called, which shuts the entire application down. 74 00:07:59,050 --> 00:08:06,400 OK, time for TASC 11 in this task, the dealer must keep hitting until the total gets to 17. 75 00:08:07,150 --> 00:08:09,840 Once again, I hope this made you think of the while loop. 76 00:08:14,510 --> 00:08:17,030 While the dealers total is less than 17. 77 00:08:20,910 --> 00:08:24,660 We'll say Inch Mukada is equal to draw a random card. 78 00:08:34,990 --> 00:08:40,600 And we'll update the dealers total by setting an equal to the minimum value between 10 and your card 79 00:08:40,600 --> 00:08:41,020 value. 80 00:08:42,820 --> 00:08:48,730 This ensures that any face card like Jack Queen or King or reduce the 10 before being added up. 81 00:09:26,290 --> 00:09:28,900 All right, I think it's time to rerun the code. 82 00:09:40,750 --> 00:09:41,320 Once. 83 00:09:43,230 --> 00:09:46,620 Twenty, I'm happy with that, I'll just stay this time. 84 00:09:48,640 --> 00:09:50,560 And after I states the dealers turn. 85 00:09:53,110 --> 00:09:56,840 And the dealer keeps hitting until their hand, the value exceeds 17. 86 00:09:57,400 --> 00:09:59,170 In this case, the dealer gets 10. 87 00:09:59,440 --> 00:10:02,020 So their new total is 23, which is higher than 17. 88 00:10:02,020 --> 00:10:04,270 And the loop breaks, their turn ends. 89 00:10:06,100 --> 00:10:09,690 Good, we can keep running this to make sure everything is working as it should. 90 00:10:15,570 --> 00:10:21,540 The dealer gets a seven and a three hits, gets a queen, so their new score is 20, which is bigger 91 00:10:21,540 --> 00:10:22,310 than 17. 92 00:10:22,620 --> 00:10:24,770 So the dealer decides to end their turn. 93 00:10:25,170 --> 00:10:26,910 In other words, the loop breaks. 94 00:10:27,660 --> 00:10:29,010 But there's something wrong here. 95 00:10:29,580 --> 00:10:32,060 An extra space is being added to the first line. 96 00:10:32,070 --> 00:10:33,660 I'm just going to fix that. 97 00:10:58,180 --> 00:11:01,960 There you go, the space is being added to the first line of the card were good. 98 00:11:06,840 --> 00:11:12,030 So on to task 12, if the dealers total is higher than 21, we need to make sure the dealer loses. 99 00:11:18,030 --> 00:11:23,760 So at this point in our code, the dealers turn is done, and before we can compare your hand value 100 00:11:23,760 --> 00:11:26,690 against the dealers, we need to make sure that dealer didn't go bust. 101 00:11:27,150 --> 00:11:30,570 So if the dealers total is bigger than 21. 102 00:11:33,450 --> 00:11:36,390 Will print bust dealer loses. 103 00:11:44,020 --> 00:11:47,440 And we will terminate the application with system that exit zero. 104 00:11:51,140 --> 00:11:56,720 OK, I'm going to test the code until we can simulate a scenario where the dealer goes bust and loses. 105 00:12:07,850 --> 00:12:10,780 I'll stay the dealer's cards are king and kings. 106 00:12:10,830 --> 00:12:12,800 That's 20, which is higher than 17. 107 00:12:13,100 --> 00:12:14,940 So the dealer doesn't hit at all. 108 00:12:15,290 --> 00:12:16,310 We'll try again. 109 00:12:26,910 --> 00:12:28,320 How the dealer is so lucky. 110 00:12:28,350 --> 00:12:29,220 This is crazy. 111 00:12:31,250 --> 00:12:37,340 And finally, the dealer loses, the dealer started with a three and a nine, the dealer hit got a J, 112 00:12:37,520 --> 00:12:43,010 which is worth ten and the dealer goes bust because their head value is bigger than twenty one. 113 00:12:43,670 --> 00:12:43,970 Good. 114 00:12:44,750 --> 00:12:49,820 Although something was bothering me as I was running the code, I just want to make sure that says Card's 115 00:12:50,000 --> 00:12:50,870 not card. 116 00:12:53,190 --> 00:12:56,790 And the dealers total should print after they revealed their initial cards. 117 00:12:56,820 --> 00:12:57,960 Right now, we don't have that. 118 00:13:01,840 --> 00:13:03,340 Something to fix this first. 119 00:13:12,000 --> 00:13:13,110 You're running my code. 120 00:13:25,710 --> 00:13:31,650 Good, everything is good so far here, the dealer gets a five and a six, their initial total, a line 121 00:13:31,650 --> 00:13:33,750 of code we just added is 11. 122 00:13:34,180 --> 00:13:37,260 Then the dealer gets a king and they stop at 21. 123 00:13:42,280 --> 00:13:48,970 All right, onto our final task task 13, if at this point the program didn't terminate, then it means 124 00:13:48,970 --> 00:13:52,810 both the user and the dealer finished their turns without going bust. 125 00:13:53,380 --> 00:13:58,480 So we can determine the winner by comparing the dealer's hand against the players hand values. 126 00:14:00,370 --> 00:14:03,790 It will say if my total is bigger than the dealer's total. 127 00:14:12,460 --> 00:14:13,690 Player wins. 128 00:14:18,510 --> 00:14:20,370 Otherwise, the dealer wins. 129 00:14:30,600 --> 00:14:32,130 All right, I run my code. 130 00:14:42,000 --> 00:14:46,530 I'm going to hit and I'm happy with 17, I'm not going to get greedy, I'll stay. 131 00:14:49,000 --> 00:14:50,500 And the dealer wins. 132 00:14:54,230 --> 00:14:58,220 The dealers initial cards are two and three, so their initial total is five. 133 00:15:01,210 --> 00:15:03,880 The dealer hits get a two, that's a total of seven. 134 00:15:06,300 --> 00:15:09,660 They hit once more, get it to once again, their total is nine. 135 00:15:11,160 --> 00:15:15,840 Either gets a king, their total is 19, the dealer stops here and they win the game. 136 00:15:16,680 --> 00:15:20,040 Now, I'm going to try and win because I always seem to lose these Jova games. 137 00:15:20,040 --> 00:15:20,580 I'll hit. 138 00:15:22,470 --> 00:15:28,260 And you know what, I guess I'm destined to lose every job again that I play, I'll try one more time. 139 00:15:31,190 --> 00:15:36,940 Ilet wow, 21, there's no way I'm losing one, and indeed I won finally. 140 00:15:37,850 --> 00:15:38,470 All right. 141 00:15:38,480 --> 00:15:42,590 And on another positive note, our application seems to be working perfectly. 142 00:15:42,950 --> 00:15:46,900 This is definitely by far the hardest and the funnest project you've built so far. 143 00:15:47,540 --> 00:15:49,460 I really hope you've been able to follow along. 144 00:15:49,460 --> 00:15:52,150 And if you have been able to, then congratulations. 145 00:15:52,610 --> 00:15:55,100 There are so many more fun projects we're going to build. 146 00:15:55,100 --> 00:16:00,200 But before we do that, we're going to add one last scale to our Java fundamentals toolbox, and that 147 00:16:00,200 --> 00:16:02,360 is the ability to work with a race. 148 00:16:02,790 --> 00:16:04,850 I will see you in the next section.