Game of Life React
How do I make an game of life react?
What is a game of life react? How do you make a game of life react? This script and codes were developed by Codoer on 07 December 2022, Wednesday.
Game of Life React - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Game of Life React</title> <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id='app'></div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.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='https://cdnjs.cloudflare.com/ajax/libs/redux/3.5.2/redux.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.4.5/react-redux.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/redux-thunk/2.1.0/redux-thunk.js'></script> <script src="js/index.js"></script>
</body>
</html>
Game of Life React - Script Codes CSS Codes
body { text-align: center; background-color: #7e3878;
}
#app { margin-right: auto; margin-left: auto;
}
.maintitle { text-align: center; margin-top: 35px; margin-bottom: 25px; font-size: 50px; color: white;
}
.but { text-align: center; margin-bottom: 25px;
}
button { margin-left: 10px;
}
.Generation { margin-bottom: 0; text-align: center; background-color: #333; padding-left: 40px; padding-right: 40px; padding-top: 10px; display: inline-block; color: white; font-size: 25px;
}
table { background-color: #222; border: 10px solid #333; border-spacing: 1px; margin-right: auto; margin-left: auto;
}
td { width: 1em; height: 1em; border-collapse: collapse; border-right: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black;
}
.live { background-color: #ddd;
}
.new { background-color: #00a300;
}
.killed { background-color: #A20025;
}
Game of Life React - 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; }
/*
Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
var arr = [];
var x = [];
var setInter;
var gen = 0;
var change;
var row = 25, column = 40;
var speed = 1;
var set1 = function set1() { clearInterval(setInter); setInter = false; if (!setInter) { setInter = setInterval(change, speed); }
};
var boardArr = function boardArr(row, column, initial) { var random = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3]; arr = JSON.parse(JSON.stringify([])); for (var i = 0; i < row; i++) { arr[i] = []; for (var j = 0; j < column; j++) { if (!random) { arr[i][j] = [initial, random]; } else { arr[i][j] = [Math.round(Math.random()), random]; } } } x = JSON.parse(JSON.stringify(arr)); return arr;
};
var Td = function Td(props) { var td1 = props.data.map(function (v, j) { var cName = v[1]; //if(props.state[props.ind][j]===1){cName='live';} return React.createElement('td', { id: props.ind + '-' + j, className: cName, onClick: props.add }); }); return React.createElement( 'div', null, td1 );
};
var Game = function (_React$Component) { _inherits(Game, _React$Component); function Game(props) { _classCallCheck(this, Game); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.tableMaker = function () { return _this.state.board.map(function (val, i) { return React.createElement( 'tr', { id: 'i-' + i }, React.createElement(Td, { state: _this.state.board, data: val, ind: i, add: _this.handleAdd }) ); }); }; _this.handleRandom = function () { gen = 0; _this.setState({ board: boardArr(row, column, 0, true) }); set1(); //clearInterval(setInter)//setInter=false;//if(!setInter){setInter=setInterval(change, 1);} }; _this.handleAdd = function (e) { console.log(_this.state.board); var idName = e.target.id.split('-'); //return current clicking tag id name's array, idName[0]=row, idName[1]=column; console.log(idName); var addArr = JSON.parse(JSON.stringify(_this.state.board)); if (addArr[idName[0]][idName[1]][0] === 0) { addArr[idName[0]][idName[1]][0] = 1;addArr[idName[0]][idName[1]][1] = 'new'; } else if (addArr[idName[0]][idName[1]][0] === 1) { addArr[idName[0]][idName[1]][0] = 0;addArr[idName[0]][idName[1]][1] = 'killed'; } //console.log(addArr) //addArr[idName[0]][idName[1]]=1; arr = $.extend(true, [], addArr); //$(e.target).attr('class', 'live'); _this.setState({ board: arr }); }; _this.handleStop = function () { clearInterval(setInter); setInter = false; }; _this.handleResume = function () { if (!setInter) { setInter = setInterval(change, speed); } }; _this.handleClear = function () { _this.setState({ board: boardArr(row, column, 0, false) }); clearInterval(setInter); setInter = false; gen = 0; }; _this.handleBig = function () { row = 40, column = 60, gen = 0; _this.setState({ board: boardArr(row, column, 0, true) }); set1(); }; _this.handleBack = function () { row = 25, column = 40, gen = 0; console.log(_this.state.board); _this.setState({ board: boardArr(row, column, 0, true) }); console.log(_this.state.board); set1(); }; _this.handleNext = function () { _this.handleStop(); change(); }; _this.state = { board: boardArr(row, column, 0, true) }; return _this; } Game.prototype.componentDidMount = function componentDidMount() { var _this2 = this; change = function change() { _this2.state.board.forEach(function (row, i) { row.forEach(function (col, j) { var lastRow = arr.length - 1, lastCol = arr[0].length - 1, prevRow = i - 1 < 0 ? lastRow : i - 1, prevCol = j - 1 < 0 ? lastCol : j - 1, nextRow = i + 1 > lastRow ? 0 : i + 1, nextCol = j + 1 > lastCol ? 0 : j + 1, sum = arr[prevRow][prevCol][0] + arr[prevRow][j][0] + arr[prevRow][nextCol][0] + arr[i][prevCol][0] + arr[i][nextCol][0] + arr[nextRow][prevCol][0] + arr[nextRow][j][0] + arr[nextRow][nextCol][0]; if (col[0] === 0) { if (sum === 3) { x[i][j][0] = 1;x[i][j][1] = 'new'; } else { x[i][j][0] = 0;x[i][j][1] = 'zero'; }; } else if (col[0] === 1) { if (sum < 2) { x[i][j][0] = 0;x[i][j][1] = 'killed'; } else if (sum > 3) { x[i][j][0] = 0;x[i][j][1] = 'killed'; } else if (sum === 3 || sum === 2) { x[i][j][0] = 1;x[i][j][1] = 'live'; } } }); }); if (arr.toString() !== x.toString()) { gen++; } //compare 2 arrays, when they are same, generation counting stops; arr = $.extend(true, [], x); //arr=JSON.parse(JSON.stringify(x)); _this2.setState({ board: arr }); }; set1(); //clearInterval(setInter);setInter=false;if(!setInter){setInter=setInterval(change, 1);} }; Game.prototype.render = function render() { return React.createElement( 'div', null, React.createElement( 'div', { className: 'maintitle' }, 'Game of Life' ), React.createElement( 'div', { className: 'but' }, React.createElement( 'button', { className: 'btn btn-success', onClick: this.handleResume }, 'Run' ), React.createElement( 'button', { className: 'btn btn-warning', onClick: this.handleStop }, 'Pause' ), React.createElement( 'button', { className: 'btn btn-info', onClick: this.handleNext }, 'Next' ), React.createElement( 'button', { className: 'btn btn-danger', onClick: this.handleClear }, 'Clear' ), React.createElement( 'button', { className: 'btn btn-primary', onClick: this.handleRandom }, 'Random' ), React.createElement( 'button', { className: 'btn btn-secondary', onClick: this.handleBack }, 'Size: 25X40' ), React.createElement( 'button', { className: 'btn btn-secondary', onClick: this.handleBig }, 'Size: 40X60' ) ), React.createElement( 'h3', { className: 'Generation' }, 'Generation: ', gen ), React.createElement( 'table', null, React.createElement( 'tbody', null, this.tableMaker() ) ) ); }; return Game;
}(React.Component);
ReactDOM.render(React.createElement(Game, null), document.getElementById('app'));
Developer | Codoer |
Username | c0d0er |
Uploaded | December 07, 2022 |
Rating | 3 |
Size | 6,561 Kb |
Views | 4,048 |
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 |
Perseverance Portfolio | 2,756 Kb |
Pomodoro positions | 2,182 Kb |
Random Quote Machine | 2,411 Kb |
Show the Local Weather | 2,904 Kb |
Visualize Data with a Heat Map | 6,445 Kb |
Visualize US GDP Data with a Bar Chart | 7,811 Kb |
Nikola Tesla Tribute Page | 3,263 Kb |
Visualize National Contiguity with a Force Directed Graph | 5,906 Kb |
React Camper Leaderboard | 5,258 Kb |
Pomodoro 1 timer | 2,589 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 |
Bootstrap Carousel Fade Transition | Rowno | 2,484 Kb |
Pictos font library from CodePen | Jstam | 3,790 Kb |
Jquery 2d character movement | Drewtadams | 4,291 Kb |
Foundation 5 Menu - Accessibility | Xporter | 1,999 Kb |
Gears | Synvox | 3,278 Kb |
Cool Page Split Effect | Anthonyadamski | 6,128 Kb |
TweetBox with React JS | J0zelito | 3,325 Kb |
Perspective Origin Demo | Agelber | 3,614 Kb |
CSS Parent Selector | Tomhodgins | 2,143 Kb |
Right Click Menu | Anodpixels | 2,252 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!