forked from bahmutov/game-of-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
191 lines (175 loc) · 5.35 KB
/
index.js
File metadata and controls
191 lines (175 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Paste this code in DevTools on GitHub profile page,
// like https://github.com/bahmutov
// Or store it as a Code Snippet to play many times
// - https://github.com/bahmutov/code-snippets
// - https://glebbahmutov.com/blog/chrome-dev-tools-code-snippets/
(function gameOfGitHub () {
/*
Rules: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
// sample "normal" implementation https://jsfiddle.net/concannon/sy9py6qa/
const weeks = document.querySelector('.js-calendar-graph-svg')
.querySelectorAll('g g')
console.log('%d weeks', weeks.length)
const textElement = document.querySelector('.js-contribution-graph h2')
const linkElement = document.querySelector('.js-contribution-graph .contrib-footer a')
linkElement.href = 'https://github.com/bahmutov/game-of-github'
linkElement.innerText = 'Game of GitHub'
const rows = 7
const columns = 52
function toDays (rect) {
const count = Number(rect.getAttribute('data-count'))
return {
rect: rect,
fill: rect.getAttribute('fill'),
count: count,
// binary value in the current tick
curr: count > 0 ? 1 : 0,
// value in the next tick
next: 0
}
}
function weekDays (week) {
console.log(week)
const days = week.querySelectorAll('.day')
return Array.from(days).map(toDays)
}
var grid = Array.from(weeks).map(weekDays)
function hideWeek (week) {
week.forEach(function (day) {
day.rect.setAttribute('fill', '#ffffff')
})
}
// drop the last "current" week
const lastWeek = grid.pop()
hideWeek(lastWeek)
function findMinMax (grid) {
var min = Infinity
var max = -Infinity
var k
var j
var count
for (k = 0; k < grid.length; k += 1) {
const week = grid[k]
for (j = 0; j < week.length; j += 1) {
count = week[j].count
if (count < min) {
min = count
}
if (count > max) {
max = count
}
}
}
return {min: min, max: max}
}
console.log(findMinMax(grid))
// for now just transform every cell into binary value
// our grid is 52x7 (52 columns, each with 7 days)
function decision (curr, neighbours) {
if (!curr && neighbours === 3) {
// Any dead cell with exactly three live neighbours becomes a live cell,
// as if by reproduction.
// (gleb): shouldn't it be 2 neighbours?!
return 1
}
if (curr) {
if (neighbours < 2) {
// Any live cell with fewer than two live neighbours dies
return 0
}
if (neighbours > 3) {
// Any live cell with more than three live neighbours dies
return 0
}
// Any live cell with two or three live neighbours lives
return 1
}
return 0
}
function computeNext (grid) {
var k
var j
var total
var curr
// do not forget to wrap around
for (k = 0; k < columns; k += 1) {
const prevColumn = k === 0 ? columns - 1 : k - 1
const nextColumn = k === columns - 1 ? 0 : k + 1
for (j = 0; j < rows; j += 1) {
const prevRow = j === 0 ? rows - 1 : j - 1
const nextRow = j === rows - 1 ? 0 : j + 1
total = 0
// prev column
total += grid[prevColumn][prevRow].curr
total += grid[prevColumn][j].curr
total += grid[prevColumn][nextRow].curr
// this column
total += grid[k][prevRow].curr
total += grid[k][nextRow].curr
// right column
total += grid[nextColumn][prevRow].curr
total += grid[nextColumn][j].curr
total += grid[nextColumn][nextRow].curr
grid[k][j].neighbours = total
curr = grid[k][j].curr
grid[k][j].next = decision(curr, total)
}
}
}
function render (grid) {
const deadFill = '#eeeeee'
const aliveFill = '#d6e685'
var k
var j
for (k = 0; k < columns; k += 1) {
for (j = 0; j < rows; j += 1) {
const day = grid[k][j]
if (day.curr) {
const fill = day.fill === deadFill ? aliveFill : day.fill
day.rect.setAttribute('fill', fill)
} else {
day.rect.setAttribute('fill', deadFill)
}
}
}
}
function moveCounts (grid) {
var changed = false
var k
var j
for (k = 0; k < columns; k += 1) {
for (j = 0; j < rows; j += 1) {
const day = grid[k][j]
if (day.curr !== day.next) {
changed = true
}
day.curr = day.next
}
}
return changed
}
var generation = 0
function tick () {
const delay = 250
computeNext(grid)
render(grid)
generation += 1
const movement = moveCounts(grid)
// TODO detect simple oscillators
var msg
if (!movement) {
msg = 'Game of GitHub lost after ' + generation + ' generations 😞'
textElement.innerText = msg
} else {
msg = '' + generation + ' generations'
textElement.innerText = msg
setTimeout(tick, delay)
}
}
tick()
}())