Game of Life

Developer
Size
6,575 Kb
Views
6,072

How do I make an game of life?

What is a game of life? How do you make a game of life? This script and codes were developed by Matheus on 13 January 2023, Friday.

Game of Life Previews

Game of Life - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Game of Life</title> <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <link href="https://fonts.googleapis.com/css?family=VT323" rel="stylesheet">
<div class="col-md-10 col-md-offset-1 text-center game-header"> <a class="title" href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"> ReactJS Game of Life (click to learn more)</a>
</div>
<br>
<div id="border-effect"> <div class="col-md-8 col-md-offset-2" id="grid"> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Game of Life - Script Codes CSS Codes

body { height: 950px; background: blue; /* For browsers that do not support gradients */ background: -webkit-radial-gradient(blue, #000066, #1a1a1a); /* Safari 5.1 to 6.0 */ background: -o-radial-gradient(blue, #000066, #1a1a1a); /* For Opera 11.6 to 12.0 */ background: -moz-radial-gradient(blue, #000066, #1a1a1a); /* For Firefox 3.6 to 15 */ background: radial-gradient(#0000ff, #000066, #1a1a1a); /* Standard syntax */
}
.game-header { background-color: #1a1a1a; height: 75px; font-size: 30px;
}
.title { position: relative; top: 15px;
}
a { color: #8c8c8c; text-decoration: none !important;
}
a:hover { color: #8c8c8c;
}
.game-controls { height: 100px; background-color: #1a1a1a; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px;
}
#grid { position: relative; margin-top: 100px;
}
.gridLine { width: 800px; height: 20px; border: 1px solid #4d4d4d; margin: auto; background-color: #000000;
}
.cell1 { width: 20px; height: 20px; border: 1px solid #4d4d4d; float: left;
}
.cell2 { width: 18px; height: 20px; border: 1px solid #4d4d4d; float: right;
}
.youngCell { background-color: #9999ff;
}
.oldCell { background-color: #4d4dff;
}
.game-button { border: 2px solid black; background-color: #1a1a1a; color: #8c8c8c; font-weight: bold; font-size: 20px; width: 150px; height: 50px; border-radius: 20px; margin-top: 15px;
}
button:focus { outline: 0;
}
.game-display { margin: auto; border: 2px solid black; width: 200px; height: 30px; background-color: black; font-size: 22px; color: blue; font-family: 'VT323', monospace;
}
.generation-counter { position: relative; bottom: 3px;
}

Game of Life - Script Codes JS Codes

"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
//comment next line for debug
console.log = function () {};
var cols = 40; //number of cols in the grid
var rows = 25; //number of rows in the grid
//===2D array for random initial state===
function randomInitialState() { var initialState = []; for (var i = 0; i < rows; i++) { var aux = []; for (var j = 0; j < cols; j++) { Math.random() < 0.3 ? aux.push(1) : aux.push(0); } initialState.push(aux); } return initialState;
}
//===return the number of live cells around specific cell===
function liveCells(currentState, rowPosition, colPosition) { var count = 0; if (colPosition - 1 >= 0 && rowPosition - 1 >= 0 && currentState[rowPosition - 1][colPosition - 1] != 0) count += 1; if (colPosition - 1 >= 0 && currentState[rowPosition][colPosition - 1] != 0) count += 1; if (colPosition - 1 >= 0 && rowPosition + 1 < rows && currentState[rowPosition + 1][colPosition - 1] != 0) count += 1; if (rowPosition - 1 >= 0 && currentState[rowPosition - 1][colPosition] != 0) count += 1; if (rowPosition + 1 < rows && currentState[rowPosition + 1][colPosition] != 0) count += 1; if (colPosition + 1 < cols && rowPosition - 1 >= 0 && currentState[rowPosition - 1][colPosition + 1] != 0) count += 1; if (colPosition + 1 < cols && currentState[rowPosition][colPosition + 1] != 0) count += 1; if (colPosition + 1 < cols && rowPosition + 1 < rows && currentState[rowPosition + 1][colPosition + 1] != 0) count += 1; return count;
}
//===define next generation cell's status===
function nextGeneration(currentState, rowsNumber, colsNumber) { var nextGenerationGrid = []; var aux = []; for (var i = 0; i < rowsNumber; i++) { var nextGenerationRow = []; for (var j = 0; j < colsNumber; j++) { aux.push(liveCells(currentState, i, j)); if (currentState[i][j] != 0 && liveCells(currentState, i, j) < 2) nextGenerationRow.push(0);else if (currentState[i][j] != 0 && liveCells(currentState, i, j) > 3) nextGenerationRow.push(0);else if (currentState[i][j] != 0) nextGenerationRow.push(2);else if (currentState[i][j] == 0 && liveCells(currentState, i, j) == 3) nextGenerationRow.push(1);else nextGenerationRow.push(0); } nextGenerationGrid.push(nextGenerationRow); } console.log(aux); console.log(nextGenerationGrid); return nextGenerationGrid;
}
var Grid = function (_React$Component) { _inherits(Grid, _React$Component); function Grid(props) { _classCallCheck(this, Grid); var _this2 = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this2.state = { cells: randomInitialState(), generation: 0, gamePaused: false }; _this2.toggleGamePlay = _this2.toggleGamePlay.bind(_this2); _this2.aliveCell = _this2.aliveCell.bind(_this2); _this2.gridLine = _this2.gridLine.bind(_this2); _this2.resetGame = _this2.resetGame.bind(_this2); return _this2; } Grid.prototype.componentDidMount = function componentDidMount() { var _this = this; var _generation = this.state.generation; setTimeout(function () { _this.setState({ cells: nextGeneration(_this.state.cells, rows, cols), generation: _generation + 1 }); }, 150); }; Grid.prototype.componentDidUpdate = function componentDidUpdate() { if (this.state.gamePaused) return; var _this = this; var _generation = this.state.generation; setTimeout(function () { _this.setState({ cells: nextGeneration(_this.state.cells, rows, cols), generation: _generation + 1 }); }, 150); }; //===grid initial jsx expression=== Grid.prototype.gridLine = function gridLine(cellState) { var jsxGrid = []; for (var i = 0; i < rows; i++) { var gridLines = []; for (var j = 0; j < cols; j++) { if (j < cols - 1) gridLines.push(React.createElement("div", { className: "cell1 " + (cellState[i][j] == 1 ? "youngCell" : "") + (cellState[i][j] == 2 ? "oldCell" : ""), "data-position_row": i, "data-position_col": j, onClick: this.aliveCell }));else gridLines.push(React.createElement("div", { className: "cell2 " + (cellState[i][j] == 1 ? "youngCell" : "") + (cellState[i][j] == 2 ? "oldCell" : ""), "data-position_row": i, "data-position_col": j, onClick: this.aliveCell })); } jsxGrid.push(React.createElement( "div", { className: "gridLine" }, gridLines )); } return jsxGrid; }; Grid.prototype.toggleGamePlay = function toggleGamePlay() { this.setState({ gamePaused: !this.state.gamePaused }); }; Grid.prototype.aliveCell = function aliveCell(event) { var cells_aux = this.state.cells; var position_row = Number(event.currentTarget.dataset.position_row); var position_col = Number(event.currentTarget.dataset.position_col); console.log(position_row); console.log(position_col); console.log(cells_aux[position_row][position_col]); cells_aux[position_row][position_col] = 1; this.setState({ cells: cells_aux }); }; Grid.prototype.resetGame = function resetGame() { var newGrid = []; var _this = this; for (var i = 0; i < rows; i++) { var newLine = []; for (var j = 0; j < cols; j++) { newLine.push(0); } newGrid.push(newLine); } _this.setState({ cells: newGrid, gamePaused: true }); setTimeout(function () { _this.setState({ generation: 0 }); }, 100); }; Grid.prototype.render = function render() { var jsxExpression = this.gridLine(this.state.cells); var gridSystem = jsxExpression.map(function (line) { return React.createElement( "div", null, line ); }); return React.createElement( "div", null, React.createElement( "div", { className: "row teste" }, React.createElement( "div", null, gridSystem ) ), React.createElement( "div", { className: "row col-md-8 col-md-offset-2 game-controls text-center" }, React.createElement( "div", { className: "game-display" }, React.createElement( "span", { className: "generation-counter" }, "GENERATION: ", this.state.generation ) ), React.createElement( "button", { className: "game-button", onClick: this.toggleGamePlay }, !this.state.gamePaused ? "Pause" : "Play", " " ), React.createElement( "button", { className: "game-button", onClick: this.resetGame }, "Reset" ) ) ); }; return Grid;
}(React.Component);
ReactDOM.render(React.createElement(Grid, null), document.getElementById("grid"));
Game of Life - Script Codes
Game of Life - Script Codes
Home Page Home
Developer Matheus
Username MatheusLima92
Uploaded January 13, 2023
Rating 3
Size 6,575 Kb
Views 6,072
Do you need developer help for Game of Life?

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!

Matheus (MatheusLima92) Script Codes
Create amazing Facebook ads with AI!

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!