Quantcast
Channel: C - Codecademy Forums
Viewing all articles
Browse latest Browse all 25

Race car simulator help: using sleep()

$
0
0

Hi there !
My name is Marc, I am new to codding and C is my very first language.
I am following the Learn C course.
I have to admit I struggled a bit getting to the last project and had to use chatGPT to gain some basic understanding.
I wanted to introduce a time lag in the printCountDown() function. As well as in the updateFirstPlace() function using sleep() however everytime I try and use sleep() all it does it delays the total running of the program.

How best can this be done?
I thank you in advance for any help !

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

// Structures section
struct Race {
  int numberOfLaps;
  int currentLap;
  char firstPlaceDriverName[20];
  char firstPlaceRaceCarColor[20];
};

struct RaceCar {
  char driverName[20];
  char raceCarColor[20];
  int totalLapTime;
};
// Print functions section
void printIntro() {
  printf("Welcome to our main event digital race fans !\nI hope everybody has their snack because we are about to begin !\n");
}

void printLap(struct Race race){
  printf("The Total number of laps is %d!\n", race.numberOfLaps);
}

int printCountDown(){
  printf("Racers Ready!\nIn...\n");
  int j = 10;
  while (j >= 0){
    if (j == 0){
      printf("Race!\n");
    } else {
      printf("%i\n", j);
    }
    j--;
  }
 }

void printFirstPlaceAfterLap(struct Race race){
 printf("After Lap %d, the leader is %s driving a %s race car.\n", race.currentLap, race.firstPlaceDriverName, race.firstPlaceRaceCarColor);
}

void printCongratulation(struct Race race){
  printf("Let's all congratulate %s in the %s race car for an amazing performance. It truly was a great race and everybody have a goodnight!\n", race.firstPlaceDriverName, race.firstPlaceRaceCarColor);
}

// Logic functions section
// The time it takes to complete a Lap is a function of Speed, Acceleration and Nerves. If all are 3 then the time will be low.
int calculateTimeToCompleteLap(){
  // This ensure that each time the function is called it generates a different number according to the actual time.
  int speed = rand() % 3 + 1;
  int acceleration = rand() % 3 + 1;
  int nerves = rand() % 3 + 1;
  return speed + acceleration + nerves;
}

void updateRaceCar(struct RaceCar *raceCar){
  raceCar->totalLapTime = calculateTimeToCompleteLap();
  }

void updateFirstPlace(struct Race *race, struct RaceCar *raceCar1, struct RaceCar *raceCar2){
  if (raceCar1->totalLapTime >= raceCar2->totalLapTime){
    strcpy(race->firstPlaceDriverName, raceCar1->driverName);
    strcpy(race->firstPlaceRaceCarColor, raceCar1->raceCarColor);
  } else {
    strcpy(race->firstPlaceDriverName, raceCar2->driverName);
    strcpy(race->firstPlaceRaceCarColor, raceCar2->raceCarColor);
  }
}

void startRace(struct RaceCar *raceCar1, struct RaceCar *raceCar2){
  struct Race race = {5, 1, "", ""};
  for (int i = 0; i < race.numberOfLaps-1; i++){
    race.currentLap++;
    updateRaceCar(raceCar1);
    updateRaceCar(raceCar2);
    updateFirstPlace(&race, raceCar1, raceCar2);
    printFirstPlaceAfterLap(race);
  }
  printCongratulation(race);
}


int main() {
	srand(time(NULL));
  struct Race race = {5, 1, "", ""};
  printIntro();
  printLap(race);
  printCountDown();
  struct RaceCar raceCar1 = {"Tom", "Blue", calculateTimeToCompleteLap()};
  struct RaceCar raceCar2 = {"Mike", "Green", calculateTimeToCompleteLap()};
  startRace(&raceCar1, &raceCar2);
};

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 25

Trending Articles