This is intended as a support material for Coaches to teach workshops that introduce Elixir to people that already knows some programming.
Nevertheless, feel free to use it for self study.
This was inspired by the workshop Fun with Elixir, Automata, and Zombies held by Rob Martin in Lambda Days 2020.
The Game of Life is a zero-player game, which means that it requires no player interacting with it.
The original setup consists of an infinite, 2-dimensional grid of cells,
and each cell can be alive
or dead
. At each step of time (tick
),
the following rules apply to decide what happens with each cell.
- Any live cell with fewer than two live neighbors dies, as if by underpopulation.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by overpopulation.
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Elixir is a dynamic, functional language designed for building scalable and maintainable applications.
Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.
We can start by cloning this repository into our own machines.
git clone https://github.com/ggpasqualino/workshop-elixir-game-of-life game-of-life
cd game-of-life
This repository already contains an skeleton test file defined, which we can run with the following command.
mix test
And this reveals that we have 4 tests defined, from which 3 are skipped and 1 is failing.
1) test Any live cell with fewer than two live neighbors dies, as if by underpopulation. (Features.GameOfLifeTest)
test/features/game_of_life_test.exs:5
** (UndefinedFunctionError) function GameOfLife.new/1 is undefined or private
code: current_generation = GameOfLife.new([{1, 1}])
stacktrace:
(game_of_life 0.1.0) GameOfLife.new([{1, 1}])
test/features/game_of_life_test.exs:9: (test)
Finished in 0.06 seconds
4 tests, 1 failure, 3 skipped
Now, let's open this repository in our favorite code editor and start coding!
Let's start together, then we will continue in small groups of 2 to 3 people.
Now that we can create new generations of the game based on the current one, let's render it to the termnal using the module GameRenderer within this repository.
That looks cool! To close, let's make the game play itself usinng a GenServer we have defined.
- Supervision trees - what happens when our
GameServer
crashes? - Application - how do we start the game without using iex?
- Executables - how can we create an executable that can be run on any system with Erlang installed?