Untitled
unknown
plain_text
8 days ago
1.0 kB
2
Indexable
import { useState } from "react"; import Chess from "chess.js"; import Chessboard from "react-chessboard"; export default function ChessGame() { const [game, setGame] = useState(new Chess()); function makeMove(move) { const newGame = new Chess(game.fen()); const result = newGame.move(move); if (result) { setGame(newGame); setTimeout(() => makeAIMove(newGame), 500); } } function makeAIMove(newGame) { const moves = newGame.moves(); if (moves.length > 0) { const randomMove = moves[Math.floor(Math.random() * moves.length)]; newGame.move(randomMove); setGame(new Chess(newGame.fen())); } } return ( <div className="flex flex-col items-center gap-4"> <h1 className="text-xl font-bold">Joue contre l'IA</h1> <Chessboard position={game.fen()} onPieceDrop={(sourceSquare, targetSquare) => { makeMove({ from: sourceSquare, to: targetSquare, promotion: "q" }); }} /> </div> ); }
Editor is loading...
Leave a Comment