I am trying to make a chess game in react.js using the libraries chess.js and chessboard.jsx
Currently i am trying to implement move validation with the following code :-
import "./App.css";
import React, { useState } from "react";
import Chessboard from "chessboardjsx";
import * as ChessJS from "chess.js"; // import { Chess } from 'chess.js' gives an error
function App() {
const Chess = typeof ChessJS === "function" ? ChessJS : ChessJS.Chess; // For VS code intellisence to work
const game = new Chess();
const [position, setPosition] = useState("start");
const onDropMove = ({ sourceSquare, targetSquare }) => {
const move = game.move({
from: sourceSquare,
to: targetSquare,
promotion: "q",
});
if (move === null) return;
setPosition(game.fen());
};
return <Chessboard position={position} onDrop={onDropMove} />;
}
export default App;
But the problem coming is:- For example: - When i move white pawn from g2 to g3 it works but then if i try to move the black pawn from g7 to g6, it counts it as an illegal move(i checked by console.log(move)) although it is legal.
I don't have any idea why this is occurring. Also, I did not find any answers online regarding this issue