fertig-classic-games/public/src/games/scrabble/ScrabbleAI.js

31 lines
1.1 KiB
JavaScript

// Thin client wrapper around the server move generator. The heavy lifting (the
// dictionary trie + anchor/cross-check search) lives in server/words/scrabbleEngine.js;
// here we just ship the board + rack and pace the AI so turns feel deliberate.
import { api } from '../../services/api.js';
// Ask the server for this AI's move. Resolves to:
// { type:'play', placements, score, word } | { type:'exchange', tiles } | { type:'pass' }
export async function requestAIMove({ board, rack, skill, firstMove, bagCount }) {
try {
return await api.post('/words/scrabble/ai-move', { board, rack, skill, firstMove, bagCount });
} catch (err) {
console.error('[scrabble] ai-move request failed:', err);
return { type: 'pass' };
}
}
// "Thinking" pause before the AI commits, in ms. Stronger players act faster.
const THINK_DELAY = {
1: [2200, 3600],
2: [1800, 3000],
3: [1400, 2400],
4: [1000, 1800],
5: [700, 1300],
};
export function nextThinkDelay(skill) {
const [min, max] = THINK_DELAY[skill] ?? THINK_DELAY[3];
return min + Math.random() * (max - min);
}