Public
Edited
Mar 31, 2023
Insert cell
Insert cell
Insert cell
stockfish = {
// const importObject = { env: { memory: new WebAssembly.Memory({ initial: 256 }) } };
const workerScript = await require.resolve("stockfish@11/src/stockfish.asm.js")
.then(fetch)
// .then(response => response.arrayBuffer())
// .then(response => response.text())
// .then(bytes => WebAssembly.instantiate(bytes, importObject))
.then(response => response.blob())
.then(blob => URL.createObjectURL(blob, {type: "text/javascript"}))
;
return new Worker(workerScript);
// return workerScript

}
Insert cell
async function ask(stockfish, command, params="") {
let
resolve,
idx = ("0000" + trigger).slice(-4),
message = command + " " + params,
promise = new Promise(r => resolve = r),
waitfor = ({
'go': 'bestmove',
'eval': 'eval',
'uci': 'uciok',
'isready': 'readyok',
'position': 'NOP',
'ucinewgame': 'NOP',
'd': 'Legal'
})[command]
;
stockfish.onmessage = e => {
console.log( e.data );

if (e.data.includes(waitfor)) {
messages.push(idx + ' <== ' + e.data, '');
mutable trigger += 1;
resolve(e.data);
} else {
messages.push(idx + ' ' + e.data);
}

}
;
if (!waitfor) throw new Error("command not available", command);
console.log( message );
messages.push(idx + ' ==> ' + message);
(waitfor === 'NOP') && messages.push('');
mutable trigger += 1;
stockfish.postMessage( message );
return promise;
}
Insert cell
Insert cell
Insert cell
{ trigger; return html`<pre style="display: block; font-size: 11px; line-height: 13px"; height: 120px !important; overflow-y: scroll>${ messages.slice(-30).map( v => `${v}</br>` ) }</pre>`}
Insert cell
{
const pgn = ``;
pgnArr = pgn
}

Insert cell
{
const btnReady = html`<button>Ready</button>`;
btnReady.onclick = () => ask(stockfish, "isready");
const Eval = html`<button>Eval</button>`;
Eval.onclick = () => ask(stockfish, "eval");

const btnUCI = html`<button>UCI</button>`;
btnUCI.onclick = () => ask(stockfish, "uci");

const btnGO = html`<button>Go depth 3</button>`;
btnGO.onclick = () => ask(stockfish, "go", "depth 3");
const btnNewGame = html`<button>New Game</button>`;
btnNewGame.onclick = () => ask(stockfish, "ucinewgame");
const btnFen = html`<button>Fen: N7/P3pk1p/3p2p1/r4p2/8....</button>`;
btnFen.onclick = () => ask(stockfish, "position", "fen " + fens[1]);
const btnDisplay = html`<button>Display</button>`;
btnDisplay.onclick = () => ask(stockfish, "d");
return html`${btnReady} ${Eval} ${btnUCI} ${btnNewGame} ${btnFen} ${btnGO} ${btnDisplay} `;
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more