-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconwaysGameOfLife.ts
125 lines (112 loc) · 2.77 KB
/
conwaysGameOfLife.ts
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
interface vec2d {
x: number;
y: number;
}
function doIteration(field: string[][]): void {
const toKill: vec2d[] = [];
const toLife: vec2d[] = [];
for (let y = 0; y < field.length; ++y) {
for (let x = 0; x < field[0].length; ++x) {
const neighbours: string[] = [];
// three upper and downer cells
for (let i = -1; i < 2; ++i) {
if (x + i >= 0 && x + i < field[0].length && y - 1 >= 0)
neighbours.push(field[y - 1][x + i]);
if (x + i >= 0 && x + i < field[0].length && y + 1 < field.length)
neighbours.push(field[y + 1][x + i]);
}
// get the two sides
if (x + 1 < field[0].length) neighbours.push(field[y][x + 1]);
if (x - 1 >= 0) neighbours.push(field[y][x - 1]);
const livingNeighbours = neighbours.filter((e) => e === '.').length;
if (field[y][x] === '.') {
// life cell
if (livingNeighbours < 2 || livingNeighbours > 3) toKill.push({x: x, y: y});
} else if (livingNeighbours === 3) toLife.push({x: x, y: y}) ;
}
}
for (const k of toKill) field[k.y][k.x] = "#";
for (const k of toLife) field[k.y][k.x] = ".";
}
function game(
initialState: vec2d[],
iterations: number = 100,
sizeX: number = 10,
sizeY: number = 10,
delay: number = 1000
): void {
let field: string[][] = new Array(sizeY)
.fill('#'.repeat(sizeX))
.map((e) => e.split(''));
for (const state of initialState)
if (
Number.isInteger(state.y) &&
Number.isInteger(state.x) &&
state.y < sizeY &&
state.x < sizeX
)
field[state.y][state.x] = '.';
const int = setInterval(() => {
console.clear();
console.log(field.map((e) => e.join('')).join('\n'));
console.log('iterations to go: %d', iterations);
doIteration(field);
if (--iterations <= 0) clearInterval(int); // break after iterations
}, delay);
}
// stable
const block = [
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 1, y: 0 },
{ x: 1, y: 1 }
];
// stable
const beehive = [
{ x: 1, y: 0 },
{ x: 2, y: 0 },
{ x: 0, y: 1 },
{ x: 3, y: 1 },
{ x: 1, y: 2 },
{ x: 2, y: 2 }
];
// oscillator (cycle 2)
const line = [
{ x: 1, y: 2 },
{ x: 2, y: 2 },
{ x: 3, y: 2 }
];
// oscilator (cycle 15)
const Pentadecathlon = [
{ x: 5, y: 5 },
{ x: 6, y: 5 },
{ x: 7, y: 5 },
{ x: 5, y: 6 },
{ x: 7, y: 6 },
{ x: 5, y: 7 },
{ x: 6, y: 7 },
{ x: 7, y: 7 },
{ x: 5, y: 8 },
{ x: 6, y: 8 },
{ x: 7, y: 8 },
{ x: 5, y: 9 },
{ x: 6, y: 9 },
{ x: 7, y: 9 },
{ x: 5, y: 10 },
{ x: 6, y: 10 },
{ x: 7, y: 10 },
{ x: 5, y: 11 },
{ x: 7, y: 11 },
{ x: 5, y: 12 },
{ x: 6, y: 12 },
{ x: 7, y: 12 }
];
// spaceship
const spaceShip = [
{ x: 1, y: 0 },
{ x: 2, y: 1 },
{ x: 0, y: 2 },
{ x: 1, y: 2 },
{ x: 2, y: 2 }
];
game(line, 100, 15, 20, 500);