function init(){ var turno = 1; var filas = [0,0,0]; var columnas = [0,0,0]; var diagonales = [0,0,0]; jug1.onclick = function(){ var nombre = prompt("Ingresa nombre"); while(nombre == ""){ nombre = prompt("Debe ingresa nombre"); } jug1.textContent = nombre; } jug2.onclick = function(){ var nombre = prompt("Ingresa nombre"); while(nombre == ""){ nombre = prompt("Debe ingresa nombre"); } jug2.textContent = nombre; } xo1.onclick = function (){ if(xo1.textContent !== 'X' && xo1.textContent !== 'O'){ textContent = 'X'; } if(xo1.textContent == 'X'){ xo1.textContent = 'O'; }else{ xo1.textContent = 'X'; } } xo2.onclick = function (){ if(xo2.textContent !== 'X' && xo2.textContent !== 'O'){ xo2.textContent = 'X'; } if(xo2.textContent == 'X'){ xo2.textContent = 'O'; }else{ xo2.textContent = 'X'; } } comenzar.onclick = function(){ var val = true; while(jug1.textContent == ""){ alert("Jugador 1 debe ingresar un nombre"); val = false; break; } while(jug2.textContent == ""){ alert("Jugador 2 debe ingresar un nombre"); val = false; break; } while(xo1.textContent !== 'X' && xo1.textContent !== 'O'){ alert("Jugador 1 debe escoger X o O"); val = false; break; } while(xo2.textContent !== 'X' && xo2.textContent !== 'O'){ alert("Jugador 2 debe escoger X o O"); val = false; break; } while(xo1.textContent == xo2.textContent ){ alert("Jugador 2 debe escoger distinta opcion a jugador 1"); val = false; break; } if(val == true){ inicio(); } } function inicio(){ var i = 0; A1.onclick = function(){ if(turno == 1){ A1.textContent = 'X' }else { A1.textContent = 'O' } analizar(); cambiarTurno(); i++; } A2.onclick = function(){ if(turno == 1){ A2.textContent = 'X' }else { A2.textContent = 'O' } analizar(); cambiarTurno(); i++; } A3.onclick = function(){ if(turno == 1){ A3.textContent = 'X' }else { A3.textContent = 'O' } analizar(); cambiarTurno(); i++; } B1.onclick = function(){ if(turno == 1){ B1.textContent = 'X' }else { B1.textContent = 'O' } analizar(); cambiarTurno(); i++; } B2.onclick = function(){ if(turno == 1){ B2.textContent = 'X' }else { B2.textContent = 'O' } analizar(); cambiarTurno(); i++; } B3.onclick = function(){ if(turno == 1){ B3.textContent = 'X' }else { B3.textContent = 'O' } analizar(); cambiarTurno(); i++; } C1.onclick = function(){ if(turno == 1){ C1.textContent = 'X' }else { C1.textContent = 'O' } analizar(); cambiarTurno(); i++; } C2.onclick = function(){ if(turno == 1){ C2.textContent = 'X' }else { C2.textContent = 'O' } analizar(); cambiarTurno(); i++; } C3.onclick = function(){ if(turno == 1){ C3.textContent = 'X' }else { C3.textContent = 'O' } analizar(); cambiarTurno(); i++; } function cambiarTurno(){ if(turno == 1){ turno = 0; }else{ turno = 1; } } alert("EMPATE"); } function analizar(){ if((A1.textContent == 'X') && (A2.textContent == 'X') && (A3.textContent == 'X')){ alert("EL GANADOR ES: X"); }else if((A1.textContent == 'O') && (A2.textContent == 'O') && (A3.textContent == 'O')){ alert("EL GANADOR ES: O"); } if((B1.textContent == 'X') && (B2.textContent == 'X') && (B3.textContent == 'X')){ alert("EL GANADOR ES: X"); }else if((B1.textContent == 'O') && (B2.textContent == 'O') && (B3.textContent == 'O')){ alert("EL GANADOR ES: O"); } if((C1.textContent == 'X') && (C2.textContent == 'X') && (C3.textContent == 'X')){ alert("EL GANADOR ES: X"); }else if((C1.textContent == 'O') && (C2.textContent == 'O') && (C3.textContent == 'O')){ alert("EL GANADOR ES: O"); } if((A1.textContent == 'X') && (B1.textContent == 'X') && (C1.textContent == 'X')){ alert("EL GANADOR ES: X"); }else if((A1.textContent == 'O') && (B1.textContent == 'O') && (C1.textContent == 'O')){ alert("EL GANADOR ES: O"); } if((A2.textContent == 'X') && (B2.textContent == 'X') && (C2.textContent == 'X')){ alert("EL GANADOR ES: X"); }else if((A2.textContent == 'O') && (B2.textContent == 'O') && (C2.textContent == 'O')){ alert("EL GANADOR ES: O"); } if((A3.textContent == 'X') && (B3.textContent == 'X') && (C3.textContent == 'X')){ alert("EL GANADOR ES: X"); }else if((A3.textContent == 'O') && (B3.textContent == 'O') && (C3.textContent == 'O')){ alert("EL GANADOR ES: O"); } if((A1.textContent == 'X') && (B2.textContent == 'X') && (C3.textContent == 'X')){ alert("EL GANADOR ES: X"); }else if((A1.textContent == 'O') && (B2.textContent == 'O') && (C3.textContent == 'O')){ alert("EL GANADOR ES: O"); } if((A3.textContent == 'X') && (B2.textContent == 'X') && (C1.textContent == 'X')){ alert("EL GANADOR ES: X"); }else if((A3.textContent == 'O') && (B2.textContent == 'O') && (C1.textContent == 'O')){ alert("EL GANADOR ES: O"); } } }