Programación General > C/C++

 4 En Raya

(1/2) > >>

Arsys:
Estoy haciendo un programa que funcione como el típico 4 en raya de toda la vida.
Pero una de las funciones  (en concreto la de mirarDiagD), no me funciona correctamente, y por mucho que miro y remiro no encuentro donde puede estar el error, ya que es similar a la de mirarDiagI pero esta si que funciona bien. Me falla solo al ejecutarse, se compila perfectamente.



A ver si alguien me puede echar una mano y se le ocurre donde puede estar el fallo.


--- Código: Text --- #include <stdio.h> int enraya(int tab[][7], int j);int mirarhoriz(int tab[][7], int f, int c, int j);int mirarvert(int tab[][7], int f, int c, int j);int mirardiagI(int tab[][7], int f, int c, int j);int mirardiagD(int tab[][7], int f, int c, int j);int esta_lleno(int tab[][7]);void vaciar_tablero(int tab[][7]);int pintar_tablero(int tab[][7]);void poner_ficha(int tab[][7], int j); void main(void){    int tablero[6][7];    int jugar;    int x;    int jugador;        jugar=1;        while(jugar==1)    {        //vaciamos el tablero        vaciar_tablero(tablero);        jugador=1;        //pintamos el tablero        pintar_tablero(tablero);                //si el tablero no está lleno ni hay 4 en raya se continúa la partida        while((!enraya(tablero,jugador)) && (!esta_lleno(tablero)))        {            poner_ficha(tablero,jugador);            if(jugador==1)jugador=2;            else jugador=1;            pintar_tablero(tablero);        }                //si hay 4 en raya se dice quien ganó y se pregunta si se quiere continuar jugando        if(enraya(tablero,jugador))        {            if(jugador==1)printf("gano 2");            else printf("gano 1");        }        else printf("\nempate");        printf("\notra?(s/n)");        scanf("%c", &x);        if(x=='n')jugar=0;    }} //función para comprobar si hay 4 en rayaint enraya(int tab[][7], int j){    int f;    int c;    int atopado;    atopado=0;    f=0;        while((atopado==0) || (f<=5))    {        c=0;        while((atopado==0) && (c<=6))        {                atopado=(mirarhoriz(tab,f,c,j) ||                 mirarvert(tab,f,c,j) || mirardiagI(tab,f,c,j)                 || mirardiagD(tab,f,c,j));                c++;        }        f++;    }    return atopado;} //función para comprobar si hay 4 en raya en horizontalint mirarhoriz(int tab[][7], int f, int c, int j){    int atopado=0;    if(c<4)    {        if((tab[f][c]==j) && (tab[f][c+1]==j) && (tab[f][c+2]==j) &&         (tab[f][c+3]==j))atopado=1;        else atopado=0;    }    else atopado=0;    return atopado;} //función para comprobar si hay 4 en raya en verticalint mirarvert(int tab[][7], int f, int c, int j){    int atopado=0;    if(f<3)    {        if((tab[f][c]==j) && (tab[f+1][c]==j) && (tab[f+2][c]==j) &&        (tab[f+3][c]==j))atopado=1;        else atopado=0;        return atopado;    }}  //función para comprobar si hay 4 en raya en diagonal izquierdaint mirardiagI(int tab[][7], int f, int c, int j){    int atopado=0;    if((c>2) && (f<3))    {        if((tab[f][c]==j) && (tab[f+1][c-1]==j) && (tab[f+2][c-2]==j) &&        (tab[f+3][c-3]==j))atopado=1;        else atopado=0;    }    else atopado=0;    return atopado;} //función para comprobar si hay 4 en raya en diagonal derecha (esta es la que falla)int mirardiagD(int tab[][7], int f, int c, int j){    int atopado=0;    if((c>4) && (f<3))    {        if((tab[f][c]==j) && (tab[f+1][c+1]==j) && (tab[f+2][c+2]==j) &&        (tab[f+3][c+3]==j))atopado=1;        else atopado=0;    }    else atopado=0;    printf("%d", atopado);    return atopado;} //función para vaciar el tablero al principio de la partidavoid vaciar_tablero(int tab[][7]){    int c;    int f=0;        while(f<=5)    {        c=0;        while(c<=6)        {             tab[f][c]=0;                c++;        }        f++;    }} //función para pintar el tablero en pantallaint pintar_tablero(int tab[][7]){    int f;    int c;    f=0;        printf("\n");    while(f<=5)    {        c=0;        while(c<=6)        {                if(tab[f][c]!=0)                {                    if(tab[f][c]==1)printf("A");                    else printf("B");                }                else printf("O");                c++;        }        f++;        printf("\n");    }} //función para comprobar si el tablero está llenoint esta_lleno(int tab[][7]){    int lleno=1;    int f=0;    int c;        while((f<=5) && (lleno))    {        c=0;        while((c<=6) && (lleno))        {            if(tab[f][c]==0)lleno=0;            c++;        }        f++;    }    return lleno;}            //función para poner fichasvoid poner_ficha(int tab[][7], int j){    int c, f=5, jugada, turno=0;        printf("\nJugador %d, introduzca columna: ", j);    scanf("%d", &jugada);        while(turno==0)    {        if(f>=0)        {            if(tab[f][jugada-1]==0)            {                tab[f][jugada-1]=j;                turno=1;            }        }        f--;    }}          

The Black Boy:
bueno.... jamas habia escuchado de 4 en raya... asi que voy ha echarle un vistazo y te digo ...

Arsys:
Pues es un juego muy conocido aquí, es una tableta vertical de 6 filas por 7 columnas donde se van metiendo las fichas, y el que consigue 4 en raya es el que gana.

Arsys:
Ya he conseguido arreglarlo y que funcione  :smartass:

Saludos  :blink:

dfsi:
cuando le doy a compilar me sale esto:

0000000
0000000
0000000
0000000
0000000
0000000
000000000000000000000000000000000000000000gano 2
otra?(s/n)

no funciona tio. y si le das s se quda igual.

Navegación

[0] Índice de Mensajes

[#] Página Siguiente

Ir a la versión completa