Загрузка данных


(function() {
    console.log("Запуск Бит-Профи v8.0... Кликните по игровому полю!");

    const DELAY = 200; 
    const boardId = 'bitsBoardWrap'; // Твой ID из кода

    function getGrid() {
        let grid = Array(4).fill(0).map(() => Array(4).fill(0));
        const board = document.getElementById(boardId);
        if (!board) return null;

        const tiles = Array.from(board.querySelectorAll('div')).filter(el => 
            el.innerText && /бит|байт|кб/i.test(el.innerText)
        );

        const bRect = board.getBoundingClientRect();
        const cellW = bRect.width / 4;
        const cellH = bRect.height / 4;

        tiles.forEach(tile => {
            const tRect = tile.getBoundingClientRect();
            const col = Math.floor((tRect.left - bRect.left + cellW / 2) / cellW);
            const row = Math.floor((tRect.top - bRect.top + cellH / 2) / cellH);
            
            let txt = tile.innerText.toLowerCase();
            let val = parseInt(txt.replace(/[^0-9]/g, '')) || 0;
            if (txt.includes('байт')) val *= 8; // Конвертация для логики
            else if (txt.includes('кб')) val *= 8192;

            if (row >= 0 && row < 4 && col >= 0 && col < 4) grid[row][col] = val;
        });
        return grid;
    }

    // НОВЫЙ МЕХАНИЗМ ДВИЖЕНИЯ
    function forceMove(dir) {
        const codes = { 'U': 38, 'R': 39, 'D': 40, 'L': 37 };
        const keyName = 'Arrow' + (dir==='U'?'Up':dir==='R'?'Right':dir==='D'?'Down':'Left');
        
        const eventObj = {
            key: keyName,
            code: keyName,
            keyCode: codes[dir],
            which: codes[dir],
            bubbles: true,
            cancelable: true,
            isTrusted: true // Имитация доверенного события
        };

        const evDown = new KeyboardEvent('keydown', eventObj);
        const evUp = new KeyboardEvent('keyup', eventObj);

        // Отправляем событие во все возможные цели
        const target = document.getElementById(boardId) || document;
        target.focus(); 
        target.dispatchEvent(evDown);
        setTimeout(() => target.dispatchEvent(evUp), 10);
        
        window.dispatchEvent(evDown);
        document.dispatchEvent(evDown);
    }

    const brain = setInterval(() => {
        const grid = getGrid();
        if (!grid) return;

        const empty = grid.flat().filter(v => v === 0).length;

        // Умная логика выбора: Down и Right - приоритет
        if (empty > 2) {
            Math.random() > 0.5 ? forceMove('D') : forceMove('R');
        } else {
            // Если тесно, пробуем Down, потом Right, в крайнем случае Left
            const r = Math.random();
            if (r > 0.3) forceMove('D');
            else if (r > 0.1) forceMove('R');
            else forceMove('L');
        }

        if (document.body.innerText.includes("Игра окончена")) clearInterval(brain);
    }, DELAY);
})();