Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Temas - inmajimenez

#1
Programación C/C++ / bingo resuelto
19 Noviembre 2018, 12:37 PM
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define FILAS_CARTON 3
#define NUMEROS_FILA 5
#define MAX_BOLAS 90

void rellenar_carton(int carton [FILAS_CARTON][NUMEROS_FILA]);
bool esta_repetido(int numero,int carton[FILAS_CARTON][NUMEROS_FILA]);
int elegir_opcion();
void imprimir_menu();
void imprimir_carton(int carton[FILAS_CARTON][NUMEROS_FILA]);
int generar_bola(int bolas_bingo[MAX_BOLAS]);
bool esta_bola_reptida(int bola, int bolas_bingo[MAX_BOLAS]);
void tachar_bola_carton(int carton[FILAS_CARTON][NUMEROS_FILA],int bola);
bool comprobar_bingo(int carton[FILAS_CARTON][NUMEROS_FILA]);
bool comprobar_linea(int carton[FILAS_CARTON][NUMEROS_FILA]);
void guardar_carton(char numero_carton[1], int carton[FILAS_CARTON][NUMEROS_FILA]);
void guardar_bolas(int bolas_bingo[MAX_BOLAS]);
void cargar_partida(int carton_1[FILAS_CARTON][NUMEROS_FILA], int carton_2[FILAS_CARTON][NUMEROS_FILA],int bolas_bingo[MAX_BOLAS]);


int main()

{

    int carton_1[FILAS_CARTON][NUMEROS_FILA];
    int carton_2[FILAS_CARTON][NUMEROS_FILA];
    int bolas_bingo[MAX_BOLAS];
     char* nom1,nom2;
     int opcion,bola;

srand(getpid());


cargar_partida(carton_1,carton_2,bolas_bingo);

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



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

    switch (opcion)

    {
        case 1:
            rellenar_carton(carton_1);
            guardar_carton("1",carton_1);
            rellenar_carton(carton_2);
            guardar_carton("2",carton_2);

            break;

        case 2:

            bola=generar_bola(bolas_bingo);
            tachar_bola_carton(carton_1,bola);
            tachar_bola_carton(carton_2,bola);
            guardar_bolas(bolas_bingo);
            guardar_carton("1",carton_1);
            guardar_carton("2",carton_2);
            comprobar_linea(carton_1);
            comprobar_linea(carton_2);

            if(comprobar_bingo(carton_1)){
                printf(" \t el jugador 1 ha ganado");

                if (comprobar_bingo(carton_2)){
                    printf("\t el jugador 2 ha ganado");
                }
            } else
                {printf("\t empate");
                }

            break;

        case 3:
            imprimir_carton(carton_1);
            imprimir_carton(carton_2);

            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 carton [FILAS_CARTON][NUMEROS_FILA]){
    int i,j,num;

    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)){
                 carton[i][j]=num;
             } else {
                j--;
            }


        }

    }


}

void imprimir_carton(int carton[FILAS_CARTON][NUMEROS_FILA]){

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

        for(j=0;j<NUMEROS_FILA;j++){
             if (carton[i][j]==-1){
                printf(" X ");
             }
             else {
                 printf(" %d",carton[i][j]);
             }

        }

                printf("\n");
    }

        printf("\n");
    }



bool esta_repetido(int numero,int carton[FILAS_CARTON][NUMEROS_FILA]){
     int i,j;

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

        }
     }
    return false;
}

int generar_bola(int bolas_bingo[MAX_BOLAS]){
int bola;
bola=rand() % (90-1+1)+1;
  while (esta_bola_reptida( bola,  bolas_bingo)){

         printf("\n desechamos %d",bola);
         bola=rand() % (90-1+1)+1;
    }
    printf("\n cinfirmamos %d",bola);
    bolas_bingo[bola-1]=-5;
return bola;
}

bool esta_bola_reptida(int bola, int bolas_bingo[MAX_BOLAS]){
    int i;

        if (bolas_bingo[bola-1]==-5){
            return true;
        }

    return false;

}

void tachar_bola_carton(int carton[FILAS_CARTON][NUMEROS_FILA],int bola){
    int i,j;
    for(i=0;i<FILAS_CARTON;i++){
        for(j=0;j<NUMEROS_FILA;j++){
            if(carton[i][j]==bola){
                carton[i][j]=-1;
            }


        }
     }



}
bool comprobar_bingo(int carton[FILAS_CARTON][NUMEROS_FILA]){

    int i,j;
    for(i=0;i<FILAS_CARTON;i++){
        for(j=0;j<NUMEROS_FILA;j++){
            if(carton[i][j]!=-1){

                return false;
            }


        }
     }
     printf("\n hay bingo");
return true;



}
bool comprobar_linea(int carton[FILAS_CARTON][NUMEROS_FILA]){
bool hay_linea=true;
int i,j;
    for(i=0;i<FILAS_CARTON;i++){

            hay_linea=true;

        for(j=0;j<NUMEROS_FILA;j++){
            if(carton[i][j]!=-1){
                hay_linea=false;

            }


        }
        if(hay_linea==true){
            printf("\n hay linea");
            return true;
        }

     }
     printf("\n no hay linea");
return false;

}

void guardar_carton(char numero_carton[1], int carton[FILAS_CARTON][NUMEROS_FILA]){
    int i,j,cont;


    char nom_fichero[8]="carton";

    FILE *fp;



    strcat(nom_fichero,numero_carton);

    fp=fopen(nom_fichero,"w+");
     for(i=0;i<FILAS_CARTON;i++){
        for(j=0;j<NUMEROS_FILA;j++){
           fprintf(fp,"%d\n",carton[i][j]);



        }
     }


    fclose(fp);
}
void guardar_bolas(int bolas_bingo[MAX_BOLAS]){
    int i,j,cont;


    char nom_fichero[8]="bolas";

    FILE *fp;





    fp=fopen(nom_fichero,"w+");
     for(i=0;i<MAX_BOLAS;i++){
        if (bolas_bingo[i]!=-5)
            fprintf(fp,"%d ",i+1);
        else
           fprintf(fp,"%d\n",bolas_bingo[i]);

     }


    fclose(fp);
}
void cargar_partida(int carton_1[FILAS_CARTON][NUMEROS_FILA], int carton_2[FILAS_CARTON][NUMEROS_FILA],int bolas_bingo[MAX_BOLAS] ){
FILE *fp;
char numero[6];
int i=0,j=0;

  fp=fopen("carton1","r");

  if(fp==NULL){
    printf("no hay datos, genere cartones\n\n");
  }
  else {
    while (feof(fp)==0){

        fgets(numero,6,fp);
        carton_1[i][j]=atoi(numero);
        j++;
        if(j==5){
            j=0;
            i++;
        }
    }
  }
  fclose(fp);

i=0;
j=0;

fp=fopen("carton2","r");

  if(fp==NULL){
    printf("no hay datos, genere cartones\n\n");
  }
  else {
    while (feof(fp)==0){

        fgets(numero,6,fp);
        carton_2[i][j]=atoi(numero);
        j++;
        if(j==5){
            j=0;
            i++;
        }
    }
  }
  fclose(fp);

}




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 C/C++ / EJERCICIO BINGO EN C
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");
    }


}