 Okay, guys, so today I want to show you how to implement Conway's given life, but before I do that, I want to show you this diagram, because I feel like this is actually pretty useful to understanding how the algorithm works if you don't really understand it whatsoever. So basically what we're going to have is we are going to have one array or buffer. It really depends on how you want to call it. So we're going to have an array or buffer here, and what we're going to do is we are going to go through this two-dimensional array, and we are going to calculate whether or not every single point is alive. So we're going to pass every single point from this array to this is alive function. So pretend this is a function, and then what we're going to do is we are going to save the result to another buffer. So the reason for that is because if we try to save the result back to the original array, it would essentially destroy the rules of the game, the game would not work anymore. So that's why we need to have two arrays. We need to have array one, and then we have array two, or buffer one, buffer two. Imagine that this is going to go through, I didn't animate this, but basically it's going to go through, it's going to test every single one. So let's say it goes here, then it tests, and then it assigns, and then it goes here, it tests, then it assigns, goes here, tests, and then it assigns. So essentially imagine you do this for every single point. Then finally, when all of the points were assigned, when all of the values are calculated, whether or not they're true or false, whether or not they're alive or dead, what we're going to do is we are actually going to swap these two buffers. And the reason we swap these two buffers is because this buffer here is going to have the latest game state. This buffer here is going to be the most up to date game state, while this one is going to be the old game state. So what we're going to do is we are essentially just going to move this in memory, we're just going to move this like this and say, okay, so what we're going to do is we are going to now calculate all of these points using these points. So I'm going to say, okay, go here, test this, and then assign, go here, test this, and then assign. And then once we do that again, we are again going to swap them. So we're going to say, okay, let's swap these two buffers, like so, okay, and then you're going to do the same thing over and over again. So basically, this is the whole algorithm, you're just going to test, select the point, test the point, then assign to the new buffer, and then you're just going to keep doing that over and over again. I think the hardest part of Conway's Game of Life is this is a life function because there are a lot of tests that you can actually do wrong, but I'm going to try to walk you through them to actually show you how to do them correctly. Anyways, let's get started and I'll see you there. This game is going to be 640 by 480. Now that we have that, the next step is we have to actually create the two buffers. The two buffers that in the previous example I showed you are going to hold the state of the game. So every single iteration of the game, these two buffers are going to update to reflect the latest, the latest state of the game. The way I'm going to implement these two buffers is I'm going to use two two-dimensional arrays. So I'm going to have this STD array, okay, great. So next up is we are going to have to actually generate a bunch of random points. So this is going to be a starting point. We are going to have a bunch of random points in this array. And the reason for that is because when we start our game, we want some initial state, right? We want some initial state. And if everything is zero in this array, we're not going to have anything alive. So we need to initialize it with something random. So therefore we're going to choose to arrange page for loop. Or actually I'm going to just explain what this is. Create random points. So we're going to create a whole bunch of random points, we save for auto. And what we're going to do here is we are going to run the generate function. And here we're going to pass in a lambda expression. So what we're going to do here is we're going to return either a one or a zero based on some random condition. Now that that's finished, we can actually begin our main loop. So we've finished writing the setup. So this was all set up. And now finally we can actually begin with a main loop. So we're going to define a main loop like this. We say, while true, this is never going to finish. What we want to do is we want to actually test. Actually, I think the first thing we have to do is we have to define our is a live function. So we're going to define a new function called survive. And this function is going to take an array. And I'm going to pass this array by reference just to save some time. OK, so why do we need this? Why do we need this is live function? If we go to Wikipedia, we can actually read the rules of game of life. So as you can see here, these are the rules for game of life. Any live cell with fewer than two live neighbors dies. Any live cell with two or three live neighbors lives on the next generation. Any live cell with more than three live neighbors dies. Any dead cell with exactly three live neighbors becomes alive. So to explain that, I guess we can go back to the diagram I showed you. OK, so taking a look at this diagram, when they say any live, so if they say any live cells, so for example, let's say we are looking at the cell. We're looking at the cell and this cell has neighbors. Its neighbors are like this. Its neighbors are this, this, this one, this one, this one, this one, this one, this one, and this one. Actually, let me just color this. The different color to make it easier to understand. So let's say we're taking a look at the cell. What we have to do is we have to look at the neighbors around it. So one of the rules states that if any cell has more than, let's say, three live neighbors, it dies. So if we assume that this is alive, this is alive, and this is alive, then on the next iteration, this cell will become dead. So I guess that color could be white. So it's dead. Next iteration of this loop is dead. These are the kind of rules that you have to implement in the game of life. So what I'm doing here is I'm passing in X and Y, and this is the position in this array that we're going to check. So we're going to check relative to this position if its neighbors are alive or dead. So basically the first thing I'm going to do is I'm going to create a new integer, and I'm going to call this alive. And we're going to set it to zero. We're going to assume that there is no neighbor which is alive in this array. Okay, so what we're going to do is we're going to test whether or not the neighbors of the position we passed in are alive or not. And we can do them like this. We can say if X is greater than zero, so the reason that we're testing if X is greater than zero is because if we go back to my other example here, if let's say we're testing this cell here and we want to test this neighbor, well, this neighbor is out of bounds. So we want to say, okay, if we are at least here, if we are at least at index one, then this neighbor is valid. Otherwise there's nothing here, so there's no point in testing anything here. So we're just going to say if X is greater than zero, so if it's not the first index, then what we're going to do is we're going to say, okay, so is this alive? Okay, so what we're going to do is we're going to test if the left neighbor is alive. So the neighbor which is relatively to the left of the point we're testing, and we're going to test if it's alive. So we're going to say if X is greater than zero, so we don't want to go out of bounds. So if we're testing the left neighbor, we want to make sure that we are not indexing negative one. We don't want to index the negative one position in this array. So therefore we're going to say, and game X minus one, Y is equal to one. So we're testing if relative to the position of X minus one, is this alive? Is this neighbor alive? And if it is, we're going to increment a life by one. And just to make sure you understand, what I'm doing here is I'm testing the left. So the left of this position. So let's say we're testing this position here. We want to make sure that we're not testing this. So if we are at this position and we test this, that's not good. We want to index something that's outside of bounds of your array. So we only want to test the left position if we are, let's say at index one, or at index two, or three, or four, right? And in the same manner, you also don't want to test the right side. So if you are at this position, if you're at the end of your array and you're trying to index something beyond the end of your array, that's not good either, so you don't want that. It's same with if you're here, you don't want to index the top like this, or if you're at the bottom, you don't want to index below that. So basically what we have to do is we have to just do a bunch of tests to make sure that we're not indexing out of the bounds of our array. Okay, so getting back on point, I'm just going to write out the other rules. Okay, so we did our test on the left, right, top, and the bottoms. Now what we have to do is we have to do the same tests, but we have to do them on the top, left, top, right, bottom, left, and bottom, right. Relative to, so for example, we basically tested, let's say this, we tested this, this, and this. And finally what we have to do is we have to test the other cases, so we also have to test something like this. So we have to test this, this one, this one, and this one. So we are testing all of the elements, or we're testing all of the points around this point. So if we are testing this point, we have to test everything around it as well. Just gonna make this blue, I guess. So if we're testing this, we have to test this, this, this, this. So basically we have to do how many? We have to do eight, we have to do eight tests about which points are alive. Okay, let's finish it up. So now that we did that, we can actually implement the rules of the game, and the rules of the game are like this. So the first rule is anything that is alive, so let's say alive and fewer than two. So anything that has fewer than two points alive next to it, anything that has fewer than two neighbors is gonna die. So the way we test that is we're gonna say if game X, Y is a two one, and alive is less than two, we're gonna infer false. So the next rule states that if we are alive and we have two or three neighbors, we live. So if alive and two or three, then live. The third rule is if we have more than three neighbors which are alive, we die. Actually, I think I could just return. Yeah, I think that's fine. And the third rule is if we have exactly three neighbors which are alive and the point we're on is dead, that point comes back to life. Actually, I think otherwise we just return false if we miss something. Okay. Okay, so getting back to this Y loop, what we're gonna do here is we are going to check every single point in display since we generate a whole bunch of points in this generate function for display. What we're gonna do is we're gonna calculate whether or not this point is gonna be alive in the next version or the next increment of this game. And we're gonna store the result in the swap array like this. So what we're gonna do is we're gonna do a for loop. And what we're gonna do is when assigned to this swap array, the result of that is a live function. Okay, so basically what we're doing here is we are assigning to this index one if we are alive and zero if we're not. So if this position IK is not alive, we are gonna assign zero. Otherwise we're gonna assign one. Then what we're gonna do is we are going to actually use, I forgot to do this but I need to instantiate a new object and this is gonna be called screen. So what I'm doing here is I'm using this screen object that or I'm using this screen class that I define up here in screen.h. If you need this, I will provide this as a GitHub link. But if you want, you can very easily just create your own draw method which is not that hard. Okay, anyways, okay, so now what we're gonna do is we're gonna draw all of these points to that screen object that I just created. Now what we're gonna do is we're gonna check if this point is live. What we're gonna do is gonna say screen.drawpixel. And finally what we're gonna do is we are going to copy everything from swap to display and finally what we're gonna do is we are actually gonna display to the screen. Now keep in mind that this is very specific to the type of drawing API I'm using. So if you decide not to use this drawing API you may have to do it a different way. Okay, let's actually compile this and see how that turned out. Okay, so there you go, that's Conway's Game of Life. I feel like this is a fairly simple program and if you enjoy this video, thank you and I'll see you later. If you like this video, please like, comment and subscribe.