1
00:00:06,181 --> 00:00:07,803
Hi, welcome to this new video.

2
00:00:07,803 --> 00:00:09,883
We are going to continue
working on our project,

3
00:00:09,883 --> 00:00:12,166
which is the wave function
collapse algorithm.

4
00:00:12,166 --> 00:00:16,174
And we have been covering
a series of functions.

5
00:00:16,174 --> 00:00:19,243
We are almost ready to conclude the loop,
or one loop,

6
00:00:19,243 --> 00:00:23,694
of what the whole algorithm would actually
do and iterate over a whole grid.

7
00:00:23,694 --> 00:00:26,854
But we are at the point where we need
to calculate the lowest entropy.

8
00:00:26,854 --> 00:00:28,314
Let's understand what that means.

9
00:00:29,374 --> 00:00:31,254
Calculating the lowest entropy.

10
00:00:31,254 --> 00:00:33,165
As we are collapsing a cell and

11
00:00:33,165 --> 00:00:37,449
we start updating the neighbors
around that cell, we'll realize

12
00:00:37,449 --> 00:00:42,134
that suddenly the grid no longer has
the same number of entropy values.

13
00:00:42,134 --> 00:00:44,872
Entropy in this case refers
to the uncertainty or

14
00:00:44,872 --> 00:00:48,654
the number of choices that
are available for each cell, right?

15
00:00:48,654 --> 00:00:50,615
So as you can see in the example here,

16
00:00:50,615 --> 00:00:53,402
there's three cells that
have been collapsed, and

17
00:00:53,402 --> 00:00:57,670
there's a few adjacent cells around
that that have a possibility space of 8.

18
00:00:57,670 --> 00:01:01,518
And there's one specific
one that has four options.

19
00:01:01,518 --> 00:01:03,778
So that would be, in fact,
the lowest entropy.

20
00:01:03,778 --> 00:01:06,306
So we want to find that cell, right?

21
00:01:06,306 --> 00:01:10,500
We want to loop through the cells and
make sure that we are picking the lowest

22
00:01:10,500 --> 00:01:14,229
entropy cell to move and apply
the collapse function to that one and

23
00:01:14,229 --> 00:01:18,194
then move on to the adjacent tiles and
kind of start looping, right?

24
00:01:18,194 --> 00:01:23,432
But what happens in this calculation if we
have a series of cells that are tied for

25
00:01:23,432 --> 00:01:25,338
the lowest entropy value?

26
00:01:25,338 --> 00:01:29,059
So we're going to have to write something
that we have written already before,

27
00:01:29,059 --> 00:01:32,682
which is finding the lowest point,
let's say, or the closest distance.

28
00:01:32,682 --> 00:01:37,648
We've written algorithms like that, but
we're going to write it in such a way

29
00:01:37,648 --> 00:01:42,313
that we will also account for what
happens if there are multiple cells that

30
00:01:42,313 --> 00:01:46,978
are tied to the lowest value,
which is something that will happen often,

31
00:01:46,978 --> 00:01:51,770
because we will actually be evaluating
cells that has discrete numbers.

32
00:01:51,770 --> 00:01:54,722
Some of them will have eight,
four, two options.

33
00:01:54,722 --> 00:01:59,202
And it's likely that we're going to
have many cells that are tied for

34
00:01:59,202 --> 00:02:00,628
the lowest number.

35
00:02:00,628 --> 00:02:04,299
So we're going to collect those in a list,
and it's going to refer to the cells that

36
00:02:04,299 --> 00:02:07,264
have the lowest entropy, and
they could be multiple, right?

37
00:02:08,404 --> 00:02:11,268
The algorithm will pick one random,
one of those.

38
00:02:11,268 --> 00:02:12,596
It doesn't really matter which one.

39
00:02:12,596 --> 00:02:15,284
At this point, we just need to choose one.

40
00:02:15,284 --> 00:02:20,084
But we need to account for the algorithm
to know that there's in fact many

41
00:02:20,084 --> 00:02:23,004
cells that are tied for the lowest value.

42
00:02:23,004 --> 00:02:25,604
So we're going to continue
where we left off.

43
00:02:25,604 --> 00:02:30,183
We are working within, this is our grid
script, which we are calling environment,

44
00:02:30,183 --> 00:02:31,224
and tile, right.

45
00:02:32,564 --> 00:02:35,884
Basically the same thing that we have so
far.

46
00:02:35,884 --> 00:02:38,764
Let's look into the grid at this point.

47
00:02:38,764 --> 00:02:41,904
I would like to write a few
functions that are going to be,

48
00:02:41,904 --> 00:02:43,940
let's just comment them out here.

49
00:02:43,940 --> 00:02:52,204
First, I would like to write
the find lowest entropy.

50
00:02:52,204 --> 00:02:58,292
And I would also like to
display the lowest entropy.

51
00:02:58,292 --> 00:03:01,724
Just as the graphics that we're
showing in some of the slides,

52
00:03:01,724 --> 00:03:05,803
it would be nice to have a very clear
representation of which is the tile that

53
00:03:05,803 --> 00:03:07,454
is being evaluated, right?

54
00:03:07,454 --> 00:03:11,744
So we're going to do a little highlight
that would display the next value.

55
00:03:11,744 --> 00:03:15,020
So those functions,
we're going to write them here,

56
00:03:15,020 --> 00:03:17,920
I'm writing them as kind of comments so
far.

57
00:03:17,920 --> 00:03:20,257
So we're going to write
those functions first and

58
00:03:20,257 --> 00:03:24,044
then we're going to basically come back
and activate them in the run, right?

59
00:03:24,044 --> 00:03:26,644
So let's just write one of them here.

60
00:03:26,644 --> 00:03:32,584
So let's define, let's give ourselves
a little bit of space to work here.

61
00:03:33,844 --> 00:03:39,604
So let's call this find lowest entropy.

62
00:03:39,604 --> 00:03:43,644
And we are just going to,
for lowest entropy cell.

63
00:03:43,644 --> 00:03:46,064
We're looking for a cell, right?

64
00:03:47,724 --> 00:03:49,204
Self, right.

65
00:03:49,204 --> 00:03:52,004
And usually what we do with
the kind of closest point or

66
00:03:52,004 --> 00:03:55,612
smaller distance calculation,
we start with a very high number.

67
00:03:55,612 --> 00:04:00,392
So we do lowest entropy
would be something like,

68
00:04:00,392 --> 00:04:04,244
something quite large, right.

69
00:04:04,244 --> 00:04:08,572
And then we're going to say cell_x and
cell_y,

70
00:04:08,572 --> 00:04:14,239
because we're looking at the x index and
the y index of that cell,

71
00:04:14,239 --> 00:04:17,962
we're going to to say none and
none, right?

72
00:04:17,962 --> 00:04:21,915
So this is a way of describing
two variables in one line.

73
00:04:21,915 --> 00:04:23,843
And we're also going to start a list here,

74
00:04:23,843 --> 00:04:26,724
which is something that
we haven't done before.

75
00:04:26,724 --> 00:04:31,564
Several lowest,
it's going to be an empty list.

76
00:04:31,564 --> 00:04:35,548
So now we loop through all
these cells in the system.

77
00:04:35,548 --> 00:04:43,768
So for x in range(self.columns) and

78
00:04:43,768 --> 00:04:49,996
for y in range(self.rows)

79
00:04:49,996 --> 00:04:58,224
let's calculate the entropy here.

80
00:04:58,224 --> 00:04:59,959
The entropy is the length,

81
00:04:59,959 --> 00:05:03,840
remember that the entropy is
the number of possibilities.

82
00:05:03,840 --> 00:05:10,700
So we're going to check the cell x and y.

83
00:05:10,700 --> 00:05:16,252
Cells x and y, right?

84
00:05:16,252 --> 00:05:18,595
And the length of that list, right,

85
00:05:18,595 --> 00:05:22,900
because we know that each cell
refers to a list of possible states.

86
00:05:22,900 --> 00:05:26,316
In a way the length of that in
this case would be probably four.

87
00:05:26,316 --> 00:05:28,284
That would be the entropy, right?

88
00:05:28,284 --> 00:05:32,215
So now we can say, well,

89
00:05:32,215 --> 00:05:36,523
if it's bigger than one and

90
00:05:36,523 --> 00:05:43,406
smaller than the lowest entropy, right.

91
00:05:43,406 --> 00:05:47,453
So we're doing inline checking
that it's in fact bigger than one,

92
00:05:47,453 --> 00:05:49,950
which is the collapsed state, right?

93
00:05:49,950 --> 00:05:53,574
And smaller than the lowest entropy,
which is a very high number.

94
00:05:53,574 --> 00:05:57,694
We can agree that we're going to update.

95
00:05:57,694 --> 00:06:05,663
We will, we're going to reset the list.

96
00:06:09,498 --> 00:06:13,140
When I reset the list,

97
00:06:13,140 --> 00:06:18,645
the lowest entropy, It's going to become,

98
00:06:22,908 --> 00:06:27,708
The entropy, right?

99
00:06:27,708 --> 00:06:33,954
We're basically saying if we're below
the lowest entropy, let's just assign.

100
00:06:33,954 --> 00:06:36,014
I did that twice, sorry.

101
00:06:36,014 --> 00:06:39,523
Lowest entropy = entropy, right?

102
00:06:39,523 --> 00:06:43,768
We're assigning the lowest entropy to be
the current entropy that we're looping

103
00:06:43,768 --> 00:06:44,326
through.

104
00:06:44,326 --> 00:06:53,819
And the index of x and y,
It's going to become x and y, right?

105
00:06:53,819 --> 00:06:55,314
From the loop, right?

106
00:06:57,694 --> 00:07:04,085
And the several lowest.append,

107
00:07:04,085 --> 00:07:10,480
we're going to pass on a tuple, x,y

108
00:07:13,206 --> 00:07:18,334
The several lowest is going to contain
a tuple of the x and y index, right?

109
00:07:18,334 --> 00:07:19,678
Let me correct a little bit.

110
00:07:19,678 --> 00:07:23,603
I've been writing a little bit fast here,
so

111
00:07:23,603 --> 00:07:27,437
I'm going to correct my spelling, several.

112
00:07:29,277 --> 00:07:32,676
And I think here,
let me see if there's another one.

113
00:07:32,676 --> 00:07:37,564
Several, I think that's fine, right?

114
00:07:37,564 --> 00:07:42,861
This is the case in which the entropy is
in fact smaller than the lowest entropy,

115
00:07:42,861 --> 00:07:44,324
right?

116
00:07:44,324 --> 00:07:48,968
We want to write a second statement
that is going to be if it's tied, right?

117
00:07:48,968 --> 00:07:51,400
I'm going to explain this line,
which I didn't explain too much.

118
00:07:51,400 --> 00:07:56,080
Why are we resetting
the several lowest list, right?

119
00:07:56,080 --> 00:08:00,601
We're going to do a second statement,
which is the elif statement here Here,

120
00:08:00,601 --> 00:08:05,212
which is what happens if
the entropy is equal to

121
00:08:05,212 --> 00:08:10,708
the lowest_entropy, Right?

122
00:08:10,708 --> 00:08:14,081
This is the case in which we
found the lowest_entropy.

123
00:08:14,081 --> 00:08:18,897
Let's say the lowest_entropy initially was
10,000 and then we reduce it to 8, right,

124
00:08:18,897 --> 00:08:22,281
and now we're finding more than
one cell that has that number 8.

125
00:08:22,281 --> 00:08:25,421
So if our entropy is equal
to the lowest_entropy,

126
00:08:25,421 --> 00:08:29,825
all right, so we have a second cell
that matches that lowest number.

127
00:08:29,825 --> 00:08:34,914
Well, at that point, we want to
store that value because any time

128
00:08:34,914 --> 00:08:40,002
that we encounter more than one
entity with that lowest entropy,

129
00:08:40,002 --> 00:08:45,205
we're going to be storing it in
this several_lowest list, right?

130
00:08:48,825 --> 00:08:50,040
There we go, right?

131
00:08:50,040 --> 00:08:52,450
So we append to this list.

132
00:08:52,450 --> 00:08:58,112
If we, in fact, let's say we're
collecting all the entropies number 8,

133
00:08:58,112 --> 00:09:00,935
but we suddenly find a number 4.

134
00:09:00,935 --> 00:09:05,150
Well, that becomes the new lowest_entropy,
and we reset the list, right?

135
00:09:05,150 --> 00:09:08,897
If we had like three or
four entries tied to the number 8 and

136
00:09:08,897 --> 00:09:12,033
that list already contained
a few elements, but

137
00:09:12,033 --> 00:09:16,314
then we find the number four,
we want to reset the list, right, and

138
00:09:16,314 --> 00:09:21,135
start storing new entities in
this new created list, right?

139
00:09:21,135 --> 00:09:25,463
So in this way,
the several_lowest will always contain

140
00:09:25,463 --> 00:09:30,060
the most updated lowest and
it will only contain either one or

141
00:09:30,060 --> 00:09:34,825
more entries that are tied
to that lowest value, right?

142
00:09:34,825 --> 00:09:37,081
So there we have it.

143
00:09:37,081 --> 00:09:40,345
We've looped through the grid.

144
00:09:40,345 --> 00:09:46,642
So let's just find,
now let's check if the length of this,

145
00:09:46,642 --> 00:09:49,841
whoops, several_lowest.

146
00:09:49,841 --> 00:09:54,805
So this list, so we can check,
is this bigger than 1?

147
00:09:58,945 --> 00:10:03,385
And if it's bigger than 1,
it means that we have several tied lowest.

148
00:10:03,385 --> 00:10:08,461
And we could say that the cell, let's pick

149
00:10:08,461 --> 00:10:13,713
a random choice from the several_lowest.

150
00:10:13,713 --> 00:10:18,665
So pick one of them is what
we spoke about before.

151
00:10:19,845 --> 00:10:22,620
And here what we're going to be
updating with this function,

152
00:10:22,620 --> 00:10:24,701
which is finding the lowest_entropy.

153
00:10:24,701 --> 00:10:30,104
If you remember, we created all the way to
the top of the script these two entries,

154
00:10:30,104 --> 00:10:34,904
which is the next cell, and we use
those to start the algorithm, right?

155
00:10:34,904 --> 00:10:38,362
We started them here,
we say the next cell,

156
00:10:38,362 --> 00:10:41,829
it's going to be based
on this information.

157
00:10:41,829 --> 00:10:44,710
So what we're going to be
doing all the way down here,

158
00:10:44,710 --> 00:10:47,256
we're going to be updating this next cell,
and

159
00:10:47,256 --> 00:10:51,615
we're not going to be using this kind of
arbitrary numbers that we used before.

160
00:10:52,635 --> 00:11:00,268
We're going to be using the cells or,
sorry,

161
00:11:00,268 --> 00:11:06,675
the selected cell from this random.

162
00:11:06,675 --> 00:11:12,509
The first entry of that is the index,
the x, right,

163
00:11:12,509 --> 00:11:19,555
because we're looping through
the tuple of indices, right?

164
00:11:20,935 --> 00:11:23,675
The 0 and the 1, right?

165
00:11:23,675 --> 00:11:30,812
And then return a cell[0] and

166
00:11:30,812 --> 00:11:36,097
cell[1], else, so

167
00:11:36,097 --> 00:11:42,175
this is the case if there's

168
00:11:42,175 --> 00:11:47,389
more than 1, right?

169
00:11:47,389 --> 00:11:53,114
But if it's only 1,
we can just do something slightly,

170
00:11:53,114 --> 00:11:58,573
it's kind of similar than this exit or
this return.

171
00:11:58,573 --> 00:12:01,045
Just write it here and
then do some changes.

172
00:12:01,045 --> 00:12:07,164
So the next cell would be a cell_x,

173
00:12:11,391 --> 00:12:12,415
cell_y.

174
00:12:16,195 --> 00:12:18,735
And you might be wondering
what are these values?

175
00:12:19,835 --> 00:12:21,115
Where are we getting these from?

176
00:12:21,115 --> 00:12:23,175
Let's just double-check that in a minute.

177
00:12:24,435 --> 00:12:26,931
So these have been declared at the very
beginning of this function, right?

178
00:12:26,931 --> 00:12:27,835
So if we go here,

179
00:12:27,835 --> 00:12:32,207
these values all the way at the beginning
of the function were defined as none.

180
00:12:32,207 --> 00:12:36,455
But as we loop through the list,
we assign those, right?

181
00:12:36,455 --> 00:12:38,842
If we only find one entropy cell,

182
00:12:38,842 --> 00:12:44,447
those are already assigned to the tuple
x and y, sorry, to the x and y index.

183
00:12:44,447 --> 00:12:46,115
So we can use those straight away.

184
00:12:47,855 --> 00:12:50,519
So that's the function of
finding the lowest_entropy.

185
00:12:50,519 --> 00:12:55,315
We mostly use it to update
the next cell value, right?

186
00:12:56,625 --> 00:12:59,505
So we start with an arbitrary
number that is 15 and 7.

187
00:12:59,505 --> 00:13:04,945
And as we loop through the algorithm,
we find the lowest entropy point.

188
00:13:04,945 --> 00:13:08,005
So let's just call this algorithm
because this is not currently called.

189
00:13:08,005 --> 00:13:16,229
So here we could say self.lowest_entropy,

190
00:13:19,462 --> 00:13:20,725
Like that, right?

191
00:13:22,455 --> 00:13:25,543
And at this point, if this runs,
we could have a loop.

192
00:13:25,543 --> 00:13:28,764
We're going to see that there's some
errors and there's some kind of exceptions

193
00:13:28,764 --> 00:13:31,623
that we might run into, but
let's see what we have at this moment.

194
00:13:31,623 --> 00:13:37,335
So we run the algorithm and
it starts looping and it crashes.

195
00:13:37,335 --> 00:13:38,591
That's totally fine.

196
00:13:38,591 --> 00:13:39,765
Let's understand what's going on.

197
00:13:39,765 --> 00:13:45,337
So the algorithm will kind of gradually
go from cell to cell, each frame, right?

198
00:13:45,337 --> 00:13:46,228
Each frame,

199
00:13:46,228 --> 00:13:51,414
it's going to collapse a cell within
this kind of wave_function_collapse,

200
00:13:51,414 --> 00:13:55,707
propagate a search,
update the entropy of the neighbors, and

201
00:13:55,707 --> 00:14:00,905
then move down the line to the
find_lowest_entropy and then start over.

202
00:14:00,905 --> 00:14:03,433
And it's going to repeat this
sequence over and over and over.

203
00:14:03,433 --> 00:14:08,695
The problem is because we have
a system that has only four tiles,

204
00:14:08,695 --> 00:14:13,958
it is possible that the algorithm
doesn't run into a solution,

205
00:14:13,958 --> 00:14:17,285
that it kind of runs into errors, right?

206
00:14:17,285 --> 00:14:21,932
So we're going to expand these tiles
to all the possible states that this

207
00:14:21,932 --> 00:14:23,743
system can actually have so

208
00:14:23,743 --> 00:14:28,245
that we in fact find a solution to
the whole configuration, right?

209
00:14:29,865 --> 00:14:31,297
But we're going to do
that in the next video.

210
00:14:31,297 --> 00:14:35,161
Let's first conclude by
writing this display entropy.

211
00:14:35,161 --> 00:14:38,706
I would like to have
a highlight of which is

212
00:14:38,706 --> 00:14:43,077
the cell that is being
evaluated at a time, right?

213
00:14:43,077 --> 00:14:48,548
So let's just do a simple
visualization for that, right?

214
00:14:48,548 --> 00:14:51,177
So I'm going to call it
display lowest entropy.

215
00:14:51,177 --> 00:14:52,872
I'm going to write it all
the way to the bottom.

216
00:14:52,872 --> 00:14:56,701
So we have display_grid somewhere here,
but

217
00:14:56,701 --> 00:15:01,316
we're starting to operate
within the display_grid,

218
00:15:01,316 --> 00:15:06,928
here we can use a function to
display the lowest_entropy, right?

219
00:15:06,928 --> 00:15:07,771
So display,

220
00:15:11,503 --> 00:15:18,725
display_lowest_entropy(self, x, y), right?

221
00:15:18,725 --> 00:15:23,220
So what we're going to do is that
we're going to pass an x and

222
00:15:23,220 --> 00:15:28,277
y coordinate for that cell,
and let's do some exceptions.

223
00:15:28,277 --> 00:15:32,634
We're making sure that if x is not None,

224
00:15:32,634 --> 00:15:39,299
it might happen that at some
point the algorithm concludes and

225
00:15:39,299 --> 00:15:46,365
we reach a point where we cannot
run this visualization anymore.

226
00:15:48,425 --> 00:15:51,721
So I like having some measures.

227
00:15:51,721 --> 00:15:57,728
So the tile size could be
calculated by a float number,

228
00:15:57,728 --> 00:16:04,071
which is the self, .world x divided

229
00:16:04,071 --> 00:16:09,678
by the columns, right?

230
00:16:09,678 --> 00:16:14,149
And then let's just

231
00:16:14,149 --> 00:16:18,621
define the stroke.

232
00:16:18,621 --> 00:16:25,661
I'm going to do a red color with
noFill just to make it a highlight.

233
00:16:25,661 --> 00:16:32,267
And a strokeweight of 3 or, sorry, 3, r?

234
00:16:32,267 --> 00:16:33,606
I think that should be enough.

235
00:16:33,606 --> 00:16:34,754
And then we pushMatrix.

236
00:16:39,721 --> 00:16:47,977
We translate in x times the tile size,
right?

237
00:16:47,977 --> 00:16:53,822
Again, we're drawing
a rectangle on top of and

238
00:16:53,822 --> 00:16:56,607
y times the tile size.

239
00:16:58,912 --> 00:17:04,564
Let's define the rectangle
rectMode to be CORNER.

240
00:17:04,564 --> 00:17:09,628
And let's draw that rectangle at 0,

241
00:17:09,628 --> 00:17:14,539
0, tile size, tile size, right?

242
00:17:14,539 --> 00:17:17,397
Because we're using it to translate
the coordinate we can use 0,

243
00:17:17,397 --> 00:17:20,061
0 because we're using push and
push and pop matrix, right?

244
00:17:20,061 --> 00:17:24,346
And now we pop the matrix.

245
00:17:24,346 --> 00:17:26,285
If you like this to be shorter,

246
00:17:26,285 --> 00:17:30,913
you can actually type the coordinates
of the position of the rectangle and

247
00:17:30,913 --> 00:17:34,136
avoid the whole kind of
push popMatrix situation.

248
00:17:34,136 --> 00:17:37,785
But this would allow you to expand on
that visualization if you want, right?

249
00:17:37,785 --> 00:17:40,290
And then we go back I
would like to kind of,

250
00:17:40,290 --> 00:17:45,235
whenever I change the strokeweight,
I like to return the strokeweight because

251
00:17:45,235 --> 00:17:49,764
we hadn't been manipulating
the strokeweight anywhere else, right?

252
00:17:49,764 --> 00:17:51,291
So if I kind of change it to 3,

253
00:17:51,291 --> 00:17:54,617
I don't want the whole drawing
to become strokeweight of 3.

254
00:17:54,617 --> 00:17:56,735
I'm going to return it to 1, right?

255
00:17:56,735 --> 00:18:01,799
So we have this function that
would draw the lowest entropy.

256
00:18:01,799 --> 00:18:07,096
Let's see if this has any errors or
it's working.

257
00:18:07,096 --> 00:18:09,646
So let's just see here.

258
00:18:09,646 --> 00:18:16,904
So we're going to use self.display
lowest entropy, right?

259
00:18:19,157 --> 00:18:22,053
And we have to provide some arguments,
right?

260
00:18:22,053 --> 00:18:25,081
We have to provide the x and the y, right?

261
00:18:25,081 --> 00:18:30,316
And what is the x and
the y for this highlight?

262
00:18:30,316 --> 00:18:33,373
Well, we can use these global values,

263
00:18:33,373 --> 00:18:38,015
these values that we have for
the next cell x and next cell y.

264
00:18:38,015 --> 00:18:43,098
This is going to be at all times the cell
that would be evaluated next, right?

265
00:18:43,098 --> 00:18:48,823
So let's see if this visualization now,
it's actually showing us.

266
00:18:48,823 --> 00:18:53,151
Well, we're running into the same
problem of crashing at some point due to

267
00:18:53,151 --> 00:18:54,879
the possibilities, right?

268
00:18:54,879 --> 00:18:56,582
But we do have a highlight, right?

269
00:18:56,582 --> 00:18:58,784
So let's just run that a couple of times.

270
00:18:58,784 --> 00:19:06,274
Because of the randomness, it might be
that we run it a few more iterations.

271
00:19:07,828 --> 00:19:12,415
You see, like different times, we run
it more or less, but we often will find

272
00:19:12,415 --> 00:19:17,146
ourselves in a position of not finding
a solution to continuing the algorithm,

273
00:19:17,146 --> 00:19:17,657
right?

274
00:19:17,657 --> 00:19:20,507
So we're going to have to
provide more tile options for

275
00:19:20,507 --> 00:19:23,176
the algorithm to really
resolve this equation.

276
00:19:23,176 --> 00:19:25,590
If you run this algorithm at this point,

277
00:19:25,590 --> 00:19:29,286
you see that it runs pretty
quickly until it crashes, right?

278
00:19:29,286 --> 00:19:33,781
One of the things that I like
doing is trying to slow down

279
00:19:33,781 --> 00:19:38,863
the algorithm to the point in
which we could actually click and

280
00:19:38,863 --> 00:19:42,302
execute a loop or kind of a frame, right?

281
00:19:42,302 --> 00:19:45,721
I would like to implement that now.

282
00:19:45,721 --> 00:19:49,431
It's actually a really simple technique,
but

283
00:19:49,431 --> 00:19:52,685
it allows us to have some kind of control.

284
00:19:52,685 --> 00:20:00,713
So in the draw,
let's just create a global count and

285
00:20:00,713 --> 00:20:05,455
a t count and we're going to say if

286
00:20:05,455 --> 00:20:11,292
the count is smaller than the t count,

287
00:20:11,292 --> 00:20:15,123
I'm going to write this and

288
00:20:15,123 --> 00:20:20,798
explain what I'm doing in a minute.

289
00:20:20,798 --> 00:20:25,331
We are going to run the environment,
right?

290
00:20:25,331 --> 00:20:27,914
So this is the loop that is happening,
right?

291
00:20:27,914 --> 00:20:34,763
Let's comment out this background for
a moment, right?

292
00:20:34,763 --> 00:20:40,835
And, If we press a key,

293
00:20:40,835 --> 00:20:45,333
let's just create a bit of space here.

294
00:20:48,425 --> 00:20:51,545
Let's use keyReleased.

295
00:20:51,545 --> 00:20:57,596
So if we release a key
we advance the t count.

296
00:20:57,596 --> 00:20:58,188
So,

297
00:21:09,824 --> 00:21:12,932
So what we're saying here is that
let's just use the keyboard.

298
00:21:12,932 --> 00:21:19,175
So the algorithm by itself,
it's not going to start, right?

299
00:21:19,175 --> 00:21:25,015
Actually, let's just
start these values here.

300
00:21:25,015 --> 00:21:32,188
count=0 and t count=0, right?

301
00:21:32,188 --> 00:21:33,889
So let's understand what we're doing here.

302
00:21:33,889 --> 00:21:37,978
We're saying we're starting both at 0,
right?

303
00:21:37,978 --> 00:21:43,050
If we press the key,
we advance the t count so

304
00:21:43,050 --> 00:21:46,073
that will go from 0 to 1.

305
00:21:46,073 --> 00:21:49,772
At that point this algorithm will run and
execute, right?

306
00:21:49,772 --> 00:21:51,360
Or will start executing.

307
00:21:51,360 --> 00:21:56,568
What we want at this point is also make
sure that after we execute one loop,

308
00:21:56,568 --> 00:21:58,674
we say count +=1, right?

309
00:21:58,674 --> 00:22:01,620
So at this point the count
will match the t count.

310
00:22:01,620 --> 00:22:04,308
So every time we press a key,

311
00:22:04,308 --> 00:22:09,802
we give it an extra possible
step to move forward, right?

312
00:22:09,802 --> 00:22:13,377
And in such a way we will actually
be able to control with our keys,

313
00:22:13,377 --> 00:22:17,667
with the keyboard or you can use the mouse
or anything else changing a little bit

314
00:22:17,667 --> 00:22:21,849
the function to control how the flow
of the algorithm is executing, right?

315
00:22:21,849 --> 00:22:26,169
So you'll see that nothing happens,
but if we press once, you'll see,

316
00:22:26,169 --> 00:22:30,560
we collapse the cell and we find
the lowest entropy point, we press again,

317
00:22:30,560 --> 00:22:35,185
we collapse the cell and we find the
lowest entropy point and so forth, right?

318
00:22:35,185 --> 00:22:41,225
So we could actually kind of start
looping through until we crash it, right?

319
00:22:41,225 --> 00:22:46,170
So this is a way of like slowing down
the execution of the algorithm and

320
00:22:46,170 --> 00:22:50,612
allows us to debug a little bit
more clearly what's going on.

321
00:22:50,612 --> 00:22:54,905
So we're going to leave it here and
I'll see you in the next video where we're

322
00:22:54,905 --> 00:22:58,880
going to kind of continue expanding and
adding more tiles to the system.

323
00:22:58,880 --> 00:22:59,636
We'll see you then.