خرید بک لینک

Vote count: 0

thank you to pay attention to my problem.

So I want to simulate cars moving on a road, I have a struct called road and another one called cars :

        struct road
    {
        voiture* tab[MAX_CARS]; //MAX_CARS = 79
        unsigned short int nbCars;
    };

    struct car
    {
        int speed;
       char name;
        bool moved;
    };

I'm using a never-ending (theoretically) loop to make them move on my road, like a game. I change the attributes of the cars based on a model named NASCH. Basically, this model have 3 steps :

  1. Increase speed by 1
  2. If next car is at a distance of less than the maximum speed (5 here), reduce the speed so the car can't collide with the next one if the next one isn't moving
  3. Randomly decreases speed by 1 based on a variable I defined

I have a loop that does the job perfectly :

void Loop()
{
    for(i=0; imoved==false && tab[i]->speed!=0)
        {
            int newpos = (i+tab[i]->speed)%MAX_CARS;
            AddCar in newpos;
            tab[newpos] = tab[i];
            RemoveCar in i;
            tab[newpos]->moved = true; //so the program won't move it again
        }
    }
}
;>

Both for() loops work great, when I print the speed of every car on screen they adjust fine, none of them are above their authorized speed.

The problem is that, somehow between the 2 loops, some cars, no matter their speed except for the ones at 5, some of them (not all of them) increase their speed by +1, never +2 or +3, always +1, which is strange because there is NOTHING supposed to be happening between the 2 loops. The problem always happen when one car reduces its speed because it is going to collide with another one, the program doesn't crash but one car is overriden by the previous one.

Any idea ?

asked 38 secs ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 25 فروردين 1396 ساعت: 17:17

صفحه بندی