qwerty084 / vue3-chessboard

Vue 3 chessboard component, built with lichess chessground and chess.js
https://qwerty084.github.io/vue3-chessboard-docs/
GNU General Public License v3.0
70 stars 16 forks source link

OnMove method? #47

Closed fabthegreat closed 1 year ago

fabthegreat commented 1 year ago

Hi, great job! Is there a way to retrieve a move (whatever the format) once it has been played?

Thanks

qwerty084 commented 1 year ago

Hey,

you could register a afterMoveCallback function and then get the last move from the lichess chessground.

<script setup lang="ts">
import { ref, toRaw } from 'vue';
import { TheChessboard, type ChessboardAPI, type BoardConfig } from '@/index';
import '@/assets/board.css';
const boardAPI = ref<ChessboardAPI>();

function afterMove() {
  console.log(toRaw(boardAPI.value?.board.state.lastMove));
}

const boardConfig: BoardConfig = {
  coordinates: true,
  autoCastle: false,
};
</script>

<template>
  <main>
    <TheChessboard
      :board-config="boardConfig"
      :after-move-cb="afterMove"
      @board-created="(api) => (boardAPI = api)"
    />
  </main>
</template>

Output:

Array [ "e2", "e4" ]

Please let me know if this works for you.

I will probably add some more methods to the boardapi in the next version, including a method to get the last move easier and with more information.

fabthegreat commented 1 year ago

Thanks for your answer! I finally coded my chessboard from scratch ;) but it is far less good looking than yours for the moment! For now my main concern is about the possible moves on the board, it works based on chessjs lib but it takes a huge amount of time (something like 180ms for a Queen with many possible moves). It is related to the chessjs lib itself, maybe I'll give a try with another one.

qwerty084 commented 1 year ago

I also use chess.js to generate the possibles moves. Its working fine.

Im going to close this for now...