WEBVTT 1 00:00:01.930 --> 00:00:07.399 so what we need to do now is implement the deal_ dealer function now this 2 00:00:07.399 --> 00:00:12.240 turns out to be very similar to the deal_player function but because 3 00:00:12.240 --> 00:00:16.600 the players turn has finished by the time the dealer gets more cards what we 4 00:00:16.600 --> 00:00:20.550 can do here in this function is we can add a bit of extra code to check to see 5 00:00:20.550 --> 00:00:24.670 who is actually won the game because of course the only check we got at the moment 6 00:00:24.670 --> 00:00:28.779 is on line 69 to check to see whether the dealer wins so we need see whether the 7 00:00:28.779 --> 00:00:32.689 player wins as well we need more details in addition to that because the moment 8 00:00:32.689 --> 00:00:36.350 we are only really basically checking to see at the moment if the players busted if the 9 00:00:36.350 --> 00:00:41.050 scores greater than 21 could be a situation of course we're both a dealer and the player got 10 00:00:41.050 --> 00:00:45.300 cards less than 21 we need to do that check as well as well as checking 11 00:00:45.300 --> 00:00:51.770 to see whether the dealer is busted so lets make a start so go up here and start 12 00:00:51.770 --> 00:00:54.770 typing.... 13 00:00:55.390 --> 00:01:06.780 ...and we need to add the score so dealer..... 14 00:01:06.780 --> 00:01:19.960 ....and then we 15 00:01:19.960 --> 00:01:25.830 want to show that on the screens so..... 16 00:01:25.830 --> 00:01:36.420 .....and we now need to do some checking to see whether it's game over 17 00:01:36.420 --> 00:01:45.820 so what we want to do first is get the players score again so.... 18 00:01:45.820 --> 00:01:50.869 ....so what is the score for the player currently then would need to do 19 00:01:50.869 --> 00:01:55.600 some testing here to see who's won or if anyone has won so..... 20 00:01:55.600 --> 00:02:02.799 .... 21 00:02:03.590 --> 00:02:06.730 .... 22 00:02:06.730 --> 00:02:13.580 ...which may already been set on line 76 but we gonna put it 23 00:02:13.580 --> 00:02:19.880 here anyway just in case we need to put... 24 00:02:19.880 --> 00:02:28.340 ....so either way the 25 00:02:28.340 --> 00:02:34.950 dealer is lost in this situation then the player won so put..... 26 00:02:34.950 --> 00:02:44.230 ..... 27 00:02:44.230 --> 00:02:48.810 ...so we already establish the both the player and the dealer 28 00:02:48.810 --> 00:02:53.500 haven't gone busted at this point because we've done those checks on line 66 29 00:02:53.500 --> 00:02:57.080 and 68 so we know that they're both less than 21 so now we are 30 00:02:57.080 --> 00:03:00.400 doing a test to see whether the dealer score is greater than the player score 31 00:03:00.400 --> 00:03:10.890 then we are going to put.... 32 00:03:10.890 --> 00:03:16.280 ....can you think what that last possibility is well the last 33 00:03:16.280 --> 00:03:20.640 possibilities is a draw we've done everything else we've done all the tests to see whether both the player and the 34 00:03:20.640 --> 00:03:24.420 dealer had gone bust or whether the players score or the dealer score is 35 00:03:24.420 --> 00:03:29.470 greater than 21 or the dealer scorer is less than players score so we have determined that 36 00:03:29.470 --> 00:03:38.980 the player can win as well and anything left to do is going to be a draw so...... 37 00:03:38.980 --> 00:03:45.120 ....because obviously if both score is the same if it gets to this point that should be our method so it's 38 00:03:45.120 --> 00:03:48.350 really quite simple no matter what the dealers score is if the player went bust 39 00:03:48.350 --> 00:03:52.590 and the dealer wins so that's the first condition tested if the players 40 00:03:52.590 --> 00:03:56.670 not bust the player wins if their score is higher than the dealer or the dealer 41 00:03:56.670 --> 00:04:02.510 went bust and obviously we get down that last else on line 72 any outcome means a draw 42 00:04:02.510 --> 00:04:08.300 alright so we should have the basis for a game here so lets run this and see what it does 43 00:04:08.300 --> 00:04:15.840 so let's see whether it actually works we are gonna put dealer well will actually do the player first the 44 00:04:15.840 --> 00:04:19.359 players got an 11 an ace which is scored 11 which 45 00:04:19.359 --> 00:04:24.430 is correct for the first ace dealer its got a 10 and according this the player has won 46 00:04:24.430 --> 00:04:32.300 and we continue on player dealer and its still not quite working at this 47 00:04:32.300 --> 00:04:36.110 stage but we've got the basis on here its actually doing some basic calculation 48 00:04:36.110 --> 00:04:40.830 but obviously we need we still need to do a bit of work here because when we first 49 00:04:40.830 --> 00:04:51.469 started it and I'll just run it again player dealer we are getting a message immediately that 50 00:04:51.469 --> 00:04:55.779 the dealer wins because the score was greater than the players so its a lil bit 51 00:04:55.779 --> 00:04:59.650 disconcerting that because that message shouldn't really be appearing until the dealer has finished 52 00:04:59.650 --> 00:05:04.740 playing or player has gone bust so it's correct it's actually processing this 53 00:05:04.740 --> 00:05:08.000 function correctly but we need to actually put an extra condition in to 54 00:05:08.000 --> 00:05:12.919 have the game go as far as it can before it actually showing who wins so I guess it'd be 55 00:05:12.919 --> 00:05:16.319 more accurate to say that deal is winning so far this point in time click on 56 00:05:16.319 --> 00:05:27.310 player again and obviously we got a bit of work here to do still so let's go ahead and continue 57 00:05:27.310 --> 00:05:31.599 looking at this so one advantage of creating functions that perform specific 58 00:05:31.599 --> 00:05:35.759 tasks is that they can be used whenever you need the functionality that they 59 00:05:35.759 --> 00:05:39.669 provide and we saw an example of that with score_hand which we 60 00:05:39.669 --> 00:05:43.120 used in a number of places which is pretty handy that we only written that 61 00:05:43.120 --> 00:05:47.759 code once and we are able to call it multiple times so in the main program we really 62 00:05:47.759 --> 00:05:51.259 don't want the dealer to play they turn as their initial cards are dealt we just 63 00:05:51.259 --> 00:05:55.509 just want them to deal a card and stored in their hands so what we gonna do is 64 00:05:55.509 --> 00:06:04.589 make a change their go down to the bottom so after here after the deal_hand we 65 00:06:04.589 --> 00:06:07.360 are setting up those entries to be empty 66 00:06:07.360 --> 00:06:12.469 where we are starting after that we want to put.....so 67 00:06:12.469 --> 00:06:20.240 the player gets the first card automatically and..... 68 00:06:20.240 --> 00:06:25.500 .... 69 00:06:28.610 --> 00:06:33.490 ..so what we want to deal player again so we want to deal first basically the player is gonna get two cards which 70 00:06:33.490 --> 00:06:37.020 is how blackjack works and the dealer is going to have one and if we run that 71 00:06:37.020 --> 00:06:48.639 again lets see what that looks like so its definitely getting better now we haven't got any initial scores showing up we got the 72 00:06:48.639 --> 00:06:52.889 correctly showing the player score to be 13 we got a problem here though the 73 00:06:52.889 --> 00:06:58.030 dealer score is showing 0 for some reason so it could be a bug or something 74 00:06:58.030 --> 00:07:00.729 that we've introduced here because the dealer score should be 75 00:07:00.729 --> 00:07:05.150 calculated and it should actually initially be 9 so its getting better but its still not 76 00:07:05.150 --> 00:07:09.300 correct yet because the person playing the game has to click the button for each of 77 00:07:09.300 --> 00:07:13.639 the dealers cards and once we clicked it then incorrectly does the score but it didn't do 78 00:07:13.639 --> 00:07:17.259 that initially but we wouldn't ordinarily want that to happen the 79 00:07:17.259 --> 00:07:20.319 computer should really play the part of the dealer and all the player should do is 80 00:07:20.319 --> 00:07:24.580 click their own button for more cards or click dealer button once they want to stick 81 00:07:24.580 --> 00:07:28.919 so the deal_dealer function that's called when the dealers 82 00:07:28.919 --> 00:07:32.310 button is clicked will need to score the dealer's hand which only has a 83 00:07:32.310 --> 00:07:36.729 single card to begin with and then automatically keep dealing more cards until 84 00:07:36.729 --> 00:07:41.919 the dealer score is greater than or equal to 17 or the dealer goes bust and once 85 00:07:41.919 --> 00:07:46.219 that has happen the score are then check and the results are displayed so let's go back 86 00:07:46.219 --> 00:07:55.729 and make a change to the code for that on deal_dealer function so we 87 00:07:55.729 --> 00:08:00.039 got the dealer score calculated correctly so what we gonna do is I'm going to temporarily 88 00:08:00.039 --> 00:08:03.620 cut that because we're gonna put that back on some code in a minute so the dealer 89 00:08:03.620 --> 00:08:09.930 score so its calculated at first then what we want to do is put..... 90 00:08:09.930 --> 00:08:14.169 .... 91 00:08:15.370 --> 00:08:20.370 ...so in other words basically what a dealer score is 0 or less 92 00:08:20.370 --> 00:08:25.319 than 17 and remember we establish the rules that if scores less than seventeen 93 00:08:25.319 --> 00:08:29.680 the dealer would automatically grab another card and that is what 94 00:08:29.680 --> 00:08:31.129 we do with that code 95 00:08:31.129 --> 00:08:36.839 and then the other thing we need to do is after we've appended we then calculate the score again so come back 96 00:08:36.839 --> 00:08:43.539 down here will copy this bit of code put that in there and then we actually show the score 97 00:08:43.539 --> 00:08:46.779 on the screens so basically what happened is we got to deal_dealer 98 00:08:46.779 --> 00:08:52.649 it should automatically keep dealing cards for us until it gets to score of 99 00:08:52.649 --> 00:08:56.399 17 or higher and obviously it could still bust and then the rest of the code will be executed 100 00:08:56.399 --> 00:09:02.160 on line 67 down and we can establish whether there's been a win or a loss and 101 00:09:02.160 --> 00:09:05.790 the reason on line 61 were actually calculating the score there is we are 102 00:09:05.790 --> 00:09:10.329 dealing with the the score for the card that's initially dealt when we first 103 00:09:10.329 --> 00:09:13.860 start because you saw that when I ran this the previous time the dealer score 104 00:09:13.860 --> 00:09:17.790 showing a 0 so that's why we are calculating that score first and then were actually 105 00:09:17.790 --> 00:09:22.519 adding the score each time another cards dealt so looking down the code we've got the 106 00:09:22.519 --> 00:09:28.819 code down here so we got deal_player which draws the player card deal_hand 107 00:09:28.819 --> 00:09:34.360 .append which actually called the dealer_card function to 108 00:09:34.360 --> 00:09:38.339 deal the first card of the dealer and then we deal the card for the player again 109 00:09:38.339 --> 00:09:43.810 so what should happen now is that we'll see two cards for the player and 110 00:09:43.810 --> 00:09:49.160 two cards or more for the dealer depending on what the score was so lets try running 111 00:09:49.160 --> 00:09:52.160 then see what happens 112 00:09:55.860 --> 00:10:02.279 so now when we click dealer you notice how it went through and automatically selected the extra cards so 113 00:10:02.279 --> 00:10:06.959 what basically won the game at that point and we got no play again button at the moments so we need to 114 00:10:06.959 --> 00:10:18.470 stop and start again and run again you can see the dealer has got 18 compared to 9 115 00:10:18.470 --> 00:10:23.160 now its still not really working correctly because what we really need to do is 116 00:10:23.160 --> 00:10:33.390 change this run it again so basically its the wrong button that I'm actually pressing so we got the score 117 00:10:33.390 --> 00:10:38.200 their players got a score of 19 at the moment so if we want another card we 118 00:10:38.200 --> 00:10:44.519 click on player to get another card and obviously we didn't get a bust we got to 21 so the dealer now when we click 119 00:10:44.519 --> 00:10:47.630 the dealer button once and it's going right through and finish the dealer shot and 120 00:10:47.630 --> 00:10:50.350 that's really the point I was making the player wins because we've got to 121 00:10:50.350 --> 00:10:53.440 seventeen you remember the score has to be less than 17 otherwise it would keep 122 00:10:53.440 --> 00:10:59.200 dealing another card so we got to 17 at that point we actually won so will try it 123 00:10:59.200 --> 00:11:09.010 again ok we got a score of 16 we are gonna click on player again to get another card and obviously 124 00:11:09.010 --> 00:11:12.940 we've gone bust so we gone over 21 so the dealer has correctly won their so 125 00:11:12.940 --> 00:11:15.360 that's good that's also working run it again 126 00:11:15.360 --> 00:11:18.360 just run it a few more times to make sure it's working correctly 127 00:11:19.149 --> 00:11:23.550 we got a score of 14 lets click on trying another card we get to 20 which is good so let's 128 00:11:23.550 --> 00:11:27.949 now click on dealer and see what the dealer does the dealer got to 25 and the dealer 129 00:11:27.949 --> 00:11:30.110 busts so player wins 130 00:11:30.110 --> 00:11:33.880 ok try it again and see what I hope we can get a draw here just to confirm that's 131 00:11:33.880 --> 00:11:40.490 working as well so player got a score of 14 player I've busted the dealer wins that's correct 132 00:11:44.060 --> 00:11:53.950 ok score of 17 I'm gonna hold on that so click on dealer and the dealer wins with a score of 21 so I'll 133 00:11:53.950 --> 00:11:58.040 try one more time to see whether we can happen to get a draw but you can see all the other are working 134 00:11:58.040 --> 00:12:01.040 now so run that one more time 135 00:12:02.090 --> 00:12:06.240 with a bit of luck will get the score being equal so we got a score 15 I'm gonna stick to that 136 00:12:06.240 --> 00:12:10.430 so I'm just going to click on a dealer and the player wins because the dealer bust so 137 00:12:10.430 --> 00:12:14.430 I won't do that but basically the code should work for the draw we've seen all 138 00:12:14.430 --> 00:12:18.360 the other scenarios what seems to be working correctly which is great so the 139 00:12:18.360 --> 00:12:22.180 programs now playing a reasonable game of blackjack and all the 140 00:12:22.180 --> 00:12:26.040 dealing is being taken care of by the computer so it's a bit annoying having 141 00:12:26.040 --> 00:12:29.910 to close the window and run the program again though for a new game and I think 142 00:12:29.910 --> 00:12:33.530 it's about time for challenges so will talk about that and also talked about 143 00:12:33.530 --> 00:12:38.610 will also go through one more time about some global variables comments that you might find 144 00:12:38.610 --> 00:12:42.140 interesting and then work on a challenge and the challenges is gonna be for 145 00:12:42.140 --> 00:12:45.890 you to come up with a new game button start a new game so we don't have to stop and 146 00:12:45.890 --> 00:12:50.260 start the game each time like you've seen me do so we will look at doing that in the 147 00:12:50.260 --> 00:12:50.570 next video