1.0.0 • Published 4 years ago

use-game-of-life v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

use-game-of-life

NPM JavaScript Style Guide

Install

npm install use-game-of-life

Usage

import useGameOfLife from 'use-game-of-life' ; 

const App  = () => {

  const {grid , setCell ,  start , stop , isRunning  } = useGameOfLife({
      updateInterval : 25 , gridRows : 50 , gridColumns : 50 , 
      randomizeGrid : true , randomGridAlivePercent : 30 
    }) ;

  return (
    <>
      <div className="App" style={{
        display: 'grid',
        gridTemplateColumns: 'repeat(50 , 1fr)',
        gridAutoRows: '19px'
      }} >
        {
          grid.map((row, r) =>
            row.map((col, c) =>
              <div key={`${r}-${c}`}
                style={{
                  border: '1px solid black',
                  backgroundColor: grid[r][c] ? 'green' : undefined
                }}
                onClick={() => {
                  setCell( r , c, !grid[r][c]) ;  
                  console.log("You clicked ", r, c)
                }}
              />
            )
          )
        }
      </div>
      <button onClick={() => { 
        if(isRunning()){
          stop() ; 
        }
        else{
          start() ; 
        }
      }}>{isRunning()? 'Stop' : 'Start'}</button>
    </>
  );
}

Prop defaults :

{ 
  updateInterval = 500,
  gridRows = 50, 
  gridColumns = 50, 
  randomizeGrid = false, 
  randomGridAlivePercent = 50  // applied when randomizeGrid is true
}

License

MIT © nateshmbhat