Skip to content

ggpasqualino/workshop-elixir-game-of-life

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Workshop

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.

Preparation

Game of Life

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.

Rules

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

Example

game of life example

Elixir

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.

Let's start coding

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.

Moments later

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.

Static game rendered with emojis

That looks cool! To close, let's make the game play itself usinng a GenServer we have defined.

Dynamic game rendered with emojis

Extra Topics

  • 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?

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages