-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLifeModel.js
More file actions
29 lines (24 loc) · 817 Bytes
/
LifeModel.js
File metadata and controls
29 lines (24 loc) · 817 Bytes
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
import Model from 'https://agentscript.org/src/Model.js'
import World from 'https://agentscript.org/src/World.js'
import * as util from 'https://agentscript.org/src/utils.js'
export default class LifeModel extends Model {
initialDensity = 20 // percent of active cells
// ======================
constructor(worldOptions = World.defaultOptions(50)) {
super(worldOptions)
}
setup() {
this.patches.ask(p => {
p.living = util.randomFloat(100) < this.initialDensity
})
}
step() {
this.patches.ask(p => {
p.liveNeighbors = p.neighbors.count(n => n.living)
})
this.patches.ask(p => {
if (p.liveNeighbors === 3) p.living = true
else if (p.liveNeighbors !== 2) p.living = false
})
}
}