Repository navigation
Expand file tree
/
Copy pathGameOfLife.ts
More file actions
153 lines (129 loc) · 5.38 KB
/
Copy pathGameOfLife.ts
File metadata and controls
153 lines (129 loc) · 5.38 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
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
class GameOfLife {
private canvas: HTMLCanvasElement;
private ctx: CanvasRenderingContext2D;
private board: boolean[][];
private boardWidth: number;
private boardHeight: number;
private directionOffsets: Offset[] = [
new Offset(-1, -1), new Offset(0, -1), new Offset(1, -1),
new Offset(-1, 0), new Offset(1, 0),
new Offset(-1, 1), new Offset(0, +1), new Offset(1, 1),
];
constructor() {
this.canvas = <HTMLCanvasElement>document.getElementById('board');
this.ctx = <CanvasRenderingContext2D>this.canvas.getContext('2d');
this.boardWidth = this.canvas.width;
this.boardHeight = this.canvas.height;
this.board = GameOfLife.initBoard(this.boardWidth, this.boardHeight);
}
public start() {
this.seedBoard();
this.nextFrame();
}
private update() {
var nextBoard: boolean[][] = GameOfLife.initBoard(this.boardWidth, this.boardHeight);
for (var y = 0; y < this.board.length; y++) {
for (var x = 0; x < this.board[y].length; x++) {
var aliveCount = this.getCountOfAliveNeighbors(x, y);
var cellCurrentState: boolean = this.board[y][x];
var cellFate: boolean = false;
if (cellCurrentState === true && (aliveCount == 2 || aliveCount == 3)) {
cellFate = true;
}
else if (cellCurrentState === false && aliveCount == 3) {
cellFate = true;
}
nextBoard[y][x] = cellFate;
}
}
this.board = nextBoard;
}
private getCountOfAliveNeighbors(x: number, y: number): number {
var aliveCount = 0;
for (var i = 0; i < this.directionOffsets.length; i++) {
var offset = this.directionOffsets[i];
var neighborX: number = x + offset.x;
var neighborY: number = y + offset.y;
if (neighborX >= 0 && neighborX < this.boardWidth &&
neighborY >= 0 && neighborY < this.boardHeight &&
this.board[neighborY][neighborX] === true) {
aliveCount++;
}
}
return aliveCount;
}
private render() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
for (var y = 0; y < this.canvas.height; y++) {
for (var x = 0; x < this.canvas.width; x++) {
if (this.board[y][x]) {
this.ctx.fillRect(x, y, 1, 1);
}
}
}
}
private nextFrame() {
this.render();
this.update();
requestAnimationFrame(this.nextFrame.bind(this));
}
private static initBoard(width: number, height: number): boolean[][] {
var state: boolean[][] = [];
for (var y = 0; y < height; y++) {
state.push([]);
for (var x = 0; x < width; x++) {
state[y][x] = false;
}
}
return state;
}
private seedBoard() {
var spaceFiller: String[] = [
"0000000000000000000011100011100000000000000000000",
"0000000000000000000100100010010000000000000000000",
"1111000000000000000000100010000000000000000001111",
"1000100000000000000000100010000000000000000010001",
"1000000001000000000000100010000000000001000000001",
"0100100110010000000000000000000000000100110010010",
"0000001000001000000011100011100000001000001000000",
"0000001000001000000001000001000000001000001000000",
"0000001000001000000001111111000000001000001000000",
"0100100110010011000010000000100001100100110010010",
"1000000001000110000111111111110000110001000000001",
"1000100000000011000000000000000001100000000010001",
"1111000000000001111111111111111111000000000001111",
"0000000000000000101000000000001010000000000000000",
"0000000000000000000111111111110000000000000000000",
"0000000000000000000100000000010000000000000000000",
"0000000000000000000011111111100000000000000000000",
"0000000000000000000000001000000000000000000000000",
"0000000000000000000011100011100000000000000000000",
"0000000000000000000000100010000000000000000000000",
"0000000000000000000000000000000000000000000000000",
"0000000000000000000001110111000000000000000000000",
"0000000000000000000001110111000000000000000000000",
"0000000000000000000010110110100000000000000000000",
"0000000000000000000011100011100000000000000000000",
"0000000000000000000001000001000000000000000000000",
];
var fillerHeight = spaceFiller.length;
var fillerWidth = spaceFiller[0].length;
var startingY = Math.floor((this.boardHeight / 2) - (fillerHeight / 2));
var startingX = Math.floor((this.boardWidth / 2) - (fillerWidth / 2));
for (var y = 0; y < spaceFiller.length; y++) {
for (var x = 0; x < spaceFiller[y].length; x++) {
this.board[startingY + y][startingX + x] = spaceFiller[y][x] == "1";
}
}
}
}
class Offset {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
var gol = new GameOfLife();
gol.start();