elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


  Mostrar Mensajes
Páginas: [1]
1  Programación / Programación C/C++ / bingo resuelto en: 19 Noviembre 2018, 12:37 pm
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define FILAS_CARTON 3
  5. #define NUMEROS_FILA 5
  6. #define MAX_BOLAS 90
  7.  
  8. void rellenar_carton(int carton [FILAS_CARTON][NUMEROS_FILA]);
  9. bool esta_repetido(int numero,int carton[FILAS_CARTON][NUMEROS_FILA]);
  10. int elegir_opcion();
  11. void imprimir_menu();
  12. void imprimir_carton(int carton[FILAS_CARTON][NUMEROS_FILA]);
  13. int generar_bola(int bolas_bingo[MAX_BOLAS]);
  14. bool esta_bola_reptida(int bola, int bolas_bingo[MAX_BOLAS]);
  15. void tachar_bola_carton(int carton[FILAS_CARTON][NUMEROS_FILA],int bola);
  16. bool comprobar_bingo(int carton[FILAS_CARTON][NUMEROS_FILA]);
  17. bool comprobar_linea(int carton[FILAS_CARTON][NUMEROS_FILA]);
  18. void guardar_carton(char numero_carton[1], int carton[FILAS_CARTON][NUMEROS_FILA]);
  19. void guardar_bolas(int bolas_bingo[MAX_BOLAS]);
  20. void cargar_partida(int carton_1[FILAS_CARTON][NUMEROS_FILA], int carton_2[FILAS_CARTON][NUMEROS_FILA],int bolas_bingo[MAX_BOLAS]);
  21.  
  22.  
  23. int main()
  24.  
  25. {
  26.  
  27.    int carton_1[FILAS_CARTON][NUMEROS_FILA];
  28.    int carton_2[FILAS_CARTON][NUMEROS_FILA];
  29.    int bolas_bingo[MAX_BOLAS];
  30.     char* nom1,nom2;
  31.     int opcion,bola;
  32.  
  33. srand(getpid());
  34.  
  35.  
  36. cargar_partida(carton_1,carton_2,bolas_bingo);
  37.  
  38. printf(" nombre jugador 1\t");
  39. scanf("%s",&nom1);
  40. printf("\n nombre jugador 2\t");
  41. scanf("%s",&nom2);
  42. opcion=1;
  43.  
  44.  
  45.  
  46. while(opcion!=0)  {
  47.        imprimir_menu();
  48.        opcion=elegir_opcion();
  49.  
  50.    switch (opcion)
  51.  
  52.    {
  53.        case 1:
  54.            rellenar_carton(carton_1);
  55.            guardar_carton("1",carton_1);
  56.            rellenar_carton(carton_2);
  57.            guardar_carton("2",carton_2);
  58.  
  59.            break;
  60.  
  61.        case 2:
  62.  
  63.            bola=generar_bola(bolas_bingo);
  64.            tachar_bola_carton(carton_1,bola);
  65.            tachar_bola_carton(carton_2,bola);
  66.            guardar_bolas(bolas_bingo);
  67.            guardar_carton("1",carton_1);
  68.            guardar_carton("2",carton_2);
  69.            comprobar_linea(carton_1);
  70.            comprobar_linea(carton_2);
  71.  
  72.            if(comprobar_bingo(carton_1)){
  73.                printf(" \t el jugador 1 ha ganado");
  74.  
  75.                if (comprobar_bingo(carton_2)){
  76.                    printf("\t el jugador 2 ha ganado");
  77.                }
  78.            } else
  79.                {printf("\t empate");
  80.                }
  81.  
  82.            break;
  83.  
  84.        case 3:
  85.            imprimir_carton(carton_1);
  86.            imprimir_carton(carton_2);
  87.  
  88.            break;
  89.  
  90.        case 4:
  91.  
  92.            break;
  93.  
  94.    }
  95.  
  96. }
  97.  
  98.    return 0;
  99. }
  100.  
  101.  
  102. void imprimir_menu(){
  103.    printf("\n--------JUEGO DEL BINGO-----");
  104.    printf("\n\t 1. generar cartones");
  105.    printf("\n\t 2. generar bola");
  106.    printf("\n\t 3. imrpimir cartones");
  107.    printf("\n\t 0. salir de la aplicacion");
  108.    printf("\n----------------------------");
  109.  
  110. }
  111.  
  112. int elegir_opcion(){
  113.    int opcion;
  114.    printf("\n elegir opcion");
  115.    scanf("%d",&opcion);
  116.  
  117.    return opcion;
  118. }
  119.  
  120. void rellenar_carton(int carton [FILAS_CARTON][NUMEROS_FILA]){
  121.    int i,j,num;
  122.  
  123.    for(i=0;i<FILAS_CARTON;i++){
  124.  
  125.        for(j=0;j<NUMEROS_FILA;j++){
  126.  
  127.             num=rand() % (90-1+1)+1;
  128.             if (!esta_repetido(num,carton)){
  129.                 carton[i][j]=num;
  130.             } else {
  131.                j--;
  132.            }
  133.  
  134.  
  135.        }
  136.  
  137.    }
  138.  
  139.  
  140. }
  141.  
  142. void imprimir_carton(int carton[FILAS_CARTON][NUMEROS_FILA]){
  143.  
  144.    int i,j;
  145.    for(i=0;i<FILAS_CARTON;i++){
  146.  
  147.        for(j=0;j<NUMEROS_FILA;j++){
  148.             if (carton[i][j]==-1){
  149.                printf(" X ");
  150.             }
  151.             else {
  152.                 printf(" %d",carton[i][j]);
  153.             }
  154.  
  155.        }
  156.  
  157.                printf("\n");
  158.    }
  159.  
  160.        printf("\n");
  161.    }
  162.  
  163.  
  164.  
  165. bool esta_repetido(int numero,int carton[FILAS_CARTON][NUMEROS_FILA]){
  166.     int i,j;
  167.  
  168.     for(i=0;i<FILAS_CARTON;i++){
  169.        for(j=0;j<NUMEROS_FILA;j++){
  170.            if(carton[i][j]==numero){
  171.                return true;
  172.            }
  173.  
  174.        }
  175.     }
  176.    return false;
  177. }
  178.  
  179. int generar_bola(int bolas_bingo[MAX_BOLAS]){
  180. int bola;
  181. bola=rand() % (90-1+1)+1;
  182.  while (esta_bola_reptida( bola,  bolas_bingo)){
  183.  
  184.         printf("\n desechamos %d",bola);
  185.         bola=rand() % (90-1+1)+1;
  186.    }
  187.    printf("\n cinfirmamos %d",bola);
  188.    bolas_bingo[bola-1]=-5;
  189. return bola;
  190. }
  191.  
  192. bool esta_bola_reptida(int bola, int bolas_bingo[MAX_BOLAS]){
  193.    int i;
  194.  
  195.        if (bolas_bingo[bola-1]==-5){
  196.            return true;
  197.        }
  198.  
  199.    return false;
  200.  
  201. }
  202.  
  203. void tachar_bola_carton(int carton[FILAS_CARTON][NUMEROS_FILA],int bola){
  204.    int i,j;
  205.    for(i=0;i<FILAS_CARTON;i++){
  206.        for(j=0;j<NUMEROS_FILA;j++){
  207.            if(carton[i][j]==bola){
  208.                carton[i][j]=-1;
  209.            }
  210.  
  211.  
  212.        }
  213.     }
  214.  
  215.  
  216.  
  217. }
  218. bool comprobar_bingo(int carton[FILAS_CARTON][NUMEROS_FILA]){
  219.  
  220.    int i,j;
  221.    for(i=0;i<FILAS_CARTON;i++){
  222.        for(j=0;j<NUMEROS_FILA;j++){
  223.            if(carton[i][j]!=-1){
  224.  
  225.                return false;
  226.            }
  227.  
  228.  
  229.        }
  230.     }
  231.     printf("\n hay bingo");
  232. return true;
  233.  
  234.  
  235.  
  236. }
  237. bool comprobar_linea(int carton[FILAS_CARTON][NUMEROS_FILA]){
  238. bool hay_linea=true;
  239. int i,j;
  240.    for(i=0;i<FILAS_CARTON;i++){
  241.  
  242.            hay_linea=true;
  243.  
  244.        for(j=0;j<NUMEROS_FILA;j++){
  245.            if(carton[i][j]!=-1){
  246.                hay_linea=false;
  247.  
  248.            }
  249.  
  250.  
  251.        }
  252.        if(hay_linea==true){
  253.            printf("\n hay linea");
  254.            return true;
  255.        }
  256.  
  257.     }
  258.     printf("\n no hay linea");
  259. return false;
  260.  
  261. }
  262.  
  263. void guardar_carton(char numero_carton[1], int carton[FILAS_CARTON][NUMEROS_FILA]){
  264.    int i,j,cont;
  265.  
  266.  
  267.    char nom_fichero[8]="carton";
  268.  
  269.    FILE *fp;
  270.  
  271.  
  272.  
  273.    strcat(nom_fichero,numero_carton);
  274.  
  275.    fp=fopen(nom_fichero,"w+");
  276.     for(i=0;i<FILAS_CARTON;i++){
  277.        for(j=0;j<NUMEROS_FILA;j++){
  278.           fprintf(fp,"%d\n",carton[i][j]);
  279.  
  280.  
  281.  
  282.        }
  283.     }
  284.  
  285.  
  286.    fclose(fp);
  287. }
  288. void guardar_bolas(int bolas_bingo[MAX_BOLAS]){
  289.    int i,j,cont;
  290.  
  291.  
  292.    char nom_fichero[8]="bolas";
  293.  
  294.    FILE *fp;
  295.  
  296.  
  297.  
  298.  
  299.  
  300.    fp=fopen(nom_fichero,"w+");
  301.     for(i=0;i<MAX_BOLAS;i++){
  302.        if (bolas_bingo[i]!=-5)
  303.            fprintf(fp,"%d ",i+1);
  304.        else
  305.           fprintf(fp,"%d\n",bolas_bingo[i]);
  306.  
  307.     }
  308.  
  309.  
  310.    fclose(fp);
  311. }
  312. void cargar_partida(int carton_1[FILAS_CARTON][NUMEROS_FILA], int carton_2[FILAS_CARTON][NUMEROS_FILA],int bolas_bingo[MAX_BOLAS] ){
  313. FILE *fp;
  314. char numero[6];
  315. int i=0,j=0;
  316.  
  317.  fp=fopen("carton1","r");
  318.  
  319.  if(fp==NULL){
  320.    printf("no hay datos, genere cartones\n\n");
  321.  }
  322.  else {
  323.    while (feof(fp)==0){
  324.  
  325.        fgets(numero,6,fp);
  326.        carton_1[i][j]=atoi(numero);
  327.        j++;
  328.        if(j==5){
  329.            j=0;
  330.            i++;
  331.        }
  332.    }
  333.  }
  334.  fclose(fp);
  335.  
  336. i=0;
  337. j=0;
  338.  
  339. fp=fopen("carton2","r");
  340.  
  341.  if(fp==NULL){
  342.    printf("no hay datos, genere cartones\n\n");
  343.  }
  344.  else {
  345.    while (feof(fp)==0){
  346.  
  347.        fgets(numero,6,fp);
  348.        carton_2[i][j]=atoi(numero);
  349.        j++;
  350.        if(j==5){
  351.            j=0;
  352.            i++;
  353.        }
  354.    }
  355.  }
  356.  fclose(fp);
  357.  
  358. }



Aqui os dejo el ejercicio de bingo resuelto hecho a mi manera que seguramente haya maneras mejores. pero esta es mi manera, espero que os sea util.

MOD: Etiquetas GeSHi. Texto a minusculas.
2  Programación / Programación C/C++ / Re: EJERCICIO BINGO EN C en: 6 Noviembre 2018, 12:33 pm
muchas gracias!!
3  Programación / Programación C/C++ / Re: EJERCICIO BINGO EN C en: 4 Noviembre 2018, 18:04 pm
ya, si su video le he visto pero en mi caso, cuando sale una bola, si el numero de la bola coincide con un numero del carton, en el carton tiene que imprimir una X en el lugar del numero.
4  Programación / Programación C/C++ / EJERCICIO BINGO EN C en: 3 Noviembre 2018, 12:26 pm
Hola a todxs, estoy haciendo un ejercicicio de programacion del bingo en c y el ejercicio es el siguiente:
juegan dos jugarores a la vez, y tienen un carton cada uno. Cada carton tiene 3 filas y 5 valores por fila. los numeros estan en el rango [1,90]. tambien se generara una bola de rango [1,90], nunca repetidos.

1-al iniciar partida se reparten los cartones generados aleatoriamente.

2-sacar bola-> el programa genreara aleatoriamente un numero [1,90] que no haya salido a lo largo de la partida. una vez generado el numero se comprobara si ha habido linea o bingo, si hubo un empate o nada en caso contrario.
la linea se cantara una vez. el bingo consiste en haber acertado todos los numeros del carton.

3- imrpimir cartones, el programa mostrara por pantalla los booletos de cada jugador formateados, indicando con una X los numeros ya elimandos. por ejemplo:
13 X 56 4 5
12 X X 4 57
15 X 72 X 7
-----------------------
EL PROGRAMA QUE TENGO ES EL SIGUIENTE, PERO ANTES OS DIGO QUE EL PROBLEMA LE TENGO A LA HORA DE SACAR LAS X YA QUE TENGO CASI TODO METIDO EN DIFERENTES FUNCIONES Y NO SE COMO SE HARIA.
-----------------------------------------------------------------


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define FILAS_CARTON 3
#define NUMEROS_FILA 5
#define MAX_BOLAS 90
int carton_1[FILAS_CARTON][NUMEROS_FILA];
int carton_2[FILAS_CARTON][NUMEROS_FILA];
int bolas_bingo[MAX_BOLAS];
int elegir_opcion();
void imprimir_menu();
void rellenar_carton();
void rellenar_carton2();
void imprimir_carton();
void imprimir_carton2();
bool esta_repetido(int numero);
void generar_bola();

int main()

{
 char* nom1,nom2;
 int opcion;





 printf("\n nombre jugador 1");
 scanf("%s",&nom1);
 printf("\n nombre jugador 2");
 scanf("%s",&nom2);
 opcion=1;

 while(opcion!=0)  {
        imprimir_menu();
        opcion=elegir_opcion();

    switch (opcion)

    {
        case 1:
            rellenar_carton();
            rellenar_carton2();

            break;

        case 2:
           generar_bola();
            break;

        case 3:

            imprimir_carton2();
            imprimir_carton();


            break;

        case 4:
            break;


    }

}






    return 0;
}


void imprimir_menu(){
    printf("\n--------JUEGO DEL BINGO-----");
    printf("\n\t 1. generar cartones");
    printf("\n\t 2. generar bola");
    printf("\n\t 3. imrpimir cartones");
    printf("\n\t 0. salir de la aplicacion");
    printf("\n----------------------------");




}
int elegir_opcion(){
    int opcion;
    printf("\n elegir opcion");
    scanf("%d",&opcion);

    return opcion;
}

void rellenar_carton(){
    int i,j,num;
    srand(getpid());
    for(i=0;i<FILAS_CARTON;i++){

        for(j=0;j<NUMEROS_FILA;j++){

             num=rand() % (90-1+1)+1;
             if (!esta_repetido(num)){
                 carton_1[j]=num;
             } else {
                j--;
            }


        }

    }


}

void imprimir_carton(){

    int i,j;
    for(i=0;i<FILAS_CARTON;i++){

        for(j=0;j<NUMEROS_FILA;j++){
             printf(" %d",carton_1[j]);
        }

                printf("\n");
        }

        printf("\n");
    }



bool esta_repetido(int numero){
     int i,j;

     for(i=0;i<FILAS_CARTON;i++){
        for(j=0;j<NUMEROS_FILA;j++){
            if(carton_1[j]==numero){
                return true;
            }


        }
     }
    return false;
}

void generar_bola(){
int bola;
    srand(getpid());

    bola=rand() % (90-1+1)+1;
    printf("%d",bola);
}
void rellenar_carton2(){
    int i,j,num;
    srand(getpid());
    for(i=0;i<FILAS_CARTON;i++){

        for(j=0;j<NUMEROS_FILA;j++){

             num=rand() % (90-1+1)+1;
             if (!esta_repetido(num)){
                 carton_2[j]=num;
             } else {
                j--;
            }


        }

        }

    }



void imprimir_carton2(){
    int i,j;
     for(i=0;i<FILAS_CARTON;i++){

        for(j=0;j<NUMEROS_FILA;j++){
             printf(" %d",carton_2[j]);
        }

        printf("\n");
    }


}
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines