Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>Game of life</title>
</head>
<body>
    <button id="go">On / Off</button>        
    <button id="go2">Random</button>
    <div id="container">
    </div>
    <script src="script.js"></script>
</body>
</html>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
}

#container {
  display: grid;
  grid-template-columns: repeat(60, 1fr);
  grid-gap: 1px;
  width: 800px;
  height: 800px;
}

#container div {
  background-color: darksalmon;
  cursor: pointer;
}

#container .alive {
  background-color: brown;
}

button {
  background-color: #4caf50;
  border: none;
  color: white;
  padding: 10px 27px;
  margin: 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 12px;
  cursor: pointer;
}

.off {
  background-color: #f44336;
}

              
            
!

JS

              
                // wymiary grida
let columns = 60;
let rows = 60;

// tablica która trzyma komórki
const grid = new Array(columns);
for(let i = 0; i < grid.length; i++) {
    grid[i] = new Array(rows);
}

// wirtualna tablica która przechowuje stan gry
const grid2 = new Array(columns);
for(let i = 0; i < grid2.length; i++) {
    grid2[i] = new Array(rows);
}

// domyślnie wszystkie komórki martwe
for (let i = 0; i < rows; i++) { 
    for (let j = 0; j < columns; j++) { 
        grid2[i][j] = false; 
    } 
} 

// rysowanie grida
function drawGrid(){
    let container = document.getElementById("container");
    container.innerHTML = ""
    for (let i = 0; i < rows; i++) { 
        for (let j = 0; j < columns; j++)    { 
            let cell = document.createElement("div");            
            if(grid2[i][j]) {
                cell.classList.add("alive");
            }
            cell.addEventListener("click", () => {
                cell.classList.toggle("alive");                
            });
            grid[i][j] = cell;
            container.append(cell);
        }     
    }
}

// liczenie zywych sąsiadów
function countLiveNeighbors(row, col) {
    let count = 0;
    for(let i = row - 1; i <= row + 1; i++) {
        if (i >= 0 && i < rows){
            for(let j = col - 1; j <= col + 1; j++) {
                if (j >= 0 && j < columns){
                    if (i != row || j != col) {
                        count += grid[i][j].classList.contains("alive") ? 1 : 0;                        
                    }                    
                }                
            }            
        }            
    }
    return count;
}


let go = document.getElementById("go");
let go2 = document.getElementById("go2");

// inicjalizacja pustego grida
drawGrid();

// zyćko
function life(){
    for (let i = 0; i < rows; i++) { 
        for (let j = 0; j < columns; j++) { 
            if(grid[i][j].classList.contains("alive")) {
                grid2[i][j] = true;
            }
            if(countLiveNeighbors(i, j) == 3) {
                grid2[i][j] = true;
            }  
            if(countLiveNeighbors(i, j) != 2 && countLiveNeighbors(i, j) != 3) {
                grid2[i][j] = false;
            }                      
        } 
    } 
    drawGrid();
}
go.addEventListener("click", ()=>{
    change();
    go.classList.toggle('off');
});

let alive;

function change() {
    if (!alive) {
        alive = window.setInterval(life,100);
    } else {
        window.clearInterval(alive);
        alive = null;
    }
}

go2.addEventListener("click", ()=>{
    for (let i = 0; i < rows; i++) { 
        for (let j = 0; j < columns; j++) { 
            grid2[i][j] = Math.random() > 0.5 ? true : false;
        } 
    } 
    drawGrid();
});
              
            
!
999px

Console