Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
2
Indexable
Never
// Function to check if the game has been won
function checkWin() {
    var winningCombinations = [
        [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 (var i = 0; i < winningCombinations.length; i++) {
        var combination = winningCombinations[i];
        var a = combination[0];
        var b = combination[1];
        var c = combination[2];

        var cellA = $("#board td").eq(a).text();
        var cellB = $("#board td").eq(b).text();
        var cellC = $("#board td").eq(c).text();

        if (cellA !== "" && cellA === cellB && cellA === cellC) {
            $("#board td").eq(a).addClass("winning");
            $("#board td").eq(b).addClass("winning");
            $("#board td").eq(c).addClass("winning");

            var winner = cellA;
            $("#message").html("Winner: <strong>" + winner + "</strong>");
            gameActive = false;
            return;
        }
    }

    var cells = $("#board td").toArray();
    var emptyCells = cells.filter(function(cell) {
        return $(cell).text() === "";
    });

    if (emptyCells.length === 0) {
        $("#message").text("It's a tie!");
        gameActive = false;
        return;
    }
}