From b3b52cb95f61a5e251a16e8e867cf39d67ad124e Mon Sep 17 00:00:00 2001 From: "remi.greub" <remi.greub@hes-so.ch> Date: Wed, 26 Mar 2025 20:41:58 +0100 Subject: [PATCH] =?UTF-8?q?factorisation=20de=20fonction=20qui=20detecte?= =?UTF-8?q?=20une=20case=20de=20libre=20dans=20une=20colonne=20donn=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- puissance4_GRB/puissance4.c | 24 +++++++++++++++++++----- puissance4_GRB/puissance4.h | 3 ++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/puissance4_GRB/puissance4.c b/puissance4_GRB/puissance4.c index b40b605..47fe5bb 100644 --- a/puissance4_GRB/puissance4.c +++ b/puissance4_GRB/puissance4.c @@ -73,14 +73,28 @@ int kill_game(){ }*/ -int put_free_cell(int j_p, int i, symbol_t symbol){ - if(game.cells[j_p][i].symbol == EMPTY){ - //put the player's symbol +int put_free_cell(int j_p, symbol_t symbol){ + int i=0; + if((i = is_cell_free(j_p, 0, game.players[game.curr_player].symbol)) != -1){ game.cells[j_p][i].symbol = symbol; + } + return i; +} + +/* +retourne la ligne d'une cellule de libre dans une +colonne donnée +(si il y en a une, retourn -1 si il n'y en a aucune) +*/ +int is_cell_free(int j_p, int i, symbol_t symbol){ + if(game.cells[j_p][i].symbol == EMPTY){ + //retourne la ligne disponible + return i; }else{ - if(i<=game.height){ - return put_free_cell(j_p, i+1, symbol); + if(i<(game.width-1)){ + return is_cell_free(j_p, i+1, symbol); }else{ + printf("il n'y a plus de case de disponible sur cette colonne\n"); return -1; } } diff --git a/puissance4_GRB/puissance4.h b/puissance4_GRB/puissance4.h index 2f5bef6..4b03419 100644 --- a/puissance4_GRB/puissance4.h +++ b/puissance4_GRB/puissance4.h @@ -45,7 +45,8 @@ void init_puissance4(int height, int width); void cell_destroy(struct cell **cells, int height); int kill_game(); -int put_free_cell(int j_p, int i, symbol_t symbol); +int put_free_cell(int j_p, symbol_t symbol); +int is_cell_free(int j_p, int i, symbol_t symbol); int Launch_puissance4(); bool Is_Grid_full(); -- GitLab