Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.7 kB
2
Indexable
$(document).ready(function() {
    var currentPlayer = "";
    var gameActive = false;

    // Function to check if the game has been won
    function checkWin() {
        // ... existing checkWin logic ...
    }

    // Function to handle a cell click
    function handleCellClick(cell) {
        if (!gameActive) {
            $("#message").text("Please select X or O to start.");
            return;
        }

        // ... existing handleCellClick logic ...
    }

    // Event handler for cell clicks
    $("#board td").click(function() {
        handleCellClick($(this));
    });

    // Event handler for X button click
    $("#xButton").click(function() {
        if (!gameActive) {
            currentPlayer = "X";
            $("#message").text("Current Player: " + currentPlayer);
            gameActive = true;
            $("#board td").text("").removeClass("winning");
            $("#xButton").addClass("selected");
            $("#oButton").removeClass("selected");
        }
    });

    // Event handler for O button click
    $("#oButton").click(function() {
        if (!gameActive) {
            currentPlayer = "O";
            $("#message").text("Current Player: " + currentPlayer);
            gameActive = true;
            $("#board td").text("").removeClass("winning");
            $("#oButton").addClass("selected");
            $("#xButton").removeClass("selected");
        }
    });

    // Event handler for Reset button click
    $("#resetButton").click(function() {
        currentPlayer = "";
        gameActive = false;
        $("#message").text("Select X or O to start");
        $("#board td").text("").removeClass("winning");
        $("#xButton, #oButton").removeClass("selected");
    });
});