-1
// Bingo Game

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define SPN 7
#define BTT 99 
#define MARKER 'X'


//Player functions
void create_card_game(int matriz[SPN][SPN]);
int create_random_number(int released_numbers[SPN*SPN], int released_numbers_count);
bool iguality_hunter_matriz(int released_numbers[SPN*SPN], int released_numbers_count, int random_number);

// Roulette functions
void create_roulette(int lista[BTT]);
int create_roulette_numbers(int roulette_released_numbers[BTT], int roulette_released_numbers_count);
bool iguality_hunter_list(int roulette_released_numbers[BTT], int roulette_released_numbers_count, int roulette_numbers);

//print functions
void print_card(int matriz[SPN][SPN], int list[BTT], int i);

//checks win
bool win_coll(int matriz[SPN][SPN]);
bool win_arow(int matriz[SPN][SPN]);


// Matriz functions, Card Game side 
int create_random_number(int released_numbers[SPN*SPN], int released_numbers_count)
{
    int random_number;
    do
    {
        random_number=rand()%100;
    }
    while(iguality_hunter_matriz(released_numbers, released_numbers_count, random_number));
    
    return random_number;
}



void create_card_game(int matriz[SPN][SPN])
{
    int released_numbers[SPN*SPN];
    int released_numbers_count = 0;
    int random_number;
    for(int i=0; i<SPN; i++)
        for(int j=0; j<SPN; j++)
        {
            random_number = create_random_number(released_numbers, released_numbers_count);
            matriz[i][j] = random_number;
            released_numbers[released_numbers_count] = random_number;
            released_numbers_count++;
        }
}



bool iguality_hunter_matriz(int released_numbers[SPN*SPN], int released_numbers_count, int random_number)
{
    for(int i=0; i<released_numbers_count; i++)
    {
        if (released_numbers[i] == random_number)
            return true;
    }
    return false;
}



// Lists functions, roulette side 
int create_roulette_numbers(int roulette_released_numbers[BTT], int roulette_released_numbers_count)
{
    int roulette_numbers;
    do
    {
        roulette_numbers=rand()%100;
    }
    while(iguality_hunter_list(roulette_released_numbers, roulette_released_numbers_count, roulette_numbers));
    
    return roulette_numbers;
}



bool iguality_hunter_list(int roulette_released_numbers[BTT], int roulette_released_numbers_count, int roulette_numbers)
{
    for(int i=0; i<roulette_released_numbers_count; i++)
    {
        if (roulette_released_numbers[i] == roulette_numbers)
            return true;
    }
    return false;
}



void create_roulette(int lista[BTT])
{
    int roulette_released_numbers[BTT];
    int roulette_released_numbers_count = 0;
    int roulette_numbers;
    for(int i=0; i<BTT; i++)
    {
        roulette_numbers = create_roulette_numbers(roulette_released_numbers, roulette_released_numbers_count);
        lista[i] = roulette_numbers;
        roulette_released_numbers[roulette_released_numbers_count] = roulette_numbers;
        roulette_released_numbers_count++;
    }
}


// print both
void print_card(int matriz[SPN][SPN], int list[BTT], int i)
{
    for(int i=0; i<BTT; i++)
    {
        printf("\n");
        for(int j=0; j<SPN; j++)
        {
            for(int n=0; n<SPN; n++)
            {
                if(matriz[j][n] == 88 || matriz[j][n] == list[i])
                {
                    matriz[j][n]=MARKER;
                    printf("|");
                    printf("%2c", matriz[j][n]);
                }
                else
                {
                    printf("|");
                    printf("%2d", matriz[j][n]);
                }
            }
            printf("|\n");
        }
        printf("\n");
        printf(" e o numero escolhido e: %2d", list[i]);
        printf("\n");
        getchar();
    }
}


// verify victory
bool win_coll(int matriz[SPN][SPN])
{
    int i=0;
    for(int j=0; j<SPN; j++)
    {
        if(matriz[i][j]==88 && matriz[i+1][j]==88 && matriz[i+2][j]==88 && matriz[i+3][j]==88 && matriz[i+4][j]==88 && matriz[i+5][j]==88 && matriz[i+6][j]==88) 
        {    
            return true;
        }
    }
    return false;
}



bool win_arow(int matriz[SPN][SPN])
{
    int j=0;
    for(int i=0; i<SPN; i++)
    {
        if(matriz[i][j]==88 && matriz[i][j+1]==88 && matriz[i][j+2]==88 && matriz[i][j+3]==88 && matriz[i][j+4]==88 && matriz[i][j+5]==88 && matriz[i][j+6]==88)
        {
            return true;
        }
    }
    return false;
}


int main()
{
    int card_game_player[SPN][SPN];
    int roulette[BTT];
    
    srand(time(NULL));
    
// Create Player 1 and Player 2 Matriz
    create_card_game(card_game_player);   

// Create Roulette List 
    create_roulette(roulette);

// Print Player 1 and Player 2 Card Game     
    for(int n=0; n<BTT; n++)
    {
        print_card(card_game_player, roulette, n);
        if(win_arow(card_game_player) || win_coll(card_game_player))
        {
            printf("vitoria");
            break;
        }
    }
    return 0;
}

In order to create a bingo game with matrix 7*7 I wrote this code. I will try to make a summary of the functions presented:

  1. Creates a random number that, thanks to the iguality_hunter_matriz function, will be different from anyone who has already left.

  2. Creates the matrix where this number will be saved and allows the iguality_hunter_matriz to succeed already. That prevents it from reading spaces that have not yet been filled by a random number.

  3. Checks whether the same numbers in the matrix.

  4. The same as 1, but now for a list.

  5. The same as 2, but now for a list.

  6. The same as 3, but now for a list.

  7. Aims not only to print the numbers of the matrix and list but also to verify equalities between hue and list and when affirmative the number in question is exchanged for the number 88 and printed as char 'X', this only in the matrix.

  8. Checks successive equalities horizontally, verify that the numbers in question are all 88, and when true, stops the program and prints "victoria".

  9. Same as 8, but vertically.

The problem is that when one of the functions win_cols or win_arows is true the program continues. I have used win_cols and win_arows in other projects and they worked so I have no clues where the problem might be.

An example of the expected output:

|55|22|40|11|38|56|73|
|89|17|27|13|72|68|43|
|94|82|70|48|24| 2|47|
|34|79| 5|44|30|81|98|
| X| X| X| X| X| X| X|
|87| 6|95| 0|63|58|37|
|92|59|14| 3|93|23|76|

 e o numero escolhido e: 97
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Note: The **random bits of bold** do not really help with understanding. – tadman Nov 09 '22 at 20:08
  • Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman Nov 09 '22 at 20:09
  • 2
    This is also way too much code to review. If you're this deep in and nothing works that's probably a sign of over-reach. I'd suggest writing simple tests for each function to verify they work correctly under the intended use cases, then move forward with integrating them into the whole. Right now you have a ton of untested code and ironing out all the bugs will not be easy if you try and test it all at once. – tadman Nov 09 '22 at 20:12
  • 2
    See [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), by Eric Lippert – John Kugelman Nov 09 '22 at 20:20
  • Aside: `create_random_number` and `create_roulette_numbers` are the same function. Both of their first arguments will decay to an `int *`. The same goes for `iguality_hunter_matriz` and `iguality_hunter_list`. – Oka Nov 09 '22 at 20:49
  • Have you tried running your code line-by-line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) Please note that [it is generally expected that you make a debugging attempt yourself before asking for help on Stack Overflow](https://idownvotedbecau.se/nodebugging/). Questions which do not demonstrate any debugging attempt are usually downvoted. – Andreas Wenzel Nov 09 '22 at 20:58

1 Answers1

0

You have one too many nested loops in print_card, since the function is already called in a loop.

for(int n=0; n<BTT; n++)
{
    print_card(card_game_player, roulette, n);
    /* ... */

The first time print_card is called, it exhausts the length of list

void print_card(int matriz[SPN][SPN], int list[BTT], int i)
{
    for(int i=0; i<BTT; i++)
    {
    /* ... */

which will fully iterate and fill matriz with 'X' characters before win_arow or win_coll are ever called.

The loop has a declaration of int i which shadows the function argument of the same name. The function argument should be used in this comparison:

matriz[j][n] == list[i]

Commenting out the loop causes the program to perform as expected.

void print_card(int matriz[SPN][SPN], int list[BTT], int i)
{
    /* for(int i=0; i<BTT; i++) */
    {
    /* ... */

Suggestion: separate the logic of modifying the array and printing the array into distinct functions.

Oka
  • 23,367
  • 6
  • 42
  • 53