-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.js
More file actions
74 lines (61 loc) · 2.39 KB
/
Copy pathservices.js
File metadata and controls
74 lines (61 loc) · 2.39 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
angular.module('gameoflife.services', [])
.factory('Game', function() {
return {
seed:function(coordinates, patterns) {
if ('object' != typeof coordinates || 'object' != typeof patterns) return []
var newCells = []
var howMany = coordinates.length
var makeCell = function(coordinate) {
var c = null
try {
c = new Cell(coordinate.x, coordinate.y)
}
catch(CellException) {
console.error(CellException.message)
c = null
}
return c
}
// Seed with the coordinates in the data entry table that don't have a pattern specified.
for (var i = 0, j = howMany; i < j; ++i) {
var c = makeCell(coordinates[i])
if (null != c) newCells[i] = c
} // for: i
// Seed with any patterns chosen, each located at the specified coordinate.
for (var p in patterns)
for (var c in patterns[p].cells)
newCells.push(patterns[p].cells[c])
return newCells
}, // seed
nextGeneration:function(all) {
var nextGen = [] // births + deaths
var numFound = -1
// each cell (dead or alive) gets a list of its live neighbors.
for (var a = 0, aMax = all.length; a < aMax; ++a) {
try { numFound = all[a].findLiveNeighbors(all) }
catch(CellException) { console.error(CellException.message) }
} // for: a
// obtain a list of survivals, births and deaths.
for (var a2 = 0, aMax = all.length; a2 < aMax; ++a2) {
for (var t = 0, tMax = transitions.length; t < tMax; ++t) {
var afterTransition = transitions[t](all[a2])
if (null != afterTransition) {
nextGen.push(afterTransition)
break // state change, continue to next cell
}
} // for: t
} // for: a2
return nextGen
} // nextGeneration
} // return
})
.factory('Plotter', function() {
return {
rasterToLogical:function(r) {
return Math.ceiling(r / PlayField.cellSize)
},
logicalToRaster:function(l) {
return l * PlayField.cellSize
}
}
})