TP4 - Correction

Version minimale : seulement la partie 1

ppompeani_cars_minimal.c

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void clear_screen() { fprintf(stdout, "\e[1;1H\e[2J"); }

void refresh() { fflush(stdout); }

void move_cursor(unsigned int y, unsigned int x) {
  fprintf(stdout, "\e[%d;%dH", y, x);
}

void print_n(char *s, unsigned int repeat) {
  while (repeat > 0) {
    fprintf(stdout, "%s", s);
    repeat--;
  }
}

const unsigned int ROAD_SIZE = 40;

void game_init() {
  srand(time(NULL));

  clear_screen();

  move_cursor(1, 1);
  print_n("-", ROAD_SIZE);

  move_cursor(3, 1);
  print_n("- ", ROAD_SIZE / 2);

  move_cursor(5, 1);
  print_n("-", ROAD_SIZE);

  move_cursor(1, ROAD_SIZE + 1);

  refresh();
}

void game_close() {
  clear_screen();
  refresh();
}

void *car_thread(void *param) {
  int car = *((int *)param);

  int car_color = rand() % 6;

  int increment, x, y;
  char logo;

  if (car == 0) {
    increment = -1;
    x = ROAD_SIZE;
    y = 2;
    logo = '<';
  } else {
    increment = 1;
    x = 1;
    y = 4;
    logo = '>';
  }

  for (int i = 1; i < ROAD_SIZE; i++) {
    // We reset old position
    move_cursor(y, x);
    fputc(' ', stdout);
    // We move to new position
    x += increment;
    move_cursor(y, x);
    fputc(logo, stdout);
    move_cursor(1, ROAD_SIZE + 1);
    refresh();
    usleep(50000 + rand() % 10000);
  }

  move_cursor(y, x);
  fputc(' ', stdout);
  move_cursor(1, ROAD_SIZE + 1);
  refresh();

  return NULL;
}

int main(int argc, char **argv) {
  game_init();

  pthread_t last;

  char input;

  int car_arg;

  do {
    input = fgetc(stdin);
    switch (input) {
    case 'j':
      car_arg = 1;
      pthread_create(&last, NULL, car_thread, &car_arg);
      break;

    case 'k':
      car_arg = 0;
      pthread_create(&last, NULL, car_thread, &car_arg);
      break;
    }
  } while (input != 'q');

  pthread_join(last, NULL);

  game_close();
}

Version étendue : parties 1 et 2

ppompeani_cars_extended.c

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

void clear_screen() { fprintf(stdout, "\e[1;1H\e[2J"); }

void refresh() { fflush(stdout); }

void move_cursor(unsigned int y, unsigned int x) {
  fprintf(stdout, "\e[%d;%dH", y, x);
}

void print_n(char *s, unsigned int repeat) {
  while (repeat > 0) {
    fprintf(stdout, "%s", s);
    repeat--;
  }
}

// Amélioration Couleur
const unsigned int COLOR_RESET = 0, COLOR_RED = 1, COLOR_GREEN = 2,
                   COLOR_YELLOW = 3, COLOR_BLUE = 4, COLOR_PURPLE = 5,
                   COLOR_LIGHTBLUE = 6;

void color(int color) {
  if (color == COLOR_RESET) {
    fprintf(stdout, "\e[0m");
  } else {
    fprintf(stdout, "\e[3%cm", color + '0');
  }
}

unsigned int ROAD_SIZE;

// Amélioration Input
struct termios OLD_TERMINAL_PARAMS;

void game_init() {
  srand(time(NULL));

  clear_screen();

  move_cursor(1, 1);
  print_n("-", ROAD_SIZE);

  move_cursor(3, 1);
  print_n("- ", ROAD_SIZE / 2);

  move_cursor(5, 1);
  print_n("-", ROAD_SIZE);

  // Amélioration Messages
  move_cursor(7, 1);
  fprintf(stdout, "Press j to spawn a car riding to the east");
  move_cursor(8, 1);
  fprintf(stdout, "Press k to spawn a car riding to the west");
  move_cursor(9, 1);
  fprintf(stdout, "Press q to quit");

  move_cursor(1, ROAD_SIZE + 1);

  // Amélioration Input
  struct termios new_terminal_params;
  // Store old params
  tcgetattr(STDIN_FILENO, &OLD_TERMINAL_PARAMS);
  // Copy params
  new_terminal_params = OLD_TERMINAL_PARAMS;
  // Change params
  // Remove ICANON flag (wait for enter) and ECHO flag (print stdin)
  new_terminal_params.c_lflag &= ~(ICANON | ECHO);
  // Apply params
  tcsetattr(STDIN_FILENO, TCSANOW, &new_terminal_params);

  refresh();
}

void game_close() {
  clear_screen();

  // Amélioration Input
  // Restore old params
  tcsetattr(STDIN_FILENO, TCSANOW, &OLD_TERMINAL_PARAMS);

  refresh();
}

// Amélioration Compteur
// Extra: une couleur différente à chaque fois que le compteur change
unsigned int CAR_COUNTER = 0;
void *counter_thread(void *param) {
  int counter_color = 0;
  int last_count = CAR_COUNTER;
  while (1) {
    if (CAR_COUNTER != last_count) {
      counter_color = (counter_color + 1) % 6;
      last_count = CAR_COUNTER;
      move_cursor(3, ROAD_SIZE + 2);
      color(counter_color);
      fprintf(stdout, "%d", CAR_COUNTER);
    }
    sleep(1);
  }
  return NULL;
}

void *car_thread(void *param) {
  int car = *((int *)param);

  int car_color = rand() % 6;

  int increment, x, y;
  char logo;

  if (car == 0) {
    increment = -1;
    x = ROAD_SIZE;
    y = 2;
    logo = '<';
  } else {
    increment = 1;
    x = 1;
    y = 4;
    logo = '>';
  }

  // Amélioration Compteur
  CAR_COUNTER++;

  for (int i = 1; i < ROAD_SIZE; i++) {
    // We reset old position
    move_cursor(y, x);
    fputc(' ', stdout);
    // We move to new position
    x += increment;
    move_cursor(y, x);
    // Amélioration Couleur
    color(car_color);
    fputc(logo, stdout);
    move_cursor(1, ROAD_SIZE + 1);
    refresh();
    usleep(50000 + rand() % 10000);
  }

  move_cursor(y, x);
  fputc(' ', stdout);
  move_cursor(1, ROAD_SIZE + 1);
  refresh();

  return NULL;
}

// Amélioration Messages
void *error_thread(void *param) {
  color(0);
  move_cursor(11, 1);
  fprintf(stdout, "Unrecognised input.");
  refresh();

  sleep(2);

  color(0);
  move_cursor(11, 1);
  fprintf(stdout, "                   ");
  refresh();
  return NULL;
}

int main(int argc, char **argv) {

  {
    char buf[10];
    printf("Quel taille doit faire la route ? (40) → ");
    fgets(buf, 10, stdin);
    int size = atoi(buf);
    if (size == 0) {
      ROAD_SIZE = 40;
    } else {
      ROAD_SIZE = size;
    }
  }

  game_init();

  pthread_t last, unused_thread_id;

  // Amélioration Compteur
  pthread_create(&unused_thread_id, NULL, counter_thread, NULL);

  char input;

  int car_arg;

  do {
    input = fgetc(stdin);
    switch (input) {
    case 'j':
    case 'k':
      if (input == 'j') {
        car_arg = 1;
      } else {
        car_arg = 0;
      }

      pthread_create(&last, NULL, car_thread, &car_arg);

      color(0);
      move_cursor(11, 1);
      fprintf(stdout, "                   ");
      refresh();
      break;
    case 'q':
      // Amélioration Messages
      color(0);
      move_cursor(11, 1);
      fprintf(stdout, "Quitting.          ");
      refresh();
      break;
    default:
      // Amélioration Messages
      pthread_create(&unused_thread_id, NULL, error_thread, NULL);
    }
  } while (input != 'q');

  pthread_join(last, NULL);

  game_close();
}