Bingo program does not identify who has won

Asked

Viewed 120 times

1

Trying to make a bingo with matrix in C, however the verification of the drawn values does not identify correctly with the matrix.

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


int main(){

    srand(time(NULL));

    int sorteio = 0;
    int p1 = 0;
    int p2 = 0; 
    int c1[4][4], c2[4][4],i, j;

    for(i = 0;i < 4; i++ ){
        for(j = 0; j < 4; j++){
            c1[i][j] = rand()%50;
            c2[i][j] = rand()%50;
        }
    }
    for(i = 0;i < 4; i++ ){
        for(j = 0; j < 4; j++){
            printf("%i ",c1[i][j]);
            }
        printf("\n");
    }
    printf("\n");
    for(i = 0;i < 4; i++ ){
        for(j = 0; j < 4; j++){
            printf("%i ",c2[i][j]);
            }
        printf("\n");
    }

    sorteio = rand() %50;
    printf ("%i ", sorteio);    

    while (p1 <= 16 || p2 <= 16){

        for(i = 0;i < 4; i++ ){
            for(j = 0; j < 4; j++){
                if (c1[i][j] == sorteio){
                    p1++;}  
                if (c2[i][j] == sorteio){
                    p2++;}
                }
            }

    sorteio = rand() %50;
    printf ("%i ", sorteio);


    if (p1 == 16){
        printf("\nBingo! Jogador 1 ganhou\n");
        return 0;}
        else{   
            if (p2 == 16){
                printf("\nBingo! Jogador 2 ganhou\n");
                return 0;}
                else{
                    printf("empate");
                    return 0;}
        }
    }
}

2 answers

1


There are several mistakes there, I’ll leave the little ones, we go to the main one that occurs more than once:

  • To draw numbers without repetition correctly and efficiently you must use the algorithm Fisher-Yates
  • For the same reason the draw can potentially catch numbers eternally and not draw all, and draw something repeated
  • In fact there is still another conceptual error, because the cartels are not assembled randomly, there is a criterion, but I will consider this an "artistic license".

That’s how it works (I hope that’s what you need):

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

void shuffle(int *array, int size) {
    for (int i = size - 1; i > 0; i--) {
        int j = rand() % (i + 1);
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }
}

int main() {
    srand(time(NULL));
    int p1 = 0;
    int p2 = 0; 
    int c1[4][4], c2[4][4];
    int numbers[50];
    for (int i = 0; i < 50; i++) numbers[i] = i + 1;
    shuffle(numbers, 50);
    for (int i = 0; i < 4; i++) {
         for (int j = 0; j < 4; j++) {
             c1[i][j] = numbers[i * 4 + j];
             printf("%i\t", c1[i][j]);
         }
         printf("\n");
    }
    printf("\n");
    shuffle(numbers, 50);
    for (int i = 0; i < 4; i++) {
         for (int j = 0; j < 4; j++) {
             c2[i][j] = numbers[i * 4 + j];
             printf("%i\t", c2[i][j]);
         }
         printf("\n");
    }
    printf("\n");
    shuffle(numbers, 50);
    for (int k = 0; k < 50 && (p1 <= 16 || p2 <= 16); k++) {
        int sorteio = numbers[k];
        printf ("%i ", sorteio);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (c1[i][j] == sorteio) p1++;
                if (c2[i][j] == sorteio) p2++;
            }
        }
    }
    if (p1 == 16) printf("\nBingo! Jogador 1 ganhou\n");
    else if (p2 == 16) printf("\nBingo! Jogador 2 ganhou\n");
    else printf("\nempate");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Note that when one of the two players reaches 16 it should no longer enter the loop.

-1

There are indentation and key closing errors in wrong place.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(){
    srand(time(NULL));
    int sorteio = 0;
    int p1 = 0;
    int p2 = 0; 
    int c1[4][4], c2[4][4],i, j;
    for(i = 0;i < 4; i++ ){
         for(j = 0; j < 4; j++){
             c1[i][j] = rand()%50;
             c2[i][j] = rand()%50;
         }
    }
    for(i = 0;i < 4; i++ ){
         for(j = 0; j < 4; j++){
             printf("%i ",c1[i][j]);
         }
         printf("\n");
    }
    printf("\n");
    for(i = 0;i < 4; i++ ){
         for(j = 0; j < 4; j++){
             printf("%i ",c2[i][j]);
         }
         printf("\n");
    }
    sorteio = rand() %50;
    printf ("%i ", sorteio);    
    while (p1 < 16 || p2 < 16){
        for(i = 0; i < 4; i++ ){
            for(j = 0; j < 4; j++){
            if (c1[i][j] == sorteio){
                p1++;
            }  
            if (c2[i][j] == sorteio){
                p2++;
            }
            }
        }
        sorteio = rand() %50;
        printf ("%i ", sorteio);
    }
    if (p1 == 16){
         printf("\nBingo! Jogador 1 ganhou\n");
         return 0;
    }
    else{   
          if (p2 == 16){
             printf("\nBingo! Jogador 2 ganhou\n");
             return 0;
            }
          else{
              printf("empate");
              return 0;
            }
        }
    }
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.