123

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.

            
              @mixin center {text-align: center;}

$dark: #333;
$darker: #222;
$boarder: 1px solid black;

body {  
  @include center;
  background-color: #7e3878;
}

tr:first-child td {
  border-top: $boarder;
}

#app{
    margin-right: auto; 
    margin-left: auto;
}

.maintitle{   
    @include center;
    margin-top: 35px;
    margin-bottom: 25px;
    font-size: 50px;
    color: white;
}
.but{
    @include center;
    margin-bottom: 25px;
}
button{   
    margin-left: 10px;
}

.Generation{
    margin-bottom: 0;
    @include center;
    background-color: $dark;
    padding-left: 40px;
    padding-right: 40px;   
    padding-top: 10px;
    display:inline-block;
    color: white;
    font-size: 25px;
  width: 260px;
  text-align: left;
}

table{
	background-color: $darker;
    border: 10px solid $dark;
    border-spacing: 1px;
    margin-right: auto; 
    margin-left: auto;
}

td {   
    width: 1em;
    height: 1em;
    border-collapse: collapse;
    border-right: $boarder;
  border-bottom: $boarder;  
  border-left: $boarder; 
}

.live {
	background-color: #ddd;
}

.new {
    background-color: #00a300;
}

.killed {
    background-color: #A20025;
}
            
          
!
            
              /*
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 = [], x=[];
var setInter;
var row = 25;
var column = 40;
var gen=0;
var idName;
var addArr=[];

const set1=()=>{//pause game;
  gen=0
  clearInterval(setInter)
    setInter=false;
};

let boardArr = (row, column, initial, random=false) => {
  arr=JSON.parse(JSON.stringify([]));
  for(let i=0; i<row; i++){
  arr[i]=[];
  for(let 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;
}

boardArr(row, column, 3, true)

let change = () => {//core function apply 4 game rules;
      arr.forEach((row, i)=>{
  row.forEach((col, j)=>{
    let 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));
    return arr;
    }

const change1 = () => {
  return {
    type: 'CHANGE'
  }
};

const clear1 = () => {
  return {
    type: 'CLEAR'
  }
};

const add1 = () => {
  return {
    type: 'ADD'
  }
};

const random1 = () => {
  return {
    type: 'RANDOM'
  }
};

const big1 = () => {
  return {
    type: 'BIG'
  }
};

const back1 = () => {
  return {
    type: 'BACK'
  }
};

const boardReducer = (state=arr, action)=>{
  switch (action.type) {
    case 'CHANGE':
    return change();
    case 'CLEAR':
    arr=JSON.parse(JSON.stringify(boardArr(row, column, 0, false)));
    return arr;
    case 'RANDOM':
    arr=JSON.parse(JSON.stringify(boardArr(row, column, 0, true)));
    return arr;
    case 'BIG':
    row=40; column=60;
    arr=JSON.parse(JSON.stringify(boardArr(row, column, 0, true)));
    return arr;
    case 'BACK':
    row=25; column=40;
    arr=JSON.parse(JSON.stringify(boardArr(row, column, 0, true)));
    return arr;
    case 'ADD':
    addArr=JSON.parse(JSON.stringify(state));
      //addArr=[...state.map(s => [...s])];
      console.log(...state)
    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';}
    //addArr[idName[0]][idName[1]]=1;
    arr=$.extend(true, [], addArr);
    return arr;
    default: 
      return state;
  }
}

const store = Redux.createStore(boardReducer);

const Provider = ReactRedux.Provider;
const connect = ReactRedux.connect;



const Td = (props) => { 
  let td1 = props.data.map((v,j)=>{
    let cName=v[1];

    return (<td id={props.ind+'-'+j} className={cName} onClick={props.add}></td>)
  });
  return (<div>{td1}</div>)
}

class Game extends React.Component{
  constructor(props){
    super(props)
  }

  tableMaker = () => {    
    return this.props.data.map((val, i)=>{
        return (       
          <tr id={'i-'+i}>          
            <Td state={this.props.data} data={val} ind={i} add={this.handleAdd}/>            
          </tr>        
      )               
    })
  };

  componentDidMount () {
    if(!setInter){
      setInter=setInterval(this.props.makeNewBoard, 100);
    }       
  }

  handleStop = () => {
    clearInterval(setInter)
    setInter=false;
  };

  handleResume = ()=> {
    if(!setInter){
      setInter=setInterval(this.props.makeNewBoard, 100);
    }   
  };

  handleClear = () => {
    set1()
    this.props.makeClearBoard();
   
  };

  handleRandom = () => {   
  set1();
    this.props.makeRandomBoard();
    if(!setInter){setInter=setInterval(this.props.makeNewBoard, 100);}

  };

  handleBig = () => {
    set1()
    this.props.makeBigBoard();
    if(!setInter){setInter=setInterval(this.props.makeNewBoard, 100);}
  };

  handleBack = () => {
    set1()
    this.props.makeBackBoard();
    if(!setInter){setInter=setInterval(this.props.makeNewBoard, 100);}
  };

  handleNext = () => {
    clearInterval(setInter)
    setInter=false;
    this.props.makeNewBoard()
  };

  handleAdd = (e) => {
    idName=e.target.id.split('-');//return current clicking tag id name's array, idName[0]=row, idName[1]=column;  
    console.log(idName)
    //$(e.target).attr('class', 'live');
    this.props.makeAddBoard();
  };

  render () {
    return (
      <div>
        <div className='maintitle'>Game of Life</div>  
      <div className='but'>
      <button className="btn btn-success" onClick={this.handleResume}>Run</button>
      <button className="btn btn-warning" onClick={this.handleStop}>Pause</button>
      <button className="btn btn-info" onClick={this.handleNext}>Next</button>
      <button className="btn btn-danger" onClick={this.handleClear}>Clear</button>
      <button className="btn btn-primary" onClick={this.handleRandom}>Random</button>
      <button className="btn btn-secondary" onClick={this.handleBack}>Size: 25X40</button>
      <button className="btn btn-secondary" onClick={this.handleBig}>Size: 40X60</button> 
      </div>
      <h3 className='Generation'>Generation:&nbsp;{gen}</h3>        
      <table>
      <tbody>
      {this.tableMaker()}
      </tbody>
      </table>
      </div>     
    )
  }
}

const mapStateToProps = (state) => {
  return {data: state}
}

const mapDispatchToProps = (dispatch) => {
  return {
    makeNewBoard: () => {
    dispatch(change1())
    },
    makeClearBoard: () => {
      dispatch(clear1())
    },
    makeRandomBoard: () => {
      dispatch(random1())
    },
    makeBigBoard: () => {
      dispatch(big1())
    },
    makeBackBoard: () => {
      dispatch(back1())
    },
    makeAddBoard: () => {
      dispatch(add1())
    }
  }  
}

const Container = connect (mapStateToProps, mapDispatchToProps)(Game);

class AppWrapper extends React.Component {
  render () {
    return (
      <Provider store={store}>
      <Container/>
      </Provider>
    )
  }
}

ReactDOM.render(<AppWrapper />,
  document.getElementById('app'));




            
          
!
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