Game of Life
How do I make an game of life?
Conway's Game of Life implemented using React and SCSS for freeCodeCamp. Uses the simple neighbor counting algorithm from the Graphics Programming Black Book by Michael Abrash.. What is a game of life? How do you make a game of life? This script and codes were developed by Zac Clemans on 14 January 2023, Saturday.
Game of Life - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Game of Life</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="app-container"></div> <script src='https://fb.me/react-0.14.7.js'></script>
<script src='https://fb.me/react-dom-0.14.7.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Game of Life - Script Codes CSS Codes
#app-container { margin: 50px auto; width: 660px;
}
#app-container h1 { color: #4c4c4c;
}
#app-container td { width: 10px; height: 10px; border: 1px solid #4c4c4c;
}
#app-container td.status-0 { background: #1a1c1d;
}
#app-container td.status-1 { background: red;
}
#app-container button { width: 100px; height: 30px; margin-top: 1px; margin-right: 1px; border: none; background: #4c4c4c; color: white; font-size: 18px; font-weight: bold;
}
#app-container #genCounter { display: inline-block; float: right; margin-top: 1px; padding-left: 5px; padding-right: 5px; height: 30px; background: #4c4c4c; color: white; font-size: 18px; font-weight: bold; line-height: 30px;
}
Game of Life - Script Codes JS Codes
"use strict";
var Cell = React.createClass({ displayName: "Cell", handleClick: function handleClick() { this.props.returnLocation(this.props.row, this.props.column); }, render: function render() { return React.createElement("td", { className: "status-" + this.props.status, onClick: this.handleClick }); }
});
var CellGrid = React.createClass({ displayName: "CellGrid", handleClick: function handleClick(row, column) { this.props.onCellClick(row, column); }, render: function render() { var gridNodes = this.props.grid.map(function (row, rowIndex) { var cellNodes = row.map(function (cell, cellIndex) { return React.createElement(Cell, { status: cell, row: rowIndex, column: cellIndex, returnLocation: this.handleClick }); }.bind(this)); return React.createElement( "tr", null, cellNodes ); }.bind(this)); return React.createElement( "table", null, React.createElement( "tbody", null, gridNodes ) ); }
});
var PauseLifeButton = React.createClass({ displayName: "PauseLifeButton", render: function render() { return React.createElement( "button", { onClick: this.props.onUse }, "Pause" ); }
});
var ResumeLifeButton = React.createClass({ displayName: "ResumeLifeButton", render: function render() { return React.createElement( "button", { onClick: this.props.onUse }, "Start" ); }
});
var ClearGridButton = React.createClass({ displayName: "ClearGridButton", render: function render() { return React.createElement( "button", { onClick: this.props.onUse }, "Clear" ); }
});
var LifeApp = React.createClass({ displayName: "LifeApp", getInitialState: function getInitialState() { var grid = []; var lifeChance = 0.45; for (var i = 0; i < this.props.initialSize; i++) { var row = []; for (var j = 0; j < this.props.initialSize; j++) { if (Math.random() < lifeChance) { row.push(1); } else { row.push(0); } } grid.push(row); } return { grid: grid, genCounter: 0 }; }, componentDidMount: function componentDidMount() { runLife = setInterval(this.lifeLoop, 300); }, lifeLoop: function lifeLoop() { var newGrid = []; for (var i = 0; i < this.props.initialSize; i++) { var newRow = []; for (var j = 0; j < this.props.initialSize; j++) { var neighborCount = this.getCellState(i - 1, j - 1) + this.getCellState(i - 1, j) + this.getCellState(i - 1, j + 1) + this.getCellState(i, j - 1) + this.getCellState(i, j + 1) + this.getCellState(i + 1, j - 1) + this.getCellState(i + 1, j) + this.getCellState(i + 1, j + 1); if (this.state.grid[i][j] == 1) { if (neighborCount == 2 || neighborCount == 3) { newRow.push(1); } else { newRow.push(0); } } else { if (neighborCount == 3) { newRow.push(1); } else { newRow.push(0); } } } newGrid.push(newRow); } this.setState({ grid: newGrid, genCounter: this.state.genCounter + 1 }); }, stopLifeLoop: function stopLifeLoop() { clearInterval(runLife); runLife = false; }, resumeLifeLoop: function resumeLifeLoop() { if (!runLife) { runLife = setInterval(this.lifeLoop, 100); } }, clearGrid: function clearGrid() { this.stopLifeLoop(); var clearedGrid = []; for (var i = 0; i < this.props.initialSize; i++) { var clearedRow = []; for (var j = 0; j < this.props.initialSize; j++) { clearedRow.push(0); } clearedGrid.push(clearedRow); } this.setState({ grid: clearedGrid, genCounter: 0 }); }, getCellState: function getCellState(x, y) { if (x < 0 || x >= this.props.initialSize || y < 0 || y >= this.props.initialSize) { return 0; } else { return this.state.grid[x][y]; } }, handleCellClick: function handleCellClick(row, column) { var newGrid = this.state.grid; newGrid[row][column] = newGrid[row][column] == 1 ? 0 : 1; this.setState({ grid: newGrid }); }, render: function render() { return React.createElement( "div", null, React.createElement( "h1", null, "Conway's Game of Life" ), React.createElement(CellGrid, { grid: this.state.grid, onCellClick: this.handleCellClick }), React.createElement(PauseLifeButton, { onUse: this.stopLifeLoop }), React.createElement(ResumeLifeButton, { onUse: this.resumeLifeLoop }), React.createElement(ClearGridButton, { onUse: this.clearGrid }), React.createElement( "div", { id: "genCounter" }, "Generation: ", this.state.genCounter ) ); }
});
var runLife = false;
ReactDOM.render(React.createElement(LifeApp, { initialSize: 40 }), document.getElementById("app-container"));
$("#stop").click(function () { clearInterval(runLife);
});
Developer | Zac Clemans |
Username | thalpha |
Uploaded | January 14, 2023 |
Rating | 3 |
Size | 4,856 Kb |
Views | 8,096 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Twitch Streamer Status | 3,948 Kb |
Camper News | 3,157 Kb |
D3 Map Project | 3,119 Kb |
Local Weather App | 6,304 Kb |
Recipe Box Redux Refactor | 11,597 Kb |
Wikipedia Viewer | 4,426 Kb |
FCC Camper Leaderboard Redux Refactor | 9,125 Kb |
OOLO Calculator - Vanilla JS | 5,734 Kb |
D3 Bar Graph Project | 3,011 Kb |
Pomodoro Timer | 3,706 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Css color for svg | Ademilter | 2,392 Kb |
Expandable Left Side Bar with jQuery animate | Retrofuturistic | 2,483 Kb |
Part 19 Bootstrap split button dropdown | Venkatesha | 1,601 Kb |
HTML5 Breakout | Jaysalvat | 4,873 Kb |
Basic HTML5 Structure | YuvarajTana | 1,289 Kb |
404 Page | Saransh | 2,666 Kb |
Vertically rotating text with CSS | Nopr | 2,141 Kb |
Bezier Animation with straight paths | Rhernando | 2,087 Kb |
VerbalExpressions Converter | Cmalven | 2,607 Kb |
SnappySnippet Test | Elmsoftware | 8,385 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!