Skip to content

Commit

Permalink
Merge pull request #1752 from legowerewolf/master
Browse files Browse the repository at this point in the history
Added Game of Life in JavaScript
  • Loading branch information
jrg94 authored Oct 23, 2019
2 parents 88f9363 + b0cbab0 commit 0fe8f61
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 28 deletions.
57 changes: 29 additions & 28 deletions archive/j/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ Welcome to Sample Programs in JavaScript!

## Sample Programs

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

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

## Fun Facts

- Debut: 1995
- Typing: Dynamic
- Debut: 1995
- Typing: Dynamic

## References

- [JavaScript Wiki][5]
- [JavaScript Docs][6]
- [Online JavaScript Editor][7]
- [Web Standards/Documentation][10]
- [JavaScript beginner tutorial][11]
- [JavaScript Wiki][5]
- [JavaScript Docs][6]
- [Online JavaScript Editor][7]
- [Web Standards/Documentation][10]
- [JavaScript beginner tutorial][11]

[1]: https://github.com/toturkmen/baklava
[2]: https://therenegadecoder.com/code/hello-world-in-javascript/
Expand All @@ -60,3 +60,4 @@ Welcome to Sample Programs in JavaScript!
[19]: https://github.com/TheRenegadeCoder/sample-programs/issues/1380
[20]: https://github.com/TheRenegadeCoder/sample-programs/issues/1649
[20]: https://github.com/TheRenegadeCoder/sample-programs/issues/1379
[22]: https://github.com/TheRenegadeCoder/sample-programs/issues/1377
69 changes: 69 additions & 0 deletions archive/j/javascript/game-of-life.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
main();

async function main() {
let parameters = {
dimension: 20,
fps: 4,
frameCount: 60,
spawnRate: 0.3,
};

for (let i = 0; i < process.argv.length - 1; i++) {
if (process.argv[i] == "--dimension") parameters.dimension = process.argv[++i];
else if (process.argv[i] == "--fps") parameters.fps = process.argv[++i];
else if (process.argv[i] == "--frameCount") parameters.frameCount = process.argv[++i];
else if (process.argv[i] == "--spawnRate") parameters.spawnRate = process.argv[++i];
}

// Initialize a board as a lifeless square array.
let board = new Array(parameters.dimension).fill(new Array(parameters.dimension).fill(false));

// Load up the array with trues (live) and falses (dead).
board = board.map((row) => row.map(() => Math.random() < parameters.spawnRate));

for (let i = 0; i < parameters.frameCount; i++) {
printBoard(board);

// Mutate the board
board = board.map((row, rowIndex) =>
row.map((cell, colIndex) => {
prevRowIndex = rowIndex - 1 < 0 ? parameters.dimension - 1 : rowIndex - 1;
nextRowIndex = rowIndex + 1 > parameters.dimension - 1 ? 0 : rowIndex + 1;
prevColIndex = colIndex - 1 < 0 ? parameters.dimension - 1 : colIndex - 1;
nextColIndex = colIndex + 1 > parameters.dimension - 1 ? 0 : colIndex + 1;

// Get the count of neighbors that are alive
neighbors = [
[prevRowIndex, prevColIndex],
[prevRowIndex, colIndex],
[prevRowIndex, nextColIndex],
[rowIndex, prevColIndex],
[rowIndex, nextColIndex],
[nextRowIndex, prevColIndex],
[nextRowIndex, colIndex],
[nextRowIndex, nextColIndex],
].reduce((sum, indices) => sum + (board[indices[0]][indices[1]] ? 1 : 0), 0);

// Mutate the current cell
if (cell && (neighbors < 2 || neighbors > 3)) return false;
if (!cell && neighbors == 3) return true;
return cell;
})
);

await sleep((1 / parameters.fps) * 1000);
}
}

function printBoard(board) {
console.clear();
board.forEach((row) => {
console.log(row.reduce((prev, curr) => prev.concat(curr ? "█" : " "), ""));
});
}

function sleep(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}

0 comments on commit 0fe8f61

Please sign in to comment.