Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              header
	h1 Conway's Game of Life
	h2 Click to add or remove cells.
#root
footer a freeCodeCamp project by kbaig
	
            
          
!
            
              body
	font-family: 'Roboto', sans-serif
	margin: 0
	color: #fff
	header
		background: #3949AB
		text-align: center
		padding: 20px 0
		h1
			margin-top: 5px
		
		h2
			font-weight: 500
	
	.App
		width: 80%
		min-width: 500px
		margin: 20px auto 40px
		button
			font: inherit
			font-size: 15px
			font-weight: 600
			color: #333
			padding: 3px 5px
			border-radius: 3px
			border: 1px solid #999
			background: #f1f1f1
			&:hover
				cursor: pointer
				background: #ddd
			&:active
				background: #ccc
			&:focus
				outline: none
	
		.button-red
			background: #dd423e
			color: #fff
			border: 1px solid #cc3e3b
			&:hover
				background: #bc3632
			&:active
				background: #a52e2b
		
		.Dashboard
			display: flex
			justify-content: space-between
			align-items: center
			width: 400px
			margin: auto
			padding: 10px 20px 0
			border-radius: 7px 7px 0 0
			background: #2A2A2A
			font-size: 18px
			button
				width: 65px
				margin-left: 2px
				&:nth-child(1)
					margin-left: 0
					
			.randomize
				width: 100px
			
		.Grid
			display: flex
			flex-direction: column
			margin: auto
			width: 500px
			height: 500px
			border: 10px solid #2A2A2A
			border-radius: 5px
			.Row
				flex: 1
				display: flex
				.Cell
					flex: 1
					border: 1px solid #2A2A2A
		
		.BoardOptions
			width: 400px
			margin: auto
			padding: 0 20px 10px
			border-radius: 0 0 7px 7px
			background: #2A2A2A
			font-size: 18px
			.ChangeBoardRow, .ChangeIntervalRow
				display: flex
				justify-content: space-between
				align-items: center
				button
					width: 75px
					margin-left: 2px
					&:nth-child(1)
						margin-left: 0
			.ChangeBoardRow
				margin-bottom: 2px
				
	footer
		position: absolute
		bottom: 0
		left: 0
		right: 0
		text-align: center
		background: #3949AB
		padding: 20px 0
		
				
	
            
          
!
            
              // new grid helper function
const newGrid = (length, width) => Array(length).fill(false).map(row => Array(width).fill(false).map(cell => Math.random() <= 0.50));

// Redux
const defaultState = {
	grid: newGrid(50, 50),
	generationInterval: 75,
	generation: 1,
	paused: false,
	size: [50, 50]
};

// actions
const NEXT_GENERATION = 'NEXT_GENERATION';
const TOGGLE_PAUSE = 'TOGGLE_PAUSE';
const CHANGE_BOARD = 'CHANGE_BOARD';
const CLEAR_BOARD = 'CLEAR_BOARD';
const TOGGLE_SQUARE = 'TOGGLE_SQUARE';
const RANDOMIZE_BOARD = 'RANDOMIZE_BOARD';
const CHANGE_INTERVAL = 'CHANGE_INTERVAL';

const nextGeneration = () => {
	return { 
		type: NEXT_GENERATION
	};
};

const togglePause = () => {
	return {
		type: TOGGLE_PAUSE
	};
};

const changeBoard = (length, width) => {
	return {
		type: CHANGE_BOARD,
		length,
		width
	};
};

const clearBoard = () => {
	return {
		type: CLEAR_BOARD
	};
};

const toggleSquare = (row, column) => {
	return {
		type: TOGGLE_SQUARE,
		row,
		column
	};
};

const randomizeBoard = () => {
	return {
		type: RANDOMIZE_BOARD
	};
};

const changeInterval = (interval) => {
	return {
		type: CHANGE_INTERVAL,
		interval
	};
};

// next grid helper function
const nextGrid = (grid) => {
	
	const lastRow = grid.length - 1;
	const lastCol = grid[0].length - 1;
	
	const next = grid.map((row, i) => {
		return row.map((cell, j) => {
			const neighbors = [
				grid[i === 0 ? lastRow : i-1][j === 0 ? lastCol : j-1], // top left
				grid[i === 0 ? lastRow : i-1][j], // top mid
				grid[i === 0 ? lastRow : i-1][j === lastCol ? 0 : j+1], // top right
				grid[i][j === 0 ? lastCol : j-1], //mid left
				grid[i][j === lastCol ? 0 : j+1], //mid right
				grid[i === lastRow ? 0 : i+1][j === 0 ? lastCol : j-1], // bot left
				grid[i === lastRow ? 0 : i+1][j], // bot mid
				grid[i === lastRow ? 0 : i+1][j === lastCol ? 0 : j+1] // bot right
			];
			
			let aliveNeighbors = neighbors.reduce((a,b) => a + b);

			return cell ? aliveNeighbors === 2 || aliveNeighbors === 3 : aliveNeighbors === 3;
		});
	});
	return next;
};

// reducer
const reducer = (state = defaultState, action) => {
	switch (action.type) {
			
		case NEXT_GENERATION:
			return {
				...state,
				grid: nextGrid(state.grid),
				generation: state.generation + 1
			};
			
		case TOGGLE_PAUSE:
			return {
				...state,
				paused: !state.paused
			};
			
		case CHANGE_BOARD:
			return {
				...state,
				grid: newGrid(action.length, action.width),
				generation: 1,
				paused: true,
				size: [action.length, action.width]
			};
			
		case CLEAR_BOARD:
			return {
				...state,
				grid: [...state.grid].map(row => row.map(cell => false)),
				generation: 1,
				paused: true
			};
			
		case TOGGLE_SQUARE:
			const grid = [...state.grid];
			grid[action.row][action.column] = !grid[action.row][action.column];
			return {
				...state,
				grid
			};
			
		case RANDOMIZE_BOARD:
			return {
				...state,
				grid: newGrid(state.grid.length, state.grid[0].length),
				generation: 1,
				paused: true
			};
			
		case CHANGE_INTERVAL:
			return {
				...state,
				generationInterval: action.interval,
			};
			
		default:
			return state;

	}
};

// create store
const store = Redux.createStore(reducer);

// React
const App = (props) => {
	return (
		<div className="App">
			
			<Dashboard
				generation = { props.generation }
				togglePause = { props.togglePause }
				paused = { props.paused }
				clearBoard = { props.clearBoard }
				randomizeBoard = { props.randomizeBoard }
			/>
			
			<Grid
				grid = { props.grid }
				generationInterval = { props.generationInterval }
				nextGeneration = { props.nextGeneration }
				paused = { props.paused }
				toggleSquare = { props.toggleSquare }
			/>
			
			<BoardOptions
				changeBoard = { props.changeBoard }
				size = { props.size }
				changeInterval = { props.changeInterval }
				generationInterval = { props.generationInterval }
			/>
		</div>
	);
}

const Dashboard = (props) => {
	return (
		<div className="Dashboard">
			<div className="dashboard-options">
				<button
					onClick={ props.togglePause }
					className={ props.paused ? 'button-red' : null }	
				>
					{ props.paused ? 'Resume' : 'Pause' }
				</button>
				<button onClick={ props.clearBoard }>Clear</button>
				<button className="randomize" onClick={ props.randomizeBoard }>Randomize</button>
			</div>
			<div>Generation { props.generation }</div>
		</div>
	);
};

class Grid extends React.Component {
	constructor() {
		super();
		this.timer = null;
	}
	
	componentDidMount() {
		this.timer = setInterval(this.props.nextGeneration, this.props.generationInterval);
	}
	
	componentWillUpdate(nextProps) {	
		if (nextProps.paused !== this.props.paused) {
			this.timer = nextProps.paused ?
				clearInterval(this.timer) :
				setInterval(nextProps.nextGeneration, nextProps.generationInterval);
		} else if (nextProps.generationInterval !== this.props.generationInterval && !nextProps.paused) {
			clearInterval(this.timer);
			this.timer = setInterval(nextProps.nextGeneration, nextProps.generationInterval);
		}
	}
	
	render () {
		const grid = this.props.grid.map((row, i) => {
			return (
				<div key={ i } className="Row">
					{ [...row].map((cell, j) => {
						return (
							<Cell
								key = { j }
								active = { this.props.grid[i][j] }
								position = { [i, j] }
								toggleSquare = { this.props.toggleSquare }
							/>
						);
					}) }
				</div>
			);
		});

		return (
			<div className="Grid">
				{ grid }
			</div>
		);
	}
}

const Cell = (props) => {
	return (
		<div
			className="Cell"
			style={{ background: props.active ? '#2DB9FF' : '#666' }}
			onClick={ () => props.toggleSquare(props.position[0], props.position[1]) }
		>
		</div>
	);
}

const BoardOptions = (props) => {
	return (
		<div className="BoardOptions">
			<ChangeBoardRow changeBoard = { props.changeBoard } size = { props.size }/>
			<ChangeIntervalRow changeInterval = { props.changeInterval } generationInterval = { props.generationInterval }/>
		</div>
	);
};

const ChangeBoardRow = (props) => {
	return (
		<div className="ChangeBoardRow">
			<div>
				Board Size: 
			</div>
			<div>
				<ChangeBoardButton changeBoard = { props.changeBoard } size = { props.size } length = { 50 } width = { 50 }/>
				<ChangeBoardButton changeBoard = { props.changeBoard } size = { props.size } length = { 70 } width = { 70 }/>
				<ChangeBoardButton changeBoard = { props.changeBoard } size = { props.size } length = { 90 } width = { 90 }/>
			</div>
		</div>
	);
};

const ChangeBoardButton = props => {
	return (
		<button
			onClick = { () => props.changeBoard(props.length, props.width) }
			className = { props.size[0] === props.length && props.size[1] === props.width ? 'button-red' : null }
		>
			{ props.length } x { props.width }
		</button>
	);
}

const ChangeIntervalRow = (props) => {
	return (
		<div className="ChangeIntervalRow">
			<div>
				Speed:
			</div>
			<div>
				<ChangeIntervalButton changeInterval = { props.changeInterval } generationInterval = { props.generationInterval } interval = { 300 } speed ="Slow" />
				<ChangeIntervalButton changeInterval = { props.changeInterval } generationInterval = { props.generationInterval } interval = { 150 } speed ="Medium" />
				<ChangeIntervalButton changeInterval = { props.changeInterval } generationInterval = { props.generationInterval } interval = { 75 } speed ="Fast" />
			</div>
		</div>
	);
};

const ChangeIntervalButton = (props) => {
	return (
		<button
			onClick = { () => props.changeInterval(props.interval) }
			className = { props.interval === props.generationInterval ? 'button-red' : null }
		>
			{ props.speed }
		</button>
	);
};

// ReactRedux
const mapStateToProps = (state) => {
	return {
		grid: state.grid,
		generationInterval: state.generationInterval,
		generation: state.generation,
		paused: state.paused,
		size: state.size
	};
};
const mapDispatchToProps = (dispatch) => {
	return {
		nextGeneration: () => dispatch(nextGeneration()),
		togglePause: () => dispatch(togglePause()),
		changeBoard: (length, width) => dispatch(changeBoard(length, width)),
		clearBoard: () => dispatch(clearBoard()),
		toggleSquare: (row, column) => dispatch(toggleSquare(row, column)),
		randomizeBoard: () => dispatch(randomizeBoard()),
		changeInterval: (interval) => dispatch(changeInterval(interval))
	};
};

const Container = ReactRedux.connect(mapStateToProps, mapDispatchToProps)(App);
const Provider = ReactRedux.Provider;
const Wrapper = () => {
	return (
		<Provider store = { store }>
			<Container/>
		</Provider>
	);
};

// Render
ReactDOM.render(<Wrapper/>, document.getElementById('root'));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console