Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: mathias_vg en 1 Julio 2013, 22:28 pm



Título: Ayuda! Problema de un menu!!!
Publicado por: mathias_vg en 1 Julio 2013, 22:28 pm
Buenas como va,

Mi problema es el siguiente. en el juego conecta 4 me piden que haga un menu donde una de las opciones es que registre todos los juegos realizados (Nombre y cedula de cada jugador). El problema es que a medida que voy eligiendo un jugador nuevo me borra el anterior. Me pueden ayudar con esto?  Les dejo el codigo

Código
  1.  
  2.    //* JUEGO CONECTA 4*//
  3.  
  4.  
  5.  
  6. /*Librerias utilizadas*/
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #define DIM 16 /*Constante utilizada para definir la maxima dimension del tablero*/
  10.  
  11.  
  12. struct partidas1{
  13.        char nom_jug[40];
  14. char ced_jug[8];
  15.    }partidas1[500];
  16. struct partidas2{
  17. char nom_jug[40];
  18. char ced_jug[8];
  19.    }partidas2[500];
  20.    struct ganadores{
  21.        char jganador;
  22.    }ganadores[1000];
  23.    int partida, aux2,aux3;
  24.  
  25. char nom_jug[40];
  26. char ced_jug[8];
  27.  
  28.  
  29. /*PROCEDIMIENTOS Y FUNCIONES*/
  30.  
  31. void inicializar_tablero(char tablero[][DIM],int);
  32. int juegan_O_(char tabla[][DIM],int,int);
  33. int juegan_X_(char tablero[][DIM],int,int);
  34. int cuatro_en_linea(char tablero[][DIM],int);
  35. int limpiar(char nom_jug[]);
  36.  
  37.  
  38. int inicia1 = 0;
  39. int inicia2 = 0, jugar=1;
  40. int main()
  41. {
  42.    char tablero[DIM][DIM];
  43.    int x,opcion,victoria=1,nombre1,nombre2,contador=1,correcta=1,aux=1,sumador = 0;
  44.    do
  45. {
  46. system("cls");
  47.    printf("\n\n\n     JUEGO CUATRO EN LINEA\n\n\n\n\n");
  48.    printf("  1) Ingresar datos del primer jugador\n\n");
  49.    printf("  2) Ingresar datos del segundo jugador\n\n");
  50.    printf("  3) Jugar\n\n");
  51.    printf("  4) Listar todos los juegos realizados\n\n");
  52.    printf("  5) Salir\n\n\n");
  53.    if (contador%2==0)
  54.     {
  55.     printf("  Error: Antes de jugar debe ingresar los datos de los dos jugadores. \n\n\n");
  56.     printf("  REINGRESE SU OPCION: ");
  57. contador--;
  58.     }
  59.     if (correcta%2==0)
  60.     {
  61.     printf("  Opcion incorrecta \n\n\n");
  62.     printf("  REINGRESE SU OPCION: ");
  63.     correcta--;
  64.     }
  65.     if (aux%2==0)
  66.     {
  67.     printf("  No se han realizado juegos. \n\n\n");
  68.     aux--;
  69.     }
  70.  
  71.    scanf("%d",&opcion);
  72.    system("cls");
  73.    switch (opcion)
  74.    {
  75.    case 1:  /*OPCION 1 DEL MENU*/
  76.    {
  77.     limpiar(nom_jug);
  78.     limpiar(ced_jug);
  79.     while(getchar() != '\n');
  80.     printf("\n Ingrese su Nombre: ");
  81.     do
  82.     {
  83.     scanf("%39[^\n]",partidas1[inicia1].nom_jug);
  84.     if (partidas1[inicia1].nom_jug[0]=='\0')
  85.     {
  86.     printf(" ERROR\n");
  87.     printf(" Ingrese nuevamente Nombre: ");
  88.     }
  89. else
  90. {
  91.  
  92. for (nombre1=0;nombre1<40;nombre1++)
  93.     {
  94.     nom_jug[nombre1];
  95.     }
  96. }while(getchar() != '\n');
  97. system("cls");
  98.        }while(partidas1[inicia1].nom_jug[0]=='\0');
  99.     printf("\n Ingrese su cedula: ");
  100.     do
  101.     {
  102.     scanf("%8[^\n]",partidas1[inicia1].ced_jug);    
  103.     if (partidas1[inicia1].ced_jug[0]=='\0')
  104.     {
  105.     printf(" ERROR\n");
  106.     printf(" Ingrese nuevamente su cedula: ");
  107.     }
  108. else
  109. {
  110.  
  111. for (nombre1=0;nombre1<8;nombre1++)
  112.     {
  113.     ced_jug[nombre1];
  114.     }
  115.        system("cls");
  116.     printf("\n");
  117. }while(getchar() != '\n');
  118.        }while(partidas1[inicia1].ced_jug[0]=='\0');
  119.        printf("\n    DATOS INGRESADOS\n");
  120.        printf("\n\n   Nombre ingresado: %s",partidas1[inicia1].nom_jug);
  121.     printf("\n\n   Cedula ingresada: %s",partidas1[inicia1].ced_jug);
  122.     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n presione ENTER para volver al menu");
  123.     partidas1[inicia1+1]=partidas1[inicia1];
  124.     jugar++;
  125.     break;
  126.     }
  127.    case 2:  /*OPCION 2 DEL MENU*/
  128.    {
  129. limpiar(nom_jug);
  130.     limpiar(ced_jug);
  131.     while(getchar() != '\n');
  132.     printf("\n Ingrese su Nombre: ");
  133.     do
  134.     {
  135.     scanf("%39[^\n]",partidas2[inicia2].nom_jug);
  136.     if (partidas2[inicia2].nom_jug[0]=='\0')
  137.     {
  138.     printf(" ERROR\n");
  139.     printf(" Ingrese nuevamente Nombre: ");
  140.     }
  141. else
  142. {                        
  143.  
  144. for (nombre2=0;nombre2<40;nombre2++)
  145.     {
  146.     nom_jug[nombre2];
  147.     }
  148. }while(getchar() != '\n');
  149. system("cls");
  150.        }while(partidas2[inicia2].nom_jug[0]=='\0');
  151.     printf("\n Ingrese su cedula: ");
  152.     do
  153.     {
  154.     scanf("%8[^\n]",partidas2[inicia2].ced_jug);  
  155. do
  156.  
  157.     if (partidas2[inicia2].ced_jug[0]=='\0')
  158.     {
  159.     printf(" ERROR\n");
  160.     printf(" Ingrese nuevamente su cedula: ");
  161.     }
  162. else
  163. {
  164.  
  165. for (nombre2=0;nombre2<8;nombre2++)
  166.     {
  167.     ced_jug[nombre2];
  168.     }
  169.     system("cls");
  170.     printf("\n");
  171. }while(getchar() != '\n');
  172.        }while(partidas2[inicia2].ced_jug[0]=='\0');
  173.  
  174.        printf("\n    DATOS INGRESADOS\n");
  175.        printf("\n\n   Nombre ingresado: %s",partidas2[inicia2].nom_jug);
  176.     printf("\n\n   Cedula ingresada: %s",partidas2[inicia2].ced_jug);
  177.     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n presione ENTER para volver al menu");
  178.     partidas2[inicia2+1]=partidas2[inicia2];
  179.     jugar++;
  180.     break;
  181.    }
  182.    case 3: /*OPCION 3 DEL MENU*/
  183.    {
  184.     if (jugar<3)
  185.     {
  186.     contador++;
  187.  
  188.     }
  189.     else  
  190.     {
  191.        cuatro_en_linea(tablero,opcion);
  192.  
  193.  
  194.        sumador++;
  195.        }
  196.        break;
  197.    }
  198.    case 4: /*OPCION 4 DEL MENU*/
  199.    {
  200.     if (sumador<1)
  201.     {
  202.     aux++;
  203.     }
  204.     else
  205.  
  206.     {
  207.             partida=1;
  208.             aux2=0;
  209. aux3=0;  
  210.             for(partida;partida<sumador+1;partida++){
  211.             printf("Partida %d\n",partida);
  212.             printf("\n");
  213.             printf("Jugador 1: %s\n",partidas1[inicia1].nom_jug);
  214.             printf("Cedula:    %s\n",partidas1[inicia1].ced_jug);
  215.             printf("Jugador 2: %s\n",partidas2[inicia2].nom_jug);
  216.             printf("Cedula:    %s\n",partidas2[inicia2].ced_jug);
  217.             aux2++;
  218.             printf("\n");
  219. }
  220.     }
  221.     system("PAUSE");
  222.     break;
  223.    }
  224.    case 5: /*OPCION 5 DEL MENU*/
  225.    {
  226.     return printf("\n\n  SALIENDO\n\n");
  227.     break;
  228.    default:
  229.     correcta++;
  230.     break;
  231.    }
  232.    }while (getchar()!='\n');
  233.    }while (victoria==1);
  234. }
  235. int cuatro_en_linea(char tablero[DIM][DIM],int x)
  236. {
  237.  
  238.    /*Defino distintas variables que voy a utilizar a lo largo del programa*/
  239.    int n=0,i,j,resto=1,k=0,jugador,partida,victoria=0;
  240.    char d;
  241.    printf("\n\nIngresa la dimension del tablero de juego(4-15): ");
  242.    /*Analizamos si la dimension del tablero ingresada es valida*/
  243.    do
  244.    {
  245.        scanf (" %d",&x);
  246.        system("cls");
  247.        if (x<4 || x>15)
  248.        {
  249.            printf("\nOpcion no valida. Ingresa la dimension (4-15): ");
  250.        }   while (getchar()!='\n');
  251.    } while (x<4 || x>15);
  252.    system("cls");
  253.    /*Elegimos judagor*/
  254.    printf("\n ELIJA JUGADOR\n\n\n");
  255.    printf("\n\n  1) JUGADOR 1 (X)\n");
  256.    printf("\n\n  2) JUGADOR 2 (O)\n");
  257.    do
  258.    {
  259.    scanf("%d",&jugador);
  260.    if ((jugador!=1) && (jugador!=2))
  261.    {
  262.        printf("\n Error: Los jugadores posibles son 1 y 2 ");
  263.    }
  264.    }while ((getchar()!='\n') || ((jugador!=1) && (jugador!=2)));
  265.    do
  266.    {
  267.        system("cls");
  268.        printf("\n\n\n\n");
  269.        inicializar_tablero(tablero,x);
  270.        while ((resto<=(x*x)) && (victoria!=1))
  271.        {
  272.            if (jugador==1)
  273.            {
  274.                jugador++;
  275.               juegan_X_(tablero,x,n);
  276.               /* Evaluo si el jugador 1 (X) gana*/
  277.  
  278.               for (i=1;i<=x;i++)
  279.           {
  280.     for (j=1;j<=x;j++)
  281. {
  282.                        /*Verifico si hay cuatro en linea horizontal para Jugador 1*/
  283. if ((tablero[i][j]=='X') && ((tablero[i][j+1])=='X') && ((tablero[i][j+2])=='X') && ((tablero[i][j+3])=='X'))
  284. {
  285. victoria = 1;
  286. }        
  287. /*Verifico si hay cuatro en linea vertical para Jugador 1*/
  288. else if ((tablero[i][j]=='X') && ((tablero[i+1][j])=='X') && ((tablero[i+2][j])=='X') && ((tablero[i+3][j])=='X'))
  289. {
  290. victoria = 1;
  291. }
  292. /*Verifico si hay cuatro en linea en diagonal ascendiente para Jugador 1*/
  293. else if ((tablero[i][j]=='X') && ((tablero[i+1][j-1])=='X') && ((tablero[i+2][j-2])=='X') && ((tablero[i+3][j-3])=='X'))
  294. {
  295. victoria = 1;
  296. }
  297. /*Verifico si hay cuatro en linea en diagonal descendiente para Jugador 1*/
  298. else if ((tablero[i][j]=='X') && ((tablero[i+1][j+1])=='X') && ((tablero[i+2][j+2])=='X') && ((tablero[i+3][j+3])=='X'))
  299. {
  300. victoria = 1;
  301. }
  302. }
  303.   }
  304.               if (victoria == 1)
  305.               {
  306.               printf("\n\n FELICITACIONES %s",partidas1[inicia1].nom_jug);
  307.               printf("!!!\n\n  Has ganado la partida.\n\n\n\n\n\n");
  308.               if(partidas1[inicia1].nom_jug[40]==partidas1[inicia1+1].nom_jug[40]){
  309.                        partidas1[inicia1+2]=partidas1[inicia1+1];
  310.                        inicia1++;
  311.                    }
  312.               }
  313.               else
  314.               {
  315.               resto++;
  316.               }
  317.            }
  318.            else if (jugador == 2)
  319.            {
  320.                jugador--;
  321. juegan_O_(tablero,x,n);
  322. /* Evaluo si el jugador 2 (O) gana*/
  323.  
  324. for (i=1;i<=x;i++)
  325.           {
  326.     for (j=1;j<=x;j++)
  327. {
  328.                        /*Verifico si hay cuatro en linea horizontal para Jugador 1*/
  329. if ((tablero[i][j]=='O') && ((tablero[i][j+1])=='O') && ((tablero[i][j+2])=='O') && ((tablero[i][j+3])=='O'))
  330. {
  331. victoria = 1;
  332. }
  333. /*Verifico si hay cuatro en linea vertical para Jugador 1*/
  334. else if ((tablero[i][j]=='O') && ((tablero[i+1][j])=='O') && ((tablero[i+2][j])=='O') && ((tablero[i+3][j])=='O'))
  335. {
  336. victoria = 1;
  337. }
  338. /*Verifico si hay cuatro en linea en diagonal ascendiente para Jugador 2*/
  339. else if ((tablero[i][j]=='O') && ((tablero[i+1][j-1])=='O') && ((tablero[i+2][j-2])=='O') && ((tablero[i+3][j-3])=='O'))
  340. {
  341. victoria = 1;
  342. }
  343. /*Verifico si hay cuatro en linea en diagonal descendiente para Jugador 2*/
  344. else if ((tablero[i][j]=='O') && ((tablero[i+1][j+1])=='O') && ((tablero[i+2][j+2])=='O') && ((tablero[i+3][j+3])=='O'))
  345. {
  346. victoria = 1;
  347. }
  348. }
  349.   }
  350.               if (victoria == 1)
  351.               {
  352.               printf("\n\n FELICITACIONES %s",partidas2[inicia2].nom_jug);
  353.               printf("!!!\n\n  Has ganado la partida.\n\n\n\n\n\n");
  354.               if(partidas2[inicia2].nom_jug[40]==partidas2[inicia2+1].nom_jug[40]){
  355.                        partidas2[inicia2+2]=partidas2[inicia2+1];
  356.                        inicia2++;
  357.                    }
  358.               }
  359.               else
  360.               {  
  361.                resto++;
  362.               }
  363.            }
  364.  
  365.            /*Si el tablero se lleno de fichas y no hay cuatro en linea
  366.             el partido termina en empate*/
  367.  
  368.            if (resto > (x*x) && (victoria!=1))
  369.         {
  370.         printf("\n\n EMPATE\n\n");
  371.            }
  372.        }
  373.    }while ((resto<=(x*x)) && (victoria!=1));
  374.    system("PAUSE");
  375.    resto = 1;
  376.    victoria = 0;
  377.    system("cls");
  378.    inicializar_tablero(tablero,x);
  379.    system("cls");
  380. }
  381. /* Procedimiento para imprimir el tablero inicial*/
  382.  
  383. void inicializar_tablero(char tablero[DIM][DIM],int x)
  384. {
  385. int i,j;
  386. int contador = 1;
  387. for (i=1; i<=x; i++)
  388. {
  389. for (j=1;j<=x; j++)
  390. {
  391.            printf("  ");
  392. printf(" %c",tablero[i][j]='.');
  393. }
  394. printf("\n\n");
  395.  
  396. }
  397. for (i=1;i<=x;i++)
  398. {
  399.        printf("   %d",contador);
  400.        contador++;
  401.    }
  402.    printf("\n");
  403. }
  404. /*Funcion para hacer jugada para el jugador 1*/
  405.  
  406. int juegan_X_(char tablero[DIM][DIM],int x,int n)
  407. {
  408. int i,j,k,contador=1;
  409. do
  410.    {
  411.        printf("\n\n Juega %s: \n",partidas1[inicia1].nom_jug);
  412.                    scanf ("%d",&n);
  413.        if (n<1 || n>x)
  414.        {
  415.            printf("Error: las columnas posibles son de 1 a %d",x);
  416.            while (getchar()!='\n');
  417.        }  
  418. else if (tablero[1][n]!='.')
  419. {
  420. printf("\nError: la columna ya esta llena.\n ");
  421.     }
  422.    }while((n<1 || n>x) || (tablero[1][n]!='.'));
  423. for (k=x;k>=1;k--)
  424.    {
  425.            if (tablero[k][n]!='.')
  426.            {
  427.                continue;
  428.            }
  429.            else
  430.            {
  431.                tablero[k][n]='X';
  432.                break;
  433.            }
  434.    }
  435.       system("cls");  
  436.   printf("\n Ultima jugada X: Columna %d\n",n);      
  437.    printf("\n\n");
  438.    for (i=1;i<=x;i++)
  439.    {
  440.        for (j=1;j<=x;j++)
  441.        {
  442.            printf("  ");
  443.            printf (" %c",tablero[i][j]);
  444.  
  445.        }
  446.        printf("\n\n");
  447.    }
  448.    for (i=1;i<=x;i++)
  449. {
  450.        printf("   %d",contador);
  451.        contador++;
  452.    }
  453.    printf("\n");
  454. }
  455. /*Funcion para hacer jugada para el jugador 2*/
  456.  
  457. int juegan_O_(char tablero[DIM][DIM],int x,int n)
  458. {
  459. int i,j,k,contador=1;
  460. do
  461.    {
  462.        printf("\n\n Juega %s: ",partidas2[inicia2].nom_jug);
  463.                    scanf ("%d",&n);
  464.        if (n<1 || n>x)
  465.        {
  466.            printf("Error: las columnas posibles son de 1 a %d",x);
  467.            while (getchar()!='\n');
  468.        }  
  469. else if (tablero[1][n]!='.')
  470. {
  471. printf("\nError: la columna ya esta llena.\n ");
  472.     }
  473.    }while((n<1 || n>x) || (tablero[1][n]!='.'));
  474. for (k=x;k>=1;k--)
  475.    {
  476.            if (tablero[k][n]!='.')
  477.            {
  478.                continue;
  479.            }
  480.            else
  481.            {
  482.                tablero[k][n]='O';
  483.                break;
  484.            }
  485.    }
  486.    system("cls");    
  487. printf("\n Ultima jugada O: Columna %d\n",n);    
  488.    printf("\n\n");
  489.    for (i=1;i<=x;i++)
  490.    {
  491.        for (j=1;j<=x;j++)
  492.        {
  493.            printf("  ");
  494.            printf (" %c",tablero[i][j]);
  495.  
  496.        }
  497.        printf("\n\n");
  498.    }
  499.    for (i=1;i<=x;i++)
  500. {
  501.        printf("   %d",contador);
  502.        contador++;
  503.    }
  504.    printf("\n");
  505. }
  506. int limpiar(char nom_jug[40])
  507. {
  508.  
  509. int t;
  510. for (t=0; t<40; t++)
  511.    {
  512.     nom_jug[t]='\0';
  513.    }
  514. }
  515.  


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: eferion en 1 Julio 2013, 23:55 pm
Código
  1. struct partidas
  2. {
  3.  char nom_jug[40];
  4.  char ced_jug[8];
  5. };
  6.  
  7. struct partidas partida1, partida2;

Es lo mismo, más claro y con menos código que:

Código
  1. struct partidas1
  2. {
  3.  char nom_jug[40];
  4.  char ced_jug[8];
  5.  
  6. } partidas1[500];
  7.  
  8. struct partidas2
  9. {
  10.  char nom_jug[40];
  11.  char ced_jug[8];
  12. } partidas2[500];

Aunque si lo que quieres es, por ejemplo almacenar los datos de las últimas 10 partidas necesitarías quizas algo así:

Código
  1. struct partidas
  2. {
  3.  char nom_jug[40];
  4.  char ced_jug[8];
  5. };
  6.  
  7. struct partidas partidas[10];

Con eso tendrías un array que te permitiría almacenar hasta 10 partidas.

Ahora la cosa, te lo dejo como tarea porque no voy a resolverte toda la práctica, es almacenar en ese array las diferentes partidas que vayas jugando.

Recuerda que lo primero es inicializar el array para que no muestre basura ( este consejo es gratis ), y luego almacenar los resultados en la posición en que correspondan.


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: ivancea96 en 2 Julio 2013, 01:48 am
Además de lo que dijo Eferion, te recomendaría, aislar el código dudoso o con errores, así no tenemos que leer 500 líneas de código xD Bueno, y además, quizas no quieras que otros se hagan con tu código :p


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: mathias_vg en 2 Julio 2013, 13:23 pm
Buenas, solo pretendo que me ayuden con el punto cuatro del menu ya que no me almacena los menu sino que solo me imprime los ultimos, es decir me borra los anteriores. Recurro a ustedes ya que mi profesor solamente dio 5 clases en todo el semestre y nos mato (al menos a mi) con este obligatorio. mi proposito es aprender a  superar esta dificultad con la que estoy trancado. les dejo el codigo un poco mas resumido.

Muchas gracias.

Código
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #define DIM 16 /*Constante utilizada para definir la maxima dimension del tablero*/
  4. //#define tam_string 40 /*Constante para la cantidad de digitos de los Nombres*/
  5. //#define ced_string 8
  6.  
  7. struct partidas1{
  8.        char nom_jug[40];
  9. char ced_jug[8];
  10.    }partidas1[500];
  11. struct partidas2{
  12. char nom_jug[40];
  13. char ced_jug[8];
  14.    }partidas2[500];
  15.    int partida, aux2;
  16.    struct partidas
  17. {
  18.  char nom_jug[40];
  19.  char ced_jug[8];
  20. };
  21.  
  22. struct partidas partidas[10];
  23.  
  24. char nom_jug[40];
  25. char ced_jug[8];
  26.  
  27.  
  28. /*PROCEDIMIENTOS Y FUNCIONES*/
  29.  
  30. void inicializar_tablero(char tablero[][DIM],int);
  31. int juegan_O_(char tabla[][DIM],int,int);
  32. int juegan_X_(char tablero[][DIM],int,int);
  33. int cuatro_en_linea(char tablero[][DIM],int);
  34. int limpiar(char nom_jug[]);
  35.  
  36.  
  37. int inicia1 = 0;
  38. int inicia2 = 0, jugar=1;
  39. int main()
  40. {
  41.    char tablero[DIM][DIM];
  42.    int x,opcion,victoria=1,nombre1,nombre2,contador=1,correcta=1,aux=1,sumador = 0;
  43.    do
  44. {
  45. system("cls");
  46.    printf("\n\n\n     JUEGO CUATRO EN LINEA\n\n\n\n\n");
  47.    printf("  1) Ingresar datos del primer jugador\n\n");
  48.    printf("  2) Ingresar datos del segundo jugador\n\n");
  49.    printf("  3) Jugar\n\n");
  50.    printf("  4) Listar todos los juegos realizados\n\n");
  51.    printf("  5) Salir\n\n\n");
  52.    if (contador%2==0)
  53.     {
  54.     printf("  Error: Antes de jugar debe ingresar los datos de los dos jugadores. \n\n\n");
  55.     printf("  REINGRESE SU OPCION: ");
  56. contador--;
  57.     }
  58.     if (correcta%2==0)
  59.     {
  60.     printf("  Opcion incorrecta \n\n\n");
  61.     printf("  REINGRESE SU OPCION: ");
  62.     correcta--;
  63.     }
  64.     if (aux%2==0)
  65.     {
  66.     printf("  No se han realizado juegos. \n\n\n");
  67.     aux--;
  68.     }
  69.  
  70.    scanf("%d",&opcion);
  71.    system("cls");
  72.    switch (opcion)
  73.    {
  74.    case 1:  /*OPCION 1 DEL MENU*/
  75.    {
  76.     limpiar(nom_jug);
  77.     limpiar(ced_jug);
  78.     while(getchar() != '\n');
  79.     printf("\n Ingrese su Nombre: ");
  80.     do
  81.     {
  82.     scanf("%39[^\n]",partidas1[inicia1].nom_jug);
  83.     if (partidas1[inicia1].nom_jug[0]=='\0')
  84.     {
  85.     printf(" ERROR\n");
  86.     printf(" Ingrese nuevamente Nombre: ");
  87.     }
  88. else
  89. {
  90.  
  91. for (nombre1=0;nombre1<40;nombre1++)
  92.     {
  93.     nom_jug[nombre1];
  94.     }
  95. }while(getchar() != '\n');
  96. system("cls");
  97.        }while(partidas1[inicia1].nom_jug[0]=='\0');
  98.     printf("\n Ingrese su cedula: ");
  99.     do
  100.     {
  101.     scanf("%8[^\n]",partidas1[inicia1].ced_jug);    
  102.     if (partidas1[inicia1].ced_jug[0]=='\0')
  103.     {
  104.     printf(" ERROR\n");
  105.     printf(" Ingrese nuevamente su cedula: ");
  106.     }
  107. else
  108. {
  109.  
  110. for (nombre1=0;nombre1<8;nombre1++)
  111.     {
  112.     ced_jug[nombre1];
  113.     }
  114.        system("cls");
  115.     printf("\n");
  116. }while(getchar() != '\n');
  117.        }while(partidas1[inicia1].ced_jug[0]=='\0');
  118.        printf("\n    DATOS INGRESADOS\n");
  119.        printf("\n\n   Nombre ingresado: %s",partidas1[inicia1].nom_jug);
  120.     printf("\n\n   Cedula ingresada: %s",partidas1[inicia1].ced_jug);
  121.     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n presione ENTER para volver al menu");
  122.     partidas1[inicia1+1]=partidas1[inicia1];
  123.     jugar++;
  124.     break;
  125.     }
  126.    case 2:  /*OPCION 2 DEL MENU*/
  127.    {
  128. limpiar(nom_jug);
  129.     limpiar(ced_jug);
  130.     while(getchar() != '\n');
  131.     printf("\n Ingrese su Nombre: ");
  132.     do
  133.     {
  134.     scanf("%39[^\n]",partidas2[inicia2].nom_jug);
  135.     if (partidas2[inicia2].nom_jug[0]=='\0')
  136.     {
  137.     printf(" ERROR\n");
  138.     printf(" Ingrese nuevamente Nombre: ");
  139.     }
  140. else
  141. {                        
  142.  
  143. for (nombre2=0;nombre2<40;nombre2++)
  144.     {
  145.     nom_jug[nombre2];
  146.     }
  147. }while(getchar() != '\n');
  148. system("cls");
  149.        }while(partidas2[inicia2].nom_jug[0]=='\0');
  150.     printf("\n Ingrese su cedula: ");
  151.     do
  152.     {
  153.     scanf("%8[^\n]",partidas2[inicia2].ced_jug);  
  154. do
  155.  
  156.     if (partidas2[inicia2].ced_jug[0]=='\0')
  157.     {
  158.     printf(" ERROR\n");
  159.     printf(" Ingrese nuevamente su cedula: ");
  160.     }
  161. else
  162. {
  163.  
  164. for (nombre2=0;nombre2<8;nombre2++)
  165.     {
  166.     ced_jug[nombre2];
  167.     }
  168.     system("cls");
  169.     printf("\n");
  170. }while(getchar() != '\n');
  171.        }while(partidas2[inicia2].ced_jug[0]=='\0');
  172.  
  173.        printf("\n    DATOS INGRESADOS\n");
  174.        printf("\n\n   Nombre ingresado: %s",partidas2[inicia2].nom_jug);
  175.     printf("\n\n   Cedula ingresada: %s",partidas2[inicia2].ced_jug);
  176.     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n presione ENTER para volver al menu");
  177.     partidas2[inicia2+1]=partidas2[inicia2];
  178.     jugar++;
  179.     break;
  180.    }
  181.    case 3: /*OPCION 3 DEL MENU*/
  182.    {
  183.     if (jugar<3)
  184.     {
  185.     contador++;
  186.  
  187.     }
  188.     else  
  189.     {
  190.        cuatro_en_linea(tablero,opcion);
  191.  
  192.  
  193.        sumador++;
  194.        }
  195.        break;
  196.    }
  197.    case 4: /*OPCION 4 DEL MENU*/
  198.    {
  199.     if (sumador<1)
  200.     {
  201.     aux++;
  202.     }
  203.     else
  204.  
  205.     {
  206.             partida=1;
  207.             aux2=0;  
  208.             for(partida;partida<sumador+1;partida++){
  209.             printf("Partida %d\n",partida);
  210.             printf("\n");
  211.             printf("Jugador 1: %s\n",partidas1[aux2].nom_jug);
  212.             printf("Cedula:    %s\n",partidas1[aux2].ced_jug);
  213.             printf("Jugador 2: %s\n",partidas2[aux2].nom_jug);
  214.             printf("Cedula:    %s\n",partidas2[aux2].ced_jug);
  215.             aux2++;
  216.             printf("\n");
  217. }
  218.     }
  219.     system("PAUSE");
  220.     break;
  221.    }
  222.    case 5: /*OPCION 5 DEL MENU*/
  223.    {
  224.     return printf("\n\n  SALIENDO\n\n");
  225.     break;
  226.    default:
  227.     correcta++;
  228.     break;
  229.    }
  230.    }while (getchar()!='\n');
  231.    }while (victoria==1);
  232. }


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: eferion en 2 Julio 2013, 14:46 pm
Código
  1. for(partida;partida<sumador+1;partida++)

Para empezar ese for está mal. El primer elemento del for sirve para asociar un valor inicial a las variables involucradas en el bucle. for ( partida; ... ) ahí no estableces ningún valor.

otra cosa sería

Código
  1. for(partida=0;partida<sumador+1;partida++)

o, si el valor de inicio ya venía definido antes, puedes dejar el campo en blanco.

Código
  1. for( ;partida<sumador+1;partida++)

Esto funciona con cualquiera de los tres campos del for. De echo, for( ; ; ) compila y es equivalente a while ( 1 ).

Y lo siento pero el tiempo que me requiere pelear con el laberinto que has montado no lo dispongo.

Te propongo una cosa... mejora la legibilidad de tu código, me da igual que solo te falle una cosa, hazlo, consigue que este código sea legible y luego te ayudamos a resolver tu problema.

Vas a ganar por partida doble... por un lado aprenderás a hacer las cosas bien, y por otro acabarás con un código que funciona.

Procura no usar variables globales, no son necesarias y dificultan la lectura del código... lo que tengas que usar en una función, pásalo como parámetro.

Para las partidas usa esto mejor:

Código
  1. struct jugador
  2. {
  3.  char nom_jug[ 40 ];
  4.  char ced_jug[ 8 ];
  5. };
  6.  
  7. struct partida
  8. {
  9.  struct jugador jugador1;
  10.  struct jugador jugador2;
  11. };
  12.  
  13. int main( )
  14. {
  15.  struct partida partidas[500];
  16.  
  17.  // ...
  18. }


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: mathias_vg en 2 Julio 2013, 18:02 pm
eferion. muchas gracias por tus consejos, estoy intentando mejorar el codigo pero se me esta haciendo realmente muy dificil, igualmente seguire luchando con el, ni bien tenga algo lo publico.

Muchas gracias de verdad!


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: eferion en 2 Julio 2013, 18:25 pm
Encantado de ayudar.

Aprender algo nuevo siempre cuesta trabajo... pero siempre es mejor ir más despacio al principio y partir de una base sólida... a corto, medio y largo plazo te da más beneficios.

Un saludo.


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: mathias_vg en 3 Julio 2013, 01:06 am
Bueno eh estado todo el dia con este codigo y no logro hacer que me almacene todos los jugadores en la opcion cuatro! Por favor si alguien me puede ayudar con este codigo se los agradezco.


Saludos!


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: eferion en 3 Julio 2013, 07:13 am
Es que el problema quizas no es listarlos... lo mismo estás almacenando mal los datos en la tabla... por qué no usas una sola variable para saber cuántas partidas tienes??

ahora mismo estás usando 3 y no tiene sentido.


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: mathias_vg en 3 Julio 2013, 16:26 pm
Eferion disculpa tantas molestias pero eh tratado de leer por otros lados el tema de estructurass y demas y no logro entender esto como para poder listar las partidas con sus respectivos jugadores. Me puedes ayudar? disculpa tantas preguntas pero de verdad estoy desesperando!!!

Saludos

Código
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #define DIM 16 /*Constante utilizada para definir la maxima dimension del tablero*/
  4. //#define tam_string 40 /*Constante para la cantidad de digitos de los Nombres*/
  5. //#define ced_string 8
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. /*PROCEDIMIENTOS Y FUNCIONES*/
  16.  
  17.  
  18. struct jugador
  19. {
  20.  char nom_jug[40];
  21.  char ced_jug[8];
  22. };
  23.  
  24. struct partida
  25. {
  26.  struct jugador jugador1;
  27.  struct jugador jugador2;
  28. };
  29.  
  30. char nom_jug[40];
  31. char ced_jug[8];
  32.  
  33.  
  34. void inicializar_tablero(char tablero[][DIM],int);
  35. int juegan_O_(char tabla[][DIM],int,int);
  36. int juegan_X_(char tablero[][DIM],int,int);
  37. int cuatro_en_linea(char tablero[][DIM],int);
  38. int limpiar(char nom_jug[]);
  39.  
  40. int main()
  41. {
  42. struct partida partidas[500];
  43.  
  44.    char tablero[DIM][DIM];
  45.    int x,opcion,victoria=1,nombre1,nombre2,contador=1,correcta=1,aux=1,sumador = 0;
  46.    int inicia1 = 0;
  47. int inicia2 = 0, jugar=1, part,aux2;
  48.    do
  49. {
  50. system("cls");
  51.    printf("\n\n\n     JUEGO CUATRO EN LINEA\n\n\n\n\n");
  52.    printf("  1) Ingresar datos del primer jugador\n\n");
  53.    printf("  2) Ingresar datos del segundo jugador\n\n");
  54.    printf("  3) Jugar\n\n");
  55.    printf("  4) Listar todos los juegos realizados\n\n");
  56.    printf("  5) Salir\n\n\n");
  57.    if (contador%2==0)
  58.     {
  59.     printf("  Error: Antes de jugar debe ingresar los datos de los dos jugadores. \n\n\n");
  60.     printf("  REINGRESE SU OPCION: ");
  61. contador--;
  62.     }
  63.     if (correcta%2==0)
  64.     {
  65.     printf("  Opcion incorrecta \n\n\n");
  66.     printf("  REINGRESE SU OPCION: ");
  67.     correcta--;
  68.     }
  69.     if (aux%2==0)
  70.     {
  71.     printf("  No se han realizado juegos. \n\n\n");
  72.     aux--;
  73.     }
  74.  
  75.    scanf("%d",&opcion);
  76.    system("cls");
  77.    switch (opcion)
  78.    {
  79.    case 1:  /*OPCION 1 DEL MENU*/
  80.    {
  81.     limpiar(nom_jug);
  82.     limpiar(ced_jug);
  83.     while(getchar() != '\n');
  84.     printf("\n Ingrese su Nombre: ");
  85.     do
  86.     {
  87.     scanf("%39[^\n]",jugador[inicia1].nom_jug);
  88.     if ((jugador[inicia1].nom_jug[0]=='\0') || (jugador[inicia1].nom_jug[0]==' '))
  89.     {
  90.     printf(" ERRR\n");
  91.     printf(" Ingrese nuevamente Nombre: ");
  92.     }
  93. else
  94. {
  95.  
  96. for (nombre1=0;nombre1<40;nombre1++)
  97.     {
  98.     nom_jug[nombre1];
  99.     }
  100.     system("cls");
  101. }while(getchar() != '\n');
  102.        }while((jugador[inicia1].nom_jug[0]=='\0') || (jugador[inicia1].nom_jug[0]==' '));
  103.     printf("\n Ingrese su cedula: ");
  104.     do
  105.     {
  106.     scanf("%8[^\n]",jugador[inicia1].ced_jug);    
  107.     if ((jugador[inicia1].ced_jug[0]=='\0') || (jugador[inicia1].ced_jug[0]==' '))
  108.     {
  109.     printf(" ERRR\n");
  110.     printf(" Ingrese nuevamente su cedula: ");
  111.     }
  112. else
  113. {
  114.  
  115. for (nombre1=0;nombre1<8;nombre1++)
  116.     {
  117.     ced_jug[nombre1];
  118.     }
  119.        system("cls");
  120.     printf("\n");
  121. }while(getchar() != '\n');
  122.        }while((jugador[inicia1].ced_jug[0]=='\0') || (jugador[inicia1].ced_jug[0]==' '));
  123.        printf("\n    DATOS INGRESADOS\n");
  124.        printf("\n\n   Nombre ingresado: %s",jugador[inicia1].nom_jug);
  125.     printf("\n\n   Cedula ingresada: %s",jugador[inicia1].ced_jug);
  126.     jugar++;
  127.     break;
  128.     }
  129.    case 2:  /*OPCION 2 DEL MENU*/
  130.    {
  131.     limpiar(nom_jug);
  132.     limpiar(ced_jug);
  133.     while(getchar() != '\n');
  134.     printf("\n Ingrese su Nombre: ");
  135.     do
  136.     {
  137.     scanf("%39[^\n]",jugador[inicia2].nom_jug);
  138.     if ((jugador[inicia2].nom_jug[0]=='\0') || (jugador[inicia2].nom_jug[0]==' '))
  139.     {
  140.     printf(" ERROR\n");
  141.     printf(" Ingrese nuevamente Nombre: ");
  142.     }
  143. else
  144. {
  145.  
  146. for (nombre2=0;nombre2<40;nombre2++)
  147.     {
  148.     nom_jug[nombre2];
  149.     }
  150.     system("cls");
  151. }while(getchar() != '\n');
  152.        }while((jugador[inicia2].nom_jug[0]=='\0') || (jugador[inicia2].nom_jug[0]==' '));
  153.     printf("\n Ingrese su cedula: ");
  154.     do
  155.     {
  156.     scanf("%8[^\n]",jugador[inicia2].ced_jug);    
  157.     if ((jugador[inicia2].ced_jug[0]=='\0') || (jugador[inicia2].ced_jug[0]==' '))
  158.     {
  159.     printf(" ERROR\n");
  160.     printf(" Ingrese nuevamente su cedula: ");
  161.     }
  162. else
  163. {
  164.  
  165. for (nombre2=0;nombre2<8;nombre2++)
  166.     {
  167.     ced_jug[nombre2];
  168.     }
  169.        system("cls");
  170.     printf("\n");
  171. }while(getchar() != '\n');
  172.        }while((jugador[inicia2].ced_jug[0]=='\0') || (jugador[inicia2].ced_jug[0]==' '));
  173.        printf("\n    DATOS INGRESADOS\n");
  174.        printf("\n\n   Nombre ingresado: %s",jugador[inicia2].nom_jug);
  175.     printf("\n\n   Cedula ingresada: %s",jugador[inicia2].ced_jug);
  176.     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n presione ENTER para volver al menu");
  177.     jugar++;
  178.     break;
  179.     }
  180.    case 3: /*OPCION 3 DEL MENU*/
  181.    {
  182.     if (jugar<3)
  183.     {
  184.     contador++;
  185.  
  186.     }
  187.     else  
  188.     {
  189.        cuatro_en_linea(tablero,opcion);
  190.        sumador++;
  191.  
  192.        }
  193.        break;
  194.    }
  195.    case 4: /*OPCION 4 DEL MENU*/
  196.    {
  197.     if (sumador<1)
  198.     {
  199.     aux++;
  200.     }
  201.     else
  202.  
  203.     {
  204.             part=1;
  205.             aux2=0;  
  206.             for(aux2=0;aux2<sumador;aux2++){
  207.             printf("Partida %d\n",part);
  208.             part++;
  209.             printf("\n");
  210.             printf("Jugador 1: %s\n",jugador[aux2].nom_jug);
  211.             printf("Cedula:    %s\n",jugador[aux2].ced_jug);
  212.             printf("Jugador 2: %s\n",jugador[aux2].nom_jug);
  213.             printf("Cedula:    %s\n",jugador[aux2].ced_jug);
  214.              printf("\n");
  215.  
  216.  
  217.     }
  218.     }
  219.     system("PAUSE");
  220.     break;
  221.    }
  222.    case 5: /*OPCION 5 DEL MENU*/
  223.    {
  224.     return printf("\n\n  SALIENDO\n\n");
  225.     break;
  226.    default:
  227.     correcta++;
  228.     break;
  229.    }
  230.    }while (getchar()!='\n');
  231.    }while (victoria==1);
  232. }


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: eferion en 3 Julio 2013, 17:46 pm
Me refería a algo más o menos así...

Fíjate que mi main tiene unas 40 líneas frente a las 200 del tuyo... aparte de que el mío funciona jejejeje.

Serás capaz de terminarlo??

Hay cosas que habría hecho de forma diferente, pero he intentado mantener el estilo de tu programa.

Código
  1. //* JUEGO CONECTA 4*//
  2.  
  3. /*Librerias utilizadas*/
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define LENGTH_JUGADOR    40
  9. #define LENGTH_CEDULA     8
  10. #define MAX_PARTIDAS      20
  11. #define DIM               16 //Constante utilizada para definir la maxima dimension del tablero
  12.  
  13. /* Estructuras */
  14.  
  15. struct jugador
  16. {
  17.  char nom_jug[ LENGTH_JUGADOR + 1 ];
  18.  char ced_jug[ LENGTH_CEDULA + 1 ];
  19. };
  20.  
  21. struct partida
  22. {
  23.  // Para almacenar los datos del jugador1
  24.  struct jugador jugador1;
  25.  
  26.  // Para almacenar los datos del jugador2
  27.  struct jugador jugador2;
  28.  
  29.  // Almacena 1 ( ganador jugador 1 ) o 2 ( ganador jugador 2 ).
  30.  int ganador;
  31. };
  32.  
  33. struct manager_partidas
  34. {
  35.  // Almacena todas las partidas jugadas
  36.  struct partida partidas[ MAX_PARTIDAS ];
  37.  
  38.  // Indica la partida actual
  39.  int num_partida;
  40. };
  41.  
  42. /*PROCEDIMIENTOS Y FUNCIONES*/
  43.  
  44. void pausa( );
  45. int mostrar_menu( int );
  46. int calcula_numero_jugadores( struct partida* );
  47. void pedir_datos_jugador( struct jugador* );
  48. void mostrar_resultados( struct manager_partidas* );
  49.  
  50. void inicializar_tablero(char tablero[][DIM],int);
  51. int juegan_O_(char tabla[][DIM],int,int);
  52. int juegan_X_(char tablero[][DIM],int,int);
  53. int cuatro_en_linea(char tablero[][DIM],int);
  54. int limpiar(char nom_jug[]);
  55.  
  56. int main()
  57. {
  58.  char tablero[ DIM ][ DIM ];
  59.  int numero_jugadores = 0;
  60.  int opcion = -1;
  61.  
  62.  // Se inicializa la estructura de las partidas
  63.  struct manager_partidas manager;
  64.  memset( &manager, 0, sizeof( manager ) );
  65.  
  66.  while ( opcion != 5 )
  67.  {
  68.    // Puntero a la partida actual... para evitar líneas de código
  69.    struct partida* partida_actual = &manager.partidas[ manager.num_partida ];
  70.  
  71.    // Se comprueba el número de jugadores en la partida actual
  72.    numero_jugadores = calcula_numero_jugadores( partida_actual );
  73.  
  74.    opcion = mostrar_menu( numero_jugadores );
  75.  
  76.    switch ( opcion )
  77.    {
  78.      case 1:
  79.        pedir_datos_jugador( &partida_actual->jugador1 );
  80.        break;
  81.  
  82.      case 2:
  83.        pedir_datos_jugador( &partida_actual->jugador2 );
  84.        break;
  85.  
  86.      case 3:
  87.        // Se simula una partida, para ello se elige un ganador
  88.        // y se incrementa el contador de la partida
  89.        partida_actual->ganador = ( manager.num_partida % 2 );
  90.        ++manager.num_partida;
  91.  
  92.        // Jugar
  93.        break;
  94.  
  95.      case 4:
  96.        // Se pasa una referencia para no copiar toda la estructura
  97.        mostrar_resultados( &manager );
  98.        break;
  99.  
  100.      case 5:
  101.        break;
  102.    }
  103.  }
  104. }
  105.  
  106. void pausa( )
  107. {
  108.  printf( "\nPulse una tecla para continuar..." );
  109.  fseek( stdin, 0, SEEK_END);
  110.  getchar( );
  111. }
  112.  
  113. int mostrar_menu( int numero_jugadores )
  114. {
  115.  int ok = 0;
  116.  int opcion = -1;
  117.  
  118.  while ( !ok )
  119.  {
  120.    // Limpiamos el buffer, por si acaso
  121.    fseek( stdin, 0, SEEK_END);
  122.  
  123.    system( "cls" );
  124.    printf( "\n\n\n     JUEGO CUATRO EN LINEA\n\n\n\n\n" );
  125.    printf( "  1) Ingresar datos del primer jugador\n\n" );
  126.    printf( "  2) Ingresar datos del segundo jugador\n\n" );
  127.    printf( "  3) Jugar\n\n" );
  128.    printf( "  4) Listar todos los juegos realizados\n\n" );
  129.    printf( "  5) Salir\n\n\n" );
  130.  
  131.    if ( opcion == 3 )
  132.    {
  133.      printf( "  Error: Antes de jugar debe ingresar los datos de los dos jugadores. \n\n\n" );
  134.      printf( "  REINGRESE SU OPCION: " );
  135.    }
  136.    else if ( opcion != -1 )
  137.    {
  138.      printf( "  Opcion incorrecta \n\n\n" );
  139.      printf( "  REINGRESE SU OPCION: " );
  140.    }
  141.    else
  142.    {
  143.      printf( "INGRESE SU OPCION: " );
  144.    }
  145.  
  146.    scanf( "%d", &opcion );
  147.    if ( opcion == 3 )
  148.      ok = ( numero_jugadores == 2 );
  149.    else
  150.      ok = ( opcion > 0 && opcion < 6 );
  151.  }
  152.  
  153.  return opcion;
  154. }
  155.  
  156.  
  157. int calcula_numero_jugadores( struct partida* partida )
  158. {
  159.  int jugadores = 0;
  160.  
  161.  if ( partida->jugador1.nom_jug[ 0 ] != '\0' )
  162.    ++jugadores;
  163.  
  164.  if ( partida->jugador2.nom_jug[ 0 ] != '\0' )
  165.    ++jugadores;
  166.  
  167.  return jugadores;
  168. }
  169.  
  170.  
  171. void pedir_datos_jugador( struct jugador* jugador )
  172. {
  173.  int ok = 0;
  174.  
  175.  while ( !ok )
  176.  {
  177.    // Se limpia el buffer de entrada.
  178.    fseek( stdin, 0, SEEK_END);
  179.  
  180.    printf( "\n Ingrese su Nombre: " );
  181.    scanf("%40[^\n]", jugador->nom_jug );
  182.    ok = ( jugador->nom_jug[ 0 ] != '\0' );
  183.  
  184.    if ( !ok )
  185.      printf("\n ERROR" );
  186.  }
  187.  
  188.  ok = 0;
  189.  while ( !ok )
  190.  {
  191.    // Se limpia el buffer de entrada.
  192.    fseek( stdin, 0, SEEK_END);
  193.  
  194.    printf( "\n Ingrese su cedula: " );
  195.    scanf("%40[^\n]", jugador->ced_jug );
  196.    ok = ( jugador->ced_jug[ 0 ] != '\0' );
  197.  
  198.    if ( !ok )
  199.      printf("\n ERROR" );
  200.  }
  201.  
  202.  pausa( );
  203. }
  204.  
  205. void mostrar_resultados( struct manager_partidas* manager )
  206. {
  207.  if ( manager->num_partida == 0 )
  208.  {
  209.    printf ( "No se han jugador partidas." );
  210.  }
  211.  else
  212.  {
  213.    int i;
  214.    for ( i=0; i<manager->num_partida; ++i )
  215.    {
  216.      printf( "Partida %d\n", i+1 );
  217.      printf( "\n");
  218.      printf( "Jugador 1: %s\n", manager->partidas[ i ].jugador1.nom_jug );
  219.      printf( "Cedula:    %s\n", manager->partidas[ i ].jugador1.ced_jug );
  220.      printf( "Jugador 2: %s\n", manager->partidas[ i ].jugador2.nom_jug );
  221.      printf( "Cedula:    %s\n", manager->partidas[ i ].jugador2.ced_jug );
  222.      printf( "Ganador jugador %d\n\n", manager->partidas[ i ].ganador );
  223.    }
  224.  }
  225.  
  226.  pausa( );
  227. }


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: mathias_vg en 4 Julio 2013, 20:09 pm
Bueno eh podido hacer que me almacene las partidas pero lo que no puedo es que me indique el ganador de cada partida alguien me puede ayudar con esto?


Muchas gracias

Nota : en la linea 200 me tira un error cuando cito a la funcion 4 en linea, algeuin sabe porque es?



Código
  1.  
  2.    //* JUEGO CUATRO EN LINEA*//
  3.  
  4.  
  5.   /*MATHIAS CASTRO*/
  6.    /*4.523.221-8*/
  7.  
  8. /*Librerias utilizadas*/
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #define DIM 16 /*Constante utilizada para definir la maxima dimension del tablero*/
  12. //#define tam_string 40 /*Constante para la cantidad de digitos de los Nombres*/
  13. //#define ced_string 8
  14.  
  15.  
  16. struct jugador1{
  17.        char nom_jug1[40];
  18.        char ced_jug1[8];
  19.    }jugador1[500];
  20. struct jugador2{
  21.        char nom_jug2[40];
  22.        char ced_jug2[8];
  23.    }jugador2[500];
  24. struct ganadores{
  25.        char jganador;
  26.    }ganadores[1000];
  27.  
  28. /*PROCEDIMIENTOS Y FUNCIONES*/
  29.  
  30. void inicializar_tablero(char tablero[][DIM],int);
  31. int juegan_O_(char tabla[][DIM],int,int);
  32. int juegan_X_(char tablero[][DIM],int,int);
  33. int cuatro_en_linea(char tablero[][DIM],int);
  34. int limpiar(char nom_jug[]);
  35.  
  36. char nom_jug1[40];
  37. char nom_jug2[40];
  38. char ced_jug1[8];
  39. char ced_jug2[8];
  40. int player1 = 0, player2 = 0;
  41. int main()
  42. {
  43.  
  44.  
  45.  
  46.    char tablero[DIM][DIM];
  47.  
  48.    int x,opcion,victoria=1,nombre1,nombre2,contador=1,correcta=1,aux=1,sumador = 0;
  49.    int ganador=0,temp = 0;
  50.    int part, aux2,aux3;
  51.    do
  52. {
  53. system("cls");
  54.    printf("\n\n\n     JUEGO CUATRO EN LINEA\n\n\n\n\n");
  55.    printf("  1) Ingresar datos del primer jugador\n\n");
  56.    printf("  2) Ingresar datos del segundo jugador\n\n");
  57.    printf("  3) Jugar\n\n");
  58.    printf("  4) Listar todos los juegos realizados\n\n");
  59.    printf("  5) Salir\n\n\n");
  60.    if (contador%2==0)
  61.     {
  62.     printf("  Error: Antes de jugar debe ingresar los datos de los dos jugadores. \n\n\n");
  63.     printf("  REINGRESE SU OPCION: ");
  64. contador--;
  65.     }
  66.     if (correcta%2==0)
  67.     {
  68.     printf("  Opcion incorrecta \n\n\n");
  69.     printf("  REINGRESE SU OPCION: ");
  70.     correcta--;
  71.     }
  72.     if (aux%2==0)
  73.     {
  74.     printf("  No se han realizado juegos. \n\n\n");
  75.     aux--;
  76.     }
  77.  
  78.    scanf("%d",&opcion);
  79.    system("cls");
  80.    switch (opcion)
  81.    {
  82.    case 1:  /*OPCION 1 DEL MENU*/
  83.    {
  84.     limpiar(nom_jug1);
  85.     limpiar(ced_jug1);
  86.     while(getchar() != '\n');
  87.     printf("\n Ingrese su Nombre: ");
  88.     do
  89.     {
  90.     scanf("%39[^\n]",jugador1[player1].nom_jug1);
  91.     if (jugador1[player1].nom_jug1[0]=='\0')
  92.     {
  93.     printf(" ERROR\n");
  94.     printf(" Ingrese nuevamente Nombre: ");
  95.     }
  96. else
  97. {
  98.  
  99. for (nombre1=0;nombre1<40;nombre1++)
  100.     {
  101.     nom_jug1[nombre1];
  102.     }
  103. }while(getchar() != '\n');
  104. system("cls");
  105.        }while(jugador1[player1].nom_jug1[0]=='\0');
  106.     printf("\n Ingrese su cedula: ");
  107.     do
  108.     {
  109.     scanf("%8[^\n]",jugador1[player1].ced_jug1);    
  110.     if (jugador1[player1].ced_jug1[0]=='\0')
  111.     {
  112.     printf(" ERROR\n");
  113.     printf(" Ingrese nuevamente su cedula: ");
  114.     }
  115. else
  116. {
  117.  
  118. for (nombre1=0;nombre1<8;nombre1++)
  119.     {
  120.     ced_jug1[nombre1];
  121.     }
  122.        system("cls");
  123.     printf("\n");
  124. }while(getchar() != '\n');
  125.        }while(jugador1[player1].ced_jug1[0]=='\0');
  126.        printf("\n    DATOS INGRESADOS\n");
  127.        printf("\n\n   Nombre ingresado: %s",jugador1[player1].nom_jug1);
  128.     printf("\n\n   Cedula ingresada: %s",jugador1[player1].ced_jug1);
  129.     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n presione ENTER para volver al menu");
  130.     jugador1[player1+1]=jugador1[player1];
  131.     break;
  132.    }
  133.    case 2:  /*OPCION 2 DEL MENU*/
  134.    {
  135. limpiar(nom_jug2);
  136.     limpiar(ced_jug2);
  137.     while(getchar() != '\n');
  138.     printf("\n Ingrese su Nombre: ");
  139.     do
  140.     {
  141.     scanf("%39[^\n]",jugador2[player2].nom_jug2);
  142.     if (jugador2[player2].nom_jug2[0]=='\0')
  143.     {
  144.     printf(" ERROR\n");
  145.     printf(" Ingrese nuevamente Nombre: ");
  146.     }
  147. else
  148. {
  149.  
  150. for (nombre2=0;nombre2<40;nombre2++)
  151.     {
  152.     nom_jug2[nombre2];
  153.     }
  154. }while(getchar() != '\n');
  155. system("cls");
  156.        }while(jugador2[player2].nom_jug2[0]=='\0');
  157.     printf("\n Ingrese su cedula: ");
  158.     do
  159.     {
  160.     scanf("%8[^\n]",jugador2[player2].ced_jug2);  
  161. do
  162.  
  163.     if (jugador2[player2].ced_jug2[0]=='\0')
  164.     {
  165.     printf(" ERROR\n");
  166.     printf(" Ingrese nuevamente su cedula: ");
  167.     }
  168. else
  169. {
  170.  
  171. for (nombre2=0;nombre2<8;nombre2++)
  172.     {
  173.     ced_jug2[nombre2];
  174.     }
  175.     system("cls");
  176.     printf("\n");
  177. }while(getchar() != '\n');
  178.        }while(jugador2[player2].ced_jug2[0]=='\0');
  179.  
  180.        printf("\n    DATOS INGRESADOS\n");
  181.        printf("\n\n   Nombre ingresado: %s",jugador2[player2].nom_jug2);
  182.     printf("\n\n   Cedula ingresada: %s",jugador2[player2].ced_jug2);
  183.     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n presione ENTER para volver al menu");
  184.  
  185.     jugador2[player2+1]=jugador2[player2];
  186.     break;
  187.    }
  188.    case 3: /*OPCION 3 DEL MENU*/
  189.     {
  190.     if ((jugador1[player1].nom_jug1[0]=='\0') || (jugador2[player2].nom_jug2[0]=='\0'))
  191.     {
  192.     contador++;
  193.  
  194.     }
  195.     else  
  196.     {
  197.        cuatro_en_linea(tablero,opcion);
  198.        sumador++;
  199.        ganadores[ganador].jganador=cuatro_en_linea();
  200.                    ganador++;
  201.        if(jugador1[player1].nom_jug1[40]==jugador1[player1+1].nom_jug1[40]){
  202.                        jugador1[player1+2]=jugador1[player1+1];
  203.                        player1++;
  204.                    }
  205.                    if(jugador2[player2].nom_jug2[40]==jugador2[player2+1].nom_jug2[40]){
  206.                        jugador2[player2+2]=jugador2[player2+1];
  207.                        player2++;
  208.                    }
  209.        }
  210.        break;
  211.    }
  212.    case 4:
  213.     if (sumador<1)
  214.     {
  215.     aux++;
  216.     }
  217.     else
  218.     {
  219.            part=1;
  220.            aux2=0;
  221.            aux3 = 0;
  222.            for(aux2=0;aux2<sumador;aux2++){
  223.            printf("Partida %d\n",part);
  224.            part++;
  225.            printf("Jugador 1: %s\n",jugador1[aux2].nom_jug1);
  226.            printf("Cedula:    %s\n",jugador1[aux2].ced_jug1);
  227.            printf("Jugador 2: %s\n",jugador2[aux2].nom_jug2);
  228.            printf("Cedula:    %s\n",jugador2[aux2].ced_jug2);
  229.            if (ganadores[aux3].jganador == 1)
  230.            {
  231.                printf("Gana el jugador 1");
  232.            }
  233.            else if (ganadores[aux3].jganador == 2)
  234.            {
  235.                printf("Gana el jugador 2");
  236.            }
  237.            else if (ganadores[aux3].jganador == 0)
  238.            {
  239.            printf("EMPATE");
  240.            }
  241.     }
  242.     system("PAUSE");
  243.     }
  244.     break;
  245.    case 5: /*OPCION 5 DEL MENU*/
  246.     return printf("\n\n  SALIENDO\n\n");
  247.     break;
  248.    default:
  249.     correcta++;
  250.     break;
  251.    }while (getchar()!='\n');
  252.    }while (victoria==1);
  253. }
  254. int cuatro_en_linea(char tablero[DIM][DIM],int x)
  255. {
  256.  
  257.    /*Defino distintas variables que voy a utilizar a lo largo del programa*/
  258.    int n=0,i,j,resto=1,k=0,jugador,partida,victoria=0;
  259.    char d;
  260.    printf("\n\nIngresa la dimension del tablero de juego(4-15): ");
  261.    /*Analizamos si la dimension del tablero ingresada es valida*/
  262.    do
  263.    {
  264.        scanf (" %d",&x);
  265.        system("cls");
  266.        if (x<4 || x>15)
  267.        {
  268.            printf("\nOpcion no valida. Ingresa la dimension (4-15): ");
  269.        }   while (getchar()!='\n');
  270.    } while (x<4 || x>15);
  271.    system("cls");
  272.    /*Elegimos judagor*/
  273.    printf("\n ELIJA JUGADOR\n\n\n");
  274.    printf("\n\n  1) JUGADOR 1 (X)\n");
  275.    printf("\n\n  2) JUGADOR 2 (O)\n");
  276.    do
  277.    {
  278.    scanf("%d",&jugador);
  279.    if ((jugador!=1) && (jugador!=2))
  280.    {
  281.        printf("\n Error: Los jugadores posibles son 1 y 2 ");
  282.    }
  283.    }while ((getchar()!='\n') || ((jugador!=1) && (jugador!=2)));
  284.    do
  285.    {
  286.        system("cls");
  287.        printf("\n\n\n\n");
  288.        inicializar_tablero(tablero,x);
  289.        while ((resto<=(x*x)) && (victoria!=1))
  290.        {
  291.            if (jugador==1)
  292.            {
  293.                jugador++;
  294.               juegan_X_(tablero,x,n);
  295.               /* Evaluo si el jugador 1 (X) gana*/
  296.  
  297.               for (i=1;i<=x;i++)
  298.           {
  299.     for (j=1;j<=x;j++)
  300. {
  301.                        /*Verifico si hay cuatro en linea horizontal para Jugador 1*/
  302. if ((tablero[i][j]=='X') && ((tablero[i][j+1])=='X') && ((tablero[i][j+2])=='X') && ((tablero[i][j+3])=='X'))
  303. {
  304. victoria = 1;
  305. }        
  306. /*Verifico si hay cuatro en linea vertical para Jugador 1*/
  307. else if ((tablero[i][j]=='X') && ((tablero[i+1][j])=='X') && ((tablero[i+2][j])=='X') && ((tablero[i+3][j])=='X'))
  308. {
  309. victoria = 1;
  310. }
  311. /*Verifico si hay cuatro en linea en diagonal ascendiente para Jugador 1*/
  312. else if ((tablero[i][j]=='X') && ((tablero[i+1][j-1])=='X') && ((tablero[i+2][j-2])=='X') && ((tablero[i+3][j-3])=='X'))
  313. {
  314. victoria = 1;
  315. }
  316. /*Verifico si hay cuatro en linea en diagonal descendiente para Jugador 1*/
  317. else if ((tablero[i][j]=='X') && ((tablero[i+1][j+1])=='X') && ((tablero[i+2][j+2])=='X') && ((tablero[i+3][j+3])=='X'))
  318. {
  319. victoria = 1;
  320. }
  321. }
  322.   }
  323.               if (victoria == 1)
  324.               {
  325.               printf("\n\n FELICITACIONES %s",nom_jug1);
  326.               printf("!!!\n\n  Has ganado la partida.\n\n\n\n\n\n");
  327.               return 1;
  328.               }
  329.               else
  330.               {
  331.               resto++;
  332.               }
  333.            }
  334.            else if (jugador == 2)
  335.            {
  336.                jugador--;
  337. juegan_O_(tablero,x,n);
  338. /* Evaluo si el jugador 2 (O) gana*/
  339.  
  340. for (i=1;i<=x;i++)
  341.           {
  342.     for (j=1;j<=x;j++)
  343. {
  344.                        /*Verifico si hay cuatro en linea horizontal para Jugador 1*/
  345. if ((tablero[i][j]=='O') && ((tablero[i][j+1])=='O') && ((tablero[i][j+2])=='O') && ((tablero[i][j+3])=='O'))
  346. {
  347. victoria = 1;
  348. }
  349. /*Verifico si hay cuatro en linea vertical para Jugador 1*/
  350. else if ((tablero[i][j]=='O') && ((tablero[i+1][j])=='O') && ((tablero[i+2][j])=='O') && ((tablero[i+3][j])=='O'))
  351. {
  352. victoria = 1;
  353. }
  354. /*Verifico si hay cuatro en linea en diagonal ascendiente para Jugador 2*/
  355. else if ((tablero[i][j]=='O') && ((tablero[i+1][j-1])=='O') && ((tablero[i+2][j-2])=='O') && ((tablero[i+3][j-3])=='O'))
  356. {
  357. victoria = 1;
  358. }
  359. /*Verifico si hay cuatro en linea en diagonal descendiente para Jugador 2*/
  360. else if ((tablero[i][j]=='O') && ((tablero[i+1][j+1])=='O') && ((tablero[i+2][j+2])=='O') && ((tablero[i+3][j+3])=='O'))
  361. {
  362. victoria = 1;
  363. }
  364. }
  365.   }
  366.               if (victoria == 1)
  367.               {
  368.               printf("\n\n FELICITACIONES %s",nom_jug2);
  369.               printf("!!!\n\n  Has ganado la partida.\n\n\n\n\n\n");
  370.               return 2;
  371.               }
  372.               else
  373.               {  
  374.                resto++;
  375.               }
  376.            }
  377.  
  378.            /*Si el tablero se lleno de fichas y no hay cuatro en linea
  379.             el partido termina en empate*/
  380.  
  381.            if (resto > (x*x) && (victoria!=1))
  382.         {
  383.         printf("\n\n EMPATE\n\n");
  384.         return 0;
  385.            }
  386.        }
  387.    }while ((resto<=(x*x)) && (victoria!=1));
  388.    system("PAUSE");
  389.    resto = 1;
  390.    victoria = 0;
  391.    system("cls");
  392.    inicializar_tablero(tablero,x);
  393.    system("cls");
  394. }
  395. /* Procedimiento para imprimir el tablero inicial*/
  396.  
  397. void inicializar_tablero(char tablero[DIM][DIM],int x)
  398. {
  399. int i,j;
  400. int contador = 1;
  401. for (i=1; i<=x; i++)
  402. {
  403. for (j=1;j<=x; j++)
  404. {
  405.            printf("  ");
  406. printf(" %c",tablero[i][j]='.');
  407. }
  408. printf("\n\n");
  409.  
  410. }
  411. for (i=1;i<=x;i++)
  412. {
  413.        printf("   %d",contador);
  414.        contador++;
  415.    }
  416.    printf("\n");
  417. }
  418. /*Funcion para hacer jugada para el jugador 1*/
  419.  
  420. int juegan_X_(char tablero[DIM][DIM],int x,int n)
  421. {
  422. int i,j,k,contador=1;
  423. do
  424.    {
  425.        printf("\n\n Juega %s: ",nom_jug1);
  426.                    scanf ("%d",&n);
  427.        if (n<1 || n>x)
  428.        {
  429.            printf("Error: las columnas posibles son de 1 a %d",x);
  430.            while (getchar()!='\n');
  431.        }  
  432. else if (tablero[1][n]!='.')
  433. {
  434. printf("\nError: la columna ya esta llena.\n ");
  435.     }
  436.    }while((n<1 || n>x) || (tablero[1][n]!='.'));
  437. for (k=x;k>=1;k--)
  438.    {
  439.            if (tablero[k][n]!='.')
  440.            {
  441.                continue;
  442.            }
  443.            else
  444.            {
  445.                tablero[k][n]='X';
  446.                break;
  447.            }
  448.    }
  449.       system("cls");  
  450.   printf("\n Ultima jugada X: Columna %d\n",n);      
  451.    printf("\n\n");
  452.    for (i=1;i<=x;i++)
  453.    {
  454.        for (j=1;j<=x;j++)
  455.        {
  456.            printf("  ");
  457.            printf (" %c",tablero[i][j]);
  458.  
  459.        }
  460.        printf("\n\n");
  461.    }
  462.    for (i=1;i<=x;i++)
  463. {
  464.        printf("   %d",contador);
  465.        contador++;
  466.    }
  467.    printf("\n");
  468. }
  469. /*Funcion para hacer jugada para el jugador 2*/
  470.  
  471. int juegan_O_(char tablero[DIM][DIM],int x,int n)
  472. {
  473. int i,j,k,contador=1;
  474. do
  475.    {
  476.        printf("\n\n Juega %s: ",nom_jug2);
  477.                    scanf ("%d",&n);
  478.        if (n<1 || n>x)
  479.        {
  480.            printf("Error: las columnas posibles son de 1 a %d",x);
  481.            while (getchar()!='\n');
  482.        }  
  483. else if (tablero[1][n]!='.')
  484. {
  485. printf("\nError: la columna ya esta llena.\n ");
  486.     }
  487.    }while((n<1 || n>x) || (tablero[1][n]!='.'));
  488. for (k=x;k>=1;k--)
  489.    {
  490.            if (tablero[k][n]!='.')
  491.            {
  492.                continue;
  493.            }
  494.            else
  495.            {
  496.                tablero[k][n]='O';
  497.                break;
  498.            }
  499.    }
  500.    system("cls");    
  501. printf("\n Ultima jugada O: Columna %d\n",n);    
  502.    printf("\n\n");
  503.    for (i=1;i<=x;i++)
  504.    {
  505.        for (j=1;j<=x;j++)
  506.        {
  507.            printf("  ");
  508.            printf (" %c",tablero[i][j]);
  509.  
  510.        }
  511.        printf("\n\n");
  512.    }
  513.    for (i=1;i<=x;i++)
  514. {
  515.        printf("   %d",contador);
  516.        contador++;
  517.    }
  518.    printf("\n");
  519. }
  520. int limpiar(char nom_jug[40])
  521. {
  522.  
  523. int t;
  524. for (t=0; t<40; t++)
  525.    {
  526.     nom_jug[t]='\0';
  527.    }
  528. }
  529.  
  530.  
  531.  
  532.  
  533.  


Título: Re: Ayuda! Problema de un menu!!!
Publicado por: eferion en 4 Julio 2013, 21:08 pm
Vaya, veo que no has usado nada de lo que te puse... la próxima vez me ahorro el esfuerzo.

Código
  1.        cuatro_en_linea(tablero,opcion);
  2.        sumador++;
  3.        ganadores[ganador].jganador=cuatro_en_linea();

La función cuatro_en_linea sin argumentos no existe...

deberías eliminar la primera llamada y dejar la segunda, en la que estableces jganador, con los dos argumentos de la primera.