Naughts & Crosses

Ugly hack for a SO question

by blowsie

HTML

<center>
<h1 >"        X ,O Game    "</h1>
<table >
    <tr>
       <td id="td1"  data-no="1" ></td>
       <td id="td2" data-no="2"></td>
       <td id="td3" data-no="3"></td>
    </tr>

    <tr>
       <td id="td4" data-no="4"></td>
       <td id="td5" data-no="5"></td>
       <td id="td6" data-no="6"></td>
    </tr>

    <tr>
       <td id="td7" data-no="7"></td>
       <td id="td8" data-no="8"></td>
       <td id="td9" data-no="9"></td>
    </tr>
</table>
</center>

CSS

td {border:1px solid #000; height:50px; width:50px}

JavaScript

function ranFun() {
    return Math.floor((Math.random() * 9) + 1);
}

var a;

function Elment(a) {
    $("#td" + a).text("o");
}

function call() {
    var x = ranFun();
    switch (x) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
        if ($('#td' + x).text() === "x" || $('#td' + x).text() == "o") call();
        else {
            $("#td" + x).text("o");
        }

        break;
    default:
        break;
    }
}
$('td').click(function() {
    var no = $(this).data('no')

    if ($('#td' + no).text() === "x" || $('#td' + no).text() == "o") return false;
    else {
        $("#td" + no).text("x");
        call();
    }
});