Advertisement
PifyZ

Untitled

Dec 19th, 2016
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const can = document.getElementById('app')
  2. const ctx = can.getContext('2d')
  3.  
  4. const CELL_SIZE = 24
  5.  
  6. const W = 36
  7. const H = 18
  8.  
  9. can.width = W * CELL_SIZE
  10. can.height = H * CELL_SIZE
  11.  
  12. let grid = [
  13.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  14.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  15.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  16.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
  17.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
  18.   [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
  19.   [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
  20.   [1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  21.   [1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
  22.   [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
  23.   [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  24.   [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  25.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  26.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  27.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  28.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  29.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  30.   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  31. ]
  32.  
  33. function render() {
  34.   ctx.strokeStyle = 'rgb(128, 128, 128)'
  35.  
  36.   for (let y = 0 ; y < H ; y++) {
  37.     for (let x = 0 ; x < W ; x++) {
  38.       let cell = grid[y][x]
  39.  
  40.       if (cell == 0)
  41.         ctx.fillStyle = 'rgb(255, 255, 255)'
  42.       else
  43.         ctx.fillStyle = 'rgb(0, 0, 0)'
  44.  
  45.       ctx.fillRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
  46.       ctx.strokeRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
  47.     }
  48.   }
  49. }
  50.  
  51. function getCellValue(x, y) {
  52.   if (typeof grid[y] != 'undefined')
  53.     if (typeof grid[y][x] != 'undefined')
  54.       return grid[y][x]
  55.  
  56.   return 0
  57. }
  58.  
  59. function getNbNeighborsAlive(x, y) {
  60.   return getCellValue(x - 1 , y - 1) +
  61.     getCellValue(x, y - 1) +
  62.     getCellValue(x + 1, y - 1) +
  63.     getCellValue(x - 1, y) +
  64.     getCellValue(x + 1, y) +
  65.     getCellValue(x - 1, y + 1) +
  66.     getCellValue(x, y + 1) +
  67.     getCellValue(x + 1, y + 1)
  68. }
  69.  
  70. function update() {
  71.   render()
  72.  
  73.   let newGrid = []
  74.  
  75.   for (let y = 0 ; y < H ; y++) {
  76.     newGrid[y] = []
  77.  
  78.     for (let x = 0 ; x < W ; x++) {
  79.       let cell = grid[y][x]
  80.       let nbNeighborsAlive = getNbNeighborsAlive(x, y)
  81.  
  82.       if (cell == 0 && nbNeighborsAlive == 3)
  83.         newGrid[y][x] = 1
  84.       else if (cell == 1 && (nbNeighborsAlive == 2 || nbNeighborsAlive == 3))
  85.         newGrid[y][x] = 1
  86.       else
  87.         newGrid[y][x] = 0
  88.     }
  89.   }
  90.  
  91.   grid = newGrid
  92.  
  93.   setTimeout(update, 100)
  94. }
  95.  
  96. update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement