From f87e6c0b31744de490cda8ae42d6472e3f81f18e Mon Sep 17 00:00:00 2001 From: Duncan Gibson Date: Wed, 23 Oct 2019 11:09:59 -0700 Subject: [PATCH 1/2] Add game of life in JavaScript. Closes TheRenegadeCoder/sample-programs#1377. --- archive/j/javascript/game-of-life.js | 69 ++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 archive/j/javascript/game-of-life.js diff --git a/archive/j/javascript/game-of-life.js b/archive/j/javascript/game-of-life.js new file mode 100644 index 000000000..f5e358800 --- /dev/null +++ b/archive/j/javascript/game-of-life.js @@ -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); + }); +} From b0cbab0763b6e0045c76b35d0b2cf8c08d412dc8 Mon Sep 17 00:00:00 2001 From: Duncan Gibson Date: Wed, 23 Oct 2019 11:13:45 -0700 Subject: [PATCH 2/2] Link the readme. --- archive/j/javascript/README.md | 57 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/archive/j/javascript/README.md b/archive/j/javascript/README.md index 973cb5de7..4dc43e91d 100644 --- a/archive/j/javascript/README.md +++ b/archive/j/javascript/README.md @@ -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/ @@ -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