1
00:00:05,040 --> 00:00:08,560
so just before we start a challenge
there's one little bugs that I want to fix

2
00:00:08,560 --> 00:00:11,950
and I just want to run it again and show you
what the bug was and you probably picked this up

3
00:00:11,950 --> 00:00:18,490
in the last video so when we first start the dealer

4
00:00:18,490 --> 00:00:23,450
score is showing 0 obviously the player scores
working and the reason that's doing that

5
00:00:23,450 --> 00:00:28,790
if you closes this down and on the code on the bottom we've got the deal player

6
00:00:28,790 --> 00:00:33,870
we call the deal_player function and we actually add the card to the

7
00:00:33,870 --> 00:00:38,470
dealer hand and notice we are not calling a
function to print it to display anything

8
00:00:38,470 --> 00:00:43,870
or anything along those lines whereas the deal_player function is sort of

9
00:00:43,870 --> 00:00:46,690
self-contained and also outputs the
actual

10
00:00:46,690 --> 00:00:50,820
scores as well on the screen so what we
want to do is make quick changes there

11
00:00:50,820 --> 00:01:02,210
to actually show this score so its gonna be
dealer.....

12
00:01:02,210 --> 00:01:09,470
...so basically we are just going
to display that's score so we've drawn a

13
00:01:09,470 --> 00:01:12,960
card and we are going to show that on the screen once and then obviously

14
00:01:12,960 --> 00:01:16,550
subsequent times its going to work
automatically when we click the dealer

15
00:01:16,550 --> 00:01:20,590
button so if we run again now that should fix that so we got an

16
00:01:20,590 --> 00:01:27,020
initial score and you can see its better now we got it working even

17
00:01:27,020 --> 00:01:31,280
though the functions still work in that
once we pressed deal it is still going

18
00:01:31,280 --> 00:01:35,340
to calculate this is a better way of
actually showing and obviously I got

19
00:01:35,340 --> 00:01:42,560
slaughtered by dealer ok so that's
working so talking a little  bit about the challenge

20
00:01:42,560 --> 00:01:47,700
its is going to be quite simple because just looking
at where we're at with the game now it

21
00:01:47,700 --> 00:01:51,800
plays a reasonably good game of blackjack
and all the dealing is now taking

22
00:01:51,800 --> 00:01:57,650
care of by the computer so we only need to
click on dealer once we are finish with our turn and the

23
00:01:57,650 --> 00:02:00,010
rest of the game finishes and the deal

24
00:02:00,010 --> 00:02:04,660
function deal_dealer function sort of handles that functionality to play the

25
00:02:04,660 --> 00:02:07,020
game as far as a computer the is concern

26
00:02:07,020 --> 00:02:10,169
their is a bit of annoying though which you see me to do having
to close the window and run the

27
00:02:10,169 --> 00:02:14,040
program again for a new game and I
mentioned in the last video it's time

28
00:02:14,040 --> 00:02:15,820
for a challenge so

29
00:02:15,820 --> 00:02:19,760
just before I do that one final comment about
global variables you probably sick about

30
00:02:19,760 --> 00:02:22,910
it now but we are harping on about it because
it's really important that you

31
00:02:22,910 --> 00:02:29,510
understand this and get it to the right mind set about how to use them so you are aware of the

32
00:02:29,510 --> 00:02:33,990
limitations you might be wondering why
the variables player_hand

33
00:02:33,990 --> 00:02:38,450
and dealer_hand are not triggering
warnings about a local variable shadowing

34
00:02:38,450 --> 00:02:42,180
the global ones because both the deal_player function if we go back and

35
00:02:42,180 --> 00:02:49,160
have a look at that as you can see the deal_player function on line 78 and

36
00:02:49,160 --> 00:02:53,630
the deal_dealer function
on line 64 they are both appending new

37
00:02:53,630 --> 00:02:57,910
cards to the corresponding list so
wouldn't that count as modifying the

38
00:02:57,910 --> 00:03:01,400
global variable in which make them local
because we talked about that in previous

39
00:03:01,400 --> 00:03:02,720
videos

40
00:03:02,720 --> 00:03:08,480
well the answer is that neither variable
is modified their initialized as lists

41
00:03:08,480 --> 00:03:11,590
if you go down and have a look

42
00:03:11,590 --> 00:03:18,470
we got player_hand and dealer_hand is initialization as you can see here in

43
00:03:18,470 --> 00:03:25,500
line 152 and 153 and continue to reference
the same lists as long as the program

44
00:03:25,500 --> 00:03:30,290
runs so adding items to list or removing
items from it is not modifying the list

45
00:03:30,290 --> 00:03:34,610
variable it always has the same value
which is a list and the contents of a list

46
00:03:34,610 --> 00:03:39,800
can change now adding items to a global
list is still a side effect but its not

47
00:03:39,800 --> 00:03:44,500
considered as dangerous as changing the
list of the variable holds for example

48
00:03:44,500 --> 00:03:49,820
lists exists to have items added and
removed and so consequently this is

49
00:03:49,820 --> 00:03:52,930
acceptable behavior and that's why
IntelliJ is not actually warning of us

50
00:03:52,930 --> 00:03:57,350
of that so it would still probably better to
pass the lists arguments to the

51
00:03:57,350 --> 00:04:02,160
functions but as we can't provide parameters to functions that are used as button

52
00:04:02,160 --> 00:04:05,870
commands we really don't have any choice
here but to use global list so that of

53
00:04:05,870 --> 00:04:11,350
course why we went ahead and did that ok so
moving on now the challenge and its going to be

54
00:04:11,350 --> 00:04:15,440
quite a simple one the challenges is to
add a new button to the program with the

55
00:04:15,440 --> 00:04:20,390
text new game now the button should call a
function that clears the cards from the

56
00:04:20,390 --> 00:04:26,050
screen it resets the players and dealers
hands and then starts a new game now the

57
00:04:26,050 --> 00:04:27,820
easiest way to clear the contents of a

58
00:04:27,820 --> 00:04:32,730
frame is to destroy the frame and create
a new one with the same name and in fact

59
00:04:32,730 --> 00:04:37,670
that's why the program has a player_card_frame and dealer_card_frame

60
00:04:37,670 --> 00:04:43,650
inside the card frame
itself so that's it go away and create a

61
00:04:43,650 --> 00:04:48,450
new button with the text new game and again the functionality clear the cards

62
00:04:48,450 --> 00:04:52,080
from the Screen reset the player and
dealers hands and then start a new game so

63
00:04:52,080 --> 00:04:58,390
pause the video go away and see if you can come up with
a solution and when you're ready to see

64
00:04:58,390 --> 00:05:01,980
our version of it come back and and I'll go through that with you pause the video now.....

65
00:05:06,540 --> 00:05:07,940
...ok so hopefully you

66
00:05:07,940 --> 00:05:12,700
managed to figure it out and have a
pretty good going getting it working so let's go through

67
00:05:12,700 --> 00:05:17,820
what our solution is to implement this and
what we're going to do is create a new

68
00:05:17,820 --> 00:05:23,930
function called new_game now the thing to remember here is
the most of the initialization code in

69
00:05:23,930 --> 00:05:28,000
the main program concern setting up the GUI and so consequently we don't need to

70
00:05:28,000 --> 00:05:32,590
repeat all of that every time a new game
started but the deal_card_frame

71
00:05:32,590 --> 00:05:38,390
and player_card_frame will need to be cleared so

72
00:05:38,390 --> 00:05:42,330
calling the destroy method will do that
and once that is done the list holding

73
00:05:42,330 --> 00:05:47,790
the hands rather will need to be cleared and the
first 3 cards dealt and we can also make

74
00:05:47,790 --> 00:05:50,620
a start on that now so let's do that
so I'm going to come down here after

75
00:05:50,620 --> 00:06:01,700
deal_player lets add the method so....and so

76
00:06:01,700 --> 00:06:06,410
we need to reference those global variables....

77
00:06:06,410 --> 00:06:25,270
....

78
00:06:25,270 --> 00:06:32,170
....ok so

79
00:06:32,170 --> 00:06:38,800
first thing we want to do is....

80
00:06:39,630 --> 00:06:53,810
.....

81
00:06:54,470 --> 00:07:02,600
....

82
00:07:03,330 --> 00:07:30,360
....

83
00:07:30,360 --> 00:07:39,330
.....and
what I could have done just scrolling down

84
00:07:39,330 --> 00:07:58,610
is just grab this code again....because its an identical code and obviously I could have done it as well for the dealer frame next we

85
00:07:58,610 --> 00:08:03,810
clear the status of the game at the top wether
dealer wins or the player wins so....

86
00:08:03,810 --> 00:08:10,510
..need to recreate the lists

87
00:08:10,510 --> 00:08:18,890
and what we can do their I can come
down here and just copy this code

88
00:08:18,890 --> 00:08:28,060
copy that and come back up here and paste it in....

89
00:08:31,260 --> 00:08:45,340
...ok so obviously we've cleared
the player hands and dealer hands now and we

90
00:08:45,340 --> 00:08:51,160
deal a new card and we will see also deal a new card for the dealer and another

91
00:08:51,160 --> 00:08:55,880
card for the player again as we've done
previously and obviously because looking

92
00:08:55,880 --> 00:09:00,270
at the code we also need to declare that we
want to use the global objects rather

93
00:09:00,270 --> 00:09:06,020
than creating local variables and that's why
we used to global key word on line

94
00:09:06,020 --> 00:09:11,620
88 to 91 now on Python 3 the 2
lists holding the hands could be cleared

95
00:09:11,620 --> 00:09:16,500
using their .clear method and that just removes
all the items for them but it doesn't create

96
00:09:16,500 --> 00:09:20,140
a new list so we could access to global
variables without declaring them in the

97
00:09:20,140 --> 00:09:24,700
function however we've also written blackjack to work with Python 2 so instead

98
00:09:24,700 --> 00:09:29,070
of that approach what we've done is we assigned empty
lists to the two variables you can see on

99
00:09:29,070 --> 00:09:33,750
line 103 and 104 and that means they do have to
be declared s global and the function

100
00:09:33,750 --> 00:09:38,660
for that to actually work now creating
the button itself is pretty easy we

101
00:09:38,660 --> 00:09:42,580
already have a frame in place so let's add
that down below the initial

102
00:09:42,580 --> 00:10:00,930
initialization code so....

103
00:10:00,930 --> 00:10:04,000
...

104
00:10:07,120 --> 00:10:11,940
....making sure we don't
use the brackets because we don't want

105
00:10:11,940 --> 00:10:16,680
to execute that we just saying this is the
function to call anytime the buttons clicked we are

106
00:10:16,680 --> 00:10:28,100
just gonna call that functionality then need to add it to the grid so.....

107
00:10:28,100 --> 00:10:31,880
....

108
00:10:31,880 --> 00:10:36,250
so that has added the button and just the other thing
we can take because you've notice we've got a lot

109
00:10:36,250 --> 00:10:43,210
of code their that's being used so we can
actually call this new_game function if I

110
00:10:43,210 --> 00:10:48,560
just scroll down I neet to put
some of this back so I'm going to copy

111
00:10:48,560 --> 00:10:58,920
this bit of code here so we need to create those
variables initially down here and once

112
00:10:58,920 --> 00:11:08,380
you've done that we can actually call
the new game functions so I'm gonna do that so....

113
00:11:08,380 --> 00:11:11,330
and what we're trying to do is avoid
duplication of code because we had the

114
00:11:11,330 --> 00:11:15,980
code here that we have originally placed
in there and so basically at the start

115
00:11:15,980 --> 00:11:19,630
of the game we can go through and
just actually run this and we have got

116
00:11:19,630 --> 00:11:23,730
a frame we are actually technically
destroying a frame and created it

117
00:11:23,730 --> 00:11:28,000
creating it again but have what we are
doing is we are avoiding this duplication

118
00:11:28,000 --> 00:11:33,150
of the code and not running it I should say sort of
in two places so the Idea is to put the

119
00:11:33,150 --> 00:11:42,860
code and only executed it only once so lets try running that and see whether it works...

120
00:11:42,860 --> 00:11:48,690
....and so
far so good we've now got another button for a

121
00:11:48,690 --> 00:11:53,640
new game and players 14 so I'm
going to click on play another

122
00:11:53,640 --> 00:11:59,180
card I should say so hit me and I
busted the dealer wins and now I click

123
00:11:59,180 --> 00:12:05,010
new game starting again thats work and
strait away I obviously got blackjack which

124
00:12:05,010 --> 00:12:10,880
is a good thing so I'm going to click on dealer and
I win because the player the dealer

125
00:12:10,880 --> 00:12:11,980
busted

126
00:12:11,980 --> 00:12:15,090
click on new game and click on play

127
00:12:16,050 --> 00:12:18,260
and I got 21 again I'm having a great run here

128
00:12:18,260 --> 00:12:25,020
and the dealer busts so click on new game again I've got 18 so I'm gonna just gonna hold

129
00:12:25,020 --> 00:12:31,260
that and I win again and lets just see whether we
get the dealer to win so click on

130
00:12:31,260 --> 00:12:32,720
dealer

131
00:12:32,720 --> 00:12:36,910
the dealer is having a bad run their new
game

132
00:12:36,910 --> 00:12:42,810
dealer again we got a draw this time so I talked
about that in the previous video wanting

133
00:12:42,810 --> 00:12:48,030
to get it draw so we got that work new game
I'm going to click on dealer again and

134
00:12:48,030 --> 00:12:52,720
lets see if we can get the dealer to win the dealer has a bad run with cards new game I'm

135
00:12:52,720 --> 00:12:58,350
going to click on dealer again and finally we get
the dealer to win so that's working well

136
00:12:58,350 --> 00:13:01,930
but we are gonna have a problem with this
and if we keep playing you should

137
00:13:01,930 --> 00:13:06,340
eventually get an error so I will see if I can do a few more
games and you think what I'm doing this what

138
00:13:06,340 --> 00:13:15,480
that error will be It might take a few more games for us to see it their we go we got it to actually crash so why

139
00:13:15,480 --> 00:13:20,590
is it crashed well the reason it crashed is that we ran out of cards their is no

140
00:13:20,590 --> 00:13:25,520
more cards remember we have we created the
deck of cards initially and we're not

141
00:13:25,520 --> 00:13:30,230
refreshing those and we're not you know
once we reach the end the deck we are not

142
00:13:30,230 --> 00:13:33,940
reinitializing that again and that is why it crashed now theirs

143
00:13:33,940 --> 00:13:38,430
many ways to solve this but the obvious
one would be to create a new deck

144
00:13:38,430 --> 00:13:44,640
list every time a new game has started by
just copying the two lines and

145
00:13:44,640 --> 00:13:52,290
in other words if I just shut this down so the two
lines and actually that turn out to be in the

146
00:13:52,290 --> 00:13:57,200
wrong place so the shuttle should
bee in strait after the deck when we

147
00:13:57,200 --> 00:14:00,560
created it for the first time so what that
actually meant was that we didn't

148
00:14:00,560 --> 00:14:05,870
execute the very first time the game is
played the shuffling of the decks so that was

149
00:14:05,870 --> 00:14:10,190
my mistake putting that in the wrong place
but what we could do the obvious approach

150
00:14:10,190 --> 00:14:16,150
would be to every time we create a new
game we basically execute these two

151
00:14:16,150 --> 00:14:19,980
lines of code create a new deck of cards and shuffle them so that each game

152
00:14:19,980 --> 00:14:25,960
literally like a standard game of blackjack with a new deck so that would be one way we can

153
00:14:25,960 --> 00:14:30,400
actually put that code in the new_game function but we're gonna take

154
00:14:30,400 --> 00:14:31,370
a slightly different approach here

155
00:14:31,370 --> 00:14:35,680
so what we gonna do is every time a new
card is dealt it is going to be put back

156
00:14:35,680 --> 00:14:39,590
at the bottom of the deck in other words
you just reusing the same deck

157
00:14:39,590 --> 00:14:43,980
continually so in Python terms the object is going to be appended back to the list so

158
00:14:43,980 --> 00:14:53,160
we want to do that in the deal_card frame and come back and have a look and you can see on line

159
00:14:53,160 --> 00:15:01,440
35 were actually popping that so what we now need to do
is before we display it....

160
00:15:01,440 --> 00:15:09,620
...so we added it back
again so.....

161
00:15:09,620 --> 00:15:14,390
.....so that should be all

162
00:15:14,390 --> 00:15:18,970
we need to do so this is actually
more realistic because it's like a real

163
00:15:18,970 --> 00:15:22,530
game and that's because when normally play a game of blackjack you don't

164
00:15:22,530 --> 00:15:26,420
start with the a brand new deck for every
single game do you just sort of just

165
00:15:26,420 --> 00:15:30,820
keep using the same deck and that is what we
are doing here essentially now there's many

166
00:15:30,820 --> 00:15:36,390
other improvements you could make to the
game if a new deck is not created for each new game

167
00:15:36,390 --> 00:15:40,850
and the game could have an option to
shuffle and let's add that as well so you can

168
00:15:40,850 --> 00:15:45,900
randomly click on a button and have it do a
shuttle for us to do that we can come down

169
00:15:45,900 --> 00:15:49,440
to the end where we actually added the new game button

170
00:15:49,440 --> 00:15:58,180
we're going to add a shuffle button so....

171
00:15:58,180 --> 00:16:04,980
....

172
00:16:08,610 --> 00:16:15,280
....

173
00:16:15,280 --> 00:16:26,290
...and we now need

174
00:16:26,290 --> 00:16:30,090
to write this function and the functions
is just going to shuffle so what I'm going to do there is

175
00:16:30,090 --> 00:16:34,730
take that line out as well and call this
function so....

176
00:16:36,380 --> 00:16:39,460
even thought we haven't written it yet we're gonna go ahead
ad write that now so gonna come up to

177
00:16:39,460 --> 00:16:50,740
the last method up here and......

178
00:16:50,740 --> 00:16:56,630
...and that should stopped those errors from showing up
now we are calling the shuttle function

179
00:16:56,630 --> 00:17:01,690
and the command shuffle is now working alright
so let's run that and just make sure

180
00:17:01,690 --> 00:17:04,690
that it is working

181
00:17:06,270 --> 00:17:09,290
ok we now got a shuffle button so just click on shuffle

182
00:17:11,130 --> 00:17:15,060
we are not getting any indication that's working but it's
also not crashing which is good so let's

183
00:17:15,060 --> 00:17:20,660
have the dealer win but the dealer lost and that's probably

184
00:17:20,660 --> 00:17:23,450
something you can also look at changing
you notice what happened there is the

185
00:17:23,450 --> 00:17:28,660
dealer actually had 15 but I continue to
draw another card and that's because our

186
00:17:28,660 --> 00:17:34,200
code had the logic that while the score
was 17 or less well less than 17 its going

187
00:17:34,200 --> 00:17:37,700
to try and draw another card so  what it should
probably be doing is each time its drawing a

188
00:17:37,700 --> 00:17:41,090
card its also checking to see whether it's
one or not that will be another enhancement you

189
00:17:41,090 --> 00:17:51,720
can look at making to the game click on new game and click on dealer again 19 so the dealer won and click on shuffle on obviously I click on new game

190
00:17:51,720 --> 00:17:56,910
that's good that's all working nicely and perhaps one other final thing you could do is you

191
00:17:56,910 --> 00:18:02,210
could add a score how many games each
player's won and display the tally at the

192
00:18:02,210 --> 00:18:06,700
top of the screen so have a go at modifying the game or to add that functionality that

193
00:18:06,700 --> 00:18:11,600
will be a nice enhancement you can make and perhaps let us
know how you went about doing it and

194
00:18:11,600 --> 00:18:13,380
maybe even show us the code

195
00:18:13,380 --> 00:18:17,640
and also perhaps look at some other features
that feature that I just mentioned

196
00:18:17,640 --> 00:18:20,740
there with the dealer when its going
to draw a new cards it's already

197
00:18:20,740 --> 00:18:24,520
won it shouldn't be drawing another card as you saw do that because ultimately lost

198
00:18:24,520 --> 00:18:30,180
because it didn't check the status to see whether it won
and I'm gonna finish the video here

199
00:18:30,180 --> 00:18:33,780
now because its getting very long but their is one last
final mini challenge and that's what

200
00:18:33,780 --> 00:18:38,430
would be how would you change the
program to play the game with several

201
00:18:38,430 --> 00:18:41,910
packs of cards in other words just assuming your adding 2 or 3

202
00:18:41,910 --> 00:18:45,930
packs of cards and you got one deck that
contains two or three packs how would

203
00:18:45,930 --> 00:18:50,920
you actually do that so if you can figure
that out right now and pause the video and

204
00:18:50,920 --> 00:18:53,930
I'll come back to me when you come back
I'll show you quickly how to do that

205
00:18:53,930 --> 00:18:56,930
so pause the video now

206
00:18:57,650 --> 00:18:58,640
...ok so the

207
00:18:58,640 --> 00:19:04,520
solution to that the moment you can see
here on line 166 deck = list cards

208
00:19:04,520 --> 00:19:13,680
that's one deck of cards so we could simply
just put.....

209
00:19:13,680 --> 00:19:18,780
and effectively you've got three packs of
cards now that are being put into our

210
00:19:18,780 --> 00:19:20,050
overall deck

211
00:19:20,050 --> 00:19:23,180
and we're still calling the shuffle which
will go through quite happily shuffle

212
00:19:23,180 --> 00:19:30,440
that entire deck which now contains the
three packs as I mentioned and looking at that the game

213
00:19:30,440 --> 00:19:33,990
functions like it would before so
that's it I'm gonna finish the video

214
00:19:33,990 --> 00:19:40,000
here and we'll move on now to more in
the way of modules I'm gonna be talking about

215
00:19:40,000 --> 00:19:43,830
using your program itself as a module so
see you in the next video

