Skip to content
Snippets Groups Projects
Example2TicTacToe.vue 1.77 KiB
Newer Older
<script setup>
import CircularCard from "./components/CircularCard.vue";
import DragGrid from "../../src/DragGrid.vue";
</script>

<template>
  <div class="ttt-container">
    <DragGrid
      :rows="3"
      :cols="3"
      v-model="ticTacToe1"
      class="tic-tac-toe"
      context="ticTacToe"
      :validate-element="randomKey"
    >
      <template #item="item">
        <CircularCard>
          {{ item.key.startsWith("a") ? "X" : "O" }}
        </CircularCard>
      </template>
    </DragGrid>
    <div>
      <p>
        These two are two different grids but we can drag from right to left!
      </p>
      <p>Drag items from the container on the right to play on the left.</p>
    </div>
    <DragGrid
      :rows="1"
      :cols="2"
      v-model="ticTacToe2"
      class="tic-tac-toe"
      context="ticTacToe"
      :pos-validation="blockAllMoving"
    >
      <template #item="item">
        <CircularCard>
          {{ item.data.text }}
        </CircularCard>
      </template>
    </DragGrid>
  </div>
</template>

<script>
export default {
  name: "Example2TicTacToe",
  methods: {
    blockAllMoving() {
      return false;
    },
    randomKey(element) {
      if (element.key.length !== 1) return;
      element.key += Math.random().toString(36).replace("0.", "");
    },
  },
  data() {
    return {
      ticTacToe1: [
        { x: 1, y: 1, w: 1, h: 1, key: "a1", data: { text: "X" } },
        { x: 3, y: 3, w: 1, h: 1, key: "b1", data: { text: "O" } },
      ],
      ticTacToe2: [
        { x: 1, y: 1, w: 1, h: 1, key: "a", data: { text: "X" } },
        { x: 2, y: 1, w: 1, h: 1, key: "b", data: { text: "O" } },
      ],
    };
  },
};
</script>

<style scoped>
.tic-tac-toe {
  max-width: 400px;
}

.ttt-container {
  display: flex;
  justify-content: space-between;
}
</style>