-
Notifications
You must be signed in to change notification settings - Fork 0
/
schelling.js
59 lines (55 loc) · 1.98 KB
/
schelling.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class SegregationModel {
constructor() {
//0 = NEUMANN NEIGHBOURHOOD, 1 = MOORE NEIGHBOURHOOD, 2 = ?
this.stratArray = [
'A',
'B',
'C',
'empty'
]
this.distributions = [0.3, 0.3, 0.35, 0.05];
this.threshold = 0.5;
this.sliceNeighbours = function(rect) {
let neighbours = undefined;
if (neighbourhoodType === 0) {
neighbours = rect.neighbours.slice(0, 4);
}
else if (neighbourhoodType === 1) {
neighbours = rect.neighbours;
}
return neighbours;
}
this.playGame = function(rect) {
rect.counter = 0;
const neighbours = this.sliceNeighbours(rect);
neighbours.forEach(neighbour => {
if (neighbour.strategy === rect.strategy) {
rect.counter += 1;
}
});
}
this.updateStrategies = function(rect, updateRule) {
const player = rectsArray[Math.floor(Math.random() * rectsArray.length)]
const neighbours = this.sliceNeighbours(player);
if (player.counter/neighbours.length < this.threshold) {
let newSpot = rectsArray[Math.floor(Math.random() * rectsArray.length)];
while (newSpot.strategy !== 'empty') {
newSpot = rectsArray[Math.floor(Math.random() * rectsArray.length)];
}
newSpot.strategy = player.strategy;
newSpot.strategyNew = player.strategy;
player.strategy = 'empty';
player.strategyNew = 'empty';
}
}
this.findEmptySpots = function(arr) {
let emptySpots = [];
rectsArray.forEach(rect => {
if (rect.strategy === 'empty') {
emptySpots.push(rect);
}
});
return emptySpots;
}
}
}