Monday | 11 NOV 2024
[ previous ]
[ next ]

Conways Game of Life

Title:
Date: 2022-01-04
Tags:  

I want to try out the wasm stuff I've been seeing every now and then, and I decided that I'll do it rust. I found some good documentation for it and as I read the documentation over, it was pretty clear that it was straightforward to follow and didn't make any assumptions about how much you know besides just knowing enough rust and javascript to be dangerous.

https://rustwasm.github.io/docs/book/

The tutorial implements conway's game of life in rust and creates a wasm package out of it which javascript then calls into. I never wrote a Conway's game of life so I took this chance to go on a side adventure and implement the game of life in javascript first and then get back to the wasm stuff.

https://nivethan.dev/game-of-life/

The game of life has some pretty basic rules which I will outline here but I think it's definitely a fun little thing to implement. I used javascript and the table tag to show the game as that was the quickest way to get something on my screen.

The game of life is a set of rules that you can either do manually or use a computer to run automatically. The first part is that you have a 2D grid that makes up the universe. The universe is made up of cells that can be reference by an x coordinate and a y coordinate. Each cell can be marked as either living or dead. This would be the initial universe. Conway's rules can then be used to figure out what the next iteration of the universe will be.

He outlines that if a living cell has less than 2 living neighbors, then that cell will die to underpopulation. If the cell has exactly 2 or 3 living neighbors, it will live. If the cell has more than 3 living neighbors, then the cell will die to overpopulation.

If a dead cell has exactly 3 living neighbors, then the cell will become alive.

The rules are pretty straightforward and it was fun to manually do a few examples just to get the structure of it. Programming it was also fun. The hardest part was that the grid isn't really infinite and so I needed to make the universe wrap around itself. This came out pretty good as my initial reaction was to do boundary checking in multiple places before I figured out that I only need to do it one place.

This was the quintessential programming excersise that people talk about but I'm not sure why. It's certainly interesting but I'm not sure why it has such a hold on people's imaginations. It could be that these simple rules leading to complex patterns is something that is just really entertaining to others. I'm not sure I quite see it yet but maybe I will. I definitely do want to read more about it and see what other implementations look like. I did do this in the most plain way and I imagine there are much better and more efficient things I could do.

I did learn something while doing this little project, the defer keyword in a script loads the script after rendering the page. I usually move script tags to the end of the body but the defer keyword does the same thing. This was also one of the few times I've found a use for reduce function which was nice. I abuse map and filter but rarely use reduce but it made sense to use reduce in calculating neighbors. I also got to use querySelector which I don't usually as I always have access to jquery. I think querySelector might be fantastic if it had a more jquery like ability to manipulate classes and css. There very well might be a thin wrapper already. I did write my own but I never got around to making it available or using it in real projects. Always something to learn and think about.

Hopefully my next post is actually about rust and wasm!