Вариант крестиков нуликов )))
<!DOCTYPE>
<html>
<head>
<title></title>
<style type="text/css">
td{
height: 22px; width: 22px; text-align: center;
border: #000000 solid 1px;
}
</style>
</head>
<body>
<div id="tv"></div>
<script type="text/javascript">
for (var m = Array(9), l = 3, b = ["background-color: #FFFF00;", "background-color: #00FF00;"],
t = document.createElement("table"), st = 0, c = 0; c < l; c++)
for (var tr = t.insertRow(c), s = 0; s < l; s++) {
var td = tr.insertCell(s);
td.innerHTML = " ";
td.onclick = function (a) {
return function () {
go(a)
}
}(st++)
}
document.body.appendChild(t);
var arr = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
],
arr_st = [
[1, 3, 5, 7],
[0, 2, 6, 8]
],
n = 1,
step = 0;
function arr_indexOf(a, e) {
for (var d = 0; d < a.length; d++) if (a[d] === e) return d;
return -1
}
function rand() {
for (var a = [], e = 0; e < m.length; e++) void 0 == m[e] && a.push(e);
return a[Math.random() * a.length | 0]
}
function rand_st(a) {
for (var a = arr_st[a], e = [], d = 0; d < a.length; d++) void 0 == m[a[d]] && e.push(a[d]);
return e[Math.random() * e.length | 0]
}
function show(a) {
if ( void 0 == a) return
step++;
t.rows[Math.floor(a / 3)].cells[a % 3].innerHTML = n ? "X" : "O";
m[a] = n;
n ^= 1;
9 == step && mess("0 == 0")
}
function vivat(a, e) {
for (var d = 0; d < a.length; d++) {
var f = a[d];
t.rows[Math.floor(f / 3)].cells[f % 3].style.cssText = b[e]
}
}
function arr_null(a) {
for (var e = 0; e < a.length && !(void 0 == m[a[e]]); e++);
return a[e]
}
function go(a) {
if (void 0 == m[a] && 9 > step) {
show(a);
for (var e = 0, d = []; e < arr.length; e++) {
for (var f = 0, g = 0; g < arr[e].length; g++) {
var h = arr[e][g];
1 === m[h] && f++;
0 === m[h] && f--
}
d[e] = f
}
- 1 != arr_indexOf(d, -3) ?
(f = arr_indexOf(d, -3), mess("vivat O"), vivat(arr[f], 0), step = 9) : -1 != arr_indexOf(d, 3) ?
(f = arr_indexOf(d, 3), mess("vivat X"), vivat(arr[f], 1), step = 9) : -1 != arr_indexOf(d, -2) ?
(f = arr_indexOf(d, -2), go(arr_null(arr[f]))) : -1 != arr_indexOf(d, 2) ?
(f = arr_indexOf(d, 2), show(arr_null(arr[f]))) : show(void 0 == m[4] ?
4 : (m[0] && m[8] || m[2] && m[6]) && void 0 != rand_st(0) ?
rand_st(0) : 4 == a ?
rand_st(1) : m[1] && m[3] && void 0 == m[0] ?
0 : m[1] && m[5] && void 0 == m[2] ?
2 : m[3] && m[7] && void 0 == m[6] ?
6 : m[5] && m[7] && void 0 == m[8] ?
8 : m[4] && (m[0] || m[2] || m[6] || m[8]) && void 0 != rand_st(1) ? rand_st(1) : rand())
}
}
function mess(a) {
document.getElementById("tv").innerHTML = a
}
function new_go() {
mess("go");
step = 0;
m = Array(9);
n = 1;
var tds = t.getElementsByTagName('td')
for (var i=0; i<tds.length; i++) {
tds[i].innerHTML = " ";
tds[i].style.cssText = ""
}
}
new_go();
</script>
<input type="button" name="" value="new" onclick="new_go()"/>
</body>
</html>