-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_working.tsx
54 lines (46 loc) · 1.26 KB
/
test_working.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useEffect, useState } from "react";
import { Chess } from "chess.js";
import { Chessboard } from "react-chessboard";
import pgn from "./pgn/example_pgn.pgn?raw";
var stockfish = new Worker("src/lib/stockfish.js");
stockfish.addEventListener("message", function (e) {
console.log(e.data);
const a: String = e.data;
if (a.startsWith("bestmove")) {
console.log(a.split("bestmove"));
}
});
const PlayRandomMoveEngine = () => {
const [game, setGame] = useState(new Chess());
const [undoCount, setUndoCount] = useState(0);
let [initialMoves, setInitialMoves] = useState([] as any);
useEffect(() => {
const b = game.load_pgn(pgn);
console.log("isValid", b);
setInitialMoves(game.history());
}, []);
return (
<>
<button
disabled={undoCount === initialMoves.length}
onClick={() => {
game.undo();
setUndoCount(undoCount + 1);
}}
>
Voltar
</button>
<button
disabled={undoCount === 0}
onClick={() => {
game.move(initialMoves[initialMoves.length - undoCount]);
setUndoCount(undoCount - 1);
}}
>
Avançar
</button>
<Chessboard position={game.fen()} />
</>
);
};
export default PlayRandomMoveEngine;