the result obtained does not correspond to the actual result

Asked

Viewed 102 times

2

I’m struggling and I don’t even know how to explain, excuse me for the title of doubt, here is the code defined...

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

#define MAX_JOGADOR       7//numero maximo de jogadores
#define MAX_BOLA_JOGADOR  2//numero maximo de bolas para cada jogador
#define MAX_BOLA          5//numero maximo de bolas para jogo

typedef struct BOLA BOLA;
typedef struct JOGADOR JOGADOR;
typedef struct CAMPO CAMPO;

struct BOLA {//cada bola
    int num_bola; //contem um numero de bola
};

struct JOGADOR {//cada jogador
    int num_jogador; //contem um numero de jogador
    struct BOLA bola[MAX_BOLA_JOGADOR]; //entre uma a duas bolas
    int qt_bola_jogador; //quantidade bola
};

struct CAMPO {
    struct JOGADOR jogadores[MAX_JOGADOR]; //contem no maximo 7 jogadores 
    struct BOLA bolas[MAX_BOLA]; //possui no maximo 5 bolas para cada jogo
};

struct JOGADOR novo_jogador; /*novo jogador*/
struct BOLA nova_bola; /*nova bola*/
struct BOLA *ptr_bola; /*apontador para  nova bola*/
struct JOGADOR *ptr_jogador; /*apontador para  novo jogador*/
struct CAMPO *ptr_campo; /*apontador para campo*/

void jogador_recebe_bola(void);
void criar_bola(void);
void criar_jogador(void);

in this function creates the players and adds to the field_pointer, and this function works perfectly

void criar_jogador(void) {

    int i, j; //identificador para incrementar criação de jogador

    //alocaçao de memoria para simular jogo
    ptr_campo = (CAMPO *) malloc(sizeof (ptr_campo));

    printf("\n--- JOGADOR ---\n");

    /* criar jogador*/
    for (i = 0, j = 0; i < MAX_JOGADOR; i++) {
        novo_jogador.num_jogador++; //incrementa o numero de jogador
        novo_jogador.qt_bola_jogador = 0;
        ptr_jogador = &novo_jogador;
        ptr_campo->jogadores[i] = *ptr_jogador; //adicionar o jogador no campo
        printf(">> Criando jogador %d...\n\n", ptr_campo->jogadores[i].num_jogador);
        j++;
    }

    if (j == MAX_JOGADOR) {
        //criar bola
        criar_bola();
    }
}

In this function, the ball is created and added to the field_pointer, and this function also works correctly

void criar_bola(void) {

    int k, m; //identificador para incrementar criação de bola

    printf("\n--- BOLA ---\n");

    /*cria bola*/
    for (k = 0, m = 0; k < MAX_BOLA; k++) {
        nova_bola.num_bola++; //incrementa o numero da bola
        ptr_bola = &nova_bola;
        ptr_campo->bolas[k] = *ptr_bola; //atribuir a baliza para cada jogador
        printf(">> Criar bola %d...\n\n", ptr_campo->bolas[k].num_bola);
        m++;
    }

    if (m == MAX_BOLA) {
        //criar bola
        jogador_recebe_bola();
    }
}

My question is, in this function player receives ball, the function works when the number of balls is 1 and/or 2 balls, and if in the case of 3 balls, as shown in the picture below, the code, instead of saying that the player has ball nº3, says that has ball nº4 and when selected does nothing...

void jogador_recebe_bola(void) {

    int recebe_bola = 0;
    int j, m;
    srand(time(NULL));

    printf("\n--- JOGADOR RECEBE BOLA---\n");

    //faz um loop
    for (m = 0; m < MAX_BOLA; m++) {
        //captura a bola
        if (m + 1 == ptr_campo->bolas[m].num_bola) {
            //escolhe um jogador aleatorio
            recebe_bola = 1 + rand() % MAX_JOGADOR;
            ////faz um loop
            for (j = 0; j < MAX_JOGADOR; j++) {
                //captura o jogador a receber bola
                if ((j + 1 == ptr_campo->jogadores[j].num_jogador)
                        //jogador escolhido igual ao que está no campo
                        && (recebe_bola == ptr_campo->jogadores[j].num_jogador)) {
                    //escolhe o jogador x
                    printf(">> Jogador %d foi escolhido para receber bola %d...\n",
                            ptr_campo->jogadores[j].num_jogador, m + 1);
                    //recebe a bola do campo
                    ptr_campo->jogadores[j].bola[m] = ptr_campo->bolas[m];
                    //incrementa a quantidade de bola
                    ptr_campo->jogadores[j].qt_bola_jogador++;
                    //a bola nº y foi para jogador x
                    printf("\t>> Bola %d foi para jogador %d...\n",
                            ptr_campo->bolas[m].num_bola,
                            ptr_campo->jogadores[j].num_jogador);
                    //o jogador x tem bola nºy
                    printf(">> Jogador %d tem a bola %d...\n\n",
                            ptr_campo->jogadores[j].num_jogador,
                            ptr_campo->jogadores[j].bola[m].num_bola);
                }
            }
        }
    }
}

Revise the code functioning on the ideone

inserir a descrição da imagem aqui

  • Hello, Renata. Your code does not compile (BALL does not have type attribute). If you can put a compileable sample and run somewhere ( Ideone.com, for example), it would be better.

  • @Pablo, only now that I could see your notification and act you’re right, I put the coidgo in a hurry and could not even edit, but I’ve managed to modify what is more and added what was missing and I already did as you indicated, here’s the Code link worked on ideone.com

  • @Pablo, I still can’t figure out how to solve this doubt, what is missing or has wrong?

  • I’ve come home now. I’ll look.

  • It’s out there, @Renata?

  • @Pablo, only now that I could answer, yesterday I fell asleep, by the time you answered the message, I was already asleep because our time zone is different, I hope you don’t mind?

  • All right. See the answer.

Show 2 more comments

1 answer

1

I couldn’t find a complete solution, but I found the cause of the problem. Since I don’t understand its application, I don’t know what is intentional and what is not, but what happens is that, in the third iteration, the variable in ptr_campo->jogadores[j].qt_bola_jogador and the variable ptr_campo->jogadores[j].bola[m].num_bola have the same memory address, then increment one increments the other. This only happens in the third iteration, for some reason.

So it’s like an exercise to figure out what’s causing it, but since it can only be somewhere that has address assignment, it’s not hard for those who understand the code, I guess.

  • actually you’re right, I decided to put the variable //ptr_campo->jogadores[j].qt_bola_jogador in comment and showed that the player has ball 3 and even so, when it is the player with ball 3 is selected says it has 3 balls and gives error because the player can only have up to two balls and in this case, I only need the number of the ball and not the amount of ball, the ideal is to use a pointer as I did only that after the third iteration, the number of the ball is assigned the amount of ball, but I will try to see if I can identify, thank you and at least I have tried to identify the error

  • My application is to simulate a multiball game, a football type game but is familiar in that each player will have only goal and then are added the balls on the field and each ball will be assigned to a randomly selected player and everything works up to simulation with two balls, now when it is with 3 or more balls, starts doing what it shouldn’t, but if it’s doing it’s because the application is getting instruction in some part of the code

  • I was testing and it only happens in the third iteration and the fourth iteration of the ball works and when the player with 4 ball is called it works, only the player with 3 ball that says it has 4 balls and this 4 balls, I think it will be the sum " ptr_campo->jogadores[j].bola[m].num_bola + ptr_campo->jogadores[j].qt_bola_jogador++; "

Browser other questions tagged

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