1
00:00:03,880 --> 00:00:08,840
Welcome to this new lesson
on data structures.

2
00:00:08,840 --> 00:00:10,820
We're going to be moving towards

3
00:00:10,820 --> 00:00:14,040
a continuation of
four-video series.

4
00:00:14,040 --> 00:00:16,020
This is the second one
of four in which we're

5
00:00:16,020 --> 00:00:19,265
actually going to build
a Langton's algorithm.

6
00:00:19,265 --> 00:00:21,760
In this case we're
going to be looking at

7
00:00:21,760 --> 00:00:24,160
the movement of an entity,

8
00:00:24,160 --> 00:00:25,945
a worker in a grid.

9
00:00:25,945 --> 00:00:28,000
We've been talking
about this already.

10
00:00:28,000 --> 00:00:30,240
We've been creating
a random walker

11
00:00:30,240 --> 00:00:32,020
and we're going to
recreate some of that,

12
00:00:32,020 --> 00:00:33,600
but we're going to allow it to

13
00:00:33,600 --> 00:00:35,060
change the data of the grid.

14
00:00:35,060 --> 00:00:37,340
We're going to start
creating this symbiosis,

15
00:00:37,340 --> 00:00:40,740
this relationship
between the walker

16
00:00:40,740 --> 00:00:42,680
affecting the grid
or the base or

17
00:00:42,680 --> 00:00:45,420
the ground in which it's
walking and also the later

18
00:00:45,420 --> 00:00:48,430
taking decisions based on that.

19
00:00:48,430 --> 00:00:52,155
If we have a coordinate,

20
00:00:52,155 --> 00:00:57,800
our position of an entity
within a grid and we identify,

21
00:00:57,800 --> 00:00:59,440
as we have been discussing,

22
00:00:59,440 --> 00:01:02,675
a variable direction, that
gives us a sense of an arrow.

23
00:01:02,675 --> 00:01:05,170
Like the arrow zero would be

24
00:01:05,170 --> 00:01:09,740
representing movement
towards the right.

25
00:01:09,740 --> 00:01:13,500
We can say that movement
of that entity within

26
00:01:13,500 --> 00:01:18,255
that grid is position
x plus equals one.

27
00:01:18,255 --> 00:01:22,905
We are moving the Indexing
1 in one direction.

28
00:01:22,905 --> 00:01:27,520
If we actually, a rotation
would mean that we could

29
00:01:27,520 --> 00:01:31,420
actually change the variable
of rotation plus one,

30
00:01:31,420 --> 00:01:33,980
and that would actually
flip the orientation,

31
00:01:33,980 --> 00:01:38,895
and in which case if we
are in the Direction 1,

32
00:01:38,895 --> 00:01:43,685
the movement down in this case
would be a movement in y.

33
00:01:43,685 --> 00:01:46,840
These are constructs
in a way that we

34
00:01:46,840 --> 00:01:49,820
are using to identify
that the coordinate

35
00:01:49,820 --> 00:01:53,380
represented by position
and direction would

36
00:01:53,380 --> 00:01:57,390
allow us to move freely
within the grid.

37
00:01:57,390 --> 00:01:59,500
We could make other moves like

38
00:01:59,500 --> 00:02:02,375
diagonally or hopping
or things like that,

39
00:02:02,375 --> 00:02:04,425
and that's just
about coming up with

40
00:02:04,425 --> 00:02:06,740
our own variables or

41
00:02:06,740 --> 00:02:11,240
our own naming conventions
for such operations.

42
00:02:12,890 --> 00:02:15,900
Let's just use some
of those principles.

43
00:02:15,900 --> 00:02:18,420
How do we actually
create a walker first

44
00:02:18,420 --> 00:02:21,660
in the grid and make
it move to the right,

45
00:02:21,660 --> 00:02:24,630
move forward and so on.

46
00:02:24,760 --> 00:02:28,360
Let's just jump into processing.

47
00:02:28,360 --> 00:02:31,940
If you remember, this is the
script where we left off.

48
00:02:31,940 --> 00:02:33,200
Let me just run it once so

49
00:02:33,200 --> 00:02:35,360
that you know where
we're starting.

50
00:02:35,360 --> 00:02:39,820
We basically have the
random information

51
00:02:39,820 --> 00:02:42,045
zero and one in the grid.

52
00:02:42,045 --> 00:02:43,880
Let's just create a worker as

53
00:02:43,880 --> 00:02:47,050
a red dot somewhere
in this grid.

54
00:02:47,050 --> 00:02:50,085
We already have the variables

55
00:02:50,085 --> 00:02:53,920
int current x and
integer current y,

56
00:02:53,920 --> 00:02:56,240
which will represent the
position of the worker.

57
00:02:56,240 --> 00:02:58,140
Let's just put it
somewhere in the middle.

58
00:02:58,140 --> 00:03:02,380
I think in the middle will
be somewhere like 50 and 25.

59
00:03:07,990 --> 00:03:11,930
Let's do a draw function.

60
00:03:11,930 --> 00:03:16,200
Let's just do something
like an ellipse.

61
00:03:16,200 --> 00:03:19,400
Let's just do feel of red.

62
00:03:22,590 --> 00:03:27,750
We know that the ellipse
will be in a position,

63
00:03:27,750 --> 00:03:29,390
that will be the
walker position.

64
00:03:29,390 --> 00:03:31,455
Let's just make
variables for that.

65
00:03:31,455 --> 00:03:34,595
Let's call it w position

66
00:03:34,595 --> 00:03:42,885
x and w for walker
position in y.

67
00:03:42,885 --> 00:03:46,875
The position will be,

68
00:03:46,875 --> 00:03:48,310
let's see, it's going to be

69
00:03:48,310 --> 00:03:51,150
certainly associated
with this variable.

70
00:03:51,870 --> 00:04:00,470
Let's just multiply that
times the cell size.

71
00:04:02,270 --> 00:04:05,160
We will see that we will
need something else.

72
00:04:05,160 --> 00:04:07,655
But maybe for now
let's just do this.

73
00:04:07,655 --> 00:04:09,480
Is the current position.

74
00:04:09,480 --> 00:04:12,480
This is the main
index information of

75
00:04:12,480 --> 00:04:14,120
the entity of the walker in

76
00:04:14,120 --> 00:04:17,125
the grid times the
size of the cell.

77
00:04:17,125 --> 00:04:24,230
Let's copy that for y, y here.

78
00:04:24,230 --> 00:04:25,700
Now we can use

79
00:04:25,700 --> 00:04:30,080
the variables for the
position of the ellipse.

80
00:04:30,080 --> 00:04:36,640
Let's do a 20 and a 20 for now.

81
00:04:36,640 --> 00:04:39,760
This is going to be
a placeholder size.

82
00:04:39,760 --> 00:04:43,290
I think that at this point
we will have some issues.

83
00:04:43,290 --> 00:04:44,310
I don't think we should have

84
00:04:44,310 --> 00:04:50,550
access to these variables here.

85
00:04:50,550 --> 00:04:52,430
Let's just make sure
that we have access to

86
00:04:52,430 --> 00:04:54,830
them by making a

87
00:04:54,830 --> 00:05:01,060
global, there we go.

88
00:05:01,060 --> 00:05:03,300
Because these are
global variables,

89
00:05:03,300 --> 00:05:06,880
we may not have access
to them down here.

90
00:05:11,110 --> 00:05:21,350
We do have up here our ellipse
all the way in the top.

91
00:05:21,350 --> 00:05:24,200
It's not really drawing itself

92
00:05:24,200 --> 00:05:28,350
where we're actually intending
to have it. Let's just

93
00:05:47,740 --> 00:05:54,935
print the lines to see what
information are we getting.

94
00:05:54,935 --> 00:05:57,330
We are getting that 50.

95
00:05:58,690 --> 00:06:01,970
What about the cell value?

96
00:06:01,970 --> 00:06:04,220
This is something
that I like doing.

97
00:06:04,220 --> 00:06:06,500
Right now we're having
a cell value of

98
00:06:06,500 --> 00:06:09,155
zero. That's the problem.

99
00:06:09,155 --> 00:06:13,040
Let's make sure that we
recalculate the cell value.

100
00:06:13,040 --> 00:06:19,050
We had it done all
the way up here,

101
00:06:24,490 --> 00:06:29,850
so let's just include
that calculation here.

102
00:06:35,140 --> 00:06:40,745
There we go. Now our walker
is somewhere in the middle,

103
00:06:40,745 --> 00:06:45,350
but it doesn't really seem to
be fitting anywhere within

104
00:06:45,350 --> 00:06:51,090
the grid and that's
because this ellipse is,

105
00:06:51,190 --> 00:06:54,575
I believe it's drawn
from its center.

106
00:06:54,575 --> 00:06:56,780
If we really wanted to place

107
00:06:56,780 --> 00:07:00,995
this object into the
position in x and y,

108
00:07:00,995 --> 00:07:02,930
let's just make it
slightly smaller,

109
00:07:02,930 --> 00:07:04,980
like maybe a 15.

110
00:07:05,140 --> 00:07:09,365
If we really wanted to make
it fit within the grid,

111
00:07:09,365 --> 00:07:13,115
let's just add half
of the cell size.

112
00:07:13,115 --> 00:07:15,050
The way we would
do that is add to

113
00:07:15,050 --> 00:07:21,420
the position the cell
size divided by two,

114
00:07:22,150 --> 00:07:26,700
that's in x and let's
do it in y as well.

115
00:07:27,280 --> 00:07:29,660
What we're adding here is

116
00:07:29,660 --> 00:07:32,285
because the ellipse
is in the center,

117
00:07:32,285 --> 00:07:34,835
if we want to displace
it half of its amount,

118
00:07:34,835 --> 00:07:38,105
let's say its radius
to the right,

119
00:07:38,105 --> 00:07:41,360
we would add to the coordinate.

120
00:07:41,360 --> 00:07:43,505
You can see here, it actually

121
00:07:43,505 --> 00:07:46,070
seems to be fitting well
within a rectangle.

122
00:07:46,070 --> 00:07:51,440
It's a little bit too big
maybe we can go back to a 10,

123
00:07:51,440 --> 00:07:56,870
some value that allows
us to identify.

124
00:07:56,870 --> 00:07:59,075
Here we have it. We have

125
00:07:59,075 --> 00:08:05,825
the walker position here
in the grid and yeah,

126
00:08:05,825 --> 00:08:10,290
it's sitting within a
rectangle of the cell.

127
00:08:13,090 --> 00:08:15,260
Let's just do some operations

128
00:08:15,260 --> 00:08:18,800
here to understand movement.

129
00:08:18,800 --> 00:08:20,945
I'm going to delete the print.

130
00:08:20,945 --> 00:08:25,895
That was a useful
line to debug and

131
00:08:25,895 --> 00:08:32,120
understand what the data is
doing behind the scenes.

132
00:08:32,120 --> 00:08:34,430
What we would say in the draw,

133
00:08:34,430 --> 00:08:42,260
that my position in x+=1.
Let's just draw that.

134
00:08:42,260 --> 00:08:44,870
Let's see. You see that

135
00:08:44,870 --> 00:08:50,220
our walker moved
towards the right,

136
00:08:50,680 --> 00:08:53,645
and that's happening
because we're not

137
00:08:53,645 --> 00:08:57,455
refreshing the background
of the screen.

138
00:08:57,455 --> 00:09:00,680
We will see that in this
example we are going

139
00:09:00,680 --> 00:09:04,205
to start having to
redraw the cells

140
00:09:04,205 --> 00:09:06,590
every frame so we might
have to copy-paste some of

141
00:09:06,590 --> 00:09:11,675
this information down
here in the draw,

142
00:09:11,675 --> 00:09:14,705
because what we actually
want to do is have

143
00:09:14,705 --> 00:09:17,570
an interactive
relationship between

144
00:09:17,570 --> 00:09:20,870
the grid and the background.

145
00:09:20,870 --> 00:09:25,020
But as you can see here,
when we add +1 in x,

146
00:09:26,860 --> 00:09:30,365
we can draw the walker
towards the right.

147
00:09:30,365 --> 00:09:35,915
If we actually do
the current y+1,

148
00:09:35,915 --> 00:09:39,980
we would actually
make it walk down.

149
00:09:39,980 --> 00:09:42,590
As you can see we
could actually do

150
00:09:42,590 --> 00:09:45,230
that in all the different.

151
00:09:45,230 --> 00:09:47,510
If you do x-1,

152
00:09:47,510 --> 00:09:51,050
it would move to the
left and you can

153
00:09:51,050 --> 00:09:55,020
understand doing negative in
y would actually move up.

154
00:09:56,200 --> 00:10:00,635
How do we redraw?

155
00:10:00,635 --> 00:10:04,745
Let's redraw the
cells in the draw.

156
00:10:04,745 --> 00:10:07,190
We're going to copy this, but we

157
00:10:07,190 --> 00:10:08,990
don't really want
to copy everything.

158
00:10:08,990 --> 00:10:10,535
We don't want to add more cells

159
00:10:10,535 --> 00:10:13,200
to the system, we just
want to draw them.

160
00:10:13,610 --> 00:10:16,870
Let's just copy that.

161
00:10:17,390 --> 00:10:23,110
We are going to include
this all the way up here.

162
00:10:24,160 --> 00:10:27,350
We certainly don't want to
append more information.

163
00:10:27,350 --> 00:10:31,470
The cells already exist
and they have their data.

164
00:10:31,660 --> 00:10:35,045
We are going to calculate
their position,

165
00:10:35,045 --> 00:10:36,830
we are going to use

166
00:10:36,830 --> 00:10:39,590
their information to determine
if they should be black or

167
00:10:39,590 --> 00:10:42,240
white and do a
rectangle and again

168
00:10:42,240 --> 00:10:45,435
continue the counting as we go.

169
00:10:45,435 --> 00:10:47,925
We also want to make
sure that the count

170
00:10:47,925 --> 00:10:52,245
x on count y start
at zero every frame.

171
00:10:52,245 --> 00:10:54,450
Because if you remember,

172
00:10:54,450 --> 00:11:00,510
the draw function, it will
draw itself every frame,

173
00:11:00,510 --> 00:11:01,920
so we want to make
sure that the counting

174
00:11:01,920 --> 00:11:03,615
variables start at zero.

175
00:11:03,615 --> 00:11:09,480
We recreate the grid
every frame and then

176
00:11:09,480 --> 00:11:16,710
we move into our walker area.

177
00:11:16,710 --> 00:11:19,930
You could actually say
that this section here,

178
00:11:21,460 --> 00:11:25,835
it's the walker section,
and if you want it,

179
00:11:25,835 --> 00:11:27,080
you could actually say, hey,

180
00:11:27,080 --> 00:11:31,140
this is going to be
my grid section.

181
00:11:32,200 --> 00:11:35,955
I like sometimes just add

182
00:11:35,955 --> 00:11:39,120
certain clarity to my script

183
00:11:39,120 --> 00:11:40,815
so that I could
start remembering,

184
00:11:40,815 --> 00:11:43,905
this is the area where I'm
working on the walker logic.

185
00:11:43,905 --> 00:11:45,480
Of course, we could do

186
00:11:45,480 --> 00:11:48,120
functions and we will start
looking at functions in

187
00:11:48,120 --> 00:11:50,550
the next lesson where we will

188
00:11:50,550 --> 00:11:53,940
start doing functions
for movement,

189
00:11:53,940 --> 00:11:55,905
for rotation, and so on.

190
00:11:55,905 --> 00:11:58,290
But for now, I think
it's good enough to

191
00:11:58,290 --> 00:12:00,870
have some clarity that this part

192
00:12:00,870 --> 00:12:02,220
of the script really refers to

193
00:12:02,220 --> 00:12:06,130
the grid and this part
here refers to the walker.

194
00:12:07,460 --> 00:12:10,890
We are running into an error
here because this is not

195
00:12:10,890 --> 00:12:13,905
running and for some reason,

196
00:12:13,905 --> 00:12:16,260
the total number of cells value,

197
00:12:16,260 --> 00:12:19,870
if we actually try to
print that information,

198
00:12:21,980 --> 00:12:24,540
let's just try to print it,

199
00:12:24,540 --> 00:12:26,925
it doesn't really exist here.

200
00:12:26,925 --> 00:12:30,375
That's because we haven't
really bring it in.

201
00:12:30,375 --> 00:12:33,900
That is a calculation that
we only want to do once.

202
00:12:33,900 --> 00:12:36,700
Total number of cells.

203
00:12:38,210 --> 00:12:41,235
We can actually take

204
00:12:41,235 --> 00:12:46,350
this information out of here
and put it all the way up

205
00:12:46,350 --> 00:12:47,550
here at the beginning of

206
00:12:47,550 --> 00:12:51,650
our script certainly
after the definition of

207
00:12:51,650 --> 00:12:54,590
resolution so that we can
actually use that information

208
00:12:54,590 --> 00:13:01,465
now in our script down here.

209
00:13:01,465 --> 00:13:04,380
Let's print again the
total number of cells,

210
00:13:04,380 --> 00:13:07,980
you have 5,000 and

211
00:13:07,980 --> 00:13:12,220
now the walker is actually
showing in the screen.

212
00:13:12,380 --> 00:13:17,910
Sometimes you forget to put

213
00:13:17,910 --> 00:13:19,020
some calculations in the right

214
00:13:19,020 --> 00:13:20,190
place and you don't
have access to

215
00:13:20,190 --> 00:13:24,345
some variables, it can happen.

216
00:13:24,345 --> 00:13:27,370
It should be working right now.

217
00:13:27,440 --> 00:13:30,630
As you can see now, the walker,

218
00:13:30,630 --> 00:13:34,410
I commented out this line
to move to the left,

219
00:13:34,410 --> 00:13:36,345
but if we would run that line,

220
00:13:36,345 --> 00:13:39,345
you'll see that the walker
moves really quickly.

221
00:13:39,345 --> 00:13:41,550
It's been redrawn every time,

222
00:13:41,550 --> 00:13:45,150
but the grid is also
redrawn every time.

223
00:13:45,150 --> 00:13:51,000
It gives an illusion of
movement as opposed to before.

224
00:13:51,000 --> 00:13:53,550
We would just have draw the
background only once and then

225
00:13:53,550 --> 00:13:57,105
redraw the walker over and
over on top of that grid.

226
00:13:57,105 --> 00:14:00,045
Now we actually could
have a system that

227
00:14:00,045 --> 00:14:02,040
establishes a
relationship between

228
00:14:02,040 --> 00:14:03,810
the walker and the grid.

229
00:14:03,810 --> 00:14:06,180
Let's do a simple operation.

230
00:14:06,180 --> 00:14:08,880
Let's say that the walker,

231
00:14:08,880 --> 00:14:11,080
it's going to move to the right,

232
00:14:12,500 --> 00:14:19,515
but it also wants to change
the information of the grid.

233
00:14:19,515 --> 00:14:21,240
As it moves to the right,

234
00:14:21,240 --> 00:14:26,115
it will flip the zero to a one,

235
00:14:26,115 --> 00:14:29,955
or a one to a zero or make
sure that all the indexes

236
00:14:29,955 --> 00:14:32,010
as it's moving around switch

237
00:14:32,010 --> 00:14:34,785
to the value of zero
which is black.

238
00:14:34,785 --> 00:14:42,180
How do we actually access the
information of the index,

239
00:14:42,180 --> 00:14:45,825
because we actually
have an information of

240
00:14:45,825 --> 00:14:49,320
the x and y, so
this information,

241
00:14:49,320 --> 00:14:52,755
the current x and
the current y are

242
00:14:52,755 --> 00:14:55,380
actually a way for us to know

243
00:14:55,380 --> 00:14:58,095
where do they sit in the
grid that it's intuitive,

244
00:14:58,095 --> 00:15:00,330
x and y, but it
doesn't really map to

245
00:15:00,330 --> 00:15:03,910
an index in the grid.

246
00:15:08,690 --> 00:15:13,435
Here we're going to use
a little bit of logic.

247
00:15:13,435 --> 00:15:16,260
The index value is

248
00:15:16,810 --> 00:15:26,540
the current index in x plus,

249
00:15:26,540 --> 00:15:30,350
we're going to use the
current index in the current

250
00:15:30,350 --> 00:15:35,640
y times the resolution in y.

251
00:15:36,430 --> 00:15:38,840
Let's just write this equation

252
00:15:38,840 --> 00:15:41,070
here and then understand it.

253
00:15:41,620 --> 00:15:49,625
The current y represents how
many rows down are we in?

254
00:15:49,625 --> 00:15:56,610
We know that we basically have
the resolution in y is 50.

255
00:15:57,070 --> 00:16:00,425
If we're, let's say,
in the fifth row,

256
00:16:00,425 --> 00:16:02,825
we're going to multiply 50

257
00:16:02,825 --> 00:16:08,045
times our current y-coordinate.

258
00:16:08,045 --> 00:16:09,785
The current x,

259
00:16:09,785 --> 00:16:12,605
it's going to be
the missing amount,

260
00:16:12,605 --> 00:16:16,175
because we will be
moving, let's say,

261
00:16:16,175 --> 00:16:18,800
10 units or whatever
number of units we have an

262
00:16:18,800 --> 00:16:21,170
x on top of all the x's

263
00:16:21,170 --> 00:16:26,840
and y's that we are
being covering.

264
00:16:26,840 --> 00:16:28,250
I don't know if
that makes sense.

265
00:16:28,250 --> 00:16:30,890
I will actually do
a small drawing

266
00:16:30,890 --> 00:16:33,360
once we run the script.

267
00:16:35,380 --> 00:16:37,400
Let me just comment out

268
00:16:37,400 --> 00:16:39,410
the movement so that

269
00:16:39,410 --> 00:16:41,555
we can actually comment
on this for a second.

270
00:16:41,555 --> 00:16:43,385
If you think about this,

271
00:16:43,385 --> 00:16:45,485
so this is 100.

272
00:16:45,485 --> 00:16:48,470
We have a 50 in x.

273
00:16:48,470 --> 00:16:52,565
The index of the
cell we're in is 50

274
00:16:52,565 --> 00:16:59,670
plus all these rows, basically.

275
00:16:59,950 --> 00:17:04,475
Twenty-five rows times 100,

276
00:17:04,475 --> 00:17:07,820
which is all the cells
included in every row.

277
00:17:07,820 --> 00:17:10,730
This is the equation in
which we can convert the x

278
00:17:10,730 --> 00:17:12,800
and y into an index number,

279
00:17:12,800 --> 00:17:15,380
which is a singular
index number.

280
00:17:15,380 --> 00:17:17,450
We could say something like,

281
00:17:17,450 --> 00:17:19,730
well, the cells,

282
00:17:19,730 --> 00:17:22,410
let's just look at the cells,

283
00:17:23,640 --> 00:17:28,520
and access the cell
with the index value.

284
00:17:31,300 --> 00:17:35,360
We know that a cell can
be either a zero or one.

285
00:17:35,360 --> 00:17:37,010
Let's just make sure
that it's a zero.

286
00:17:37,010 --> 00:17:43,970
Let's just force it to be a
zero wherever the agent is.

287
00:17:43,970 --> 00:17:47,525
Let's also make the
agent walk to the right.

288
00:17:47,525 --> 00:17:50,030
What we're trying
to do here is do

289
00:17:50,030 --> 00:17:55,020
a first painting of the agent.

290
00:17:57,610 --> 00:18:00,380
As you can see here, it's

291
00:18:00,380 --> 00:18:03,920
painting the information
of the grid to black.

292
00:18:03,920 --> 00:18:06,710
We have some unexpected
behavior here,

293
00:18:06,710 --> 00:18:09,870
because we don't have
a boundary condition.

294
00:18:10,720 --> 00:18:13,550
We discussed this last week,

295
00:18:13,550 --> 00:18:15,440
or in last session,

296
00:18:15,440 --> 00:18:20,660
that whenever we are creating
some behavior of movement,

297
00:18:20,660 --> 00:18:22,550
we want to determine what

298
00:18:22,550 --> 00:18:26,135
happens if I reach the
edge of the screen.

299
00:18:26,135 --> 00:18:29,555
If we remember, I'm going to
rewrite some of that code.

300
00:18:29,555 --> 00:18:35,060
If the current index in
x is smaller than zero,

301
00:18:35,060 --> 00:18:39,350
then the current
index in x is zero.

302
00:18:39,350 --> 00:18:48,695
We block that x to be any
number smaller than zero,

303
00:18:48,695 --> 00:18:51,150
and the same thing in y.

304
00:18:51,250 --> 00:18:56,075
Then we also want to make
sure that if the value of x

305
00:18:56,075 --> 00:19:01,445
is bigger or equals that
the resolution in x,

306
00:19:01,445 --> 00:19:07,925
the number in x becomes
the resolution in x.

307
00:19:07,925 --> 00:19:11,810
Let's just copy
based that for y.

308
00:19:11,810 --> 00:19:15,140
Let's see if this actually

309
00:19:15,140 --> 00:19:23,645
changes the behavior

310
00:19:23,645 --> 00:19:26,580
of our agent.

311
00:19:29,290 --> 00:19:33,095
We are, in fact, stopping,

312
00:19:33,095 --> 00:19:35,495
but you see that we actually

313
00:19:35,495 --> 00:19:39,560
are not affecting
the right index.

314
00:19:39,560 --> 00:19:44,060
We have to double-check
our index operation here.

315
00:19:44,060 --> 00:19:52,025
I think that this equation,

316
00:19:52,025 --> 00:19:54,740
I just wrote it wrongly.

317
00:19:54,740 --> 00:19:56,780
I think that we should
do resolution in x,

318
00:19:56,780 --> 00:19:59,060
because resolution
in y, it's 50.

319
00:19:59,060 --> 00:20:01,325
If we actually want to calculate

320
00:20:01,325 --> 00:20:07,190
how many rows we have down,

321
00:20:07,190 --> 00:20:10,475
we actually should
use x here, not y.

322
00:20:10,475 --> 00:20:13,760
There we go. I
think that there's

323
00:20:13,760 --> 00:20:15,860
something wrong with the
equation of the index.

324
00:20:15,860 --> 00:20:19,775
I couldn't quite see
it, but it's here.

325
00:20:19,775 --> 00:20:22,340
We are calculating the
resolution in x which is

326
00:20:22,340 --> 00:20:26,870
100 times how many times
we've covered 100,

327
00:20:26,870 --> 00:20:30,845
and then we're adding
the current x to it.

328
00:20:30,845 --> 00:20:34,070
In this way we're actually
setting up the cell value

329
00:20:34,070 --> 00:20:38,060
to zero and we're
painting it black.

330
00:20:38,060 --> 00:20:43,640
If you wanted to move in
the negative direction,

331
00:20:43,640 --> 00:20:45,530
let's see if that is
actually working well.

332
00:20:45,530 --> 00:20:48,545
We can move to the
left and we stay

333
00:20:48,545 --> 00:20:54,470
in zero. That's great.

334
00:20:54,470 --> 00:20:57,335
At this point, what
we are having,

335
00:20:57,335 --> 00:20:58,790
it's a way in which the agent

336
00:20:58,790 --> 00:21:00,590
can walk on top of the grid,

337
00:21:00,590 --> 00:21:06,155
but it's also able to affect
the value of the grid.

338
00:21:06,155 --> 00:21:08,690
Let's do one final thing.

339
00:21:08,690 --> 00:21:13,745
Let's just bring back this
idea of random movement.

340
00:21:13,745 --> 00:21:19,385
The movement in x is
going to be plus equals

341
00:21:19,385 --> 00:21:28,410
a random rand int
between -1 and 1.

342
00:21:28,900 --> 00:21:33,210
It's bringing back the
idea of the random walker.

343
00:21:34,000 --> 00:21:38,945
We have both of those,

344
00:21:38,945 --> 00:21:45,725
x and y, changing dynamically
with a random number.

345
00:21:45,725 --> 00:21:48,380
You can see we have the worker

346
00:21:48,380 --> 00:21:49,940
that we had back and
this time is actually

347
00:21:49,940 --> 00:21:54,900
printing black. But
instead of printing black.

348
00:21:57,520 --> 00:22:02,730
It actually runs
into an issue here.

349
00:22:04,090 --> 00:22:06,860
We'll come back to that
issue in a minute.

350
00:22:06,860 --> 00:22:09,005
But let's just,
instead of saying

351
00:22:09,005 --> 00:22:15,245
change the value to
zero all the time,

352
00:22:15,245 --> 00:22:16,790
let's just flip the value.

353
00:22:16,790 --> 00:22:21,020
If the cell is zero,
converted to one.

354
00:22:21,020 --> 00:22:22,940
Let's do that as
an if statement.

355
00:22:22,940 --> 00:22:31,430
We could say the
cell value is zero,

356
00:22:31,430 --> 00:22:40,745
then the cell value equals one.

357
00:22:40,745 --> 00:22:45,030
Then we can do an
L if statement.

358
00:22:45,180 --> 00:22:51,950
If the cell value equals one,

359
00:22:53,410 --> 00:22:57,305
we will assign it to zero.

360
00:22:57,305 --> 00:22:59,270
We want an agent that is able to

361
00:22:59,270 --> 00:23:03,770
flip the value of the grid,

362
00:23:03,770 --> 00:23:06,140
where whites become blood,

363
00:23:06,140 --> 00:23:11,390
black becomes white and

364
00:23:11,390 --> 00:23:14,430
it's basically
repainting that pattern.

365
00:23:17,560 --> 00:23:23,345
This is the beginning of
the Langton's algorithm.

366
00:23:23,345 --> 00:23:27,530
The Langton plays
heavily on an entity,

367
00:23:27,530 --> 00:23:29,120
a walker, that uses

368
00:23:29,120 --> 00:23:30,320
the information of
the grid to take

369
00:23:30,320 --> 00:23:31,790
a decision on how
to move forward.

370
00:23:31,790 --> 00:23:33,080
But at the same time it changes

371
00:23:33,080 --> 00:23:35,820
that information as it moves.

372
00:23:36,820 --> 00:23:40,490
Basically, the result of
this condition is that

373
00:23:40,490 --> 00:23:41,795
you end up having

374
00:23:41,795 --> 00:23:44,795
an emergent pattern
and emergent behavior.

375
00:23:44,795 --> 00:23:48,650
If you want to see the
work of this worker a

376
00:23:48,650 --> 00:23:51,950
little bit more in a
more interesting way.

377
00:23:51,950 --> 00:23:57,065
Instead of starting all our
cells with a value of zero,

378
00:23:57,065 --> 00:23:59,990
we could start like this.

379
00:23:59,990 --> 00:24:01,670
We can make a comment here,

380
00:24:01,670 --> 00:24:08,870
and this is a random
data for cells.

381
00:24:08,870 --> 00:24:12,785
Let's comment that
out to just leave.

382
00:24:12,785 --> 00:24:15,260
Instead of that,
I'm going to append

383
00:24:15,260 --> 00:24:17,300
a number zero which is going to

384
00:24:17,300 --> 00:24:19,670
force the fact that
all the cells are

385
00:24:19,670 --> 00:24:23,090
going to start black.

386
00:24:23,090 --> 00:24:27,545
But the behavior of the cell,

387
00:24:27,545 --> 00:24:29,720
it's always going to flip.

388
00:24:29,720 --> 00:24:33,080
It's going to start painting
white as it flip around,

389
00:24:33,080 --> 00:24:34,820
but that's it move
back into white.

390
00:24:34,820 --> 00:24:38,700
Cells are going to be
repainted black and so forth.

391
00:24:38,950 --> 00:24:41,090
It's a very powerful system,

392
00:24:41,090 --> 00:24:45,275
is starting to explain how a
data structure of the grid

393
00:24:45,275 --> 00:24:50,325
becomes the driver of the
behavior of this walker.

394
00:24:50,325 --> 00:24:52,300
Then again, the
behavior of the walker,

395
00:24:52,300 --> 00:24:55,435
it's affecting the
pattern of the grid.

396
00:24:55,435 --> 00:24:56,935
Very interesting ideas here.

397
00:24:56,935 --> 00:24:58,420
I'm going to see you in
the next video where we're

398
00:24:58,420 --> 00:24:59,980
going to continue
talking about how to

399
00:24:59,980 --> 00:25:04,880
build the lantern sand in
its entirety. See you then.