Skip to content

Instantly share code, notes, and snippets.

View PabloRegen's full-sized avatar
🏠
Working from home

Pablo Regen PabloRegen

🏠
Working from home
  • New York City
View GitHub Profile
/*
Components structure:
- App
- Title
- NewTaskBar
- UserDisplayPreference
- ViewBar
- OrderByBar
- SearchBar
@PabloRegen
PabloRegen / newBoardStatus.js
Last active March 4, 2019 05:37
function to generate a board status
const newBoardStatus = (cellStatus = () => Math.random() < 0.3) => {
const grid = [];
for (let r = 0; r < totalBoardRows; r++) {
grid[r] = [];
for (let c = 0; c < totalBoardColumns; c++) {
grid[r][c] = cellStatus();
}
}
return grid;
};
@PabloRegen
PabloRegen / settingApp.js
Last active March 4, 2019 04:54
Setting App.js file
import React, { Component } from 'react';
const totalBoardRows = 40;
const totalBoardColumns = 60;
const newBoardStatus = () => {};
const BoardGrid = () => {};
const Slider = () => {};
class App extends Component {
@PabloRegen
PabloRegen / BoardGrid.js
Last active March 4, 2019 06:40
Create grid with a table
const BoardGrid = ({ boardStatus, onToggleCellStatus }) => {
const handleClick = (r,c) => onToggleCellStatus(r,c);
const tr = [];
for (let r = 0; r < totalBoardRows; r++) {
const td = [];
for (let c = 0; c < totalBoardColumns; c++) {
td.push(
<td
key={`${r},${c}`}
@PabloRegen
PabloRegen / Slider.js
Last active March 4, 2019 23:53
Create a slider input
const Slider = ({ speed, onSpeedChange }) => {
const handleChange = e => onSpeedChange(e.target.value);
return (
<input
type='range'
min='50'
max='1000'
step='50'
value={speed}
@PabloRegen
PabloRegen / state.js
Last active March 4, 2019 07:42
Starting state of the application
class App extends Component {
state = {
boardStatus: newBoardStatus(),
generation: 0,
isGameRunning: false,
speed: 500
};
// Other methods ...
@PabloRegen
PabloRegen / runStopButton.js
Last active March 4, 2019 23:55
Start/Stop button
class App extends Component {
state = {...};
runStopButton = () => {
return this.state.isGameRunning ?
<button type='button' onClick={this.handleStop}>Stop</button> :
<button type='button' onClick={this.handleRun}>Start</button>;
}
// Other methods ...
@PabloRegen
PabloRegen / handleClearBoard.js
Last active March 5, 2019 00:00
Clear board and new board methods
class App extends Component {
state = {...};
runStopButton = () => {...}
handleClearBoard = () => {
this.setState({
boardStatus: newBoardStatus(() => false),
generation: 0
});
}
@PabloRegen
PabloRegen / handleToggleCellStatus.js
Last active March 5, 2019 00:02
Toggle an individual cell status
class App extends Component {
state = {...};
runStopButton = () => {...}
handleClearBoard = () => {...}
handleNewBoard = () => {...}
handleToggleCellStatus = (r,c) => {
const toggleBoardStatus = prevState => {
const clonedBoardStatus = JSON.parse(JSON.stringify(prevState.boardStatus));
clonedBoardStatus[r][c] = !clonedBoardStatus[r][c];
@PabloRegen
PabloRegen / handleStep.js
Last active March 5, 2019 00:05
Generate next game iteration
class App extends Component {
state = {...};
runStopButton = () => {...}
handleClearBoard = () => {...}
handleNewBoard = () => {...}
handleToggleCellStatus = () => {...}
handleStep = () => {
const nextStep = prevState => {
const boardStatus = prevState.boardStatus;