Last active
August 29, 2015 14:26
-
-
Save rick4470/cc9c798770cf5abdaf3e to your computer and use it in GitHub Desktop.
game of life functional
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var grid = initGrid(30, 30, 10); | |
| printGrid(grid); | |
| startGame(); | |
| function startGame() { | |
| (function generation(generations) { | |
| setTimeout(function() { | |
| var tempGrid = initGrid(30, 30, 10); | |
| grid.forEach(function(rowArray, row){ | |
| rowArray.forEach(function(value, col){ | |
| if (value == 1 || value == 0) { | |
| var neighbors = countNeighbors(row, col); | |
| if (value == 1 && (neighbors < 2 || neighbors > 3)) { | |
| tempGrid[row][col] = 0; // DIE | |
| } else if ((value == 0) && neighbors == 3) { | |
| tempGrid[row][col] = 1; // LIVE | |
| } | |
| } | |
| }); | |
| }); | |
| grid.forEach(function(rowArray, row){ | |
| rowArray.forEach(function(value, col){ | |
| grid[row][col] = tempGrid[row][col]; | |
| }); | |
| }); | |
| printGrid(grid); | |
| if (--generations) generation(generations); | |
| }, 150) | |
| })(30000); | |
| function countNeighbors(row, col) { | |
| var neighbors = 0; | |
| for (var j = col - 1; j < (col + 2); j++) { | |
| for (var i = row - 1; i < (row + 2); i++) { | |
| if (i > 0 && j > 0 && j < grid.length && i < grid.length) { | |
| if (grid[i][j] !== undefined) | |
| neighbors += grid[i][j]; | |
| } | |
| } | |
| } | |
| return neighbors; | |
| } | |
| } | |
| function initGrid(rows, cols, randomNumbersQuantity) { | |
| var grid = []; | |
| if (randomNumbersQuantity) { | |
| var randomNumbers = getRandomNumbers(randomNumbersQuantity , []); | |
| } | |
| for (var i = 0; i < rows; i++) { | |
| var row = []; | |
| for (var j = 0; j < cols; j++) { | |
| if (i == 0 || i == (rows - 1) || j == (cols - 1) || j == 0) { | |
| row.push(2); // Set to an edge case of grid | |
| } else { | |
| row.push(0); | |
| } | |
| } | |
| grid.push(row); | |
| } | |
| if (randomNumbersQuantity > 0) { | |
| for (var i = 0; i < randomNumbers.length; i++) { | |
| var numbers = randomNumbers[i]; | |
| grid[numbers.x][numbers.y] = 1; | |
| } | |
| } else { | |
| for (var i = 0; i < rows; i++) { | |
| for (var j = 0; j < cols; j++) { | |
| if (grid[i][j] == 0) { | |
| /* Linear Lines | |
| if (j == 15){ | |
| if (i >= 2 && i <= 15){ | |
| grid[i][j] = 1; | |
| grid[i+9][j] = 1; | |
| grid[i+15][j] = 1; | |
| } | |
| } | |
| if (j == 10){ | |
| if (i >= 2 && i <= 15){ | |
| grid[i][j] = 1; | |
| grid[i+9][j] = 1; | |
| grid[i+15][j] = 1; | |
| } | |
| } | |
| if (j == 5){ | |
| if (i >= 2 && i <= 15){ | |
| grid[i][j] = 1; | |
| grid[i+9][j] = 1; | |
| grid[i+15][j] = 1; | |
| } | |
| } | |
| if (j == 20){ | |
| if (i >= 2 && i <= 15){ | |
| grid[i][j] = 1; | |
| grid[i+9][j] = 1; | |
| grid[i+15][j] = 1; | |
| } | |
| } */ | |
| /* Talking Face */ | |
| if (i == 14) { | |
| if (j > 10 && j < 20) { | |
| grid[i][j] = 1; | |
| } | |
| } | |
| if (j == 10) { | |
| if (i >= 8 && i <= 10) { | |
| grid[i][j] = 1; | |
| } | |
| } | |
| if (j == 15) { | |
| if (i >= 8 && i <= 10) { | |
| grid[i][j] = 1; | |
| } | |
| } | |
| if (j == 20) { | |
| if (i >= 8 && i <= 10) { | |
| grid[i][j] = 1; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return grid; | |
| function getRandomNumbers(numbers, randomNumbers) { | |
| if (numbers == 0) { | |
| return randomNumbers; | |
| }else{ | |
| var Numbers = {}; | |
| Numbers.x = Math.floor(Math.random() * 29) + 1; | |
| Numbers.y = Math.floor(Math.random() * 29) + 1; | |
| var filtered = randomNumbers.filter(function(object,index, array){ | |
| var exist = false; | |
| var count = 0; | |
| array.forEach(function(obj){ | |
| if (object.x == obj.x && object.y == obj.y && count < 2) { | |
| if (count == 1) { | |
| count++; | |
| exist = true; | |
| }else{ | |
| count++; | |
| exist = false; | |
| } | |
| } | |
| }); | |
| if (!exist) { | |
| return object; | |
| } | |
| }); | |
| if (filtered.length === randomNumbers.length) { | |
| filtered.push(Numbers); | |
| return getRandomNumbers(numbers - 1, filtered); | |
| }else{ | |
| return getRandomNumbers(numbers, filtered); | |
| } | |
| } | |
| } | |
| } | |
| function printGrid(grid) { | |
| for (var i = 0; i < grid.length; i++) { | |
| var rowData; | |
| for (var j = 0; j < grid[i].length; j++) { | |
| if (rowData == undefined) | |
| rowData = ''; | |
| if (grid[i][j] == 2) { | |
| if (i == 0 || i == (grid.length - 1)) | |
| rowData += ' > '; | |
| else | |
| rowData += ' V '; | |
| } else { | |
| if (grid[i][j] == 0) | |
| rowData += ' . '; | |
| else | |
| rowData += ' * '; | |
| } | |
| if (j == grid.length - 1) { | |
| rowData += '\n' | |
| } | |
| } | |
| } | |
| console.log(rowData); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment