Une TUI pour jouer au go - retour accueil
git clone git://bebou.netlib.re/go-tui
Log | Files | Refs |
commit a7ef224c256e592d7871a7c70e319c27b447fa48 parent 73400a014e4c48e584fb162191f9e65133be09f5 Auteurice: Arthur Pons <arthur.pons@unistra.fr> Date: Wed, 16 Jul 2025 17:05:25 +0200 Factorisation d'ajout et retrait des pierres Bon on factorise pas grand chose en vrai Diffstat:
M | gotui.c | | | 27 | +++++++++++++++++---------- |
1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/gotui.c b/gotui.c @@ -15,6 +15,8 @@ struct stone_list { int count; }; +/*List functions*/ + struct stone_list new_stone_list() { struct stone_list s; s.count=0; @@ -22,6 +24,16 @@ struct stone_list new_stone_list() { return s; } +void add_stone_to_list(struct stone_list *s_list, struct stone s) { + s_list->list[s_list->count]=s; + s_list->count++; +} + +void remove_stone_from_list(struct stone_list *s_list, int i) { + for (int j=i;j<s_list->count;j++) s_list->list[j]=s_list->list[j+1]; + s_list->count--; +} + /*Game function*/ struct stone create_stone(int x, int y, int color) { @@ -107,26 +119,21 @@ int main(int argc, char **argv) { if(ev.x<0 || ev.x>size*2+2 || ev.x <0 || ev.y>size-1 || ev.x%2) break; spot=spot_is_taken(s_list,ev.x,ev.y); if(spot!=-1) { - for (int j=spot;j<s_list.count;j++) s_list.list[j]=s_list.list[j+1]; - s_list.count--; + remove_stone_from_list(&s_list,spot); if(color==BLACK) { color=WHITE; } else { color=BLACK; } break; } s=create_stone(ev.x,ev.y,color); - s_list.list[s_list.count]=s; - s_list.count++; + add_stone_to_list(&s_list,s); if(color==BLACK) { color=WHITE; } else { color=BLACK; } break; case 65511: /*MouseRight*/ if(ev.x<0 || ev.x>size*2+2 || ev.x <0 || ev.y>size-1 || ev.x%2) break; spot=spot_is_taken(s_list,ev.x,ev.y); if(spot!=-1) { - int captured_color=s_list.list[spot].color; - s=create_stone(ev.x,ev.y,captured_color); - c_list.list[c_list.count]=s; - c_list.count++; - for (int j=spot;j<s_list.count;j++) s_list.list[j]=s_list.list[j+1]; - s_list.count--; + s=create_stone(ev.x,ev.y,s_list.list[spot].color); + add_stone_to_list(&c_list,s); + remove_stone_from_list(&s_list,spot); } break; case 113: