1
00:00:04,250 --> 00:00:07,350
Hi. Welcome to this new lesson.

2
00:00:07,350 --> 00:00:11,080
This is the fourth video of
our complex systems project,

3
00:00:11,080 --> 00:00:16,160
which is the final project
of this specialization.

4
00:00:16,280 --> 00:00:20,195
We have been working on a
Langton sank algorithm.

5
00:00:20,195 --> 00:00:22,340
By all means, I invite you to go

6
00:00:22,340 --> 00:00:24,960
and revisit some
of those videos.

7
00:00:24,960 --> 00:00:27,300
We are going to continue
working on the code that

8
00:00:27,300 --> 00:00:30,600
we started a few videos back.

9
00:00:30,600 --> 00:00:32,220
Let's see where we're at,

10
00:00:32,220 --> 00:00:35,980
and what comes next.

11
00:00:36,170 --> 00:00:38,280
Let's just run the code.

12
00:00:38,280 --> 00:00:40,520
We have a grid class.

13
00:00:40,520 --> 00:00:44,410
We're writing everything in
an object oriented fashion.

14
00:00:44,410 --> 00:00:47,585
We have a grid class and
we have an at class.

15
00:00:47,585 --> 00:00:50,820
We have two instances
of the ants.

16
00:00:50,820 --> 00:00:52,540
The ant is an entity,

17
00:00:52,540 --> 00:00:56,420
it's an agent that operates
on top of the grid.

18
00:00:56,420 --> 00:00:59,460
Currently, it's just
not doing much,

19
00:00:59,460 --> 00:01:02,900
just appearing there showing
itself with this red color.

20
00:01:02,900 --> 00:01:05,600
What we're going to start
doing today is starting

21
00:01:05,600 --> 00:01:08,340
to create some behaviors
for this class.

22
00:01:08,340 --> 00:01:10,800
We're going to make the entities

23
00:01:10,800 --> 00:01:13,445
move based on their direction.

24
00:01:13,445 --> 00:01:15,320
Obviously, as they move,

25
00:01:15,320 --> 00:01:17,220
they might reach the
boundary condition,

26
00:01:17,220 --> 00:01:18,520
they might reach out of bounds.

27
00:01:18,520 --> 00:01:22,740
We're going to deal
with that as we go.

28
00:01:22,740 --> 00:01:26,775
Let's go into the script in.

29
00:01:26,775 --> 00:01:28,300
We actually wrote here

30
00:01:28,300 --> 00:01:31,520
that we wanted to work
on the ant behavior.

31
00:01:31,980 --> 00:01:34,740
Let's do just that.

32
00:01:34,740 --> 00:01:39,340
We are going to do
that just below here.

33
00:01:39,340 --> 00:01:43,770
Let's just create a bit
of space for us to work.

34
00:01:43,770 --> 00:01:45,540
Let's create a function

35
00:01:45,540 --> 00:01:51,400
called the definition
of ant behavior.

36
00:01:58,460 --> 00:02:01,970
There we go. The ant behavior

37
00:02:01,970 --> 00:02:04,910
in itself is going to be
a series of functions.

38
00:02:04,910 --> 00:02:09,050
Things like basically,

39
00:02:09,050 --> 00:02:11,790
the following the
Langton and algorithm.

40
00:02:11,790 --> 00:02:15,430
The Langton and algorithm
has a series of

41
00:02:15,430 --> 00:02:17,870
requirements of
behaving differently

42
00:02:17,870 --> 00:02:20,900
based on the information
of the grid.

43
00:02:20,900 --> 00:02:26,090
But for now, we are just going
to create a few functions.

44
00:02:26,090 --> 00:02:28,650
Let's start with move forward.

45
00:02:30,510 --> 00:02:34,990
We're going to have rotate
90 degrees clockwise,

46
00:02:34,990 --> 00:02:40,495
count clockwise, and also
change the grid data.

47
00:02:40,495 --> 00:02:42,330
There's a few things
that we might need to

48
00:02:42,330 --> 00:02:45,005
do within this ant behavior.

49
00:02:45,005 --> 00:02:48,075
Let's start with
the move forward.

50
00:02:48,075 --> 00:02:51,710
I'm going to create, let's
just write a few other things

51
00:02:51,710 --> 00:02:58,005
and rotate. Flip cells.

52
00:02:58,005 --> 00:02:59,610
Those are things that
we might want to

53
00:02:59,610 --> 00:03:01,290
do within this function.

54
00:03:01,290 --> 00:03:05,140
Let's create the move
forward script first.

55
00:03:08,570 --> 00:03:12,540
They move forward.
It could be quite

56
00:03:12,540 --> 00:03:17,540
easy if we didn't
have a direction.

57
00:03:18,230 --> 00:03:21,375
But let's just consider
the direction.

58
00:03:21,375 --> 00:03:27,460
We're going to do the
this direction matters.

59
00:03:27,770 --> 00:03:31,065
You're going to
provide the direction.

60
00:03:31,065 --> 00:03:32,690
The direction is basically

61
00:03:32,690 --> 00:03:34,995
a value between zero and three,

62
00:03:34,995 --> 00:03:37,035
0, 1, 2, 3,

63
00:03:37,035 --> 00:03:40,230
and that would represent
the direction,

64
00:03:40,230 --> 00:03:42,230
the axis of movement or

65
00:03:42,230 --> 00:03:45,690
the axis in which
the ant is facing.

66
00:03:46,630 --> 00:03:53,880
Let's just do an if
direction is zero.

67
00:03:55,220 --> 00:04:05,545
Then self.x+=1.

68
00:04:05,545 --> 00:04:09,120
The moving of the ant is
just basically changing

69
00:04:09,120 --> 00:04:14,160
its index to +1 in an entity.

70
00:04:14,160 --> 00:04:17,040
Let's just do an
elif statement for

71
00:04:17,040 --> 00:04:20,875
all the other conditions
that we have here.

72
00:04:20,875 --> 00:04:23,480
Let's just copy paste this.

73
00:04:25,250 --> 00:04:28,335
If the direction is one,

74
00:04:28,335 --> 00:04:35,720
the direction that we're
going to be moving is in y+1.

75
00:04:37,010 --> 00:04:42,160
This elif is going to
be repeated four times.

76
00:04:43,730 --> 00:04:46,020
If the direction is two,

77
00:04:46,020 --> 00:04:53,080
then x is minus one.

78
00:04:54,440 --> 00:04:57,735
Finally, if the
direction is three,

79
00:04:57,735 --> 00:05:03,430
then, sorry, y is minus one.

80
00:05:04,490 --> 00:05:08,280
Zero would be moving
towards the right,

81
00:05:08,280 --> 00:05:12,880
one would be moving down,

82
00:05:13,970 --> 00:05:16,710
two would be moving to the left,

83
00:05:16,710 --> 00:05:18,570
and three would be moving up.

84
00:05:18,570 --> 00:05:21,990
That's what move forward would
mean based on a direction

85
00:05:21,990 --> 00:05:26,330
that it's a value that
we already have declare.

86
00:05:26,330 --> 00:05:30,210
We can say here.

87
00:05:30,350 --> 00:05:35,175
Let's just use move
forward function.

88
00:05:35,175 --> 00:05:42,880
Move forward and self.direction.

89
00:05:44,270 --> 00:05:48,750
That's great. We actually
have our first line of

90
00:05:48,750 --> 00:05:52,170
behavior that should allow

91
00:05:52,170 --> 00:05:54,930
us to see our ants
moving in the screen.

92
00:05:54,930 --> 00:05:59,880
Let's just see how we could
actually get this going.

93
00:05:59,880 --> 00:06:03,570
Because our classes
actually are being drawn in

94
00:06:03,570 --> 00:06:09,660
to our setup function
and effectively,

95
00:06:09,660 --> 00:06:12,315
they're not being drawn
every frame differently.

96
00:06:12,315 --> 00:06:14,100
Let's just break this up.

97
00:06:14,100 --> 00:06:21,000
Here I'm going to define

98
00:06:21,000 --> 00:06:26,310
draw and here

99
00:06:26,310 --> 00:06:33,180
the ant Actually,

100
00:06:33,180 --> 00:06:34,545
in the draw function,

101
00:06:34,545 --> 00:06:37,540
let's just draw the background.

102
00:06:39,380 --> 00:06:45,540
Well, we will need
to redraw the grid.

103
00:06:45,540 --> 00:06:49,290
Let's just cut this
and put it here.

104
00:06:49,290 --> 00:06:54,240
We want to redraw the grid
every frame and we also want

105
00:06:54,240 --> 00:07:00,520
to execute our ant every frame.

106
00:07:03,190 --> 00:07:05,750
The only thing that we
actually doing only

107
00:07:05,750 --> 00:07:07,910
once is the initiation
of the data.

108
00:07:07,910 --> 00:07:11,265
If you actually do this
initiation of the data,

109
00:07:11,265 --> 00:07:13,260
which is the grid class,

110
00:07:13,260 --> 00:07:15,330
if you initialize the
data, every frame,

111
00:07:15,330 --> 00:07:16,770
you're going to be
basically having

112
00:07:16,770 --> 00:07:20,100
your grid switching its data.

113
00:07:20,100 --> 00:07:22,185
Every frame, you're
going to have new data,

114
00:07:22,185 --> 00:07:24,525
or as a matter of fact,

115
00:07:24,525 --> 00:07:28,095
you might be adding new
data to that class.

116
00:07:28,095 --> 00:07:32,040
That would actually create
quite a bit of error.

117
00:07:32,040 --> 00:07:35,025
If you want to reset the data,

118
00:07:35,025 --> 00:07:36,810
make sure that you include.

119
00:07:36,810 --> 00:07:41,490
I'm just thinking of this
as we go but in this data,

120
00:07:41,490 --> 00:07:43,440
if you wanted to
initiate the data,

121
00:07:43,440 --> 00:07:45,120
you might want to put
a safety measure,

122
00:07:45,120 --> 00:07:47,760
which is data that clear.

123
00:07:47,760 --> 00:07:49,650
Reset the data from

124
00:07:49,650 --> 00:07:54,225
scratch every time you
call this function,

125
00:07:54,225 --> 00:07:56,355
so that you're not

126
00:07:56,355 --> 00:07:58,710
just adding more and
more data every frame.

127
00:07:58,710 --> 00:08:01,050
But this function is
only meant to be run

128
00:08:01,050 --> 00:08:04,245
once in the setup.

129
00:08:04,245 --> 00:08:07,680
Then we can actually execute
the drawing of the grid

130
00:08:07,680 --> 00:08:10,475
and the running of the ends,

131
00:08:10,475 --> 00:08:11,915
which already should contain.

132
00:08:11,915 --> 00:08:14,150
Let's review what's
there and the run

133
00:08:14,150 --> 00:08:17,660
contains the drawing of the ant,

134
00:08:17,660 --> 00:08:19,670
and it should contain
non behavior,

135
00:08:19,670 --> 00:08:21,815
but you see we haven't
really turned it on yet.

136
00:08:21,815 --> 00:08:31,290
It's just that self behavior,

137
00:08:31,290 --> 00:08:33,240
Now we have the run function,

138
00:08:33,240 --> 00:08:34,965
which is basically our outline

139
00:08:34,965 --> 00:08:36,780
of all the things
that the ant will be

140
00:08:36,780 --> 00:08:42,285
doing includes to displaying
yourself and moving.

141
00:08:42,285 --> 00:08:45,090
Let's see. If we
try to run this,

142
00:08:45,090 --> 00:08:47,260
we're running into an issue.

143
00:08:48,080 --> 00:08:52,245
Let's just see
where is the issue.

144
00:08:52,245 --> 00:08:55,830
Let's just coming out
this for a moment.

145
00:08:55,830 --> 00:08:58,845
It's certainly within the aunt

146
00:08:58,845 --> 00:09:02,070
because as we turn this
off, this is totally fine.

147
00:09:02,070 --> 00:09:04,920
As we bring one of those in,

148
00:09:04,920 --> 00:09:06,900
we have our crash.

149
00:09:06,900 --> 00:09:09,930
Let's see what are
we doing wrong.

150
00:09:09,930 --> 00:09:11,985
Well, here we go.

151
00:09:11,985 --> 00:09:15,705
Self. move forward.

152
00:09:15,705 --> 00:09:19,300
This is potentially one
of the issues, let's see.

153
00:09:25,910 --> 00:09:29,250
We have that working here.

154
00:09:29,250 --> 00:09:34,155
You can see, we do run
into an H condition.

155
00:09:34,155 --> 00:09:38,820
Let's just try our second aunt.

156
00:09:38,820 --> 00:09:43,950
These two ants because they
have the direction of,

157
00:09:43,950 --> 00:09:47,445
let's just give one aunt
the direction of zero.

158
00:09:47,445 --> 00:09:49,545
This one should
move to the right,

159
00:09:49,545 --> 00:09:52,210
and then this one
should move down.

160
00:09:54,140 --> 00:10:01,630
You can see that both
aunts actually do work.

161
00:10:08,000 --> 00:10:12,130
Let's just comment out
this aunts for a second.

162
00:10:14,750 --> 00:10:17,490
There seems to be
something as we

163
00:10:17,490 --> 00:10:20,290
reach the edge condition.

164
00:10:21,410 --> 00:10:24,525
There seems to be like
a grid displacement.

165
00:10:24,525 --> 00:10:26,670
I think what we're doing when

166
00:10:26,670 --> 00:10:28,680
we're getting to
this edge condition,

167
00:10:28,680 --> 00:10:30,285
we run into an error.

168
00:10:30,285 --> 00:10:34,020
Because we're trying to

169
00:10:34,020 --> 00:10:36,975
access an information in the
grid that doesn't exist.

170
00:10:36,975 --> 00:10:41,195
Let's just write something
that would stay within bounds

171
00:10:41,195 --> 00:10:45,485
or there's different ways
of thinking of the data.

172
00:10:45,485 --> 00:10:48,960
What happens when you
reach the edge condition.

173
00:10:49,270 --> 00:10:51,965
Let's just write a definition

174
00:10:51,965 --> 00:10:55,725
for what to do in those cases.

175
00:10:55,725 --> 00:11:09,675
Stay within pounds.

176
00:11:09,675 --> 00:11:16,800
We're going to say if self.x

177
00:11:16,800 --> 00:11:19,960
is smaller than
zero, what happens?

178
00:11:21,350 --> 00:11:25,090
Self.x equals

179
00:11:28,880 --> 00:11:36,280
self.grid object.columns-1.

180
00:11:37,820 --> 00:11:40,470
What does this line
say? It's saying, well,

181
00:11:40,470 --> 00:11:42,255
if my x value,

182
00:11:42,255 --> 00:11:45,510
if I'm moving left in x,

183
00:11:45,510 --> 00:11:48,570
and I reach zero, well,

184
00:11:48,570 --> 00:11:51,150
why don't I then appear on

185
00:11:51,150 --> 00:11:54,435
the other side by
defining my new x,

186
00:11:54,435 --> 00:11:58,000
it's going to be the
size of the grid.

187
00:11:58,160 --> 00:12:01,740
The final index of the grid
because we start at zero,

188
00:12:01,740 --> 00:12:03,525
we have to add this
-1 at the end.

189
00:12:03,525 --> 00:12:06,090
But it's basically the
size of the grid -1,

190
00:12:06,090 --> 00:12:09,870
which is the left most unit.

191
00:12:09,870 --> 00:12:13,050
We're doing like a wrap space

192
00:12:13,050 --> 00:12:14,550
if you play something
like Pac Man,

193
00:12:14,550 --> 00:12:16,500
you probably are familiar
with something like this.

194
00:12:16,500 --> 00:12:19,530
Like you go on one side and
you appear on the other side.

195
00:12:19,530 --> 00:12:21,390
That's what we're
actually doing.

196
00:12:21,390 --> 00:12:23,160
We're actually
wrapping the world.

197
00:12:23,160 --> 00:12:24,630
It's infinite in the sense

198
00:12:24,630 --> 00:12:25,860
that you can move infinitely in

199
00:12:25,860 --> 00:12:27,150
one direction and appear on

200
00:12:27,150 --> 00:12:29,460
the side on the other
side of the screen.

201
00:12:29,460 --> 00:12:37,500
Let's copy this
line a few times.

202
00:12:42,920 --> 00:12:45,510
I was getting this
ventation problem

203
00:12:45,510 --> 00:12:46,140
because we don't really

204
00:12:46,140 --> 00:12:49,710
need these parentheses here.

205
00:12:49,710 --> 00:12:52,155
These are going to
be for if statements

206
00:12:52,155 --> 00:12:55,030
for all the four conditions of.

207
00:13:05,450 --> 00:13:08,265
This is going to be
the inverted version.

208
00:13:08,265 --> 00:13:09,900
If we're bigger or equal,

209
00:13:09,900 --> 00:13:17,175
than the size of the column
then x becomes zero.

210
00:13:17,175 --> 00:13:19,920
The left most boundary,

211
00:13:19,920 --> 00:13:22,290
if we reached the
right most boundary,

212
00:13:22,290 --> 00:13:24,370
we go back to zero.

213
00:13:25,160 --> 00:13:29,535
Then these two, now we can

214
00:13:29,535 --> 00:13:36,400
add two more for the y variable.

215
00:13:38,900 --> 00:13:42,580
Instead of columns, we add rows

216
00:13:51,580 --> 00:13:54,880
and then this function.

217
00:13:54,880 --> 00:13:57,000
Now, we have the definition.

218
00:13:57,000 --> 00:13:58,770
This is making sure
that we're not

219
00:13:58,770 --> 00:14:01,695
running into an error if
we get to the boundary.

220
00:14:01,695 --> 00:14:03,870
We would like to add it in

221
00:14:03,870 --> 00:14:05,820
the add behavior,
maybe towards the end.

222
00:14:05,820 --> 00:14:08,490
This is stay within bounds,

223
00:14:08,490 --> 00:14:15,070
and let's just not forget

224
00:14:15,320 --> 00:14:20,890
that we need that
self.stay within bounds.

225
00:14:21,320 --> 00:14:23,880
As we run the script,

226
00:14:23,880 --> 00:14:27,945
you'll see that there's
something not as expected.

227
00:14:27,945 --> 00:14:30,450
The behavior of the seems
to be working well,

228
00:14:30,450 --> 00:14:31,650
but there seems to be

229
00:14:31,650 --> 00:14:33,495
some kind of virtual
artifacts here.

230
00:14:33,495 --> 00:14:36,960
There's some, ants that
stick to the right.

231
00:14:36,960 --> 00:14:40,065
The grid seems to be displaced.

232
00:14:40,065 --> 00:14:43,450
So what's wrong?

233
00:14:44,600 --> 00:14:48,465
I actually spend a bit of
time trying to find this.

234
00:14:48,465 --> 00:14:51,540
The first thing I would like
to test is to make sure that

235
00:14:51,540 --> 00:14:55,200
we can draw a background
here in the back.

236
00:14:55,200 --> 00:14:58,365
That should get rid of
some of the problem.

237
00:14:58,365 --> 00:15:01,770
As you can see, we can get
rid of the edge condition,

238
00:15:01,770 --> 00:15:03,555
but it just seems
that the grid is

239
00:15:03,555 --> 00:15:05,820
not longer well placed.

240
00:15:05,820 --> 00:15:12,600
Well, there's something we
did with the end that we

241
00:15:12,600 --> 00:15:16,830
actually change the default form

242
00:15:16,830 --> 00:15:20,400
by which the rectangles
are drawn in the screen.

243
00:15:20,400 --> 00:15:23,880
We're using the form
rectangle center.

244
00:15:23,880 --> 00:15:27,045
By default processing
would actually

245
00:15:27,045 --> 00:15:29,970
assume that you're using a
different style of drawing,

246
00:15:29,970 --> 00:15:32,050
which is using the corner.

247
00:15:32,210 --> 00:15:35,340
What we need to do here
is just make sure that

248
00:15:35,340 --> 00:15:40,215
we actually go back to our grid,

249
00:15:40,215 --> 00:15:43,185
and when we draw the
rectangle off the grid,

250
00:15:43,185 --> 00:15:45,690
we type the same thing,

251
00:15:45,690 --> 00:15:47,860
but we do corner.

252
00:15:48,050 --> 00:15:53,910
Here. Because the grid is
drawn with a different style,

253
00:15:53,910 --> 00:15:55,950
which is the corner style.

254
00:15:55,950 --> 00:15:59,730
The calculation was done
for that using the default.

255
00:15:59,730 --> 00:16:03,240
That's why the grid was actually
working well initially.

256
00:16:03,240 --> 00:16:06,480
But as we transition to a
different kind of drawing

257
00:16:06,480 --> 00:16:11,680
style with the ant,

258
00:16:12,290 --> 00:16:14,940
then the grid in the next frame.

259
00:16:14,940 --> 00:16:16,620
The first frame is actually
executed correctly,

260
00:16:16,620 --> 00:16:18,660
but the second
frame is trying to

261
00:16:18,660 --> 00:16:21,750
draw the grid in a
displaced position.

262
00:16:21,750 --> 00:16:25,890
So we lose the perfect alignment
that we had originally.

263
00:16:25,890 --> 00:16:29,565
Let's see. You see we
have it corrected now.

264
00:16:29,565 --> 00:16:31,410
We have our ants

265
00:16:31,410 --> 00:16:34,710
starting in particular
two different positions.

266
00:16:34,710 --> 00:16:36,150
One is moving down,

267
00:16:36,150 --> 00:16:37,410
one is moving to the right.

268
00:16:37,410 --> 00:16:40,605
Both of them have a
stay within bounds.

269
00:16:40,605 --> 00:16:43,905
Function, so we're not
running into any errors.

270
00:16:43,905 --> 00:16:46,590
This is the foundation
for what is going to

271
00:16:46,590 --> 00:16:49,380
be our ant behavior.

272
00:16:49,380 --> 00:16:51,240
Things are working very well.

273
00:16:51,240 --> 00:16:53,475
We have one final video missing.

274
00:16:53,475 --> 00:16:55,425
We're going to
really write most of

275
00:16:55,425 --> 00:16:58,620
the functionality of the
ant within that video.

276
00:16:58,620 --> 00:17:01,330
I'll see you then.