text: function make2DArray(cols, rows) {
    let arr = new Array(cols)
    for (let i = 0; i < arr.length; i++) {
      arr[i] = new Array(rows)
    }
    return arr
  }
  
  let grid
  let rows
  let cols
  let resolution = 10
  let energy = 0
  let listOfEnergy = []
  
  function setup() {
    createCanvas(1000, 600)
    cols = width / resolution
    rows = height / resolution
  
    // Creating the grid
    grid = make2DArray(cols, rows)
  
    // Creating the random the cells initial disposition
    // energy: this listOfEnergy tracks the number of alive cells at the start
    for (let i = 0; i < cols; i++) {
      for (let j = 0; j < rows; j++) {
        grid[i][j] = floor(random(2))
        if (grid[i][j] == 1) {
          energy += 1
        }
      }
    }
    // let's collect energy over time
    listOfEnergy.push(energy)
  }
  
  function draw() {
    background(0)
  
    let next = make2DArray(cols, rows)
  
    for (let i = 0; i < cols; i++) {
      for (let j = 0; j < rows; j++) {
        let x = i * resolution
        let y = j * resolution
        if (grid[i][j] == 1) {
          fill(255)
          circle(x, y, resolution - 1)
        }
      }
    }
  
    // Compute next based on grid
  
    for (let i = 0; i < cols; i++) {
      for (let j = 0; j < rows; j++) {
        let state = grid[i][j]
  
        // // Edge
        // if (i == 0 || i == cols - 1 || j == 0 || j == rows - 1){
        //   next[i][j] = state;
        // }
        // else{
  
        // Compute live neightbors
        let neightbours = countNeighbors(grid, i, j)
  
        if (state == 0 && neightbours == 3) {
          next[i][j] = 1
          energy += 1 // if a new cell arises energy rises
          listOfEnergy.push(energy)
        } else if (state == 1 && (neightbours < 2 || neightbours > 3)) {
          next[i][j] = 0
          energy -= 1 // if a cell dies energy decreases
          listOfEnergy.push(energy)
        } else {
          next[i][j] = state
          energy += 0
          listOfEnergy.push(energy)
        }
        // if (next[i][j] == 1){
        //   energy += 1
        //   listOfEnergy.push(energy)
        // }
        // else if (next[i][j] == 0){
        //   energy -= 1
        //   listOfEnergy.push(energy)
        // }
        // }
        //console.log(energy)
      }
    }
  
    grid = next
  
  
    // if (listOfEnergy.length > 2_000_000){
    //   let values = new Set(listOfEnergy).length
    //   if (values < listOfEnergy.length/2){
    //     console.log(listOfEnergy)
    //   }
    // }
  
    // if (listOfEnergy.length > 100000){
    //   if (listOfEnergy[listOfEnergy.length-1] == listOfEnergy[listOfEnergy.length-50000]  && listOfEnergy[listOfEnergy.length-1] == listOfEnergy[listOfEnergy.length-100000-3]){
    //     noLoop()
    //     console.log(listOfEnergy)
    //     sendToServer(listOfEnergy)
    //   }
    // }
  }
  
  
  function mouseClicked() {
    noLoop()
    console.log(listOfEnergy)
    sendToServer(listOfEnergy)
    window.location.reload();
  }
  
  function countNeighbors(grid, x, y) {
    let sum = 0
    for (let i = -1; i < 2; i++) {
      for (let j = -1; j < 2; j++) {
        let col = (x + i + cols) % cols
        let row = (y + j + rows) % rows
  
        sum += grid[col][row]
      }
    }
    sum -= grid[x][y]
    return sum
  }
  
  function sendToServer(data) {
    let xhr = new XMLHttpRequest()
    xhr.open("POST", "http://localhost:3000/saveData", true)
    xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8")
    xhr.send(JSON.stringify(data))
  }
  