Skip to content

Commit 381bb40

Browse files
Add Game of Life exercise (#2281)
Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com>
1 parent e40d792 commit 381bb40

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
{
2+
"exercise": "game-of-life",
3+
"cases": [
4+
{
5+
"uuid": "ae86ea7d-bd07-4357-90b3-ac7d256bd5c5",
6+
"description": "empty matrix",
7+
"property": "tick",
8+
"input": {
9+
"matrix": []
10+
},
11+
"expected": []
12+
},
13+
{
14+
"uuid": "4ea5ccb7-7b73-4281-954a-bed1b0f139a5",
15+
"description": "live cells with zero live neighbors die",
16+
"property": "tick",
17+
"input": {
18+
"matrix": [
19+
[0, 0, 0],
20+
[0, 1, 0],
21+
[0, 0, 0]
22+
]
23+
},
24+
"expected": [
25+
[0, 0, 0],
26+
[0, 0, 0],
27+
[0, 0, 0]
28+
]
29+
},
30+
{
31+
"uuid": "df245adc-14ff-4f9c-b2ae-f465ef5321b2",
32+
"description": "live cells with only one live neighbor die",
33+
"property": "tick",
34+
"input": {
35+
"matrix": [
36+
[0, 0, 0],
37+
[0, 1, 0],
38+
[0, 1, 0]
39+
]
40+
},
41+
"expected": [
42+
[0, 0, 0],
43+
[0, 0, 0],
44+
[0, 0, 0]
45+
]
46+
},
47+
{
48+
"uuid": "2a713b56-283c-48c8-adae-1d21306c80ae",
49+
"description": "live cells with two live neighbors stay alive",
50+
"property": "tick",
51+
"input": {
52+
"matrix": [
53+
[1, 0, 1],
54+
[1, 0, 1],
55+
[1, 0, 1]
56+
]
57+
},
58+
"expected": [
59+
[0, 0, 0],
60+
[1, 0, 1],
61+
[0, 0, 0]
62+
]
63+
},
64+
{
65+
"uuid": "86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae",
66+
"description": "live cells with three live neighbors stay alive",
67+
"property": "tick",
68+
"input": {
69+
"matrix": [
70+
[0, 1, 0],
71+
[1, 0, 0],
72+
[1, 1, 0]
73+
]
74+
},
75+
"expected": [
76+
[0, 0, 0],
77+
[1, 0, 0],
78+
[1, 1, 0]
79+
]
80+
},
81+
{
82+
"uuid": "015f60ac-39d8-4c6c-8328-57f334fc9f89",
83+
"description": "dead cells with three live neighbors become alive",
84+
"property": "tick",
85+
"input": {
86+
"matrix": [
87+
[1, 1, 0],
88+
[0, 0, 0],
89+
[1, 0, 0]
90+
]
91+
},
92+
"expected": [
93+
[0, 0, 0],
94+
[1, 1, 0],
95+
[0, 0, 0]
96+
]
97+
},
98+
{
99+
"uuid": "2ee69c00-9d41-4b8b-89da-5832e735ccf1",
100+
"description": "live cells with four or more neighbors die",
101+
"property": "tick",
102+
"input": {
103+
"matrix": [
104+
[1, 1, 1],
105+
[1, 1, 1],
106+
[1, 1, 1]
107+
]
108+
},
109+
"expected": [
110+
[1, 0, 1],
111+
[0, 0, 0],
112+
[1, 0, 1]
113+
]
114+
},
115+
{
116+
"uuid": "a79b42be-ed6c-4e27-9206-43da08697ef6",
117+
"description": "bigger matrix",
118+
"property": "tick",
119+
"input": {
120+
"matrix": [
121+
[1, 1, 0, 1, 1, 0, 0, 0],
122+
[1, 0, 1, 1, 0, 0, 0, 0],
123+
[1, 1, 1, 0, 0, 1, 1, 1],
124+
[0, 0, 0, 0, 0, 1, 1, 0],
125+
[1, 0, 0, 0, 1, 1, 0, 0],
126+
[1, 1, 0, 0, 0, 1, 1, 1],
127+
[0, 0, 1, 0, 1, 0, 0, 1],
128+
[1, 0, 0, 0, 0, 0, 1, 1]
129+
]
130+
},
131+
"expected": [
132+
[1, 1, 0, 1, 1, 0, 0, 0],
133+
[0, 0, 0, 0, 0, 1, 1, 0],
134+
[1, 0, 1, 1, 1, 1, 0, 1],
135+
[1, 0, 0, 0, 0, 0, 0, 1],
136+
[1, 1, 0, 0, 1, 0, 0, 1],
137+
[1, 1, 0, 1, 0, 0, 0, 1],
138+
[1, 0, 0, 0, 0, 0, 0, 0],
139+
[0, 0, 0, 0, 0, 0, 1, 1]
140+
]
141+
}
142+
]
143+
}
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

exercises/game-of-life/metadata.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
title = "Conway's Game of Life"
2+
blurb = "Implement Conway's Game of Life."
3+
source = "Wikipedia"
4+
source_url = "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"

0 commit comments

Comments
 (0)