tateti.js 4.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
var sheet = "";
var nombre1 = "";
var nombre2 = "";
var ficha1 = "X";
var ficha2 = "X";
var tablero = ["pf", "pf1", "pf2", "pf3", "pf4", "pf5", "pf6", "pf7", "pf8", ];
var player = nombre1;
//Funcion para validar los datos ingresados por el usuario antes de empezar la partida
function validardatos() {
    var empezarjuego = false; //Booleano que controla si el juego puede comenzar
    nombre1 = document.getElementById("jugador1").value;
    nombre2 = document.getElementById("jugador2").value;
    ficha1 = document.getElementById("ficha1").value;
    ficha2 = document.getElementById("ficha2").value;
    //Condicional para que los nombres no se repitan ni esten vacios, en caso contrario envia un mensaje
    if (nombre1 == "" || nombre1 == nombre2 || nombre2 == "") {
        empezarjuego = false;
    } else {
        empezarjuego = true;
    }
    //Condicional para que las fichas no se repitan ni esten vacias
    if ((ficha1 == "X" || ficha1 == "O") && (ficha2 == "X" || ficha2 == "O") && ficha1 != ficha2) {
        empezarjuego = true;
    } else {
        empezarjuego = false;
    }
    //Si se cumplen las condiciones empieza el juego y se limpia la pantalla y el tablero
    if (empezarjuego) {
        document.getElementById("form-container").reset();
        let x = document.getElementById("seleccionPadre");
        x.classList.add("hidden");
        document.getElementById("player").innerHTML = "Turno jugador: " + nombre1;
        //En este ciclo se limpia el tablero
        for (let index = 0; index < 9; index++) {
            document.getElementById(tablero[index]).innerHTML = " ";

        }
    } else {
        alert("Datos invalidos intente de nuevo");
    }
}

//Funcion que posiciona las fichas y controla la partida
function ficha(id) {
    let pf = [" ", " ", " ", " ", " ", " ", " ", " ", " ", ]; // pf viene de "posicion fichas"
    let Empate = 0;
    let ganador = false;
    if (sheet == "") {
        sheet = ficha1;
    }
    //Condicional para solo posicionar las fichas en filas vacias
    if (document.getElementById(id).innerHTML == " ") {
        document.getElementById(id).innerHTML = sheet; //Posiciono la ficha donde el usuario decide
        //Ciclo que se actualiza segun las fichas presente en el tablero para controlar el juego
        for (let index = 0; index < tablero.length; index++) {
            pf[index] = document.getElementById(tablero[index]).innerHTML;
            if (pf[index] != " ") {
                Empate++;
            }
        }
        //Condicional por el cual no tenes que preocuparte profe <3
        //En este codicional solo entra en caso que exista un ganador
        if ((pf[0] === pf[1] && pf[1] === pf[2] && pf[0] != " ") || (pf[3] === pf[4] && pf[4] === pf[5] && pf[3] != " ") || (pf[6] === pf[7] && pf[7] === pf[8] && pf[6] != " ") || (pf[0] === pf[3] && pf[3] === pf[6] && pf[0] != " ") || (pf[1] === pf[4] && pf[4] === pf[7] && pf[1] != " ") || (pf[2] === pf[5] && pf[5] === pf[8] && pf[2] != " ") || (pf[0] === pf[4] && pf[4] === pf[8] && pf[0] != " ") || (pf[2] === pf[4] && pf[4] === pf[6] && pf[2] != " ")) {
            ganador = true;
            //Dejo de mostrar el turno del jugador
            document.getElementById("player").innerHTML = "";
            for (let index = 0; index < 9; index++) {
                //Reemplazo los espacios vacios por un guion para terminar el juego
                if (document.getElementById(tablero[index]).innerHTML === " ") {
                    document.getElementById(tablero[index]).innerHTML = "-";
                }
            }
            document.getElementById("ganador").innerHTML = "Gana el jugador: " + player;
        } else {
            //Condicional para cambiar de ficha y de jugadores
            if (sheet == ficha1) {
                sheet = ficha2;
                player = nombre2;
                document.getElementById("player").innerHTML = "Turno jugador: " + player;
            } else {
                sheet = ficha1;
                player = nombre1;
                document.getElementById("player").innerHTML = "Turno jugador: " + player;
            }
            //Condicional en caso que se de un empate
            if (Empate == 9 && ganador == false) {
                document.getElementById("player").innerHTML = "Empate.";
            }
        }

    }

}