A game of life🔬 simulator on an infinite♾️ plane

Overview

game-of-life

A game of life 🔬 simulator on an infinite ♾️ plane

NOTE: This is a toy project! I did this just for fun, not as a packaged product.

About the Author

I'm a bored sophomore in college working on projects to fill the time. If you enjoy my work, consider supporting me by buying me a coffee!

Buy Me A Coffee

What possessed me to write yet another Game of Life 🦠 simulator?

Everyone who has learned to program at this point has probably written a Game of Life simulator; it's a bit of a rite of passage, like a more sophisticated "Hello World!" program. For those of you who haven't seen the Game of Life yet, it's a cellular automata performed on a 2D plane with the following rules:

Rules

  1. Any live cell with fewer than two live neighbours dies, as if by starvation.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overpopulation.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

When making a Game of Life simulator, though, most people just get the minimal, barebones simulation working on a glider or a few other patterns before finishing up the project, and more importantly, we only ever learn how to implement the Game on a statically sized grid. How do the more complicated simulators on the internet support theoretically infinitely sized worlds 🧫 ???

Thinking about this got me curious, so I took a stab at it myself. I think I did an okay job!

How does it work?

The key to the solution is expressed in the wording of the problem itself: to make an implementation agnostic of infinitely sized worlds, the implementation must not depend on the world's size.

So what does that mean in practice? Well, it means that any implementation using statically allocated arrays for the world is already out of the running. But what about dynamically sized arrays? Although, yes, in theory these are possible, they would be incredibly inefficient. Every time a glider moved beyond a corner of the world, the program would have to allocate a new row, and a new column: which would compound as the glider keeps going! Your program would very quickly run out of memory, unless it could deallocate parts of the grid it wasn't using, which would take quite a bit of time on its own.

For some, it might be painfully obvious that the solution is to only store the live cells mapped from their position. So for a glider traversing the entire world for an infinitely long, your program only every stores data for 5 cells at once! This has the added benefit that this makes it very simple to only ever consider cells which are already neighbors of live cells.

The entire algorithm works as follows for each iteration:

  1. For every live cell, if it is overcrowded (>3 neighbors) or starving (<2 neighbors), remove it from the next iteration.
  2. Additionally, for every neighboring position of that cell (regardless of whether it was removed):
  3. If the position has already been considered, skip the following steps. Otherwise, add the position to the set of previously considered positions.
  4. If the position is overcrowded or starving, remove the cell at that position from the next iteration (if it exists).
  5. If the position has exactly three live neighbors, however, insert a cell at that position in the next iteration.
  6. Voila! You have the next iteration of the world!

To see it in action, run some of the examples!

Usage

To run, you must download Rust from here.

# Clone the repo and run from source
git clone https://github.com/adam-mcdaniel/game-of-life
cd game-of-life
cargo run --example random --release
You might also like...
This is an implementation of
This is an implementation of "Game of Life" by Horton Conway in Rust using raylib.

Game-of-Life This is an implementation of "Game of Life" by Horton Conway in Rust using raylib. What is Game of Life? The Game of Life, also known sim

Conway's Game of Life, visualised using Rust and WebAssembly

Game of Life, using Rust and WebAssembly Tutorial Built with 🦀 🕸 by The Rust and WebAssembly Working Group About Conway's Game of Life, which consis

Game Of Life using webgpu, written in Rust
Game Of Life using webgpu, written in Rust

Game of Life using webgpu ✨ Description This is the implementation of the tutorial Your first WebGPU app in Rust where we're using webgpu to implement

Solana Game Server is a decentralized game server running on Solana, designed for game developers

Solana Game Server* is the first decentralized Game Server (aka web3 game server) designed for game devs. (Think web3 SDK for game developers as a ser

Shared cockpit for Microsoft Flight Simulator.
Shared cockpit for Microsoft Flight Simulator.

This project is currently on hiatus until further notice. I'd love to continue improving this project to fulfill my vision of a no hassle, fully featu

Sandbox is a pixel physics simulator inspired by other such like Sandspiel and Noita
Sandbox is a pixel physics simulator inspired by other such like Sandspiel and Noita

Sandbox Sandbox is a pixel physics simulator inspired by other such like Sandspiel and Noita. It's also a precursor for an ongoing game project. Sandb

Life simulation written in rust

Life simulation written in Rust, inspired by this very old screensaver: https://sourceforge.net/projects/primlife/ Demo: https://joelthelion.github.io

Victorem - easy UDP game server and client framework for creating simple 2D and 3D online game prototype in Rust.

Victorem Easy UDP game server and client framework for creating simple 2D and 3D online game prototype in Rust. Example Cargo.toml [dependencies] vict

2d Endless Runner Game made with Bevy Game Engine
2d Endless Runner Game made with Bevy Game Engine

Cute-runner A 2d Endless Runner Game made with Bevy Game Engine. Table of contents Project Infos Usage Screenshots Disclaimer Project Infos Date: Sept

Owner
adam mcdaniel
open sourcerer🔓🏗️, musician🎸🎶, and student🎓
adam mcdaniel
2-player game made with Rust and "ggez" engine, based on "Conway's Game of Life"

fight-for-your-life A 2-player game based on the "Conway's Game of Life", made with Rust and the game engine "ggez". Create shapes on the grid that wi

Petros 3 Oct 25, 2021
Conway's Game of Life implemented for Game Boy Advance in Rust

game-of-life An implementation of a Conway's Game of Life environment on GBA. The ultimate game should have two modes: Edit and Run mode which can be

Shane Snover 1 Feb 16, 2022
A simple Rust and WebAssembly example implementing the Game of Life

rust-wasm-game-of-life rust-wasm-game-of-life is a simple Rust and WebAssembly example implementing the Game of Life based on Rust and WebAssembly boo

Chris Ohk 14 Nov 24, 2021
Game of life implementation written in Rust.

Game of life Game of life implementation written in Rust. Part of my journey in learning Rust. Pattern files The patterns are based on the example pat

Hashem Hashem 2 Nov 17, 2022
Conway's game of life written in Rust.

This is my implementation of Conway's game of life to get my hands dirty with Rust. Rules of the game: Any live cell with two or three live neighbors

null 0 Apr 9, 2022
A simple implementation of Conway's Game of Life using Fully homomorphic Encryption

Game of life using Fully homomorphic encryption A simple implementation of Conway's Game of Life built using Zama's concrete-boolean library. Build Ju

Florent Michel 4 Oct 3, 2022
An implementation of the Game of Life

Lifeee – An implementation of the Game of Life I realized this application to keep learning Rust, discover the front-end library Yew, and because I’m

Sébastien Castiel 58 Nov 23, 2022
A parser for Conway’s Game of Life Lexicon

Lexicon A convenient interface to get patterns from the Lexicon and use them in your implementation of Conway’s Game of Life. Used by Lifeee, my web a

Sébastien Castiel 1 Nov 13, 2021
A first-time implementation of Conway's Game of Life in Rust: Adventure and Commentary

A Commentary on Life This project documents the process and final result of my first-ever attempt at implementing Conway's Game of Life. I'll be using

Avery R. 2 Feb 25, 2022
A Conway's Game Of Life application for the gnome desktop

Game Of Life A simple Conway's game of life simulator for the Gnome desktop Installation The easieast way to install is from Flathub. Using Gnome Buil

Andrea Coronese 7 Dec 30, 2022