-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife2.js
117 lines (108 loc) · 3.07 KB
/
gameoflife2.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
function Board(size, offset) {
this.size = size;
this.offset = offset;
this.grid = this.makeGrid();
this.round = 0;
this.nextRound = this.prepareNextRound(this.grid);
}
Board.prototype.advanceRound = function() {
this.round++;
this.grid = this.nextRound;
this.nextRound = this.prepareNextRound(this.grid);
}
Board.prototype.makeGrid = function(size = this.size,
offset = this.offset) {
var grid = [];
for (var y = 0; y < size+offset*2; y++) {
grid.push([]);
for (var x = 0; x < size+offset*2; x++) {
grid[y][x] = Math.random() > 0.5;
}
}
return grid;
};
Board.prototype.stringState = function(grid = this.grid) {
var string = "";
for (var y = 0+this.offset; y < grid.length-this.offset; y++) {
for (var x = this.offset; x < grid[y].length-this.offset; x++) {
var char = grid[y][x] ? 'O' : 'X';
string = string + char;
}
string = string + '\n';
}
return string;
};
Board.prototype.getCellNeighbors = function(grid, x, y) {
function test(testX, testY) {
if (testY < 0 || testY > grid.length - 1)
return undefined;
else
return grid[testY][testX];
}
var neighbors = Object.create(null);
neighbors.N = test(x, y-1);
neighbors.NE = test(x+1, y-1);
neighbors.E = test(x+1, y);
neighbors.SE = test(x+1, y+1);
neighbors.S = test(x, y+1);
neighbors.SW = test(x-1, y+1);
neighbors.W = test(x-1, y);
neighbors.NW = test(x-1, y-1);
return neighbors;
};
Board.prototype.prepareNextRound = function(grid = this.grid) {
var nextRound = [];
for (var y = 0; y < grid.length; y++) {
nextRound.push([]);
for (var x = 0; x < grid[y].length; x++) {
var neighbors = this.getCellNeighbors(grid, x, y);
nextRound[y][x] = this.resolveCell(grid[y][x], neighbors);
}
}
return nextRound;
};
Board.prototype.resolveCell = function(cellState, neighbors) {
var liveNeighbors = 0;
for (var direction in neighbors) {
if (neighbors[direction] &&
neighbors[direction] != undefined)
liveNeighbors++;
}
if (!cellState && liveNeighbors == 3) {
return true;
} else if (cellState && liveNeighbors > 3) {
return false;
} else if (cellState && liveNeighbors < 2) {
return false;
} else if (cellState && 2 <= liveNeighbors <= 3) {
return true;
}
}
function drawGrid(parent, board) {
for (var y = 0; y < board.grid.length; y++) {
var row = document.createElement("div");
for (var x = 0; x < board.grid[y].length; x++) {
var checkbox = document.createElement("input");
checkbox.row = y;
checkbox.col = x;
checkbox.type = "checkbox";
checkbox.checked = board.grid[y][x];
row.appendChild(checkbox);
}
parent.appendChild(row);
}
parent.addEventListener("change", function(event) {
board.nextRound[event.target.row][event.target.col] = event.target.checked;
});
}
var game = new Board(20, 2);
var grid = document.querySelector("#grid");
var button = document.querySelector("#next");
button.addEventListener("click", function(event) {
game.advanceRound();
while (grid.lastChild) {
grid.removeChild(grid.lastChild);
}
drawGrid(grid, game);
});
drawGrid(grid, game);