1
00:00:05,380 --> 00:00:10,840
so what we need to do now is implement the deal_ dealer function now this

2
00:00:10,840 --> 00:00:15,690
turns out to be very similar to the deal_player function but because

3
00:00:15,690 --> 00:00:20,050
the players turn has finished by the
time the dealer gets more cards what we

4
00:00:20,050 --> 00:00:24,000
can do here in this function is we can
add a bit of extra code to check to see

5
00:00:24,000 --> 00:00:28,120
who is actually won the game because of
course the only check we got at the moment

6
00:00:28,120 --> 00:00:32,220
is on line 69 to check to see whether
the dealer wins so we need see whether the

7
00:00:32,220 --> 00:00:36,130
player wins as well we need more details
in addition to that because the moment

8
00:00:36,130 --> 00:00:39,800
we are only really basically checking to
see at the moment if the players busted if the

9
00:00:39,800 --> 00:00:44,500
scores greater than 21 could be a situation of
course we're both a dealer and the player got

10
00:00:44,500 --> 00:00:48,750
cards less than 21 we need to do
that check as well as well as checking

11
00:00:48,750 --> 00:00:55,220
to see whether the dealer is busted so lets make a start so go up here and start

12
00:00:55,220 --> 00:00:58,220
typing....

13
00:00:58,840 --> 00:01:10,230
...and we need to add the score so dealer.....

14
00:01:10,230 --> 00:01:23,410
....and then we

15
00:01:23,410 --> 00:01:29,280
want to show that on the screens so.....

16
00:01:29,280 --> 00:01:39,870
.....and we now need to do some
checking to see whether it's game over

17
00:01:39,870 --> 00:01:49,270
so what we want to do first is get the players score
again so....

18
00:01:49,270 --> 00:01:54,310
....so what is the score for the
player currently then would need to do

19
00:01:54,310 --> 00:01:59,050
some testing here to see who's won or if anyone has won so.....

20
00:01:59,050 --> 00:02:06,240
....

21
00:02:07,040 --> 00:02:10,180
....

22
00:02:10,180 --> 00:02:17,030
...which may already been set on line 76 but we gonna put it

23
00:02:17,030 --> 00:02:23,330
here anyway just in case we need to
put...

24
00:02:23,330 --> 00:02:31,790
....so either way the

25
00:02:31,790 --> 00:02:38,400
dealer is lost in this situation then the
player won so put.....

26
00:02:38,400 --> 00:02:47,680
.....

27
00:02:47,680 --> 00:02:52,260
...so we already establish
the both the player and the dealer

28
00:02:52,260 --> 00:02:56,950
haven't gone busted at this point
because we've done those checks on line 66

29
00:02:56,950 --> 00:03:00,530
and 68 so we know that they're both
less than 21 so now we are

30
00:03:00,530 --> 00:03:03,850
doing a test to see whether the dealer
score is greater than the player score

31
00:03:03,850 --> 00:03:14,340
then we are going to put....

32
00:03:14,340 --> 00:03:19,730
....can you think what that
last possibility is well the last

33
00:03:19,730 --> 00:03:24,090
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:24,090 --> 00:03:27,870
dealer had gone bust or whether the
players score or the dealer score is

35
00:03:27,870 --> 00:03:32,920
greater than 21 or the dealer scorer is less than players score so we have determined that

36
00:03:32,920 --> 00:03:42,430
the player can win as well and anything
left to do is going to be a draw so......

37
00:03:42,430 --> 00:03:48,570
....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:48,570 --> 00:03:51,800
really quite simple no matter what the
dealers score is if the player went bust

39
00:03:51,800 --> 00:03:56,040
and the dealer wins so that's the
first condition tested if the players

40
00:03:56,040 --> 00:04:00,120
not bust the player wins if their score
is higher than the dealer or the dealer

41
00:04:00,120 --> 00:04:05,960
went bust and obviously we get down
that last else on line 72 any outcome means a draw

42
00:04:05,960 --> 00:04:11,750
alright so we should have the basis for a
game here so lets run this and see what it does

43
00:04:11,750 --> 00:04:19,290
so let's see whether it actually works
we are gonna put dealer well will actually do the player first the

44
00:04:19,290 --> 00:04:22,800
players got an 11 an ace which is
scored 11 which

45
00:04:22,800 --> 00:04:27,880
is correct for the first ace dealer its got a 10 and according this the player has won

46
00:04:27,880 --> 00:04:35,750
and we continue on player dealer and its still not quite working at this

47
00:04:35,750 --> 00:04:39,560
stage but we've got the basis on here its
actually doing some basic calculation

48
00:04:39,560 --> 00:04:44,280
but obviously we need we still need to
do a bit of work here because when we first

49
00:04:44,280 --> 00:04:54,910
started it and I'll just run it again player dealer we are getting a message immediately that

50
00:04:54,910 --> 00:04:59,220
the dealer wins because the score was
greater than the players so its a lil bit

51
00:04:59,220 --> 00:05:03,100
disconcerting that because that message shouldn't really
be appearing until the dealer has finished

52
00:05:03,100 --> 00:05:08,190
playing or player has gone bust so it's correct it's actually processing this

53
00:05:08,190 --> 00:05:11,450
function correctly but we need to
actually put an extra condition in to

54
00:05:11,450 --> 00:05:16,360
have the game go as far as it can before it actually
showing who wins so I guess it'd be

55
00:05:16,360 --> 00:05:19,760
more accurate to say that deal is
winning so far this point in time click on

56
00:05:19,760 --> 00:05:30,760
player again and obviously we got a bit of work here to do still so let's go ahead and continue

57
00:05:30,760 --> 00:05:35,040
looking at this so one advantage of
creating functions that perform specific

58
00:05:35,040 --> 00:05:39,200
tasks is that they can be used whenever
you need the functionality that they

59
00:05:39,200 --> 00:05:43,110
provide and we saw an example of
that with score_hand which we

60
00:05:43,110 --> 00:05:46,570
used in a number of places which
is pretty handy that we only written that

61
00:05:46,570 --> 00:05:51,200
code once and we are able to call it multiple
times so in the main program we really

62
00:05:51,200 --> 00:05:54,700
don't want the dealer to play they turn as their initial cards are dealt we just

63
00:05:54,700 --> 00:05:58,950
just want them to deal a card and stored
in their hands so what we gonna do is

64
00:05:58,950 --> 00:06:08,030
make a change their go down to the bottom so after
here after the deal_hand we

65
00:06:08,030 --> 00:06:10,810
are setting up those entries to be empty

66
00:06:10,810 --> 00:06:15,910
where we are starting after that we want to put.....so

67
00:06:15,910 --> 00:06:23,690
the player gets the first card automatically and.....

68
00:06:23,690 --> 00:06:28,950
....

69
00:06:32,060 --> 00:06:36,940
..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:36,940 --> 00:06:40,470
is how blackjack works and the dealer
is going to have one and if we run that

71
00:06:40,470 --> 00:06:52,080
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:52,080 --> 00:06:56,330
correctly showing the player score to be 13 we got a problem here though the

73
00:06:56,330 --> 00:07:01,480
dealer score is showing 0 for some
reason so it could be a bug or something

74
00:07:01,480 --> 00:07:04,170
that we've introduced here because the dealer score should be

75
00:07:04,170 --> 00:07:08,600
calculated and it should actually initially
be 9 so its getting better but its still not

76
00:07:08,600 --> 00:07:12,750
correct yet because the person playing the
game has to click the button for each of

77
00:07:12,750 --> 00:07:17,080
the dealers cards and once we clicked it
then incorrectly does the score but it didn't do

78
00:07:17,080 --> 00:07:20,700
that initially but we wouldn't
ordinarily want that to happen the

79
00:07:20,700 --> 00:07:23,760
computer should really play the part of
the dealer and all the player should do is

80
00:07:23,760 --> 00:07:28,030
click their own button for more cards or
click dealer button once they want to stick

81
00:07:28,030 --> 00:07:32,360
so the deal_dealer function
that's called when the dealers

82
00:07:32,360 --> 00:07:35,760
button is clicked will need to score
the dealer's hand which only has a

83
00:07:35,760 --> 00:07:40,170
single card to begin with and then
automatically keep dealing more cards until

84
00:07:40,170 --> 00:07:45,360
the dealer score is greater than or equal
to 17 or the dealer goes bust and once

85
00:07:45,360 --> 00:07:49,660
that has happen the score are then check and
the results are displayed so let's go back

86
00:07:49,660 --> 00:07:59,170
and make a change to the code for that on deal_dealer function so we

87
00:07:59,170 --> 00:08:03,480
got the dealer score calculated correctly so
what we gonna do is I'm going to temporarily

88
00:08:03,480 --> 00:08:07,070
cut that because we're gonna put that
back on some code in a minute so the dealer

89
00:08:07,070 --> 00:08:13,380
score so its calculated at first then
what we want to do is put.....

90
00:08:13,380 --> 00:08:17,610
....

91
00:08:18,820 --> 00:08:23,820
...so in other
words basically what a dealer score is 0 or less

92
00:08:23,820 --> 00:08:28,760
than 17 and remember we establish the
rules that if scores less than seventeen

93
00:08:28,760 --> 00:08:33,130
the dealer would
automatically grab another card and that is what

94
00:08:33,130 --> 00:08:34,570
we do with that code

95
00:08:34,570 --> 00:08:40,280
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:40,280 --> 00:08:46,980
down here will copy this bit of code put that in
there and then we actually show the score

97
00:08:46,980 --> 00:08:50,220
on the screens so basically what
happened is we got to deal_dealer

98
00:08:50,220 --> 00:08:56,090
it should automatically keep dealing
cards for us until it gets to score of

99
00:08:56,090 --> 00:08:59,840
17 or higher and obviously it could still bust and
then the rest of the code will be executed

100
00:08:59,840 --> 00:09:05,610
on line 67 down and we can establish
whether there's been a win or a loss and

101
00:09:05,610 --> 00:09:09,240
the reason on line 61 were actually
calculating the score there is we are

102
00:09:09,240 --> 00:09:13,770
dealing with the the score for the card
that's initially dealt when we first

103
00:09:13,770 --> 00:09:17,310
start because you saw that when I ran this the previous time the dealer score

104
00:09:17,310 --> 00:09:21,240
showing a 0 so that's why we are calculating that score first and then were actually

105
00:09:21,240 --> 00:09:25,960
adding the score each time another cards dealt
so looking down the code we've got the

106
00:09:25,960 --> 00:09:32,260
code down here so we got deal_player which draws the player card deal_hand

107
00:09:32,260 --> 00:09:37,810
.append which actually called the dealer_card function to

108
00:09:37,810 --> 00:09:41,780
deal the first card of the dealer and then we deal the card for the player again

109
00:09:41,780 --> 00:09:47,260
so what should happen now is that
we'll see two cards for the player and

110
00:09:47,260 --> 00:09:52,610
two cards or more for the dealer
depending on what the score was so lets try running

111
00:09:52,610 --> 00:09:55,610
then see what happens

112
00:09:59,310 --> 00:10:05,720
so now when we click dealer you notice how it went through and automatically selected the extra cards so

113
00:10:05,720 --> 00:10:10,400
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:10,400 --> 00:10:21,920
stop and start again and run again you can
see the dealer has got 18 compared to 9

115
00:10:21,920 --> 00:10:26,610
now its still not really working correctly
because what we really need to do is

116
00:10:26,610 --> 00:10:36,840
change this run it again so basically its the
wrong button that I'm actually pressing so we got the score

117
00:10:36,840 --> 00:10:41,650
their players got a score of 19 at the
moment so if we want another card we

118
00:10:41,650 --> 00:10:47,960
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:47,960 --> 00:10:51,080
the dealer button once and it's going right
through and finish the dealer shot and

120
00:10:51,080 --> 00:10:53,800
that's really the point I was making the
player wins because we've got to

121
00:10:53,800 --> 00:10:56,890
seventeen you remember the score has to be less than 17 otherwise it would keep

122
00:10:56,890 --> 00:11:02,650
dealing another card so we got to 17 at that
point we actually won so will try it

123
00:11:02,650 --> 00:11:12,460
again ok we got a score of 16 we are gonna click on player
again to get another card and obviously

124
00:11:12,460 --> 00:11:16,390
we've gone bust so we gone over 21 so the
dealer has correctly won their so

125
00:11:16,390 --> 00:11:18,810
that's good that's also working run it
again

126
00:11:18,810 --> 00:11:21,810
just run it a few more times to make sure
it's working correctly

127
00:11:22,590 --> 00:11:27,000
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:27,000 --> 00:11:31,390
now click on dealer and see what the
dealer does the dealer got to 25 and the dealer

129
00:11:31,390 --> 00:11:33,560
busts so player wins

130
00:11:33,560 --> 00:11:37,330
ok try it again and see what I hope we can
get a draw here just to confirm that's

131
00:11:37,330 --> 00:11:43,940
working as well so player got a score of 14 player I've busted the dealer wins that's correct

132
00:11:47,510 --> 00:11:57,400
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:57,400 --> 00:12:01,490
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:12:01,490 --> 00:12:04,490
now so run that one more time

135
00:12:05,540 --> 00:12:09,690
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:09,690 --> 00:12:13,880
so I'm just going to click on a dealer and
the player wins because the dealer bust so

137
00:12:13,880 --> 00:12:17,880
I won't do that but basically the code
should work for the draw we've seen all

138
00:12:17,880 --> 00:12:21,810
the other scenarios what seems to be
working correctly which is great so the

139
00:12:21,810 --> 00:12:25,630
programs now playing a
reasonable game of blackjack and all the

140
00:12:25,630 --> 00:12:29,490
dealing is being taken care of by the
computer so it's a bit annoying having

141
00:12:29,490 --> 00:12:33,360
to close the window and run the program
again though for a new game and I think

142
00:12:33,360 --> 00:12:36,980
it's about time for challenges so will
talk about that and also talked about

143
00:12:36,980 --> 00:12:42,060
will also go through one more time about some
global variables comments that you might find

144
00:12:42,060 --> 00:12:45,590
interesting and then work on a challenge
and the challenges is gonna be for

145
00:12:45,590 --> 00:12:49,340
you to come up with a new game button start
a new game so we don't have to stop and

146
00:12:49,340 --> 00:12:53,710
start the game each time like you've
seen me do so we will look at doing that in the

147
00:12:53,710 --> 00:12:54,020
next video

