Código: Seleccionar todo
<html>
<head>
<script type="text/javascript" language="javascript">
/*****************
* Tres en raya
* By linkgl
*****************/
var turno=0;
function x(i)
{
if(turno==0)
{
if(document.getElementById(i).innerHTML!="X" && document.getElementById(i).innerHTML!="O")
{
document.getElementById(i).innerHTML="X";
ganar("X");
turno=1;
computadora();
}
else
{
alert("No puede colocar la X ahí");
}
}
}
function computadora()
{
if(turno==1)
{
var a=rand();
if(document.getElementById(a).innerHTML!="X" && document.getElementById(a).innerHTML!="O")
{
document.getElementById(a).innerHTML="O";
ganar("O");
turno=0;
}
else
{
computadora();
}
}
}
function rand()
{
var num
num=Math.floor(Math.random()*10)
if(num!=0)
{
return num;
}
else
{
rand();
}
}
function ganar(x)
{
//verticales
for(i=1;i<7;i++)
{
if(document.getElementById(i).innerHTML==x && document.getElementById(i+3).innerHTML==x && document.getElementById(i+6).innerHTML==x)
{
alert("Se acabo!");
reset();
}
}
//horizontales
for(o=1;o<10;o++)
{
if(document.getElementById(o).innerHTML==x && document.getElementById(o+1).innerHTML==x && document.getElementById(o+2).innerHTML==x)
{
alert("Se acabo!");
reset();
}
o=o+2;
}
//diagonales
if(document.getElementById('1').innerHTML==x && document.getElementById('5').innerHTML==x && document.getElementById('9').innerHTML==x)
{
alert("Se acabo!");
reset();
}
if(document.getElementById('3').innerHTML==x && document.getElementById('5').innerHTML==x && document.getElementById('7').innerHTML==x)
{
alert("Se acabo!");
reset();
}
}
function reset()
{
location.reload();
}
</script>
</head>
<body>
<div id="global" style="width:160px">
<div id="1" onClick="x(1)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
<div id="2" onClick="x(2)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
<div id="3" onClick="x(3)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
<div id="4" onClick="x(4)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
<div id="5" onClick="x(5)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
<div id="6" onClick="x(6)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
<div id="7" onClick="x(7)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
<div id="8" onClick="x(8)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
<div id="9" onClick="x(9)" style="border: #000 1px solid; height:50px;width:50px;float:left">-</div>
</body>
</html>