-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameoflife.js
156 lines (155 loc) · 5.01 KB
/
gameoflife.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function Grid(sizex, sizey) {
this.world = new Array();
this.szx = sizex;
this.szy = sizey;
this.age = null;
this.automaticIntervalID = null;
this.updateTime = 1000;
this.automatic = false;
this.isGridEnabled = true;
this.cellSize = 8;
this.draw();
}
Grid.prototype.at = function at(x,y) {
return this.world[this.idx(x,y)];
}
Grid.prototype.idx = function idx(x,y) {
return ((this.szy + (y % this.szy)) + this.szy)*this.szx + ((this.szx + (x % this.szx)) % this.szx);
}
Grid.prototype.neighborsCount = function neighborsCount(x,y) {
var nct = 0;
nct += (this.isAlive(x, y-1) ? 1 : 0) + (this.isAlive(x+1, y-1) ? 1 : 0) + (this.isAlive(x+1, y) ? 1 : 0) + (this.isAlive(x+1, y+1) ? 1 : 0) + (this.isAlive(x, y+1) ? 1 : 0)
+ (this.isAlive(x-1, y+1) ? 1 : 0) + (this.isAlive(x-1, y) ? 1 : 0) + (this.isAlive(x-1, y-1) ? 1 : 0);
return nct;
}
Grid.prototype.generate = function generate(x,y) {
this.place(1,x,y);
}
Grid.prototype.isAlive = function isAlive(x,y) {
return this.at(x,y)===1;
}
Grid.prototype.place = function place(val, x,y) {
this.world[this.idx(x,y)] = val;
}
Grid.prototype.kill = function kill(x,y) {
this.place(0, x,y);
}
Grid.prototype.isNew = function() {
return this.age === null;
}
Grid.prototype.advance = function advance() {
var nworld = new Array();
var nct = 0;
var alive = false;
for(var y = 0; y < this.szy; y++) {
for(var x = 0; x < this.szx; x++) {
nct = this.neighborsCount(x,y);
alive = this.isAlive(x,y);
if(alive && (nct < 2 || nct > 3)) {
alive = false;
}
else if (nct==3) {
alive = true;
}
nworld[this.idx(x,y)] = (alive) ? 1 : 0 ;
}
}
alive = false;
for(var y = 0; y < this.szy; y++) {
for(var x = 0; x < this.szx; x++) {
alive = this.isAlive(x,y);
if (alive && nworld[this.idx(x,y)]===0) {
this.kill(x,y);
}
if (!alive && nworld[this.idx(x,y)]===1) {
this.generate(x,y);
}
}
}
this.age++;
}
Grid.prototype.draw = function draw(cellSize) {
var canvas = document.getElementById("view");
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 512, 512);
ctx.fillStyle = "cadetblue";
ctx.strokeStyle = '#e1e1e1';
for(var y = 0; y < this.szy; y++) {
for(var x = 0; x < this.szx; x++) {
if(this.isAlive(x,y)) {
ctx.beginPath();
ctx.rect(x*cellSize, y*this.cellSize, this.cellSize, this.cellSize);
ctx.fill();
}
//else ctx.stroke();
}
}
console.log("Grid:" + Boolean(this.isGridEnabled));
if (this.isGridEnabled) {
ctx.strokeStyle = "black";
ctx.lineWidth = .5;
for (var i = 1; i < 512 / this.cellSize; i++) {
ctx.beginPath();
ctx.moveTo(this.cellSize * i, 0);
ctx.lineTo(this.cellSize * i, 512);
ctx.stroke();
}
for (var i = 1; i < 512 / this.cellSize; i++) {
ctx.beginPath();
ctx.moveTo(0, this.cellSize * i);
ctx.lineTo(512, this.cellSize * i);
ctx.stroke();
}
}
}
Grid.prototype.randomize = function randomize() {
var t = 0;
for(var y = 0; y < this.szy; y++) {
for(var x = 0; x < this.szx; x++) {
t = Math.random();
this.place((t > 0.5) ? 0 : 1, x, y);
}
}
this.age++;
}
Grid.prototype.next = function next() {
(this.isNew()) ? this.randomize() : this.advance();
document.getElementById("day").innerHTML = "Day " + this.age;
var canvas = document.getElementById("view");
var ctx = canvas.getContext("2d");
ctx.clearRect(0,0,canvas.width, canvas.height);
this.draw(this.cellSize);
}
Grid.prototype.switchAutomatic = function switchAutomatic() {
this.automatic = !this.automatic;
if (this.automatic) {
this.automaticIntervalID = setInterval("world.next()", this.updateTime);
document.getElementById("switch").innerHTML = "Manual";
document.getElementById("next").disabled = true; //Disable manual controls during automatic
} else {
window.clearInterval(this.automaticIntervalID);
document.getElementById("switch").innerHTML = "Automatic";
document.getElementById("next").disabled = false; //Reenable manual controls
}
}
Grid.prototype.checkOptions = function checkOptions() {
if (this.cellSize != parseInt(document.getElementById("cellSize").value)) {
this.cellSize = parseInt(document.getElementById("cellSize").value);
this.draw(this.cellSize);
}
if (this.isGridEnabled != document.getElementById("grid").checked) {
this.isGridEnabled = document.getElementById("grid").checked;
this.draw(this.cellSize);
}
if (this.automatic) {
var currMSPD = (1 / document.getElementById("dps").value) * 1000; //The current Milliseconds per Day can be found as (1 / Days per Second) times 1000 (conversion factor)
if (this.updateTime != currMSPD) { //If the update time does not reflect the user input, change it
this.updateTime = currMSPD;
window.clearInterval(this.automaticIntervalID);
this.automaticIntervalID = setInterval("world.next()", this.updateTime);
}
}
}
var world = new Grid(200,200);
setInterval("world.checkOptions()", 500);
//setInterval("world.next()",40);