-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.js
107 lines (95 loc) · 3.42 KB
/
world.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
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
function World(newSizeCells, newCellSizePixels, newCellsColor) {
'use strict';
var sizeCells = newSizeCells,
cellSizePixels = newCellSizePixels,
ctx,
cells = [sizeCells],
ruleEngine = new RuleEngine(),
cellsColor = newCellsColor;
this.cells = cells;
createWorld();
function createWorld() {
var x,
y,
isAlive;
if (sizeCells < 1 || cellSizePixels < 1) {
throw new Error("unfeasible world");
}
for (x = 0; x < sizeCells; x = x + 1) {
cells[x] = [sizeCells];
for (y = 0; y < sizeCells; y = y + 1) {
isAlive = randomBoolean();
cells[x][y] = new Cell(isAlive);
}
}
}
function randomBoolean() {
return Math.random() < 0.5;
}
this.setNewCellSizePixels = function (newCellSizePixels) {
cellSizePixels = newCellSizePixels;
}
this.paint = function (ctx1) {
var x,
y;
ctx = ctx1;
ctx.canvas.width = sizeCells * cellSizePixels;
ctx.canvas.height = sizeCells * cellSizePixels;
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
for (x = 0; x < sizeCells; x = x + 1) {
for (y = 0; y < sizeCells; y = y + 1) {
paintPoint(this.cells[x][y], x, y);
}
}
};
function paintPoint(cell, x, y) {
if (cell.isAlive) {
ctx.fillStyle = cellsColor;
ctx.fillRect(x * cellSizePixels, y * cellSizePixels, cellSizePixels - 1, cellSizePixels - 1);
}
}
function wrappedValue(value) {
if (value < 0) {
return sizeCells + value;
}
else if (value >= sizeCells){
return value - sizeCells;
}
else {
return value;
}
}
this.getNeighboursPositions = function (x, y) {
var positions = [8];
positions[0] = { x: wrappedValue(x - 1), y: wrappedValue(y - 1) };
positions[1] = { x: wrappedValue(x - 1), y: y };
positions[2] = { x: wrappedValue(x - 1), y: wrappedValue(y + 1)};
positions[3] = { x: x, y: wrappedValue(y + 1)};
positions[4] = { x: wrappedValue(x + 1), y: wrappedValue(y + 1)};
positions[5] = { x: wrappedValue(x + 1), y: y};
positions[6] = { x: wrappedValue(x + 1), y: wrappedValue(y - 1)};
positions[7] = { x: x, y: wrappedValue(y - 1)};
return positions;
}
function getCellsByPositions(positions) {
var gotCells = [positions.length],
i;
for(i = 0; i < positions.length; i = i + 1) {
gotCells[i] = cells[positions[i].x][positions[i].y];
}
return gotCells;
}
this.makeNewGeneration = function(newWorld) {
var x,
y;
for (x = 0; x < sizeCells; x = x + 1) {
for (y = 0; y < sizeCells; y = y + 1) {
var neighboursPositions = this.getNeighboursPositions(x, y),
neighbours = getCellsByPositions(neighboursPositions),
liveNeighboursCount = ruleEngine.getAliveCellsCount(neighbours),
willLive = ruleEngine.willLive(cells[x][y], liveNeighboursCount);
newWorld.cells[x][y] = new Cell(willLive);
}
}
};
}