cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <div ng-app="gameOfLifeApp" ng-controller="gameOfLifeCtrl"> 
  
  <h3 style="text-align: center">Game Of Life</h3>
  <h5 style="text-align: center"><a target="_blank" href="https://youtu.be/SL75BV9khkY">Video Tutorial</a></h5>
 
   <table>
     <tr ng-repeat='row in rows'>
      <td ng-click=activate(rows.indexOf(row),$index) 
          ng-repeat='col in row.cols'
          ng-class="{'new':col.active == 1, 'old':col.active >1 }">        
     </tr>
   </table>
  
   <div class="components">
   <button ng-click="stopGame();">stop</button>
   <button ng-click="startGame(false);">start</button>
   <button ng-click="clear();">clear</button>
   Generation: {{generation}}
  </div>
   
  <div class="text-center" id="footer" style="text-align:center"> <!-- for footer -->
      <div class="col-xs-6">
      <h5><a href="http://www.eleftheriabatsou.com" target="_blank">Eleftheria</a> | <a href="https://codepen.io/EleftheriaBatsou" target="_blank">Projects</a> | <a href="https://www.youtube.com/channel/UCC-WwYv3DEW7Nkm_IP6VeQQ" target="_blank"> Coding videos </a></h5>
      </div>
      <!-- social media -->
      <div class="col-xs-6 text-primary">
        <a href="https://twitter.com/BatsouElef" target="_blank">
     <img src="https://left.gr/sites/all/themes/newleft/images/socialicons/twitter.png" 
      style="text-align:center" alt="twitter"> 
    </a>
         <a href="https://github.com/EleftheriaBatsou" target="_blank">
     <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSqSttU57LZvtB2Y77j2E2EkC6qgiTbYjVK75XU2SOzUXz2EFjX" 
      style="text-align:center" alt="github"> 
    </a>
        <a href="https://www.youtube.com/channel/UCC-WwYv3DEW7Nkm_IP6VeQQ" target="_blank">
     <img id="footerSvg" src="https://image.flaticon.com/icons/svg/25/25312.svg" 
       style="text-align:center" alt="youtube"> 
    </a>
      </div>
    </div> <!-- end footer -->
  
  
</div>
            
          
!
            
              body{background-color: lightgray;}

table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
table{ 
  margin: auto; 
  background-color: whitesmoke;}

td{
  width: 15px;
  height: 15px;
}

.new{ background-color:orange; }
.old{ background-color: orangered; }

.components{
  display:block;
  margin: 20px auto 20px 30%;
}

// FOOTER
a:link    {color:orangered;  text-decoration:none}
a:visited {color:orangered; text-decoration:none}
a:hover   {color:red; text-decoration:underline} 

#footerSvg{
  width: 32px;
}
            
          
!
            
              var app = angular.module('gameOfLifeApp', []);
app.controller('gameOfLifeCtrl', function($scope, $interval) {
  
  //grid size
  var rowMax = 25;
  var colMax = 30;
  
  // create grid
  $scope.rows = [];
  for(var i=0; i<rowMax; i++){
    var col = [];
    for(var j=0; j<colMax; j++){
      var elem = {'active': 0};
      col.push(elem);
    }
    $scope.rows.push({'cols': col});
  }
   
  
  //generate seeds
  var randomSeeds = function(){
    for(var i=0; i<(rowMax*colMax/2); i++){
      var row = Math.floor(Math.random() * (rowMax -  1));
      var col = Math.floor(Math.random() * (colMax -  1));
      $scope.activate(row,col);
    }
  }
 
  // count live neighbors
   var checkNeighbours = function(row, col){
     var count = 0;
     for(var i = row-1; i<=row+1; i++){
       for(var j= col-1; j<=col+1; j++){
         if((i!=row || j!=col) &&($scope.rows[i]) && ($scope.rows[i].cols[j]) ){
           if($scope.rows[i].cols[j].active >= 1){
             count++;
           }
         }
       }
     }
     return count;
   }
  
  $scope.stopGame = function(){
    $interval.cancel(interval);
  }
  
  $scope.activate = function(row, col){
    $scope.rows[row].cols[col].active = 1;
  }
  
  var interval;
  $scope.startGame = function(random){
    $scope.generation = 0;
    if(random){
      randomSeeds();
    }      
     interval = $interval(function(){
       $scope.generation++;
       for(var i=0; i<rowMax; i++){
         for(var j=0; j<colMax; j++){
           var noOfNeighbours = checkNeighbours(i,j);
           $scope.rows[i].cols[j].neighbours = noOfNeighbours;
         }
       }
       for(var i=0; i<rowMax; i++){
         for(var j=0; j<colMax; j++){
           if($scope.rows[i].cols[j].active >= 1){
             if($scope.rows[i].cols[j].neighbours >=4 || $scope.rows[i].cols[j].neighbours <=1){
               $scope.rows[i].cols[j].active = 0;
             }else{
               $scope.rows[i].cols[j].active++;
             }
           }else{
             if($scope.rows[i].cols[j].neighbours == 3){
               $scope.rows[i].cols[j].active = 1;
             }
           }
         }
       }     
   }, 100);
  }
  
  $scope.startGame(true);

  $scope.clear = function(){
    for(var i=0; i<rowMax; i++){
      for(var j=0; j<colMax; j++){
        $scope.rows[i].cols[j].active=0;
      }    
    }
  }
  
 });
// board size
// optimize code please

            
          
!
999px
Loading ..................

Console