Skip to content

Commit 0fe8f61

Browse files
authored
Merge pull request #1752 from legowerewolf/master
Added Game of Life in JavaScript
2 parents 88f9363 + b0cbab0 commit 0fe8f61

File tree

2 files changed

+98
-28
lines changed

2 files changed

+98
-28
lines changed

archive/j/javascript/README.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,40 @@ Welcome to Sample Programs in JavaScript!
44

55
## Sample Programs
66

7-
- [Baklava in JavaScript][8]
8-
- Solution borrowed from @toturkmen via the [baklava repo][1]
9-
- [Bubblesort in Javascript][18]
10-
- [Capitalize in JavaScript][12]
11-
- Even Odd in JavaScript
12-
- [Export in JavaScript][13]
13-
- [Factorial in JavaScript][15]
14-
- [Fibonacci in JavaScript][9]
15-
- File IO in JavaScript
16-
- [Fizz Buzz in JavaScript][4]
17-
- [Hello World in JavaScript][2]
18-
- [Insertion sort in JavaScript][16]
19-
- [Import in JavaScript][13]
20-
- [Prime Number in JavaScript][14]
21-
- [Reverse a String in JavaScript (No Emoji Support)][3]
22-
- [Roman Numeral Conversion in JavaScript][17]
23-
- [Convex Hull in Javascript][18]
24-
- [Selection Sort in JavaSciprt][19]
25-
- [Quick Sort in JavaScript][20]
26-
- [Rotate by 13 in JavaScript][21]
27-
7+
- [Baklava in JavaScript][8]
8+
- Solution borrowed from @toturkmen via the [baklava repo][1]
9+
- [Bubblesort in Javascript][18]
10+
- [Capitalize in JavaScript][12]
11+
- Even Odd in JavaScript
12+
- [Export in JavaScript][13]
13+
- [Factorial in JavaScript][15]
14+
- [Fibonacci in JavaScript][9]
15+
- File IO in JavaScript
16+
- [Fizz Buzz in JavaScript][4]
17+
- [Hello World in JavaScript][2]
18+
- [Insertion sort in JavaScript][16]
19+
- [Import in JavaScript][13]
20+
- [Prime Number in JavaScript][14]
21+
- [Reverse a String in JavaScript (No Emoji Support)][3]
22+
- [Roman Numeral Conversion in JavaScript][17]
23+
- [Convex Hull in Javascript][18]
24+
- [Selection Sort in JavaSciprt][19]
25+
- [Quick Sort in JavaScript][20]
26+
- [Rotate by 13 in JavaScript][21]
27+
- [Game of Life in JavaScript][22]
2828

2929
## Fun Facts
3030

31-
- Debut: 1995
32-
- Typing: Dynamic
31+
- Debut: 1995
32+
- Typing: Dynamic
3333

3434
## References
3535

36-
- [JavaScript Wiki][5]
37-
- [JavaScript Docs][6]
38-
- [Online JavaScript Editor][7]
39-
- [Web Standards/Documentation][10]
40-
- [JavaScript beginner tutorial][11]
36+
- [JavaScript Wiki][5]
37+
- [JavaScript Docs][6]
38+
- [Online JavaScript Editor][7]
39+
- [Web Standards/Documentation][10]
40+
- [JavaScript beginner tutorial][11]
4141

4242
[1]: https://github.com/toturkmen/baklava
4343
[2]: https://therenegadecoder.com/code/hello-world-in-javascript/
@@ -60,3 +60,4 @@ Welcome to Sample Programs in JavaScript!
6060
[19]: https://github.com/TheRenegadeCoder/sample-programs/issues/1380
6161
[20]: https://github.com/TheRenegadeCoder/sample-programs/issues/1649
6262
[20]: https://github.com/TheRenegadeCoder/sample-programs/issues/1379
63+
[22]: https://github.com/TheRenegadeCoder/sample-programs/issues/1377
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
main();
2+
3+
async function main() {
4+
let parameters = {
5+
dimension: 20,
6+
fps: 4,
7+
frameCount: 60,
8+
spawnRate: 0.3,
9+
};
10+
11+
for (let i = 0; i < process.argv.length - 1; i++) {
12+
if (process.argv[i] == "--dimension") parameters.dimension = process.argv[++i];
13+
else if (process.argv[i] == "--fps") parameters.fps = process.argv[++i];
14+
else if (process.argv[i] == "--frameCount") parameters.frameCount = process.argv[++i];
15+
else if (process.argv[i] == "--spawnRate") parameters.spawnRate = process.argv[++i];
16+
}
17+
18+
// Initialize a board as a lifeless square array.
19+
let board = new Array(parameters.dimension).fill(new Array(parameters.dimension).fill(false));
20+
21+
// Load up the array with trues (live) and falses (dead).
22+
board = board.map((row) => row.map(() => Math.random() < parameters.spawnRate));
23+
24+
for (let i = 0; i < parameters.frameCount; i++) {
25+
printBoard(board);
26+
27+
// Mutate the board
28+
board = board.map((row, rowIndex) =>
29+
row.map((cell, colIndex) => {
30+
prevRowIndex = rowIndex - 1 < 0 ? parameters.dimension - 1 : rowIndex - 1;
31+
nextRowIndex = rowIndex + 1 > parameters.dimension - 1 ? 0 : rowIndex + 1;
32+
prevColIndex = colIndex - 1 < 0 ? parameters.dimension - 1 : colIndex - 1;
33+
nextColIndex = colIndex + 1 > parameters.dimension - 1 ? 0 : colIndex + 1;
34+
35+
// Get the count of neighbors that are alive
36+
neighbors = [
37+
[prevRowIndex, prevColIndex],
38+
[prevRowIndex, colIndex],
39+
[prevRowIndex, nextColIndex],
40+
[rowIndex, prevColIndex],
41+
[rowIndex, nextColIndex],
42+
[nextRowIndex, prevColIndex],
43+
[nextRowIndex, colIndex],
44+
[nextRowIndex, nextColIndex],
45+
].reduce((sum, indices) => sum + (board[indices[0]][indices[1]] ? 1 : 0), 0);
46+
47+
// Mutate the current cell
48+
if (cell && (neighbors < 2 || neighbors > 3)) return false;
49+
if (!cell && neighbors == 3) return true;
50+
return cell;
51+
})
52+
);
53+
54+
await sleep((1 / parameters.fps) * 1000);
55+
}
56+
}
57+
58+
function printBoard(board) {
59+
console.clear();
60+
board.forEach((row) => {
61+
console.log(row.reduce((prev, curr) => prev.concat(curr ? "█" : " "), ""));
62+
});
63+
}
64+
65+
function sleep(time) {
66+
return new Promise((resolve) => {
67+
setTimeout(resolve, time);
68+
});
69+
}

0 commit comments

Comments
 (0)