Coding Challenge #85: The Game of Life

673,437
0
Published 2017-12-11
In this coding challenge, I attempt to code Conway’s Game of Life cellular automata simulation in JavaScript using the p5.js library. Code: thecodingtrain.com/challenges/85-the-game-of-life

🕹️ p5.js Web Editor Sketch: editor.p5js.org/codingtrain/sketches/UtSMCB1zv

🎥 Next video:    • Coding Challenge #86: Cube Wave by Be...  
🎥 All videos:    • Coding Challenges  

References:
🗄 Conway's Game of Life on Wikipedia: en.wikipedia.org/wiki/Conway%27s_Game_of_Life
📓 Mathematical Games: www.ibiblio.org/lifepatterns/october1970.html

Videos:
🚂 My Video on The Game of Life:    • 7.3: The Game of Life - The Nature of...  
🚂 2D Arrays in JavaScript:    • 9.15: 2D Arrays in JavaScript - p5.js...  
🚂 The Modulo Operator with Golan Levin:    • Guest Tutorial #6: The Modulo Operato...  
🔴 Coding Train Live 108:    • Coding Train Live 108: More OOP with ...  t=8518s

Related Coding Challenges:
🚂 #89 Langton's Ant:    • Coding Challenge #89: Langton's Ant  
🚂 #107 Sandpiles:    • Coding Challenge #107: Sandpiles  
🚂 #102 2D Water Ripple:    • Coding Challenge 102: 2D Water Ripple  

Timestamps:
0:00 Introduction
2:00 Generations
7:18 make2DArray()
17:20 Compute Next Generation
21:19 countNeighbors()
23:14 Deal with Edges
25:44 Implement Rules
30:00 Implement Wraparound with Modulus
36:13 Variations

Editing by Mathieu Blanchette
Animations by Jason Heglund
Music from Epidemic Sound

🚂 Website: thecodingtrain.com/
👾 Share Your Creation! thecodingtrain.com/guides/passenger-showcase-guide
🚩 Suggest Topics: github.com/CodingTrain/Suggestion-Box
💡 GitHub: github.com/CodingTrain
💬 Discord: discord.gg/hPuGy2g
💖 Membership: youtube.com/thecodingtrain/join
🛒 Store: standard.tv/codingtrain
🖋️ Twitter: twitter.com/thecodingtrain
📸 Instagram: www.instagram.com/the.coding.train/

🎥 Coding Challenges:    • Coding Challenges  
🎥 Intro to Programming:    • Start learning here!  

🔗 p5.js: p5js.org/
🔗 p5.js Web Editor: editor.p5js.org/
🔗 Processing: processing.org/

📄 Code of Conduct: github.com/CodingTrain/Code-of-Conduct

This description was auto-generated. If you see a problem, please open an issue: github.com/CodingTrain/thecodingtrain.com/issues/n…

#gameoflife #cellularautomata #modulus #javascript #p5js

All Comments (21)
  • @diegoc3749
    Im really glad you do all your code from scratch without internal functions or third party libraries. It really helps understand the logic involved.
  • @kenhaley4
    Dan, maybe someone already told you this, but I couldn't help noticing... In the live video last Friday you laughed at the suggestion of the Game of Life on a torus. But, ironically, this is exactly what you've done. Think about it... Wrapping left to right is equivalent to making the flat map into a cylinder. And then wrapping top to bottom takes that cylinder and bends it around so it forms a torus! The only thing you didn't do is render it in 3D. Now, that would be a coding challenge. :)
  • @zhabiboss
    This is one of if not the best examples of how random and interesting an idea can be with such simple rules
  • @Calz20Videos
    When the cells just stay in one spot, We know they have gotten too smart, We know that they are building civilization, And we know they are going to war.
  • @StanleySeow
    I really love the “game of life” , it on my Mac screen saver too, really enjoy the way you explain how the code works, checking the rules and then wrap around logic. Pls keep up the great work!!!
  • Thanks for this - nice to see it done in a modern language! I wrote this in BASIC on an 8-bit computer many moons ago, but each iteration took more than a minute, so I rewrote it to use a single 2-D grid. The state was held in the least significant bit, i.e. odd values were alive, evens were dead. I started with only 0 or 1 values, then scanned the array. If a cell was odd, I added 2 to each of the surrounding 8 cells, otherwise I skipped, saving a lot of processing time. Next I scanned the grid and if a cell contained 5, 6 or 7 the cell was alive so I made it 1, otherwise it was dead so I made it 0. This change got the processing time for each iteration down to a few seconds.
  • Perfect for training list, of course there´s easier ways to do it, but there are also people who need these kind of videos, this guy is th Bob Ross of coding
  • @letsgocamping88
    Every now and again Dan will check his super special ceiling notes.
  • Thanks for uploading, and showing. This is helping me understand the fun aspects of coding. Trying to land a job, no luck yet.
  • @Calidrelle
    Wonderfull 35:46, the final music note just when the game fall into a stable state !!!! That was written ;) Nice job, Dan !!
  • @DasHemdchen
    Thank you, Daniel, another instructive and fun video. You‘re such an inspiration to me. Please keep on. I „co-programmed“ Asteroids following your suggestions, now I‘m working on Solitaire, learning the hard way how the loop() function is actually an interrupt, thus it is difficult to expect some variables to be set by functions called AFTER the loop() call😂
  • @Melloboros
    Sooo weird - I just discovered this channel with your old CA videos a few days ago and then here's this new video. My current Introduction to Programming/Java project is to make a tic tac toe game, and that seems so much less daunting after soaking in all of this witchery. lol I'm quite keen on trying to tackle The Game of Life. It's so helpful seeing a professional programmer's process step-by-step like this. Thanks!
  • @dvendator
    I am working on mine.And I was Looking for some resource and then your video poped up in the notification.You always Help.Thanks
  • @12Q46HPRN
    Outstanding! I have been fascinated by the Game Of Life for decades. I've wanted to try my hand at coding it, but always wondered how to get it from one generation to the next. I'll try making each cell an object and see what happens!!!
  • Daniel! Thanks for teaching me the moulus trick :) Glad you're well and of course... Welcome back ;)
  • @mixxer0512
    The video is really helpful for me because this game was a few years ago on the matura exam in Poland so I enjoy watching you code the game. Greetings from Poland, dude :D
  • Love your vids, thank you so much for your service here. Once I heard you started with lingo like myself it all made sense why your methods clicked so well. cheers