All right, so let's begin creating our game.
So I just want to start by cleaning up a little bit here.
And now we can start off by creating our cell and we're going to want our cell to be exposed to
the Java script.
So let's go ahead and use ASM, Bind Gen to expose it.
And we want it to be represented, represented by an unsigned.
Eight bit integer and.
And we're going to need to derive a couple of traits.
And I.
So the couple of traits that we're going to want as a couple, we're going to want many traits.
We're going to want clone copy.
Debug partial IQ for equality and IQ as well.
And now we're going to create an enum called Cell.
And then inside of here, we're going to have dead equals zero and then a live equals one.
And so to expand on the repr unit, that's important because we want each cell to be represented as
a single byte.
And so the next thing we can define is our universe, which is also something that we're going to want
accessible inside of the Java script.
So we're going to say pub struct universe and then inside of here we're going to have a width of an
unsigned 30 to a height, which is also unsigned 32.
And then we want our cells to be in the universe as well, which we will represent as a vector of cells.
And so now that we have our universe defined, we can start implementing some methods on it.
The first method we want to implement is accessing the cell at a given row and column into an index
into the cells vector.
So we will implement some methods on our universe.
And again, we want to be able to get an index into the vector.
So we'll create a get index function.
We're going to pass in self.
And then we have our row and our.
Call them.
And we're going to return a few size.
So the logic in here is going to be pretty straightforward.
We're just going to return a the row times our self dot width plus the column and we want to return
it as you size.
And that's all we need to do in order to get our index.
And now that we have the method implemented, we can calculate the next state of a cell.
So we need to get a count of how many of its neighbors are alive and the method is going to use deltas
and a modulo which is the.
Percent sign, and we're using that to avoid a special casing the edges of the universe with if statements.
So we're just trying to avoid having.
Extra if statements when we can, we don't need to do them by using this logic.
And so by applying a delta of minus one, we are actually adding.
Minus one to the height and then we mod it.
So this allows us to avoid unsigned integer under flow, meaning we went below zero and since its unsigned
are unsigned our results would be end incorrect.
So let's begin.
Let's start implementing this and this will make a little bit more sense.
So we're going to say our live neighbor count again, we're passing in our self, our row
and our column and we're going to return back au8.
So we'll say let mutable count equals zero.
And then for delta rho in self dot height.
Minus one
zero for the row.
One for the column and then we are going to iterate.
Clone.
And now we're going do the same thing for the row.
So we're going to have Delta or excuse me, the column, Delta column in self dot width.
Minus one 01. eter dot cloned.
And then if Delta Rho is equal to zero and Delta column is also equal to zero.
Then continue.
So now down here we can say let neighbor row is equal to the row plus.
Delta Rho.
Mod self dot height.
And let neighbor call equals column plus delta call mod self dot width.
And then ah we'll just go for index self dot get index of our neighbor row and then for our column,
neighbor column.
And now we can add to count self dott sells.
At.
The index as a unsigned.
And then all we want to do when this loop is done is return our count.
So all we're doing is getting the count of all the cells located of all the neighbor cells located at
that row and that column inside of our universe.
And so now that we have that done, we have everything needed to compute the next generation.
And we.
We can have a straightforward implementation of the game's rules by using a match expression.
And since we want JavaScript to control this portion, we are also going to play.
We are going to place this inside a ASM.
Engine block.
So let's go ahead and add it.
So we have Ys and Bingen and now we're going to pull Universe again because we want this to be exposed
to the the JavaScript interface.
And now inside of here we're going to create a function called tick.
And basically a tick is a new generation.
And so we're going to pass in our self.
And now inside here let me.
Next equals self cells.
That clone.
And now we can say for ro in zero to self dot height and for call and zero to self dot with.
You can say let it x equals self dot get index of our row and r column and then let cell equals to self
dot cells.
I oop.
Of our index.
And then let live.
Neighbors equals to self live neighbor count of our row.
In our column.
And now we can start implementing in the rules.
So let next sell equals match on the cell and the live neighbors.
So we have rule one, which is any live cell with fewer than two live neighbors dies because of under
population.
And so we can say sell alive.
X if x is.
Less than to then sell is.
And now we have put another rule to any live cell.
With two or three live neighbors lives on.
And we'll just go ahead and write out the rest.
Rule three Any live cell with more than three live name brs dies because of over population and then
rule for.
Any dead cell with exactly three live neighbors.
Becomes a live sell due to re pro duction.
So now let's implement rule two.
So we have cell is alive.
To.
Or.
Cell is alive.
Three.
So alive.
So now we can implement rule three.
So we will have a cell alive.
If X.
Greater than three.
Then our cell is now dead.
Excuse me.
All right.
We have a little air here.
What do we.
And now we can implement on rule four.
So our dead cell.
If has three neighbors now becomes in a life cell.
And then otherwise, because remember, you have to handle all situations.
Otherwise, all cells maintain themselves.
Otherwise sell remains in the same state as before.
So now we can end that block and then we can have our next iteration on the next index equals to our
next cell.
And we can say self dot cells equals to next.
So currently we don't have any way to display the game currently.
So we need to implement a method that will make this easy for us to see what is going on when we execute
our program.
We will use empty and full squares to signify if a cell is dead or alive.
So just copy and paste the symbols from the code that I provide you.
So we need to use standard format.
And down here we're going to implement format display for our universe.
And in here we're going to have a formatting function going to refer to self and then it's going to
have a mutable formatter which is going to return a result for us.
So in here we have for each line and the self dot cells as a slice dot chunks self dot width as you
size.
And then for a reference to our sell in line, we're going to say let symbol equals to if cell equals
is equivalent to a dead cell.
Then we want a.
T and I'm just going to copy and paste this in for myself else.
Here we go.
Else?
We want that guy.
So let me format this real quick to make it pretty.
All right, so let's symbol.
Cool.
If cell is equal to dead cell, then we want.
If the cell is dead, we want an empty square.
Else of the cell is alive.
Then we want a full square.
That is correct.
So now we want to write F
and then symbol propagate if there is an air.
And now again.
Right.
But this time, we just want to write out a new line character.
And return.
Oh, okay.
All right.
So it doesn't look like we have any air, so everything seems very happy with that.
So now we have a way to display out our universe.
So I'm going to take a quick pause right here.
That way, this lecture doesn't get too terribly long, and we will pick up in the next lecture.