Greasy Fork is available in English.

Agarix крестики-нолики

Добавляет игру крестики-нолики вместо рекламы

// ==UserScript==
// @name         Agarix крестики-нолики
// @namespace    http://agarix.ru/
// @version      1.3
// @description  Добавляет игру крестики-нолики вместо рекламы
// @author       Зуенко Михаил
// @run-at       document-start
// @include      *://agarix.ru/
// @grant        unsafeWindow
// ==/UserScript==

if (!unsafeWindow.agarixPlusPlus_scripts) {
    unsafeWindow.agarixPlusPlus_scripts = [start];
    document.addEventListener("DOMContentLoaded", start);
}
else unsafeWindow.agarixPlusPlus_scripts.push(start);

let cells = [0, 0, 0, 0, 0, 0, 0, 0, 0];
function start () {
    if (unsafeWindow.agarixPlusPlus_scripts.indexOf(start) != -1) {
        unsafeWindow.agarixPlusPlus_scripts.splice(unsafeWindow.agarixPlusPlus_scripts.indexOf(start), 1);
    }
    document.removeEventListener("DOMContentLoaded", start);
    if (!document.querySelector("#advertisement")) return;
    const style = document.createElement("style");
    style.innerHTML = "#g{position:relative;display:block;width:400px;height:325px;border-radius:10px;margin:6px;padding:15px 15px 15px 15px;}#game{border-collapse:collapse;width:295px;height:295px;margin:0 auto;}#game td{border:3px solid #ddd;transition:0.5s;width:98.3px;height:98.3px}#game-dialog{width:100%;height:50%;top:-85%;text-align:center;font-size:40px;padding:40px;transition:0.5s;}";
    document.head.append(style);
    document.querySelector("#advertisement").innerHTML = '<table id="game"><tr><td id="game-cell0"></td><td id="game-cell1"></td><td id="game-cell2"></td></tr><tr><td id="game-cell3"></td><td id="game-cell4"></td><td id="game-cell5"></td></tr><tr><td id="game-cell6"></td><td id="game-cell7"></td><td id="game-cell8"></td></tr></table><div id="game-dialog" class="modal-dialog modal-content" style="display:none;opacity:0"></div>';
    document.querySelector("#advertisement").id = "g";
    document.querySelector("#game").addEventListener("click", function (e) {
        for (const elm of e.path) if (elm.tagName == "TD") {
            if (cells[+elm.id.replace(/game-cell/, "")] == 0) {
                cells[+elm.id.replace(/game-cell/, "")] = 1;
                elm.innerHTML = '<svg width="100%" height="100%" style="display:block;"><line x1="20%" y1="20%" x2="80%" y2="80%" stroke="#ddd" stroke-width="10" stroke-linecap="round"/><line x1="80%" y1="20%" x2="20%" y2="80%" stroke="#ddd" stroke-width="10" stroke-linecap="round"/></svg>';
                if (!checkEnd()) ai();
            }
        }
    });
}
function checkEnd () {
    const combs = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6],
    ];
    for (const comb of combs){
        if (cells[comb[0]] == cells[comb[1]] && cells[comb[1]] == cells[comb[2]] && cells[comb[0]] != 0) {
            for (const c of comb) document.querySelector("#game-cell"+c).style.background = "rgba(0,200,0,0.3)";
            setTimeout(function () {
                showDialog(cells[comb[0]] == 1 ? "Вы победили!" : "Вы проиграли!");
                for (const c of comb) document.querySelector("#game-cell"+c).style.background = "";
            }, 1000);
            return true;
        }
    }
    if (cells.indexOf(0) == -1) {
        showDialog("Ничья!");
        return true;
    }
    return false;
}
function ai () {
    function bot(a){function b(a,e){var f=c(a);if(d(a,"O"))return{score:-10};if(d(a,"X"))return{score:10};if(0===f.length)return{score:0};for(var g,h=[],j=0;j<f.length;j++){if(g={},g.index=a[f[j]],a[f[j]]=e,"X"==e){var k=b(a,"O");g.score=k.score}else{var k=b(a,"X");g.score=k.score}a[f[j]]=g.index,h.push(g)}var l;if(e==="X")for(var m=-1e4,j=0;j<h.length;j++)h[j].score>m&&(m=h[j].score,l=j);else for(var m=1e4,j=0;j<h.length;j++)h[j].score<m&&(m=h[j].score,l=j);return h[l]}function c(a){return a.filter(a=>"O"!=a&&"X"!=a)}function d(a,b){return!((a[0]!=b||a[1]!=b||a[2]!=b)&&(a[3]!=b||a[4]!=b||a[5]!=b)&&(a[6]!=b||a[7]!=b||a[8]!=b)&&(a[0]!=b||a[3]!=b||a[6]!=b)&&(a[1]!=b||a[4]!=b||a[7]!=b)&&(a[2]!=b||a[5]!=b||a[8]!=b)&&(a[0]!=b||a[4]!=b||a[8]!=b)&&(a[2]!=b||a[4]!=b||a[6]!=b))}var e=[0,1,2,3,4,5,6,7,8];for(var f in a)0!=a[f]&&(e[f]=["O","X"][a[f]-1]);return b(e,"X").index}
    let res = bot(cells);
    cells[res] = 2;
    document.querySelector("#game-cell" + res).innerHTML = '<svg width="100%" height="100%" style="display:block;"><circle r="30%" cx="50%" cy="50%" fill="none" stroke="#ddd" stroke-width="10"/></svg>';
    checkEnd();
}
function showDialog (text) {
    document.querySelector("#game-dialog").style.display = "";
    setTimeout(function () {
        document.querySelector("#game-dialog").innerText = text;
        document.querySelector("#game-dialog").style.opacity = "1";
        setTimeout(function () {
            document.querySelector("#game-dialog").style.opacity = "0";
            setTimeout(function () {
                document.querySelector("#game-dialog").style.display = "none";
                for (const i of document.querySelectorAll("#game td")) {
                    i.innerHTML = "";
                    i.classList.remove("selected");
                }
                cells = [0, 0, 0, 0, 0, 0, 0, 0, 0];
            }, 500);
        }, 1000);
    }, 500);
}