-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLifeModel.js
41 lines (33 loc) · 1.1 KB
/
LifeModel.js
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
import Model from 'https://code.agentscript.org/src/Model.js'
import World from 'https://code.agentscript.org/src/World.js'
import * as util from 'https://code.agentscript.org/src/utils.js'
export default class LifeModel extends Model {
population = 10 // number of turtles
initialDensity = 30 // percent of active cells
// ======================
constructor(worldOptions = World.defaultOptions(50)) {
super(worldOptions)
}
setup() {
this.patches.ask(p => {
p.living = util.randomFloat(100) < this.initialDensity
p.liveNeighbors = 0
})
}
get density() {
const living = this.patches.with(p => p.living).length
return living / this.patches.length
}
step() {
//
this.patches.ask(p => {
p.liveNeighbors = p.neighbors.with(n => n.living).length
})
this.patches.ask(p => {
if (p.liveNeighbors === 3) p.living = true
else if (p.liveNeighbors !== 2) p.living = false
})
// console.log(this.density)
}
}
// export default LifeModel