Skip to content

Commit 2a3c610

Browse files
authored
Add game-of-life (#152)
1 parent 4476d22 commit 2a3c610

10 files changed

Lines changed: 250 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,14 @@
682682
"prerequisites": [],
683683
"difficulty": 5
684684
},
685+
{
686+
"slug": "game-of-life",
687+
"name": "Conway's Game of Life",
688+
"uuid": "a12cd1e2-4108-40ad-aa72-80706d1eeb30",
689+
"practices": [],
690+
"prerequisites": [],
691+
"difficulty": 5
692+
},
685693
{
686694
"slug": "crypto-square",
687695
"name": "Crypto Square",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
After each generation, the cells interact with their eight neighbors, which are cells adjacent horizontally, vertically, or diagonally.
4+
5+
The following rules are applied to each cell:
6+
7+
- Any live cell with two or three live neighbors lives on.
8+
- Any dead cell with exactly three live neighbors becomes a live cell.
9+
- All other cells die or stay dead.
10+
11+
Given a matrix of 1s and 0s (corresponding to live and dead cells), apply the rules to each cell, and return the next generation.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Introduction
2+
3+
[Conway's Game of Life][game-of-life] is a fascinating cellular automaton created by the British mathematician John Horton Conway in 1970.
4+
5+
The game consists of a two-dimensional grid of cells that can either be "alive" or "dead."
6+
7+
After each generation, the cells interact with their eight neighbors via a set of rules, which define the new generation.
8+
9+
[game-of-life]: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"game_of_life.moon"
8+
],
9+
"test": [
10+
"game_of_life_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Implement Conway's Game of Life.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Deltas = {
2+
{-1, -1}, {-1, 0}, {-1, 1},
3+
{0, -1}, {0, 1},
4+
{1, -1}, {1, 0}, {1, 1}
5+
}
6+
7+
tick = (matrix) ->
8+
height = #matrix
9+
return {} if height == 0
10+
width = #matrix[1]
11+
12+
count_neighbours = (x, y) ->
13+
count = 0
14+
for d in *Deltas
15+
dx, dy = x + d[1], y + d[2]
16+
if 1 <= dx and dx <= height and 1 <= dy and dy <= width
17+
count += matrix[dx][dy]
18+
count
19+
20+
result = [{} for _ = 1, height]
21+
for r = 1, height
22+
for c = 1, width
23+
result[r][c] = switch count_neighbours r, c
24+
when 3 then 1
25+
when 2 then matrix[r][c]
26+
else 0
27+
result
28+
29+
30+
{ :tick }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import int_lists, word_list from require 'test_helpers'
2+
3+
{
4+
module_name: 'GameOfLife',
5+
6+
generate_test: (case, level) ->
7+
lines = {
8+
"matrix = #{int_lists case.input.matrix, level}"
9+
"result = GameOfLife.#{case.property} matrix"
10+
"expected = #{int_lists case.expected, level}",
11+
"assert.are.same expected, result"
12+
}
13+
table.concat [indent line, level for line in *lines], '\n'
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ae86ea7d-bd07-4357-90b3-ac7d256bd5c5]
13+
description = "empty matrix"
14+
15+
[4ea5ccb7-7b73-4281-954a-bed1b0f139a5]
16+
description = "live cells with zero live neighbors die"
17+
18+
[df245adc-14ff-4f9c-b2ae-f465ef5321b2]
19+
description = "live cells with only one live neighbor die"
20+
21+
[2a713b56-283c-48c8-adae-1d21306c80ae]
22+
description = "live cells with two live neighbors stay alive"
23+
24+
[86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae]
25+
description = "live cells with three live neighbors stay alive"
26+
27+
[015f60ac-39d8-4c6c-8328-57f334fc9f89]
28+
description = "dead cells with three live neighbors become alive"
29+
30+
[2ee69c00-9d41-4b8b-89da-5832e735ccf1]
31+
description = "live cells with four or more neighbors die"
32+
33+
[a79b42be-ed6c-4e27-9206-43da08697ef6]
34+
description = "bigger matrix"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
tick: (matrix) ->
3+
error 'Implement me'
4+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
GameOfLife = require 'game_of_life'
2+
3+
describe 'game-of-life', ->
4+
it 'empty matrix', ->
5+
matrix = {}
6+
result = GameOfLife.tick matrix
7+
expected = {}
8+
assert.are.same expected, result
9+
10+
pending 'live cells with zero live neighbors die', ->
11+
matrix = {
12+
{0, 0, 0},
13+
{0, 1, 0},
14+
{0, 0, 0},
15+
}
16+
result = GameOfLife.tick matrix
17+
expected = {
18+
{0, 0, 0},
19+
{0, 0, 0},
20+
{0, 0, 0},
21+
}
22+
assert.are.same expected, result
23+
24+
pending 'live cells with only one live neighbor die', ->
25+
matrix = {
26+
{0, 0, 0},
27+
{0, 1, 0},
28+
{0, 1, 0},
29+
}
30+
result = GameOfLife.tick matrix
31+
expected = {
32+
{0, 0, 0},
33+
{0, 0, 0},
34+
{0, 0, 0},
35+
}
36+
assert.are.same expected, result
37+
38+
pending 'live cells with two live neighbors stay alive', ->
39+
matrix = {
40+
{1, 0, 1},
41+
{1, 0, 1},
42+
{1, 0, 1},
43+
}
44+
result = GameOfLife.tick matrix
45+
expected = {
46+
{0, 0, 0},
47+
{1, 0, 1},
48+
{0, 0, 0},
49+
}
50+
assert.are.same expected, result
51+
52+
pending 'live cells with three live neighbors stay alive', ->
53+
matrix = {
54+
{0, 1, 0},
55+
{1, 0, 0},
56+
{1, 1, 0},
57+
}
58+
result = GameOfLife.tick matrix
59+
expected = {
60+
{0, 0, 0},
61+
{1, 0, 0},
62+
{1, 1, 0},
63+
}
64+
assert.are.same expected, result
65+
66+
pending 'dead cells with three live neighbors become alive', ->
67+
matrix = {
68+
{1, 1, 0},
69+
{0, 0, 0},
70+
{1, 0, 0},
71+
}
72+
result = GameOfLife.tick matrix
73+
expected = {
74+
{0, 0, 0},
75+
{1, 1, 0},
76+
{0, 0, 0},
77+
}
78+
assert.are.same expected, result
79+
80+
pending 'live cells with four or more neighbors die', ->
81+
matrix = {
82+
{1, 1, 1},
83+
{1, 1, 1},
84+
{1, 1, 1},
85+
}
86+
result = GameOfLife.tick matrix
87+
expected = {
88+
{1, 0, 1},
89+
{0, 0, 0},
90+
{1, 0, 1},
91+
}
92+
assert.are.same expected, result
93+
94+
pending 'bigger matrix', ->
95+
matrix = {
96+
{1, 1, 0, 1, 1, 0, 0, 0},
97+
{1, 0, 1, 1, 0, 0, 0, 0},
98+
{1, 1, 1, 0, 0, 1, 1, 1},
99+
{0, 0, 0, 0, 0, 1, 1, 0},
100+
{1, 0, 0, 0, 1, 1, 0, 0},
101+
{1, 1, 0, 0, 0, 1, 1, 1},
102+
{0, 0, 1, 0, 1, 0, 0, 1},
103+
{1, 0, 0, 0, 0, 0, 1, 1},
104+
}
105+
result = GameOfLife.tick matrix
106+
expected = {
107+
{1, 1, 0, 1, 1, 0, 0, 0},
108+
{0, 0, 0, 0, 0, 1, 1, 0},
109+
{1, 0, 1, 1, 1, 1, 0, 1},
110+
{1, 0, 0, 0, 0, 0, 0, 1},
111+
{1, 1, 0, 0, 1, 0, 0, 1},
112+
{1, 1, 0, 1, 0, 0, 0, 1},
113+
{1, 0, 0, 0, 0, 0, 0, 0},
114+
{0, 0, 0, 0, 0, 0, 1, 1},
115+
}
116+
assert.are.same expected, result

0 commit comments

Comments
 (0)