Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,470 --> 00:00:05,150
In this section, we're going to move on to our next method, the deal method, the deal method is going
2
00:00:05,150 --> 00:00:09,680
to be pretty critical inside of our program because just about every card game in the world involves
3
00:00:09,680 --> 00:00:11,540
dealing some number of cards to a player.
4
00:00:12,470 --> 00:00:17,900
We're going to, again, make use of a built-In method on the list class for implementing deal, but
5
00:00:17,900 --> 00:00:21,540
it's not going to be quite as easy as it was the last time that we used a built in method.
6
00:00:22,100 --> 00:00:26,300
Let's go look at the documentation of what we're going to use and we'll talk about why this is going
7
00:00:26,300 --> 00:00:28,370
to be a little bit more on the challenging side.
8
00:00:29,170 --> 00:00:35,350
OK, so here's the latest class documentation, I'm going to scroll down a bit until I find the method
9
00:00:35,350 --> 00:00:41,050
section and then in the method section, I'm going to find the method called sublist.
10
00:00:41,560 --> 00:00:42,390
Here it is right here.
11
00:00:43,240 --> 00:00:48,610
So the sublist method takes two integers representing a start point and an endpoint.
12
00:00:49,910 --> 00:00:56,270
It will then return a new list that contains all the objects from that START index or that kind of start
13
00:00:56,270 --> 00:01:00,920
element that we passed in all the way to the end element exclusive.
14
00:01:00,950 --> 00:01:04,550
So that means that we'll take it all the way, not including to that last one.
15
00:01:05,940 --> 00:01:11,100
So the sublist method right here is a little bit deceptive, a little bit deceptive.
16
00:01:11,130 --> 00:01:13,500
Let me show you a diagram that's going to help you understand why.
17
00:01:14,340 --> 00:01:17,790
OK, so let's imagine that you and I in the real world.
18
00:01:17,940 --> 00:01:21,410
OK, so this diagram right here is going to be reflecting the real world.
19
00:01:22,020 --> 00:01:28,320
If I had a deck of cards like you see right here, and I wanted to deal you a couple of cards out of
20
00:01:28,320 --> 00:01:31,710
his hand, you would probably expect to see something like this happen.
21
00:01:31,710 --> 00:01:32,760
You would expect to see.
22
00:01:33,940 --> 00:01:39,550
Let me just put this in back real quick, that if I took card zero and put it into your hands, it would
23
00:01:39,550 --> 00:01:40,240
be moved down.
24
00:01:40,480 --> 00:01:44,990
And if I took a card one and gave it to you, it would move down to your hand as well.
25
00:01:45,010 --> 00:01:49,690
And so this big box down here represents your hand or the cards that I'm dealing to you.
26
00:01:50,290 --> 00:01:58,110
So in other words, if I give you a card out of my deck, chances are I don't retain access to it anymore.
27
00:01:58,870 --> 00:02:02,770
So just to be clear, that's probably how you would expect this deal function to work.
28
00:02:02,950 --> 00:02:06,030
If I deal you a card, I no longer have control of it.
29
00:02:06,670 --> 00:02:10,770
However, that is not how the sublist method right here works.
30
00:02:10,780 --> 00:02:12,500
And so it's a little bit surprising.
31
00:02:13,090 --> 00:02:14,800
Here's how sublist works instead.
32
00:02:16,240 --> 00:02:22,210
So a sublist, we're going to take some number of elements out of this original list and they're going
33
00:02:22,210 --> 00:02:27,740
to be it's going to appear that they get duplicated over to this new list as well.
34
00:02:28,480 --> 00:02:32,680
So when I deal you a card, it's going to appear that it stays in my hand.
35
00:02:32,680 --> 00:02:35,320
But it also appears in your hand as well.
36
00:02:36,920 --> 00:02:39,060
So this is kind of interesting on two fronts.
37
00:02:39,410 --> 00:02:45,350
First off, it really doesn't reflect how you and I probably want our deal method to work, you and
38
00:02:45,350 --> 00:02:47,060
I, for the purposes of our program.
39
00:02:47,180 --> 00:02:51,860
We probably want to make sure that if I deal you a hand of cards, I no longer have access to them.
40
00:02:52,200 --> 00:02:54,460
So that's kind of a requirement of our program.
41
00:02:54,470 --> 00:02:56,230
We definitely want to make sure things work that way.
42
00:02:56,660 --> 00:03:02,300
But the second thing that's kind of interesting is how it will appear that our card is being duplicated
43
00:03:02,300 --> 00:03:03,420
between these two lists.
44
00:03:03,950 --> 00:03:09,650
However, here's the interesting thing is that this is the entire point of all this discussion about
45
00:03:09,650 --> 00:03:10,670
dealing these cards.
46
00:03:11,270 --> 00:03:16,670
These cards do not actually get duplicated when they are put into this new list.
47
00:03:17,300 --> 00:03:22,250
So let's take a look at a diagram that's going to help you understand how these list objects work in
48
00:03:22,250 --> 00:03:22,630
DART.
49
00:03:22,790 --> 00:03:23,130
OK.
50
00:03:23,960 --> 00:03:24,440
All right.
51
00:03:24,440 --> 00:03:25,480
So we're going to pull up a diagram.
52
00:03:26,120 --> 00:03:29,760
So we've again, got a diagram of the memory in your computer.
53
00:03:29,810 --> 00:03:34,520
So this is kind of a diagram of what's going on inside the memory on your machine when you are going
54
00:03:34,520 --> 00:03:35,600
to run our program.
55
00:03:36,890 --> 00:03:42,830
When you use Dart and you create a list, you end up putting some number of elements into that list
56
00:03:42,830 --> 00:03:47,690
and we really think of that list as like containing these elements or containing these objects.
57
00:03:47,990 --> 00:03:49,880
But in reality, that's not quite the case.
58
00:03:50,510 --> 00:03:56,420
You see, when we create these cards, they are created in some random location, in memory, like maybe
59
00:03:56,420 --> 00:03:59,110
once here and once here in another one's here.
60
00:03:59,540 --> 00:04:05,330
And so each of these boxes represent the actual instance of the class that contains the rank and the
61
00:04:05,330 --> 00:04:07,130
suit of a particular card.
62
00:04:08,160 --> 00:04:13,180
The card object itself in memory is not actually inserted into the list.
63
00:04:13,650 --> 00:04:20,790
Instead, our list object right here contains a reference that points over to card number one or at
64
00:04:20,790 --> 00:04:21,600
index number one.
65
00:04:21,600 --> 00:04:26,370
Here inside the list, we've got a reference that points over to card number two in a reference that
66
00:04:26,370 --> 00:04:27,690
points over to card number three.
67
00:04:29,130 --> 00:04:34,230
So when we run the sublist method on this list right here and try to pull off kind of like a subset
68
00:04:34,230 --> 00:04:38,070
of records, the records are not actually being duplicated.
69
00:04:38,430 --> 00:04:41,240
Instead, we are creating a second list.
70
00:04:41,580 --> 00:04:46,470
It's like it's kind of you can kind of imagine, it appears over here and in that list, in this brand
71
00:04:46,470 --> 00:04:52,770
new one, we get the exact same references that point back to those same objects.
72
00:04:53,520 --> 00:04:58,560
So, again, it's going to appear that these cards get duplicated over to the new list, but they're
73
00:04:58,560 --> 00:05:00,180
not actually duplicated.
74
00:05:00,480 --> 00:05:02,720
It's the same exact card in memory.
75
00:05:03,090 --> 00:05:09,090
And so if we change the card right here or what appears to be right here, it's going to modify that
76
00:05:09,090 --> 00:05:14,220
card that appears to be over here as well, because they are not actually contained within the list.
77
00:05:15,080 --> 00:05:21,560
OK, so this is this idea of references versus actual values, you might be kind of getting the idea
78
00:05:21,560 --> 00:05:25,010
here that it's kind of important in the world of dirt, and that's completely correct.
79
00:05:25,310 --> 00:05:30,530
So we really want to make sure that whenever we're working with values or references or variables,
80
00:05:30,530 --> 00:05:33,140
we really have an idea of which one we're dealing with.
81
00:05:33,830 --> 00:05:35,030
So let's take a break right here.
82
00:05:35,060 --> 00:05:36,530
We're going to come back to the next section.
83
00:05:36,650 --> 00:05:39,560
And now that we're armed with this knowledge of how that.
84
00:05:40,610 --> 00:05:45,610
Sublist method actually works, I think we'll be able to put together a working deal function.
85
00:05:45,860 --> 00:05:47,980
So quick break and I'll see you in just a minute.
8711
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.