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

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  bingo resuelto
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: bingo resuelto  (Leído 1,311 veces)
inmajimenez

Desconectado Desconectado

Mensajes: 4


Ver Perfil
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.


« Última modificación: 19 Noviembre 2018, 17:43 pm por MCKSys Argentina » En línea

MAFUS


Desconectado Desconectado

Mensajes: 1.603



Ver Perfil
Re: bingo resuelto
« Respuesta #1 en: 19 Noviembre 2018, 17:00 pm »

Siempre que subas un código mételo entre las etiquetas generadas por el desplegable 'Código GeSHi' que tienes en las herramientas de edición, encima del recuadro donde escribes.

Si el código es un snippet basta que uses el botón #.


En línea

Serapis
Colaborador
***
Desconectado Desconectado

Mensajes: 3.351


Ver Perfil
Re: bingo resuelto
« Respuesta #2 en: 19 Noviembre 2018, 22:55 pm »

Para cumplir la tarea, vale... pero lamentablemente rellenar un cartón es un poco más complicado.

Verás, para un bingo, cuando emite cartones, emite como mínimo 6 cartones de 15 bolas (para el bingo de 90 bolas). Eso queire decir que las 90 bolas deben repartirse entre los 6 cartones, si no se cumple tal condición, los cartones estarían 'trucados'.
Además para cada cartón debe haber bolas de todas las decenas (9 bolas), y las otras 6 bolas son de 6 decenas distintas. Es decir ningún cartón puede tener más de dos bolas de una misma decena, y debe tener bolas de cada decena, esto garantiza lo dicho anteriormente, pués es otra forma de decir lo mismo.

Por tanto para hacer el reparto, justo, primero debe crearse un array de 90 bolas (1 al 90, ó 0 al 89)y ser barajado, por decenas (1-10, 11-19, etc..), es entonces cuando puedes hacer el reparto... de las 90 bolas entre los 6 cartones, eliges al azar para cada decena una bola de las que queden en esa decena, con cada elección bajas las bolas de encima (de esa misma decena) y restas una bola a dicha decena. Así ya has elegido las 9 primeras, las otras 6 lo harás eleigiendo al azar la decena (estas 6 no pueden repetirse entre sí), antes de elegir una bola en esa decena entre las que resten. Luego se complica porque para algunas decenas ya se han elegido suficientes, y el reparto debe ser equitativo, etc ...al repartir para 5 cartones, las que quedan son el 6º cartón...
En fin, que es algo más complicado que simplemente elegir al azar 15 bolas, si se pretende que no haya parcialidad.
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Programar un bingo
Programación Visual Basic
a_rodriguez24_07 2 4,485 Último mensaje 2 Octubre 2008, 00:41 am
por a_rodriguez24_07
Juego del bingo
Ejercicios
hansviron 0 4,560 Último mensaje 26 Junio 2010, 18:14 pm
por hansviron
Bingo WEB
.NET (C#, VB.NET, ASP)
WRGuide 3 3,795 Último mensaje 15 Junio 2016, 00:05 am
por Eleкtro
Cartón de Bingo en C.
Programación C/C++
zkraven 9 9,026 Último mensaje 12 Febrero 2018, 04:13 am
por Serapis
Juego de Bingo
Programación C/C++
zkraven 0 2,719 Último mensaje 28 Abril 2018, 19:23 pm
por zkraven
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines