Clariity / react-chessboard

The React Chessboard Library used at ChessOpenings.co.uk. Inspired and adapted from the unmaintained Chessboard.jsx.
https://react-chessboard.vercel.app
MIT License
335 stars 101 forks source link

Issue with promotion #135

Closed barakdaniel1 closed 3 months ago

barakdaniel1 commented 5 months ago

Hello! I just started using this package. This is my code so far:

import { Chessboard } from 'react-chessboard'; import {Chess} from 'chess.js'; import { useState } from 'react'; import './App.css'

const App = () => { const [chess] = useState(new Chess()); const [board,setBoard] = useState("start");

const handleMove = (source, target, promotion) =>{ console.log(promotion); try{ const move = chess.move({from: source, to: target, promotion: promotion}); setBoard(chess.fen()); } catch(error){ console.log(error); return; }

}

return (

) }

export default App;

If I remove the "promotion" argument from my handleMove function, and just define the promotion to 'q' in the chess.move(), then everything is working. However, at this current state, no matter which piece I choose to promote to, I get an "Invalid move" error, despite having the correct arguments.

chessError

Thank you in advance for any help

P.S - I couldn't get my code to display here as code for some reason, so it's quite hard to read, sorry for that.

Manukyanq commented 5 months ago

Hi! Promotion field should reprezent to which piece you want to promote your pawn, which has reached to the final line. In chess.js library It can be only one of the listed values ['q', 'r', 'b', 'n']. Try this instead:

try{
    const move = chess.move({from: source, to: target, promotion:  promotion[1].toLowerCase() ?? "q"});
    setBoard(chess.fen());
}
barakdaniel1 commented 5 months ago

Hi! Promotion field should reprezent to which piece you want to promote your pawn, which has reached to the final line. In chess.js library It can be only one of the listed values ['q', 'r', 'b', 'n']. Try this instead:

try{
    const move = chess.move({from: source, to: target, promotion:  promotion[1].toLowerCase() ?? "q"});
    setBoard(chess.fen());
}

Brilliant, it worked!! Thank you so much, I can't believe I missed such a detail haha.