Skip to content

Commit dc5cd07

Browse files
authored
Add game-of-life (#367)
1 parent 1ae9715 commit dc5cd07

File tree

8 files changed

+230
-0
lines changed

8 files changed

+230
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@
145145
"prerequisites": [],
146146
"difficulty": 2
147147
},
148+
{
149+
"slug": "game-of-life",
150+
"name": "Conway's Game of Life",
151+
"uuid": "50824213-832d-466d-ba9c-f722ed3814ac",
152+
"practices": [],
153+
"prerequisites": [],
154+
"difficulty": 2
155+
},
148156
{
149157
"slug": "dnd-character",
150158
"name": "D&D Character",
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.
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"game-of-life.coffee"
8+
],
9+
"test": [
10+
"game-of-life.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class GameOfLife
2+
constructor: (@matrix) ->
3+
4+
tick: () ->
5+
return if not @matrix.length
6+
7+
rows = @matrix.length
8+
cols = @matrix[0].length
9+
newMatrix = JSON.parse(JSON.stringify(@matrix))
10+
11+
for row in [0...rows]
12+
for col in [0...cols]
13+
liveNeighbors = 0
14+
for newRow in [row-1..row+1]
15+
for newCol in [col-1..col+1]
16+
if (newRow == row && newCol == col)
17+
continue
18+
if newRow < 0 || newCol < 0 || newRow >= rows || newCol >= cols
19+
continue
20+
liveNeighbors += @matrix[newRow][newCol]
21+
22+
liveNeighbors
23+
24+
cell = @matrix[row][col]
25+
# Apply the rules
26+
if cell == 1
27+
if liveNeighbors < 2 || liveNeighbors > 3
28+
cell = 0
29+
else if liveNeighbors == 3
30+
cell = 1
31+
newMatrix[row][col] = cell
32+
33+
@matrix = newMatrix
34+
35+
module.exports = GameOfLife
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class GameOfLife
2+
constructor: (matrix) ->
3+
4+
tick: () ->
5+
6+
module.exports = GameOfLife
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
GameOfLife = require './game-of-life'
2+
3+
describe 'Game Of Life', ->
4+
it 'Empty matrix', ->
5+
game = new GameOfLife []
6+
game.tick()
7+
expect(game.matrix).toEqual []
8+
9+
xit 'Live cells with zero live neighbors die', ->
10+
game = new GameOfLife [
11+
[0, 0, 0]
12+
[0, 1, 0]
13+
[0, 0, 0]
14+
]
15+
game.tick()
16+
expect(game.matrix).toEqual [
17+
[0, 0, 0]
18+
[0, 0, 0]
19+
[0, 0, 0]
20+
]
21+
22+
xit 'Live cells with only one live neighbor die', ->
23+
game = new GameOfLife [
24+
[0, 0, 0]
25+
[0, 1, 0]
26+
[0, 1, 0]
27+
]
28+
game.tick()
29+
expect(game.matrix).toEqual [
30+
[0, 0, 0]
31+
[0, 0, 0]
32+
[0, 0, 0]
33+
]
34+
35+
xit 'Live cells with two live neighbors stay alive', ->
36+
game = new GameOfLife [
37+
[1, 0, 1]
38+
[1, 0, 1]
39+
[1, 0, 1]
40+
]
41+
game.tick()
42+
expect(game.matrix).toEqual [
43+
[0, 0, 0]
44+
[1, 0, 1]
45+
[0, 0, 0]
46+
]
47+
48+
xit 'Live cells with three live neighbors stay alive', ->
49+
game = new GameOfLife [
50+
[0, 1, 0]
51+
[1, 0, 0]
52+
[1, 1, 0]
53+
]
54+
game.tick()
55+
expect(game.matrix).toEqual [
56+
[0, 0, 0]
57+
[1, 0, 0]
58+
[1, 1, 0]
59+
]
60+
61+
xit 'Dead cells with three live neighbors become alive', ->
62+
game = new GameOfLife [
63+
[1, 1, 0]
64+
[0, 0, 0]
65+
[1, 0, 0]
66+
]
67+
game.tick()
68+
expect(game.matrix).toEqual [
69+
[0, 0, 0]
70+
[1, 1, 0]
71+
[0, 0, 0]
72+
]
73+
74+
xit 'Live cells with four or more neighbors die', ->
75+
game = new GameOfLife [
76+
[1, 1, 1]
77+
[1, 1, 1]
78+
[1, 1, 1]
79+
]
80+
game.tick()
81+
expect(game.matrix).toEqual [
82+
[1, 0, 1]
83+
[0, 0, 0]
84+
[1, 0, 1]
85+
]
86+
87+
xit 'Bigger matrix', ->
88+
game = new GameOfLife [
89+
[1, 1, 0, 1, 1, 0, 0, 0]
90+
[1, 0, 1, 1, 0, 0, 0, 0]
91+
[1, 1, 1, 0, 0, 1, 1, 1]
92+
[0, 0, 0, 0, 0, 1, 1, 0]
93+
[1, 0, 0, 0, 1, 1, 0, 0]
94+
[1, 1, 0, 0, 0, 1, 1, 1]
95+
[0, 0, 1, 0, 1, 0, 0, 1]
96+
[1, 0, 0, 0, 0, 0, 1, 1]
97+
]
98+
game.tick()
99+
expect(game.matrix).toEqual [
100+
[1, 1, 0, 1, 1, 0, 0, 0]
101+
[0, 0, 0, 0, 0, 1, 1, 0]
102+
[1, 0, 1, 1, 1, 1, 0, 1]
103+
[1, 0, 0, 0, 0, 0, 0, 1]
104+
[1, 1, 0, 0, 1, 0, 0, 1]
105+
[1, 1, 0, 1, 0, 0, 0, 1]
106+
[1, 0, 0, 0, 0, 0, 0, 0]
107+
[0, 0, 0, 0, 0, 0, 1, 1]
108+
]

0 commit comments

Comments
 (0)