The Game of Life: A Refactoring Project
We have this dot props dot grid size event, which is going to be the event key down here one two or three. It's going to be passed into the event here, so we're going to get a number one two or three on in the grid size function down here. So, this is a good time for a switch statement. So, it's going to this size is either going to be 1, 2, or 3.
So, case one, we're going to set this dot calls to equal 20 and this dot rows to equal 10. That's the small one. And then we're gonna have to break. I'm going to select this and then command shift d, command shift d, i duplicated two times.
Case two, we're going to set this to 50 and 30. And case 3 or we can actually just put default because that just means if anything else comes through, we're going to set this to 70 and 50. And we don't need the break statement right here. And then I'm going to call this dot clear. So, this.clear is just going to reset everything.
And then when it calls the this.dot.set state, that's going to end up updating automatically the cells in the rows from here is automatically going to update to this dot set state and that will propagate through our app. So, we're ready to test this out. I'm just going to save this. Let's see if it compiles. It compiled successfully.
Let's try this out. Let's uh zoom in a little bit. Uh, i don't need this. Let's see pause play clear that works. I can seed, I can make it go slower. Can I? Hmm, that doesn't seem to be working. This dot play button is not a function. Okay, we made a mistake over there.
Let's go back. So, for slow and fast this is supposed to be a lowercase p. Save that. Oh, and i forgot to make this fast here. No wonder you guys are probably seeing that earlier. Okay, let's save that. Um, let's see if it compiles and it compiles.
Should refresh okay now, let's try slow. Okay, it's going slower. I like to test it by creating this thing. It's kind of like a little spaceship and see if it does. See if this works. Let's go to fast and it's just going to go across the screen.
There are a few things like that that people have figured out in the game of life that if you make a certain pattern, does cool things. You can see that got to the end. And just turned to a square that's something that you can do in refactoring right now.
If you get to the end there's no no squares over here but you could refactor it to make it so so this is something like this would go completely off screen. So we can seat it while it's going. We can clear it. Um, we can seat it.
Let's see if we can change the grid size 20 by 10 yep that worked. Let's see it play and here's another thing you notice that when the grid size is big this is it's on the fastest speed but you can see this is a lot slower than when the grid is small.
So i i've definitely seen some game of life implementations that go the same speed no matter what so that would be another thing to look at if you're trying to refactor my code. Actually, you can kind of see why it goes so slow if i open up the javascript console. You'll see these items hidden by filters.
If i do default filter and i go to verbose you can see the set interval is taking too long. It's only supposed to take 50 milliseconds if it takes more than 50 milliseconds, it says it's taking too long. If we go to the small grid side and see that, see you're saying it's not it's not showing that at all.
Now if we go to the medium grid size anything over 50 milliseconds is going to say it's taking too long but that's not really bad that's why on default doesn't even show that. So as far as refactoring the code that's something that you can do to try to make this run even faster so that's pretty much it for the game of life.
We've completely finished this you can check the final code on on github if anybody sees any mistakes or if you find a way to improve this game just do a pull request on github. I'm going to have a special folder i'm going to have a special folder in the repository just for improvements so if it's a mistake you can do a pull request to the main code but if it's an improvement you can create a new directory in the improvements folder and do a pull request with your improved file.
Thanks for watching. Don't forget to like, subscribe, and turn on notifications for more coding tutorials and challenges.