Coding "Conway's Game of Life" Cellular Automaton in C++/ SFML

Coding "Conway's Game of Life" Cellular Automaton in C++/ SFML

Views:42813|Rating:4.93|View Time:4:28Minutes|Likes:1310|Dislikes:19
Coways Game of life is a very famous cellula automaton, created by John Conway.

In this video, I implement it in C++ and SFML.





Silver Star – Traffic
Doom Original – Level 3


Discord Server:
Support me on Patreon:
Follow me on Twitter:
Follow me on Twich:
Subscribe to the subreddit:

Community Channel:

hello and welcome to a video in this video on our interior which is probably the most famous cellular automaton of all time Conway's Game of Life it's a simulation of the set rules and these rules are as follows so the weld is made out of a grid of cells each cell the simulation is leave her alive represented by a black cell or dead represented by an empty cell in each step of the simulation each of the leading cells and chest hits surrounding neighbors depending on how many living cells are surrounding it bumping one happened to the cell that was checking the neighbors if there is less than two living cells then the cell will die due to under population if there is either T or free surrounding living cells then the server will live on to the next generation and lastly if there is free or more living cells surrounding it then the cell will die due to overpopulation so that is what the living cells do however the dead cells will also count their neighbors and there is only one rule for this if there's exactly free living mavis surrounding the dead cell than the dead cell will turn into a living cell due to a reproduction and that's pretty much it so let's get into creating this now so like collimated the predator and prey simulation the first thing but I want to do here is to get some kind of representation of the cells drawn onto the window when I made the predator and prey simulation I made it a one pixel equal to one cell however this has some problems because sometimes it was a bit hard to see what was going on so if it Conway's Game of Life I've decided to make it so that one cord equals one cell which means a lower resolution which would make it a lot easier to see what was going on for testing I've made it so that each cell just have a random shade of gray for now and this is what it looks like as you can see each of the cells is a lot larger which is going to make it a lot easier to see what is going on and now that I have a drawable representation of the cells it was no time to implement the rules the four rules can actually be implemented I needed to have some kind of representation of whether these cells were alive or dead and for this I simply just went with an enum class which has two types alive all dead the obvious option would have been to go with boolean values but I decided not do that that would have decreased the readability in the code for now for testing purposes I'm just going to make every cell either randomly alive or dead which is going to make from either black or white and this is what it looks like and so now that I did draw a representation of the cells as well as a representation of the you know the cells themselves over there dead or alive it was no time to actually implement the rules for real this time this is simply being done by using a fee for loops through our loop through all the cells and count the alive neighbors surrounding them and then using a few different switch statements I apply the rules that I describe the style of the video to the cell currently being checked and here is the result and as you can see it's pretty terrible something must be going on because all of the cells are basically dying out instantly so I decided that rather than going to the random start point I would instead go for a manual start point this simply means that the class sort of chooses walks cells were alive and desert star unimpressive space far to actually begin simulation you can see me here turning the dead cells into a live cells and one attempting to do is to create something known as a Costigan a ghost of the gun is simply an aberration of cells so as you tell these things colliders where gliders are a group of living cells with just move intimately in n direction and this can be seen in action right here where you can see the gliders moving from the gospel gun down to the bottom right of the simulation now while creating this input thing I've realized I actually as a major logic error in the code and when I realized this Eamonn I could go back to the random arrangement at the start and as you can see it just works absolutely perfectly you can see lots of gliders and other things just moving around the window infinitely so yeah that's pretty much sure that it's Conway's Game of Life I would now like to take a moment to thank my super patreon supporters so yeah Thank You Stanley Morris synthetic Timothy Gibbons and alchemy ik so anyways once again thank you for watching enjoy the rest of the simulation and goodbye

28 Replies to “Coding "Conway's Game of Life" Cellular Automaton in C++/ SFML”

  1. Anton Reinecke

    When I tried coding it, the whole Automaton almost instantly turns into a bunch of selfsustaining formations, that either don't move at all, or have a blinking pixel somewhere.
    Sadly there are no gliders, or even spinners, or anything that moves…

  2. Antricks

    switch statement for the rules in cpp:

    switch (neighbours)
    case 0:field[x][y] = 0; break;
    case 1:field[x][y] = 0; break;
    case 2:break;
    case 3:field[x][y] = 1; break;
    case 4:field[x][y] = 0; break;
    case 5:field[x][y] = 0; break;
    case 6:field[x][y] = 0; break;
    case 7:field[x][y] = 0; break;
    case 8:field[x][y] = 0; break;
    default: break;

  3. John Montgomery

    I assume that each cell is checked in some fixed order. What happens if cells are checked in random order?

  4. Kevin van den Hoek

    I made this in unity, but when I run it with 90*160 the fps averages out at 4. I update all cells before rendering each frame. I used quads as well, and tried to optimize as much as I could code wise. The camera takes most of the performance (rendering quads with the simplest possible shader). How can yours be so blazingly fast with such a huge amount of pixels? Did you pre-render it, or is your c++ code just that much more powerful?

    The camera rendering is 50% of the performance, but just the update alone is also already 40%. So even if rendering would be completely free, I would still only get 8 fps. How can that be possible?

    Great video btw, love your vids!

  5. Sakuya Izayoi

    I once did an interesting challenge in CUDA. Code Conway's game of life in 3D (adjusting the numbers of course). If CUDA is a tech that interests you, I can only suggest you to do it 🙂

  6. Phraggers

    you could probably save a few keystrokes by doing git add . or git add -A instead of –all (i'm quite new to coding in general so feel free to tell me if/why i'm wrong)

  7. Nathan Eaves

    You could have seriously increased the fps by having three matrices instead of two, so that one matrix would represent neighbours making it far more efficient to calculate the number of neighbours that each and every cell has.

  8. Blynd3 Dev

    Since each cell can only be either dead or alive, wouldn't it technically be better to store all the cells in a large array and use bitwise manipulation to read out the states as represented by individual bits in the array? I mean, obviously it's more convoluted, but seeing as the minimum storage size for most machines is 8 bits, using a separate Boolean or enum for each cell would effectively mean increasing the memory allocation size by 8 compared to a single array…

  9. Lion H.

    I also created a Game of Life Simulator half a year ago in Java 😀 it had a map creator and random world generator depending on an entered seed and you could also change the rules your own 🙂 It was a lot of fun to create just because you could see much progress in a small amount of time 😀
    Nice to see you also building such a simulator now! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *