[Vuejs]-Vue matching game turns over all matches on single click

0👍

createGameCardsArray creates an array of references to the same instance of items[random index], so editing the card from one reference would reflect the change in all references to the same card. The solution is to clone each card with the spread operator (or Object.assign):

function createGameCardsArray() {
  const cards = [];
  for (let i = 0; i <= 17; i++) {
    cards[i] = { ...items[Math.floor(Math.random() * 6)] };
  }
  return cards;
}
// This is all the data for each card
const items = [
  {
    front: 'images/cardfront.png',
    back: 'images/coin10.png',
    name: 'coin10',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/coin20.png',
    name: 'coin20',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/flower.png',
    name: 'flower',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/mushroom.png',
    name: 'mushroom',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/oneup.png',
    name: 'oneup',
    flipped: false
  },
  {
    front: 'images/cardfront.png',
    back: 'images/star.png',
    name: 'star',
    flipped: false
  }
];


// This is a function to create an array of 18 randomly shuffled cards
function createGameCardsArray() {
  const cards = [];
  for (let i = 0; i <= 17; i++) {
    cards[i] = { ...items[Math.floor(Math.random() * 6)] };
  }
  return cards;
}

new Vue({
  el: '#app',
  data: {
    // This holds the array of randomly shuffled cards
    cards: createGameCardsArray(),
    // gameSounds: audio,
    started: false
  },
  methods: {
    // When this method is called, all matches are flipped
    flipCard: function(card) {
      card.flipped = !card.flipped;
    },
    start: function() {
      console.log(this.cards);
      this.started = true;
      //this.gameSounds.background.play();
      //this.gameSounds.background.loop = true;
    },
    stop: function() {
      this.started = false;
      this.gameSounds.background.pause();
      this.gameSounds.background.currentTime = 0
      this.gameSounds.background.loop = false;
    }
  }
})
<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>

<div id="app">
  <div class="container" id="matchGame">
    <div class="col-6 mx-auto bg-dark" style="margin-top:230px;">
      <h3 class="text-light text-center pt-3">Mario Bros Matching Game</h>
      <button v-on:click="start" v-show="!started" class="btn btn-lg btn-success mb-2">Start</button>
      <ul v-if="started" class="d-flex flex-sm-wrap justify-content-between mt-5 pb-5">
        <li class="mb-3 ml-2" v-for="(card, index) in cards" v-on:click="flipCard(card)">
          <transition name="flip">
            <span v-on:click="flipCard(index)" v-if="!card.flipped">FRONT: {{ card.front }}</span>
            <span v-on:click="flipCard(index)" v-else>BACK: {{ card.back }}</span>
          </transition>
        </li>
      </ul>
      <button v-on:click="stop" v-show="started" class="btn btn-danger mb-3">Stop</button>
    </div>
  </div>
</div>

Leave a comment