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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <script type="text/x-template" id='cellular-automata'>
    <table>
      <v-timer time="50" @buzz="next" v-if="play"/>
      <tr v-for="cellsLine, lineId of cellsYX" :key="lineId">
        <td v-for="cell, rowId of cellsLine" :key="rowId" :class="{ alive: cell === 1 }" @click="toggleCell(lineId, rowId)"></td>
      </tr>
    </table>
</script>

<div id="app">
  <div class="controles">
    <button @click="playPauseToggle">{{ isPlaying ? '&#9646;&#9646;' : '&#9654;' }}</button>
    <input v-model="rule" />
    <input v-model="size" />
  </div>
  <cellular-automata :rule="rule" :size="parseInt(size)" :play='isPlaying' />
</div>
              
            
!

CSS

              
                table
  border-spacing: 0px
  border-collapse: separate
  margin: auto
td
  width: 5px
  height: 5px
  background: #ccc
  &.alive
    background: #444

.controles
  margin: 50px auto 20px
  text-align: center
              
            
!

JS

              
                var vTimer = {
  props: ['time'],
  data () {
    return { timer: undefined }
  },
  // S'execute lors de l'apparition de l'element
  beforeMount () {
    this.timer = setInterval(e => this.$emit("buzz"), this.time)
  },
  // Se supprime lors de la disparition de l'element
  beforeDestroy () {
    clearInterval(this.timer)
  },
  watch: {
    time () {
      this.reset()
    }
  },
  reset () {
    clearInterval(this.timer)
    this.timer = setInterval(e => this.$emit("buzz"), parseFloat(this.time)*1000)
  }
}

var cellularAutomata = {
  template: '#cellular-automata',
  props: ['rule', 'size', 'play'],
  components: { vTimer },
  computed: {
    birthRules () {
      return this.rule.split('/')[1]
    },
    surviveRules () {
      return this.rule.split('/')[0]
    }
  },
  data () {
    // Initialiser un array permet de le créer avec x éléments vide au départ
    // que l'on fill avec des 0
    let cellsYX = Array(60).fill(0).map((i)=> Array(120).fill(0))
    cellsYX = cellsYX.map(i => i.map( j => Math.random() < 0.01 ? 1 : 0))
    
    console.log(cellsYX)
    return {
      cellsYX,
      isPlaying: false
    }
  },
  watch: {
    size () {
      this.cellsYX = Array(this.size).fill(0).map((i)=> Array(this.size).fill(0))
    }
  },
  methods: {
    toggleCell(lineId, rowId) {
      let cell = this.cellsYX[lineId][rowId]
      Vue.set(this.cellsYX[lineId], rowId, (cell + 1) % 2)
    },
    next () {
      Vue.set(this, 'cellsYX', this.getNextCellsYX())
    },
    getNearAliveCellsCount(lineId, rowId) {
      let nearCellsAlive = 0
      let s = rowId > 0
        ? rowId - 1
        : 0
      // +2 car slice est non inclusif sur l'index de fin
      let e = rowId + 2
      
      // Ligne precedente
      if (this.cellsYX[lineId - 1]) {
        nearCellsAlive += this.cellsYX[lineId - 1].slice(s, e).reduce((stack, current) => stack + current, 0)
      }
      // Ligne suivante
      if (this.cellsYX[lineId + 1]) {
        nearCellsAlive += this.cellsYX[lineId + 1].slice(s, e).reduce((stack, current) => stack + current, 0)
      }
      nearCellsAlive += this.cellsYX[lineId    ].slice(s, e).reduce((stack, current) => stack + current, 0)
      
      // On soustrait la cible au résultat, car elle est au centre de la grille
      return nearCellsAlive - this.cellsYX[lineId][rowId]
    },
    getNextCellsYX () {
       return this.cellsYX.map((cellsLine, lineId) => {
         return cellsLine.map((cell, rowId) => {
           let currentCellIsAlive = cell
           let nearCellsAlive = this.getNearAliveCellsCount(lineId, rowId)
           
           if (currentCellIsAlive === 0 && this.birthRules.indexOf(nearCellsAlive) !== -1 ) {
             return 1
           } else if (currentCellIsAlive === 1 && this.surviveRules.indexOf(nearCellsAlive) === -1 ) {
             return 0
           } else {
             return cell
           }
         })
      })
    }
  }
}


new Vue({
  el: '#app',
    components: { cellularAutomata },
  data: {
    rule: '12345/3',
    size: 100,
    isPlaying: false
  },
  watch: {
    size () {
      this.cellsYX = Array(this.size).fill(0).map((i)=> Array(this.size).fill(0))
    }
  },
  methods: {
    playPauseToggle () {
      this.isPlaying = !this.isPlaying
    }
  }
 });
              
            
!
999px

Console