Skip to content

Instantly share code, notes, and snippets.

@adrixvoid
Last active June 6, 2018 21:56
Show Gist options
  • Save adrixvoid/7a79b2ac59d5e069ed473d83285bec8b to your computer and use it in GitHub Desktop.
Save adrixvoid/7a79b2ac59d5e069ed473d83285bec8b to your computer and use it in GitHub Desktop.
#include <stdio.h>

const int cantmaxJugadas= 9;
int tateti[3][3];


//Funcion de termino juego
int ValidarFilas()
{
    int gano;
    if((tateti[0][0] == 1 && tateti[0][1] == 1 && tateti[0][2] == 1) || (tateti[0][0] == 2 && tateti[0][1] == 2 && tateti[0][2] == 2))
    {
        gano=1;

    }
    if((tateti[1][0] == 1 && tateti[1][1] == 1 && tateti[1][2] == 1) || (tateti[1][0] ==2 && tateti[1][1]== 2 &&tateti[1][2]==2))
    {
        gano=1;
    }

    if((tateti[2][0] == 1 && tateti[2][1] == 1 && tateti[2][2] == 1) || (tateti[2][0] ==2 && tateti[2][1]== 2 &&tateti[2][3]==2))
    {
        gano=1;
    }
    return gano;
}
int ValidarColumnas()
{
    int gano;
    if((tateti[0][0] ==1 && tateti[1][0]== 1 &&tateti[2][0]==1) || (tateti[0][0] ==2 && tateti[1][0]== 2 &&tateti[2][0]==2))
    {
        gano=1;

    }
    if((tateti[0][1] == 1 && tateti[1][1] == 1 &&tateti[2][1] ==1) || (tateti[0][1] == 2 && tateti[1][1]== 2 &&tateti[2][1]==2))
    {
        gano=1;

    }

    if((tateti[0][2] == 1 && tateti[1][2] == 1 &&tateti[2][2] == 1) || (tateti[0][2] ==2 && tateti[1][2] == 2 &&tateti[2][2] == 2))
    {
        gano=1;

    }
    return gano;
}
int ValidarDiagonales()
{
    int gano;
    if((tateti[0][0] == 1 && tateti[1][1] == 1 && tateti[2][2] == 1) || (tateti[0][0] == 2 && tateti[1][1] == 2 && tateti[2][2] == 2))
    {
        gano=1;

    }
    if((tateti[0][2] == 1 && tateti[1][1] == 1 &&tateti[2][0] == 1) || (tateti[0][2] == 2 && tateti[1][1] == 2 &&tateti[2][0] == 2))
    {
        gano=1;

    }

    return gano;
}



int terminoJuego()
{

    int termino =0;

//Las funciones validar devuelven 1 si es true o 0 si es false;

    if ( (ValidarColumnas()== 1) ||  (ValidarFilas()==1) || (ValidarDiagonales()==1))
        termino=1;

    return termino;
}


//Inicio de aplicacion
int main()
{



    int i =0;
//Jugador es 1  o 2
    int jugador = 2;
    //pos es el entero que ingresa el usuario
    int pos=0;
    int intentar = 1;


//Inicializa matriz
    for(int f = 0; f<3; f++ )
    {
        for (int c=0; c<3; c++)
        {
            tateti[f][c]=0;

        }
    }


//Fin inicializa matriz




    do
    {

//Cambio de turno

        if(jugador == 1)
        {
            jugador = 2;
        }
        else
        {
            jugador=1;
        }

//Muestra matriz
        for(int f = 0; f<3; f++ )
        {
            for (int c=0; c<3; c++)
            {
                printf(" %d", tateti[f][c]);
            }
            printf("\n");
        }
//Ingrese posición
        do
        {
            printf("Ingrese la posicion jugador %d\n", jugador);
            scanf("%d",&pos);
        }
        while ( !(pos>=1 && pos <=9) );

        intentar = 1;

//Unicación de la posición en el tablero

        while (intentar == 1)
        {

            intentar = 0;

            if (pos == 1 & tateti[0][0] == 0)
            {
                tateti[0][0] = jugador;
            }
            else if (pos == 2 & tateti[0][1] == 0)
            {
                tateti[0][1] = jugador;
            }
            else if (pos == 3 & tateti[0][2] == 0)
            {
                tateti[0][2] = jugador;
            }
            else if (pos == 4 & tateti[1][0] == 0)
            {
                tateti[1][0] = jugador;
            }
            else if (pos == 5 & tateti[1][1]  == 0)
            {
                tateti[1][1]  = jugador;
            }
            else if (pos == 6 & tateti[1][2]  == 0)
            {
                tateti[1][2]  = jugador;
            }
            else if (pos == 7 & tateti[2][0]  == 0)
            {
                tateti[2][0]  = jugador;
            }
            else if (pos == 8 & tateti[2][1]  == 0)
            {
                tateti[2][1]  = jugador;
            }
            else if (pos == 9 & tateti[2][2]  == 0)
            {
                tateti[2][2]  = jugador;
            }
            else
            {

//Si ingreso en un lugar ya tomado, vuelve a ingresar

                printf("Vuelva a intentar \n");

                intentar = 1;

                do
                {
                    printf("Ingrese la posicion jugador %d\n", jugador);
                    scanf("%d",&pos);
                }
                while ( !(pos>=1 && pos <=9) );
            }
        }


        i++;


    }
    while ((terminoJuego()==0) && (i<cantmaxJugadas));


//Ver quien ganó


    if (terminoJuego())
    {
        printf("Gano %d", jugador);
    }
    else
    {
        printf("Empate");
    }


    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment