#include #include #include #include #include /* msleep(): Sleep for the requested number of milliseconds. */ int msleep(long msec) { struct timespec ts; int res; if (msec < 0) { errno = EINVAL; return -1; } ts.tv_sec = msec / 1000; ts.tv_nsec = (msec % 1000) * 1000000; do { res = nanosleep(&ts, &ts); } while (res && errno == EINTR); return res; } int valabs(int x) { if (x > 0) { return x; } else { return -x; } } #define ACCMAX 5 /* Acceleration maximum par tour */ #define NBPORTES 13 /* Nombre de portes à passer */ #define TAILLEPLATEAU 100 /* taille du plateau */ #define WAITTIME 100 /* temps par tour de simulation */ int ok_acceleration(int ax, int ay) { return (valabs(ax) + valabs(ay) <= ACCMAX); } int dans_plateau(int pos_x, int pos_y) { return pos_x >= 0 && pos_y >= 0 && pos_x < TAILLEPLATEAU && pos_y < TAILLEPLATEAU; } typedef struct { int ax; int ay; } acceleration; /* Ce n'est qu'un exemple pour vous entraîner */ int portes[NBPORTES][2] = {{20, 20}, {94, 10}, {97, 20}, {90, 30}, {97, 40}, {85, 50}, {97, 70}, {75, 90}, {40, 90}, {75, 50}, {25, 50}, {20, 90}, {1, 1}}; /* TOUTES VOS FONCTIONS DOIVENT ÊTRE SOUS CE COMMENTAIRE */ acceleration nom_prenom_comportement(int pos_x, int pos_y, int spe_x, int spe_y, int nextx, int nexty, int nextnextx, int nextnexty) { /* pos_x est la coordonnée x de votre pod pos_y est la coordonnée y de votre pod spe_x est la vitesse suivant x de votre pod spe_y est la vitesse suivant y de votre pod nextx est la coordonnée x de la prochaine porte à passer nexty est la coordonnée y de la prochaine porte à passer nextnextx est la coordonnée x de la prochaine prochaine porte à passerp nextnexty est la coordonnée y de la prochaine prochaine porte à passer */ int acc_x = 0; int acc_y = 0; /* compléter ici avec votre code */ return ((acceleration){acc_x, acc_y}); } /* TOUTES VOS FONCTIONS DOIVENT ÊTRE AU DESSUS DE CE COMMENTAIRE */ int main() { acceleration res; int pos_x = 1; int pos_y = 1; int spe_x = 0; int spe_y = 0; int obj_courant = 0; int obj_next = 1; int acc_x = 0; int acc_y = 0; int cpt_etape = 0; int nb_lap = 0; while (1) { obj_next = (obj_courant + 1) % NBPORTES; printf("========== Début du tour ==========\n"); printf(" Position : (%d, %d)\n Vitesse: (%d, %d)\n Objectif: (%d, %d)\n Suivant: (%d, %d)\n", pos_x, pos_y, spe_x, spe_y, portes[obj_courant][0], portes[obj_courant][1], portes[obj_next][0], portes[obj_next][1]); res = nom_prenom_comportement(pos_x, pos_y, spe_x, spe_y, portes[obj_courant][0], portes[obj_courant][1], portes[obj_next][0], portes[obj_next][1]); if (ok_acceleration(res.ax, res.ay)) { acc_x = res.ax; acc_y = res.ay; } else { printf("WARNING : votre acceleration a échoué : vous avez proposé (%d, %d) qui est plus grand que %d\n", res.ax, res.ay, ACCMAX); } printf(" Vous proposez l'accélération : (%d, %d)\n",acc_x, acc_y); spe_x = spe_x + acc_x; spe_y = spe_y + acc_y; pos_x = pos_x + spe_x; pos_y = pos_y + spe_y; printf(" Nouvelle Vitesse : (%d, %d)\n Nouvelle position : (%d, %d)\n", spe_x, spe_y, pos_x, pos_y); if (!dans_plateau(pos_x, pos_y)) { printf("WARNING : Vous n'êtes plus dans le plateau\n"); } if (pos_x == portes[obj_courant][0] && pos_y == portes[obj_courant][1]) { printf(" Nouvel objectif (%d/%d) en %d étapes\n", obj_courant, NBPORTES, cpt_etape); if (obj_courant + 1 >= NBPORTES) { obj_courant = 0; nb_lap = nb_lap + 1; printf(" Nouveau tour (%d) après %d étapes\n", nb_lap, cpt_etape); } else { obj_courant = obj_courant + 1; } } printf("========== Fin du tour ==========\n"); msleep(WAITTIME); } }