le jeu des motos dans tron - retour accueil
git clone git://bebou.netlib.re/tron
Log | Files | Refs | README |
commit 89e79175f91b4710ff39e727e056ece635f622e2 parent 64af3c94adbcf580ed101016fc7e6f6bfbd9b850 Auteurice: Arthur Pons <arthur.pons@unistra.fr> Date: Thu, 26 Jun 2025 18:39:39 +0200 Correction bug mort simultanée et réarrangement Vu que is_in_conflict ne pouvait renvoyer qu'un seul entier lorsque deux vélo mourrait sur le même tick seulement le vélo avec l'index le plus petit mourrait et l'autre dépassait l'UI. En vrai super drôle mais pas vraiment ce qu'on veut. Diffstat:
M | tron.c | | | 82 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 40 insertions(+), 42 deletions(-)
diff --git a/tron.c b/tron.c @@ -32,6 +32,32 @@ struct bikes { int count; }; +/*Helper functions*/ + +int rd(int min, int max) { return (rand()%(max-min+1))+min; } + +/*Display functions*/ + +void display_title_screen(int height, int width) { + tb_printf(width/2-35/2-5,height/2-5/2,0,0,"\ +████████ ██████ ██████ ███ ██\n\ + ██ ██ ██ ██ ██ ████ ██\n\ + ██ ██████ ██ ██ ██ ██ ██\n\ + ██ ██ ██ ██ ██ ██ ██ ██\n\ + ██ ██ ██ ██████ ██ ████"); + + tb_printf(width/2-35/2-5,height/2-5/2+5+2,0,0,"\ +Entrée pour continuer\n\ +\n\ ++ pour ajouter une vélo\n\ +- pour retirer une vélo\n\ +\n\ +Vélo 1 ↑←↓→\n\ +Vélo 2 zqsd\n\ +Vélo 3 ijkl\n"); +} + + void display(struct bikes bikes, int ticks, struct arena arena, int timeout) { for (int i=0;i<bikes.count;i++) { if(bikes.list[i].status==DEAD) continue; @@ -56,6 +82,8 @@ void display(struct bikes bikes, int ticks, struct arena arena, int timeout) { } } +/*Logic functions*/ + void update(struct bike *bike, coord *incs) { for(int i=TAIL-1;i>0;i--) bike->path[i]=bike->path[i-1]; bike->path[0]=bike->position; @@ -63,27 +91,6 @@ void update(struct bike *bike, coord *incs) { bike->position.y=bike->position.y+incs[bike->direction].y; } -int is_in_conflict(struct bikes bikes, struct arena arena) { - for (int i=0;i<bikes.count;i++) { - if(bikes.list[i].status==DEAD) continue; - if(bikes.list[i].position.x==0 || bikes.list[i].position.x==arena.width || bikes.list[i].position.y==0 || bikes.list[i].position.y==arena.height) - return i; - for (int j=0;j<TAIL;j++) - for (int k=0;k<bikes.count;k++) - if (bikes.list[i].position.x==bikes.list[k].path[j].x && bikes.list[i].position.y==bikes.list[k].path[j].y && bikes.list[k].status==ALIVE) - return i; - } - return -1; -} - -void kill_bike(struct bikes *bikes, int index) { - bikes->list[index].status=DEAD; -} - -int rd(int min, int max) { - return (rand()%(max-min+1))+min; -} - void createbike(int color, struct bikes *bikes, struct arena a) { struct bike c; c.status=ALIVE; @@ -99,23 +106,16 @@ void createbike(int color, struct bikes *bikes, struct arena a) { bikes->count++; } -void display_title_screen(int height, int width) { - tb_printf(width/2-35/2-5,height/2-5/2,0,0,"\ -████████ ██████ ██████ ███ ██\n\ - ██ ██ ██ ██ ██ ████ ██\n\ - ██ ██████ ██ ██ ██ ██ ██\n\ - ██ ██ ██ ██ ██ ██ ██ ██\n\ - ██ ██ ██ ██████ ██ ████"); - - tb_printf(width/2-35/2-5,height/2-5/2+5+2,0,0,"\ -Entrée pour continuer\n\ -\n\ -+ pour ajouter une vélo\n\ -- pour retirer une vélo\n\ -\n\ -Vélo 1 ↑←↓→\n\ -Vélo 2 zqsd\n\ -Vélo 3 ijkl\n"); +void kill_bikes(struct bikes *bikes, struct arena arena) { + for (int i=0;i<bikes->count;i++) { + if(bikes->list[i].status==DEAD) continue; + if(bikes->list[i].position.x==0 || bikes->list[i].position.x==arena.width || bikes->list[i].position.y==0 || bikes->list[i].position.y==arena.height) + bikes->list[i].status=DEAD; + for (int j=0;j<TAIL;j++) + for (int k=0;k<bikes->count;k++) + if (bikes->list[i].position.x==bikes->list[k].path[j].x && bikes->list[i].position.y==bikes->list[k].path[j].y && bikes->list[k].status==ALIVE) + bikes->list[i].status=DEAD; + } } int main(int argc, char **argv) { @@ -131,7 +131,6 @@ int main(int argc, char **argv) { coord incs[4] = { up, right, down, left }; srand(time(NULL)); - int dead_bike; tb_init(); tb_hide_cursor(); @@ -176,7 +175,7 @@ int main(int argc, char **argv) { case 45: /*-*/ for (int i=bikes.count-1;i>=0;i--) if(bikes.list[i].status==ALIVE) { - kill_bike(&bikes,i); + bikes.list[i].status=DEAD; break; } } @@ -187,8 +186,7 @@ int main(int argc, char **argv) { while(1) { for(int i=0;i<bikes.count;i++) update(&bikes.list[i],incs); - dead_bike=is_in_conflict(bikes,arena); - if (dead_bike>-1) kill_bike(&bikes,dead_bike); + kill_bikes(&bikes,arena); tb_clear(); display(bikes,ticknb,arena,timeout); tb_present();