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

              
                <script type="x-template" id="board-checker-template">
  <transition
    @enter="enter"
    :css="false">
    <circle
      :cx="centerX"
      :cy="centerY"
      :r="checkerRadius"
      :fill="adjustedColor"
      />
  </transition>
</script>

<script type="x-template" id="board-column-template">
  <svg :x="col * cellSize" y="0">
    <g @click="drop(col)" class="column">
      <board-checker
        v-for="checker in checkers"
        :key="key(checker)"
        :checker="checker"
        :cellSize="cellSize"
        :rowCount="rowCount"
        :checkerRadius="checkerRadius"
        :status="status"
        />
      <rect
        :key="col"
        :col="col"
        :width="cellSize"
        :height="boardHeight"
        :fill="color"
        :mask="mask"
        />
    </g>
  </svg>
</script>

<script type="x-template" id="game-board-template">
  <svg width="350px"
    :viewBox="`0 0 ${boardWidth} ${boardHeight}`"
    xmlns="http://www.w3.org/2000/svg"
    class="game-board" stroke="none">
    <defs>
      <pattern :id="patternId" patternUnits="userSpaceOnUse" :width="cellSize" :height="cellSize">
        <circle :cx="cellSize / 2" :cy="cellSize / 2" :r="checkerRadius" fill="black"></circle>
      </pattern>
      <mask :id="maskId">
        <rect :width="cellSize" :height="boardHeight" fill="white"></rect>
        <rect :width="cellSize" :height="boardHeight" :fill="pattern"></rect>
      </mask>
    </defs>
    <board-column
      v-for="col in cols"
      :key="col"
      :checkers="colCheckers(col)"
      :col="col"
      :color="'cadetblue'"
      :cellSize="cellSize"
      :checkerRadius="checkerRadius"
      :boardHeight="boardHeight"
      :rowCount="rowCount"
      :mask="mask"
      @drop="drop"
      />
  </svg>
</script>

<script type="x-template" id="game-container-template">
  <game-board
    :checkers="checkers"
    :rowCount="rowCount"
    :colCount="colCount"
    @drop="drop"
    ></game-board>
</script>

<div id="app">
  <h3>Connect Four</h3>
  <p>Click columns to add checkers</p>
  <game-container></game-container>
</div>
              
            
!

CSS

              
                body {
  background-color: azure;
}

a {
  text-decoration: none;
}

.logo h3 {
  margin: 0;
}

.game-board {
  border: 5px cadetblue solid;

  width: 350px;
  margin: 0 auto;
}

.column {
  cursor: pointer;
}

#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
              
            
!

JS

              
                const range = num => [...Array(num).keys()];

const key = (row, col) => `${row}${col}`;
const cssUrl = id => `url(#${id})`;

const RED = 'red';
const BLACK = 'black';
const EMPTY = 'empty';

const BoardChecker = Vue.component('board-checker', {
  template: '#board-checker-template',
  props: ['checker', 'cellSize', 'rowCount', 'checkerRadius'],

  data() {
    return {
      colorHexes: {
        red: '#FC7E69',
        black: '#254689',
      },
    };
  },

  computed: {
    row() { return this.checker.row; },
    col() { return this.checker.col; },
    color() { return this.checker.color; },

    adjustedColor() {
      return this.colorHexes[this.color];
    },
    
    centerX() {
      return (this.cellSize / 2);
    },

    centerY() {
      return (this.cellSize / 2) + (this.cellSize * (this.rowCount - 1 - this.row));
    },

    fromY() {
      return -1 * (this.centerY + this.cellSize);
    },

    destY() {
      return 0;
    },
  },

  methods: {
    duration() {
      const percentage = (this.rowCount - this.row) / this.rowCount;

      return 0.2 + 0.4 * percentage;
    },
    
    enter(el, done) {
      const fromParams = { y: this.fromY };
      const destParams = {
        y: this.destY,
        ease: Bounce.easeOut,
        onComplete: done,
      };

      return TweenMax.fromTo(el, this.duration(), fromParams, destParams);
    },
  },
});

const BoardColumn = Vue.component('board-column', {
  template: '#board-column-template',
  props: ['checkers', 'col', 'color', 'cellSize', 'boardHeight', 'checkerRadius', 'rowCount', 'mask', 'status'],

  components: {
    BoardChecker,
  },

  computed: {
    // Find the current max occupied row and add 1
    nextOpenRow() {
      return Math.max(...this.checkers.map(c => c.row).concat(-1)) + 1;
    },
  },

  methods: {
    key({ row, col }) {
      return `${row}${col}`;
    },
    
    drop() {
      const col = this.col;
      const row = this.nextOpenRow;

      if (row < this.rowCount) {
        this.$emit('drop', { row, col });
      } else {
        console.log('cannot drop', { row, col });
      }
    },
  },
});

const GameBoard = Vue.component('game-board', {
  template: '#game-board-template',
  props: ['checkers', 'rowCount', 'colCount'],
  components: {
    BoardColumn,
  },

  data() {
    return {
      patternId: 'cell-pattern',
      maskId: 'cell-mask',
      cellSize: 100,
    };
  },

  computed: {
    pattern() { return cssUrl(this.patternId); },
    mask() { return cssUrl(this.maskId); },

    rows() { return range(this.rowCount); },
    cols() { return range(this.colCount); },

    boardWidth() { return this.colCount * this.cellSize; },
    boardHeight() { return this.rowCount * this.cellSize; },
    checkerRadius() { return this.cellSize * 0.45; },
  },

  methods: {
    colCheckers(col) {
      return Object.values(this.checkers).filter(c => c.col === col)
              .sort((a, b) => a.row - b.row);
    },
    drop(data) {
      this.$emit('drop', data);
    },
  },
});

const GameContainer = Vue.component('game-container', {
  template: "#game-container-template",
  
  components: {
    GameBoard,
  },
  
  data() {
    return {
      checkers: {},
      playerColor: RED,
      rowCount: 6,
      colCount: 7,
    };
  },

  methods: {
    key,

    toggleColor() {
      if (this.playerColor === RED) {
        this.playerColor = BLACK;
      } else {
        this.playerColor = RED;
      }
    },

    setChecker({ row, col, color }) {
      return Vue.set(this.checkers, key(row, col), { row, col, color });
    },
    
    getChecker({ row, col }) {
      return this.checkers[key(row, col)] || { row, col, color: 'empty' };
    },
    
    drop({ col, row }) {
      const color = this.playerColor;
      
      console.log('setting checker', key(row, col), { row, col, color });
      this.setChecker({ row, col, color });
      this.toggleColor();     
    },    
  },
});

new Vue({
  el: '#app',
});
              
            
!
999px

Console