Coding Challenge #85: The Game of Life

Поделиться
HTML-код
  • Опубликовано: 25 дек 2024

Комментарии •

  • @diegoc3749
    @diegoc3749 6 лет назад +347

    Im really glad you do all your code from scratch without internal functions or third party libraries. It really helps understand the logic involved.

    • @NStripleseven
      @NStripleseven 3 года назад +6

      @Stephen Davies Yeah but that’s not the logic-y bit

    • @adrian5b
      @adrian5b 3 года назад +10

      @@NStripleseven I think rendering it's the harder part of the project. The code for the cell logic is quite simple.

    • @NStripleseven
      @NStripleseven 3 года назад +4

      @@adrian5b that’s true, but it’s not the interesting bit algorithm-wise.

  • @dustmodebros
    @dustmodebros 7 лет назад +332

    if you already know the rules of Life, skip to 7:09

  • @TheFeralRobot
    @TheFeralRobot 7 лет назад +120

    This is the best coding channel ever!

  • @darcipeeps
    @darcipeeps 4 года назад +436

    Rest In Peace, John Conway 💜

    • @kavinbharathi
      @kavinbharathi 4 года назад +13

      Wait is he dead...what kind of Dwayne Johnson am I living under...

    • @dreamy6517
      @dreamy6517 4 года назад +9

      2020 suck huh?

    • @Falcrist
      @Falcrist 3 года назад +33

      He was getting pretty reclusive in his old age. Maybe he only had one neighbor.

    • @jaystee1427
      @jaystee1427 3 года назад

      I m here

    • @Brahvim
      @Brahvim 3 года назад +1

      @@kavinbharathi xDDD

  • @zhabiboss
    @zhabiboss 2 года назад +16

    This is one of if not the best examples of how random and interesting an idea can be with such simple rules

  • @clivemitchell3229
    @clivemitchell3229 3 года назад +10

    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.

    • @SylouCool
      @SylouCool 2 года назад +1

      Nice to see the old tricks to save memory and processing!
      Even if i never had to deal with those early computer ;-) i do try those kind of tricks (i've done a bit af asm for game cheat or modding, i think its there i learn to work on bits).
      On mine like you i only consider alive cell but instead of adding 2 to there own value i left shift their dedicated neighbour value (firstly set to 1) so i could use 2 mask of rule (each bit corresponding to an amount of neighbours with the least significant meaning 0) so with the same binary AND between those value i can compute next state no matter what the rules are. and since left shifting and binary AND are really easy for computer it's a bit faster than adding and testing (but apparently not in js... still quite the same ^^)
      Here is my implementation but don't mind the code it's too messy (implementing stuff over stuff over stuff ... etc ... one day i will refactor it ^^) vassilyd.github.io/GameOfLife/ (And by checking that 4 year old code i realised that what i was saying before was only idea i was about to implement... but never done it... right now it still work by incrementing, but do use a binary AND for test)
      I do like the 4,5,6 birth and 2,3,4,5 survive rules, thing turn into linear stable shape (i mean only horizontal, vertical and diagonal line) for the outside with unstable kinda glowing inner + those shape slowly grow and merge until they have only those linear outside line, i call this mode "crystal mode" ^^ (one day i should also implement a selection option of some nice rules like this one)

    • @thedave1771
      @thedave1771 2 месяца назад

      You’ve got me thinking now, I almost want to go write this in batch.
      Which, you know, doesn’t have arrays (and I’m not going to sit there and enter 80x80 variables either 😂 if you wish it has arrays hard enough you can just pretend it does and write the code anyway. I did Minesweeper in batch earlier this year, the underlying concepts are pretty similar (counting neighbours), the trick would be switching between two “boards” but I don’t see why you couldn’t, in Minesweeper I pre-computed the counts because it’s slow doing it while playing.
      I wonder what would run faster, your GWBASIC implementation on a couple MHz computer of the day, or the batch version on a modern GHz machine. I don’t really miss basic, but there are times when I’d consider some good ‘ol GOTO for nostalgia.
      I bet it would be close, because batch is… Yeah.

    • @clivemitchell3229
      @clivemitchell3229 2 месяца назад

      @@thedave1771 Oh to have the time to play with coding again!
      Batch probably allows a text string of 6400 characters which could be manipulated to contain a two-dimensional array.
      I used the method described with a single array as only the eight cells surrounding a living cell need accessing, rather than the eight cells surrounding every cell, making the process several times quicker.

    • @thedave1771
      @thedave1771 2 месяца назад

      @@clivemitchell3229 there’s actually a way to include a variable inside another variable’s name, so if you wish hard enough can have an “array” accessible as %myarray[%%x,%%y]% and it’s a lot faster than pulling arbitrary characters out of a huge string.
      It’s not an array, just loose variables, but if we’re all okay with pretending JavaScript has arrays…
      Setting is easy, reading is harder, and of course you’re writing your own functions for every little thing.
      I just can’t bring myself to like Powershell. I should, if you asked me to describe what I want in a CLI it would look a lot more like Powershell than anything else out there, but the only thing I find enjoyable about writing Powershell is being finished, so I end up in batch.
      I really should stop.

  • @waltercisneros9535
    @waltercisneros9535 3 года назад +6

    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

  • @StanleySeow
    @StanleySeow 6 лет назад +10

    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!!!

  • @yjr4df758
    @yjr4df758 4 года назад +7

    16:35
    you could use different numbers for modes, 0 - old=dead, new=dead
    1 - old=dead, new=alive
    2 - old=alive, new=dead
    3 - old=alive, new=alive

  • @DasHemdchen
    @DasHemdchen 2 года назад +7

    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😂

  • @Manzana1C
    @Manzana1C 2 года назад +5

    Took me a few months of very interrupted work, but i got it working just with the processing reference and my knowledge of classes. It's super inefficient, but it works beautifully.

  • @kenhaley4
    @kenhaley4 7 лет назад +402

    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. :)

    • @TheCodingTrain
      @TheCodingTrain  7 лет назад +145

      Yes, yes, yes, this is true! Thanks for the reminder, I love the idea of actually trying to render it on a torus in 3D!

    • @rachelzimet8310
      @rachelzimet8310 7 лет назад +2

      That makes sense since you can think about a long rectangle being curled into a hollow cylinder, and then the circles on the end curled around to touch each other - and since opposing edges on a rectangle are the same length by definition, this will all match up exactly. The only problem is when it's too square, it'll have to be stretched a lot...

    • @stephaneportron9807
      @stephaneportron9807 6 лет назад +20

      You know what would be even nicer? By changing the direction in which you wrap you could simulate the Game of Life on a Mobius strip, or even on a Klein bottle! *Laughs Mathematically*
      simple.wikipedia.org/wiki/Klein_bottle
      simple.wikipedia.org/wiki/M%C3%B6bius_strip

    • @dandanthedandan7558
      @dandanthedandan7558 6 лет назад

      Rory Slegtenhorst So laggy... lol

    • @Eirikhalvard
      @Eirikhalvard 6 лет назад +11

      If anyone is interested, i made exactly this! I put it up on this website: eirik.tech/Game-of-life/index.html
      Feel free to check out the source code too:
      github.com/eirikhalvard/game-of-life

  • @Calidrelle
    @Calidrelle 7 лет назад +9

    Wonderfull 35:46, the final music note just when the game fall into a stable state !!!! That was written ;)
    Nice job, Dan !!

  • @snapman218
    @snapman218 Год назад +5

    Imagine if our universe was just some alien making a tutorial.

  • @Calz20Videos
    @Calz20Videos 7 лет назад +214

    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.

    • @funkyb6598
      @funkyb6598 7 лет назад +3

      haha. Then I guess the most intelligent cells are those that join together to form a square? That seemed to be the most common "stable" shape. (That is if I did it right : (

    • @Calz20Videos
      @Calz20Videos 7 лет назад +6

      Funky B they have built walls of fortification. We must bow down to our new leaders.

    • @cubemage3336
      @cubemage3336 5 лет назад +3

      Holy mother of god...

  • @dynamicgecko1213
    @dynamicgecko1213 7 лет назад +78

    I have a small coding challenge idea. Imagine a grid of dots, like a really big grid. And when you click on any part of the grid, it creates an illusion of a ripple effect, like you're looking at water from a top-down view. And the illusion will be created by manipulating the size of dots (or circles if you will, since they will get bigger and smaller)

    • @luftigo
      @luftigo 7 лет назад +6

      Post it in his Git Repo for Challenge Ideas

    • @NStripleseven
      @NStripleseven 3 года назад +4

      I know this is old but that would be cool

    • @kasetophono
      @kasetophono 3 года назад +1

      there's a lreally cool ibrary for that actually!! RippleJS

    • @o5-1-formerlycalvinlucien60
      @o5-1-formerlycalvinlucien60 Год назад

      wouldnt this work by just making every cell next to a live cell switch states? It wouldn't be a circular, it would be more like a roughly rotated square increasing in size but it would be a "ripple" effect

  • @Melloboros
    @Melloboros 7 лет назад +1

    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!

  • @JimmyBrayne
    @JimmyBrayne 7 лет назад +76

    I had a whiteboard interview today with this challenge, so coincidental!

    • @Pradeep.Poonia
      @Pradeep.Poonia 5 лет назад +3

      this was asked in interview? for what role?

    • @Mayank-mf7xr
      @Mayank-mf7xr 5 лет назад +2

      bruh what?

    • @nanda_8
      @nanda_8 4 года назад +1

      Why did they ask this??
      Did they ask to code the above thing??

    • @Hasan.T
      @Hasan.T 4 года назад +3

      @@nanda_8 I'm a software developer and I was asked to do this as a coding challenge in one of the stages of applying for the position.

  • @dvendator
    @dvendator 7 лет назад +8

    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

  • @thedave1771
    @thedave1771 2 месяца назад

    I was watching some interviews with John Conway recently and it occurred to me that since he grew to hate the Game of Life, it would be an appropriate way to celebrate his life and legacy. And also it’s just about the only thing he did that I can genuinely understand well enough to explain it, his contributions to higher math are… Impressive.
    I wanted to do it a bit differently though, in that I wanted to support no fixed board size. In particular if you start four gliders going up, down, left and right, it should run until they exceed the 2^32 board. This means they can take 2^31 steps in any direction, although since a step takes at least two generations we’re helpfully back to at least 2^32 generations before encountering the border. I ended up using sparse storage for the cells, so the 4 gliders will take the same RAM (and CPU) whether they’re a few squares apart or are 2^30 away in each direction. and I don’t need to purchase the 2exabytes of storage to maintain the board. The calculate function can impose borders, which turned out to be trivially simple (no need to do bounds-checks while computing, for example), I just don’t calculate neighbours beyond the range and therefore they’re automatically dead. Since you can query and cell, the neighbours function doesn’t need to “not” check beyond borders, it just sees dead cells. Technically this reduces the theoretical board by 1 in every direction but the implementation is so trivially simple.
    Next up, I want to multithread the generations so I can throw all 20 of my CPU cores at it, and down the road it might be interesting to use a GPU for calculations too, but that’s well beyond my abilities right now.
    How to visualize it? That’s easy, you can’t. The viewport can be moved anywhere, and can return whatever size you want, but I am not attempting to render beyond 1 pixel per cell and I’m not aware of any patterns that are designed to work with 4x or more cells comprising of a single pixel, so a true zoom wouldn’t be that useful. Or would it?
    It took about 45 minutes to write the backend, but I’ve spent a bunch of hours here and there playing with the UI as I decided I wanted no flickering, and to use colour to represent the distance from the centre point, and since I’m a console monkey, the interface has been the learning curve. I did write an interactive console version first, then a UI using the same backend. I want to eventually do a iOS and Android version as well, just to toss it in the portfolio.
    The colour effect turned out so nice that I decided to give the option to render the colour gradient over ~1000 steps, so a reasonably large monitor can visualize the effect. I’ve never before made anything I found visually appealing, nor do I usually care about such things, so it’s been an interesting challenge.
    Watching it in JS is amusing and fun, especially since I don’t know any JS at all.

  • @gummansgubbe6225
    @gummansgubbe6225 7 лет назад +43

    Great! I really hope I will remember the modulus 'trick' if I ever need it. Awesome.

  • @nathenmartin349
    @nathenmartin349 3 года назад +1

    @The Coding Train, this video helped me get my current job and I'm extremely grateful!

    • @thourayasboui376
      @thourayasboui376 2 года назад

      Hi Nathen, I have the same challenge, did you implement it in python??

  • @Not-THAT-ChrisPratt
    @Not-THAT-ChrisPratt 3 года назад +2

    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!!!

  • @peopletakepicturesofeachother
    @peopletakepicturesofeachother Год назад

    You are one of the best teachers of my life. Thanks for all the videos and all the inspiration.

  • @letsgocamping88
    @letsgocamping88 7 лет назад +37

    Every now and again Dan will check his super special ceiling notes.

  • @markh.harris9271
    @markh.harris9271 3 года назад +1

    I enjoyed watching; thanks

  • @nathanlewis8217
    @nathanlewis8217 6 лет назад +3

    Thank you so much for this! I was able to take your video tutorial and transfer the knowledge from it into Processing’s python library. I successfully coded the GameOfLife on my own! :)

  • @franchello1105
    @franchello1105 5 лет назад +4

    I remember being assigned this as a project freshman year in my computer science program. I was able to get it working correctly but others did not. The trick was to start with an empty array as your next generation and calculate from the current generartion. We did no animate it, we just displayed 0s for empty cells and 1 for non empty cells.

  • @XoIoRouge
    @XoIoRouge 6 лет назад +1

    Rather than having two 2d arrays, I gave each Cell (I made them objects) an "active" and "expected" boolean. The Active boolean was the "current" 2d array, and the expected boolean was for the next grid. I would loop through every cell, calculating the next state, and set the Expected boolean to match the desired state. The active state didn't change.
    Once I finished this iteration with every cell, I looped through the grid once more to set active to equal expected.

  • @antonnym214
    @antonnym214 6 лет назад +2

    back when the IBM PC was a new computer, I wrote a game of Life for it. I think it was about 50 lines of TurboBASIC code. 2D arrays are easy-peezy in BASIC. You DIM and that's it. I used one array for the current state, and a second array I called WORK, which is where we built the new generation. Then I simply copied the Work array to the current array and displayed. My version was the only one I knew of that could save and load shapes. I made mine wrap also, and I allowed you to chose the size of your playing field as well as delay interval to control the speed. Yours looks good. You're a better man than I. I wouldn't even try that in Javascript.

  • @Jmzz542
    @Jmzz542 2 года назад +2

    23:54 I love how when he's thinking it's like he's being touched by demons 😂😂

  • @riguidixrodriguez1090
    @riguidixrodriguez1090 5 лет назад +11

    I really love the way you work, the charisma, the good vibes all you make it's so good enough to make it interesting and incredible... I really enjoy your videos :-)

  • @officialspaceefrain
    @officialspaceefrain 5 лет назад +13

    I only know python but goddamn this guy can teach javascript

    • @RubyPiec
      @RubyPiec 4 года назад +1

      I only know javascript but goddamn this guy can teach me more

  • @fairydragonbreath9192
    @fairydragonbreath9192 7 лет назад +2

    Thanks for uploading, and showing. This is helping me understand the fun aspects of coding. Trying to land a job, no luck yet.

  • @ViralVerse817
    @ViralVerse817 6 лет назад

    I'm a fairly new CS student in college and this guy is crazy good at this stuff I hope to be as good as you one day great video.

  • @daylen577
    @daylen577 3 года назад +1

    I know this video is really old at this point, but you could very easily just start a new array filled with zeroes, loop through the 'old' grid once, then for each 1 you encounter add one to the 'new' array, if a value reaches above 0 you add it to a separate 'alive' array, if a value reaches above 3 you remove it from the 'alive' array if it exists. Then, once you're done, simply redraw the original array, checking for each cell if it exists in the 'alive' array, and if so setting it to 1, if not setting it to 0.
    This way you only go through the original array once instead of counting neighbours for each cell, then in the drawing step you only check for values in an indexed array (that could be string keys (`x+'-'+y`)) and you basically end up with something that's 4x as fast as counting each neighbour.

  • @fisouz
    @fisouz 7 лет назад +16

    I have no intention of coding in java but i'm watching pretty much all your coding challenges for some insight like that % solution. Classy!

    • @TheCodingTrain
      @TheCodingTrain  7 лет назад +1

      Glad to hear!

    • @nothappyz
      @nothappyz 7 лет назад +7

      it's javascript, not java :P

    • @deniamnet2
      @deniamnet2 6 лет назад +1

      He's not coding in Java here, it's JavaScript. Java and JavaScript are like day and night.

  • @kevnar
    @kevnar 6 лет назад +1

    Edge cases, simplified:
    if ( x > (cols-1)) x -= cols; // 10 becomes 0
    if ( x < 0) x += cols; // -1 becomes 9
    if (y > (rows-1)) y -= rows; // 10 becomes 0
    if (y < 0) y += cols; // -1 becomes 9

  • @eahemming
    @eahemming 2 года назад

    I admire your teaching. Thanks for all the videos you make

  • @mixxer0512
    @mixxer0512 6 лет назад

    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

  • @luosimuershi5881
    @luosimuershi5881 4 года назад

    This is the best solution for leetcode 289 I have seen,thank you !

  • @SpringySpring04
    @SpringySpring04 3 года назад +1

    This is very helpful because i personally use p5js for JavaScript stuff, that's awesome

  • @dhvalden
    @dhvalden 7 лет назад +33

    Dude you're awesome... I'll try to replicate this in python just to learn and for fun :-)

    • @dhvalden
      @dhvalden 7 лет назад +3

      as I promise: github.com/dhvalden/learning_python/blob/master/my_game_of_life.py
      However, I just translated it to Python, but I haven't been successful in adding another features :( part of the learning process I guess

    • @k12i
      @k12i 5 лет назад +5

      @@dhvalden page not found :(

    • @thourayasboui376
      @thourayasboui376 2 года назад

      ​@@dhvalden still you have the python implementation please??

  • @annahri
    @annahri 7 лет назад +1

    I finally made my own on C#!
    Thank you so much! You're the best!

  • @MrMadododo
    @MrMadododo 4 года назад

    Thank you for this! I was confused with the edges and modulo, your explanation cleared it up! Around the 29:47 time mark

    • @thourayasboui376
      @thourayasboui376 2 года назад

      Hii, I didn't get it for edge ... and it doesn't work for me in python !! any help please??

  • @florianbernard2525
    @florianbernard2525 4 года назад +1

    dude you're so awsome, thx a lot for this

  • @aguy2241
    @aguy2241 3 года назад +1

    Got a real mad scientist vibe goin on

  • @cpaulicka
    @cpaulicka 2 года назад +3

    The Game of Life has been my primary interview question (though I also have a few graph/algorithm questions for variety.) It's great because it leads to obvious extensions, such as borderless with wrapping, and opens the discussion of how to make an infinite game of life, which many have difficulty answering. Finally, it's fun to introduce n-dimensional extension, and how to change existing loops to deal with multiple dimensions, and finally, how to make the system parallelizable for truly massive user experience. If they get through all that, they almost always get hired, and do great later.

    • @kolkonut
      @kolkonut Год назад

      That's really interesting. Do you mean infinite over time or over an infinite grid? In Python, I would start with a list of lists, and have add rows/columns whenever a live cell needs to be created outside the original lists, although the size of the list will grow exponentially. Parallelization is also very interesting, maybe dividing the board into slightly overlapping zones, each of which is processed by a separate thread, then stitching the zones together? I'm naively guessing, but the minimum overlap should be around 3 tiles, to make sure no cell at the border is confused.

  • @Krondelo
    @Krondelo 7 лет назад

    YES! I've been wanting this, thank you Dan!

  • @solarpellets
    @solarpellets Год назад

    Whenever I work with grids, I usually end up using a single array from top left to bottom right, and to check relationships, I use (i - gridsize) for up, where i is the number of the gridspace itself in the array, (i + gridsize) for down, (i + 1) for right and (i - 1) for left. Getting edges is a little harder, where if i % gridsize is 0 then you're on the left edge and if if i % size is equal to size - 1 then you're on the right edge, but that doesn't handle having different x and y sized very well. In that case, I guess having two variables, rowSize and columnSize could suffice, though.

  • @markh.harris9271
    @markh.harris9271 3 года назад +5

    rather than fixed arrays, use twin dual circular linked lists of "active" cells; then you have an undefined infinite grid. The "Life" can grow ad hoc forever, and you never have to consider the entire grid... only the cells that are active.
    marcus

    • @dannygjk
      @dannygjk 2 года назад

      I'm not sure what you mean but have you implemented that? It sounds like it doesn't take into consideration the fact that during each cycle of the simulation the generation of the next cycle has to take into consideration that all the cells have to be processed before making changes to any cell.

    • @SylouCool
      @SylouCool 2 года назад

      @@dannygjk Not necessarily, for example i've done it by only processing the alive cell, incrementing each neighbour's neightbourAmount value first then indeed i still process it for all cell, i guess i could also ignore cell with no alive neightbour here, but only in base rules, if you use another rule that specify birth with 0 neighbours then you have to process all cell... It all depend on how far you wanna extend the possibility ;-)
      Anyway indeed a linked list type (with also a link to each neighbour in cell object) could allow an infinite grid, as long as your computer handle it ^^ (but not sure how to handle interactivity, without getting too laggy, since to get a particular position you need to go through the whole list, don't you?)

    • @dannygjk
      @dannygjk 2 года назад

      @@SylouCool So you are processing all cells before you modify?

    • @SylouCool
      @SylouCool 2 года назад

      @@dannygjk Yes cause i wanted a GOL that can work with any rules (including a 0 neighbours rule for birth...). but like i said if you stick to the base one there is no reason to process dead cell not surrounded by at least one alive cell. So you could only process alive cell and their neightbours in the second loop too.
      For Mark idea to work i guess he would need at least an x and y property in his cell object and a pointer to each neightbours + a previous and next pointer. In that list you will only have alive cell and their neighbour, creating new one when needed and eventually destroying those that become isolated (or use a secondary list where all isolated cell goes and not get processed but can be switch back to the active one at any time). And for the rendering, during the process you save minimum and maximum of x and y of cells and there you know the needed size of the grid.
      I'm not saying this is what Mark was talking about but might be close enough ^^
      Anyway i do prefer using 2D array for that (also array can be extended even if it could get messy ^^)

    • @dannygjk
      @dannygjk 2 года назад +1

      @@SylouCool I found that keeping it simple seemed to work fastest for the 'normal' living/dying rules tho I was working with only cells that fit in the display. I would scan for live cells then increment the neighbor count for the surrounding cells in a neighbor count 2D array.

  • @melkiorwiseman5234
    @melkiorwiseman5234 2 года назад

    The starting point for The Game Of Life is always two, two-dimensional arrays. If you make each array two indexes "wider" and "taller" than your display, this simplifies the coding since positions -1 and +1 to the pixel which you are calculating will always exist and so you won't need any "exception processing" to handle the edges of the map.
    You flip the "working" array (which is the one to be displayed) between the two arrays, with the other array being the "target" array. The calculations are done on the "working" array and the results are placed in the "target" array. Once all pixels have been calculated, the arrays are switched and the new "working" array is displayed.
    If done correctly, the array elements along each edge (beyond the displayed range) are never calculated and so always stay "dead."
    Of course, you can always decide to challenge yourself and make the field "wrap around" so that the pixel on the furthest-right of the screen in a particular row is considered to be a neighbour to the furthest-left pixel in the same row, but this requires slightly more complicated logic. Which I now see was actually done in this video, in spite of the fact that by default the game's rules assume that the field does not wrap around.

  • @MathHunter
    @MathHunter Год назад

    "Of course you guys are watching this on a television" famous last words

  • @cookiecrumbler758
    @cookiecrumbler758 11 месяцев назад

    love your energy man ^^

  • @LifEsYouTubeChannel
    @LifEsYouTubeChannel 7 лет назад +1

    Daniel! Thanks for teaching me the moulus trick :) Glad you're well and of course... Welcome back ;)

  • @Jellylamps
    @Jellylamps 2 года назад

    A lot of this code is really similar to the framework i used to build my minesweeper clone in python.
    An idea i had about updating the grid would be to make a 3D array with the extra nested array being there to hold a value which basically makes sure each cell “remembers” whether it must change or not. After setting that value for each cell, you would then check it for each cell, if it’s a 1, change it, if it’s a 0, leave it alone, set it to 0, and then you have your next grid. I have no idea whether this solution is efficient or not but i personally like it

    • @Jellylamps
      @Jellylamps 2 года назад

      Or instead of a 1 or 0 you could make it a boolean of course

  • @Farzriyaz
    @Farzriyaz 2 года назад +1

    5:10
    no number is less than 2 and greater than 3 at the same time
    maybe you can use 'or' instead of 'and'

  • @Alexcav37
    @Alexcav37 2 года назад +1

    thank u for this amazing video

  • @NicoA47
    @NicoA47 7 лет назад

    These coding challenges are awesome.

  • @jamesbarker1994
    @jamesbarker1994 3 года назад +1

    Your awesome dude

  • @softbread1728
    @softbread1728 Год назад

    When you're doing 2D arrays it's WAY better that your "Initial array" is of the rows and inside each row there's the column. Not only is that how it's normally done when doing stuff with Matrices, but also, it's far better to have your outer loop be the Rows (which tends to be less than the columns), and then iterate through each cell in that row.

  • @tekhiun
    @tekhiun 6 лет назад

    You actually don't need two grids, since each cell is either on or off, you can use the first bit to represent the current state and the the second bit to represent the previous (or next state) so basically you fill the grids with 0,1,2 and 3

  • @PBalint817
    @PBalint817 3 года назад +1

    I recently made the game of life in minecraft as a datapack, and I get recommended this.
    *_RUclips knows everything. RUclips sees everything. RUclips is everything._*

  • @niharranjanbhuyan9839
    @niharranjanbhuyan9839 7 лет назад +18

    Nice to see you again

  • @stevema2
    @stevema2 3 года назад +2

    perfect! next video, how to build skynet 🤷‍♂️👍

  • @johnwagner9935
    @johnwagner9935 7 лет назад +6

    Hi Dan, I'd be keen to see how you would tackle this in 3D, and what rules you would pick for the cells, that's version 2 of this coding challenge....

  • @ananiasacts
    @ananiasacts 2 года назад

    It’s a different game if you play it on a torus. Better to implement it as a list of live cells. Each generation then consists of first generating a list of neighbors by incrementing a counter for each of the eight cells around each live cell. Then generating the next list of live cells by examining the state and count of each entry in that list.
    This allows for an arbitrarily large grid, implements the game correctly, and makes it much easier to detect and deal with repeating and traveling patterns.

  • @domjanabi6006
    @domjanabi6006 4 года назад

    for anyone that needs the branchless version for calculating next state:
    (assuming you are using an array of booleans for handling state)
    int counter = sum of all 8 booleans around current[xy];
    current[xy] = previous[xy];
    current[xy] = current[xy] and (counter == 2);
    current[xy] = current[xy] or (counter == 3);
    same behaviour as:
    if(counter == 3)
    {
    current[xy] = true;
    }
    else if(counter != 2)
    {
    current[xy] = false;
    }
    else
    {
    current[xy] = previous[xy];
    }

  • @ManifoldSky
    @ManifoldSky 5 лет назад +1

    Arrays are mathematical constructs, and there most certainly are two dimensional arrays, regardless of their CS implementation.

  • @mehrdadkhorasani6009
    @mehrdadkhorasani6009 8 месяцев назад

    This video is amazing. Thanks you, I learned a lot

  • @BoognishMonsterWeen
    @BoognishMonsterWeen 6 лет назад

    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

  • @nathanc11
    @nathanc11 7 лет назад

    I love you videos. It inspired me to code in JavaScript. Keep it up

  • @RyanDurbin10
    @RyanDurbin10 3 года назад +1

    Another great video 😊

  • @MrBadSmash
    @MrBadSmash 10 месяцев назад

    This is awesome! I have a couple of pretty rudimentary ideas that I want to do. I love the idea of being able to "draw" on it, so I'm going to implement that. I also want to make it pretty! So I'm going to cycle through a rainbow of color values and, for every generation, I'm going to change the fill color for live cells. It'll be a rainbow explosion!

  • @Falcrist
    @Falcrist 3 года назад

    I just wrote this in C#, so it's kind of interesting watching someone else tackle it quickly in a different language.

  • @imadetheuniverse4fun
    @imadetheuniverse4fun 3 года назад +2

    I'm racking my brain trying to figure out where "width" and "height" were defined at 13:49
    at line 16 you have: 'cols = width / resolution'
    But I don't see width defined anywhere else in the code, what am I missing?

    • @diamondsmasher
      @diamondsmasher 11 месяцев назад

      I think it’s the language or environment he’s using, some default variables we don’t see. When he checks for mousePressed(), for instance, he uses mouseX and mouseY values he doesn’t define either.

  • @flyingface
    @flyingface 6 лет назад +1

    you're a great teacher

  • @FlyGamingChannel
    @FlyGamingChannel Год назад

    Please do a video on Lenia. Bert Chan’s work on continuous cellular automata is fascinating.

  • @j.pricey
    @j.pricey 7 лет назад

    Love your videos man! Very informative and helpful👍

  • @kevnar
    @kevnar 4 года назад +2

    Rest in Peace, John Conway. :'-(

  • @jonahtheking8803
    @jonahtheking8803 7 лет назад

    hello i love your videos i watch for 3 years

  • @yannickperret1586
    @yannickperret1586 2 года назад

    A little late! Thanks for the video(s). I played around with the code. I added some features: 1. I added the 'grid'/'next' array switching, because voila! 2. I added an age for cells (back and white) with a color table for display. It allows to have nice effects 3. I added mouseClicked event, which allows to set pointed cell to "reactivate" some parts of the grid. Bye!

  • @pingudaniel
    @pingudaniel 20 часов назад

    35:29 "Modulus Joe" 😂

  • @mobalic8673
    @mobalic8673 3 года назад

    your explanation is awesome

  • @hesterclapp9717
    @hesterclapp9717 2 года назад +1

    I just had the idea of Minesweeper but the mines are living cells and are always changing. After all, both algorithms contain a grid of cells which know how many neighbours they have

  • @0u0ak
    @0u0ak 6 лет назад +1

    Challenge: Create a hexagonal (6 sided) version. Hint: the rules need to be simplified to not check 2 living neighbours, only 3. Bonus points: find a glider (I’ve never found one myself)

  • @imsohigh4520
    @imsohigh4520 2 года назад +3

    I have created many, many, initial conditions, aka rules, I can configure them symmetrical, non symmetrical, horizontal, and diagonal. I also have rules that are infinite. They never shrink, and they never die, they only evolve. Conway’s rule is too destructive, on average, it dies off faster than I can toast some bread in a toaster. There’s probably 50 different rules, I have created.

    • @wlockuz4467
      @wlockuz4467 2 года назад

      Neat, I've always wanted to try it with different types of cells because the vanilla game of life doesn't have any cell diversity,
      Imagine a cell that randomly mutates into stronger version of its self, so it can survive overpopulation and underpopulation better.
      or a cell that has greater influence around it so instead of just influencing its direct neighbours it can influence in lets say a 5x5 grid
      or destructive cell that just annihilates everything around it
      and so on, I believe more diversity in cells can create more complex behaviours compared to having multiple rules with basic cell types, the possibilities are endless really.

    • @imsohigh4520
      @imsohigh4520 2 года назад

      @@wlockuz4467 that would be crazy man! And it’s true it would diversify it.

  • @kieran.stafford
    @kieran.stafford 7 лет назад

    Many thanks Dan and happy new year to you. Really looking forward to your continuation of machine learning.

  • @KingOfLeeYT
    @KingOfLeeYT 4 года назад

    Great Video Man!

  • @bruninhohenrri
    @bruninhohenrri 3 года назад

    Idea: Make a 2 game where the map is based on the Game of Life. The player will have to move through the map and manipulate the game to make the map behave as intended. Like, the player can have a weapon that shoot the food blocks to certain parts of the map, to start a new event, freeze some parts of the map, something like that ;)

  • @shashwat2838
    @shashwat2838 7 лет назад

    Love your videos!!

  • @praetrorian7481
    @praetrorian7481 7 лет назад +24

    I did this cellular automata a while ago! www.openprocessing.org/sketch/437331

    • @Valvex_
      @Valvex_ 7 лет назад

      Really pleasing to play with :D

    • @praetrorian7481
      @praetrorian7481 7 лет назад

      Thx ;)

    • @DarkEssence26
      @DarkEssence26 7 лет назад

      Nice thing, really. But seems like you missed to process left and bottom edges. Anyway, great work

  • @codinginterface4283
    @codinginterface4283 3 года назад

    You are awesome man

  • @GodOfBreadd
    @GodOfBreadd 6 лет назад

    Would be interesting if you could implement more than one option for each of the cells and form little "tribes" to see which ones take over/fight for survival of the planet. I'm no coder myself so I don't think I could ever accomplish this but it's a nice thought and I think if someone ever did do this it would be pretty fun to watch.

  • @kryptoid2568
    @kryptoid2568 3 года назад +2

    20:57 me who is watching this on a smartphone:😐

  • @ducking_fonkey
    @ducking_fonkey 3 года назад +1

    Better to make an array with string indexes like x+" "+y ,but it's only a proposition.

  • @dimanarinull9122
    @dimanarinull9122 3 года назад +1

    well, you could just make a 1D array instead of a 2d one and use that with x * rows + y making an access function if you get confused.
    it's as dumb as this:
    char _string[] = "hello\0world"; //cursed string?
    printf("%s %s",_string,_string+6);
    but it works just fine.

  • @DKSubconscious
    @DKSubconscious 4 года назад

    Was thinking, if you run through each square and it's association with another, you now have a link with the other active square which means you can store information in an object for the connected square. So each square in the array would have an object of { tl: 0, t: 0, tr: 0, ml: 0, mr: 0, bl: 0, b: 0, br: 0 } if a block finds a square at mr (middle right) it can tell the other square's object (ml) is also 1 which means that following squares can skip anything [tl, t, tr, ml] if you get what I mean :-P

  • @FOGSUser
    @FOGSUser 2 года назад +2

    Added if (update == true) {...} at top of rules section of Draw and two functions (below)
    function keyPressed () {
    update = true;
    }
    function mouseClicked () {
    update = false;
    grid[floor (mouseX / resolution)][floor (mouseY / resolution)] = !grid[
    floor (mouseX / resolution)
    ][floor (mouseY / resolution)];
    }
    This let me start and stop cell updates and toggle individual cells between alive and dead.

  • @dumnor
    @dumnor 4 года назад +3

    2:22 so is advancing a generation a rotation or mirroring? (4x5 -> 5x4)