Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // USE THIS AT YOUR OWN RISK - I AM NOT AT FAULT OR RESPONSIBLE FOR ANYTHING
- // How to use: 1. Go to chess.com 2. Go into a game 3. Open inspect element/developer tools, the console and then paste the code and press enter 4. Voila! The best move is being highlighted as a blue square.
- // Load Stockfish 15 engine
- const stockfish = new Worker('https://cdn.jsdelivr.net/npm/stockfish-15-js/stockfish-15-js.js');
- // Set the position of the chess board
- const position = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
- // Set the difficulty level of the engine
- const level = 20;
- // Send commands to Stockfish engine
- stockfish.postMessage('uci');
- stockfish.postMessage(`position fen ${position}`);
- stockfish.postMessage(`go depth ${level}`);
- // Listen for the best move from Stockfish engine
- stockfish.onmessage = (event) => {
- const message = event.data;
- if (message.startsWith('bestmove')) {
- const bestMove = message.split(' ')[1];
- // Highlight the best move on the chess board
- const squares = document.querySelectorAll('.cg-board .cg-square');
- squares.forEach((square) => {
- if (square.dataset.square === bestMove.slice(0, 2)) {
- square.style.backgroundColor = 'green';
- } else if (square.dataset.square === bestMove.slice(2, 4)) {
- square.style.backgroundColor = 'yellow';
- }
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement