Pseudo-Object Orientation in C and Dynamic Allocation

Asked

Viewed 31 times

0

I’m trying to make a minefield in c using ncurses. h and when I’m going to do the vector of "bomb objects" Linux says it can’t find the memory space in which the values of the bombs are:

PS: no I can’t use c++

typedef struct Bomb
{
    int x, y;
} Bomb;

Bomb* setBomb(int i){

    Bomb* newBomb = malloc(sizeof(Bomb));

    srand(time(NULL) / i);
    newBomb->x = rand() % i * (i + 1);

    srand(time(NULL) * i);
    newBomb->y =  rand() % i * (i + 1);

    return newBomb;
}

Bomb** setBombs(WINDOW* win, int amountBombs)
{

    Bomb** bombs = malloc(sizeof(Bomb) * amountBombs);

    for(int i = 0; i < amountBombs; ++i)
    {
        bombs[i] = setBomb(i);
    }

    drawBomb(win, bombs, amountBombs);

    return bombs;
}

int drawBomb(WINDOW* win, Bomb** bomb, int amount)
{

    for (int i = 0; i < amount; i++)
    {
        mvwprintw(win, bomb[0]->y, bomb[0]->x, "O");
    }
}

1 answer

0

I fixed the bug: the problem was in srand(), I was making it generate a floating point number and that n is allowed

Browser other questions tagged

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