Une TUI pour jouer au go - retour accueil
git clone git://bebou.netlib.re/go-tui
Log | Files | Refs | README |
commit 2932c12cb9c360b522a1f75d12efe5a403dfc8aa parent 2d39f2d8c4cfca30627005fab1ca8ef7ec840405 Auteurice: Arthur Pons <arthur.pons@unistra.fr> Date: Fri, 18 Jul 2025 18:41:40 +0200 On peut remonter dans le temps ! Bon c'est craaaaade et y'en pleins de bugs Diffstat:
M | gotui.c | | | 75 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ |
1 file changed, 63 insertions(+), 12 deletions(-)
diff --git a/gotui.c b/gotui.c @@ -15,6 +15,17 @@ struct stone_list { int count; }; +struct game_state { + struct stone_list played_list; + struct stone_list captured_list; + int color_to_play; +}; + +/*Helper functions*/ + +int max(int a, int b) { return a > b ? a : b; } +int min(int a, int b) { return a < b ? a : b; } + /*List functions*/ struct stone_list new_stone_list() { @@ -49,6 +60,20 @@ int spot_is_taken(struct stone_list s_list, int x, int y) { return -1; } +struct game_state create_game_state(struct stone_list played_list, struct stone_list captured_list, int color) { + struct game_state g; + /*struct stone_list p=new_stone_list(); + struct stone_list c=new_stone_list(); + int co; + memcpy(&p,&played_list,played_list.count*sizeof(struct stone_list)); + memcpy(&c,&captured_list,captured_list.count*sizeof(struct stone_list)); + co=color;*/ + g.played_list=played_list; + g.captured_list=captured_list; + g.color_to_play=color; + return g; +} + /*Display functions*/ void print_line(int y, int size) { @@ -73,21 +98,22 @@ void print_line(int y, int size) { } } -void display(struct stone_list s_list, struct stone_list c_list, int size) { +void display(struct game_state g, int size, int turn) { for (int i=0;i<size;i++) print_line(i,size); - for(int i=0;i<s_list.count;i++) { - if(s_list.list[i].color==BLACK) tb_printf(s_list.list[i].x,s_list.list[i].y,0,0,"○"); - else tb_printf(s_list.list[i].x,s_list.list[i].y,0,0,"●"); + for(int i=0;i<g.played_list.count;i++) { + if(g.played_list.list[i].color==BLACK) tb_printf(g.played_list.list[i].x,g.played_list.list[i].y,0,0,"○"); + else tb_printf(g.played_list.list[i].x,g.played_list.list[i].y,0,0,"●"); } int bc=0; int ac=0; - for(int i=0;i<c_list.count;i++) { - if(c_list.list[i].color==BLACK) bc++; - else ac++; + for(int i=0;i<g.captured_list.count;i++) { + if(g.captured_list.list[i].color==BLACK) bc++; + else ac++; } tb_printf(0,size,0,0,"%d ○ capturées",bc); tb_printf(0,size+1,0,0,"%d ● capturées",ac); - tb_print(0,size+2,0,0,"\nq pour quitter"); + tb_printf(0,size+2,0,0,"tour : %d",turn); + tb_print(0,size+4,0,0,"q pour quitter"); } /* Main */ @@ -95,18 +121,29 @@ void display(struct stone_list s_list, struct stone_list c_list, int size) { int main(int argc, char **argv) { int size=9; if (argc>1) { size=atoi(argv[1]); } - int input=0; + tb_init(); tb_hide_cursor(); tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE); struct tb_event ev; + int input=0; + + struct game_state history[1000]; + int turn=0; + int shown_turn=turn; + struct stone_list s_list=new_stone_list(); struct stone_list c_list=new_stone_list(); int color=BLACK; + struct game_state g=create_game_state(s_list,c_list,color); + history[turn]=g; + turn++; + shown_turn++; + while(1) { tb_clear(); - display(s_list,c_list,size); + display(history[shown_turn-1],size,shown_turn); tb_present(); tb_poll_event(&ev); @@ -117,19 +154,27 @@ int main(int argc, char **argv) { int spot=-1; switch(input) { case 65512: /*MouseLeft*/ - if(ev.x<0 || ev.x>size*2-2 || ev.x <0 || ev.y>size-1 || ev.x%2) break; + if(ev.x<0 || ev.x>size*2-2 || ev.x <0 || ev.y>size-1 || ev.x%2 || turn!=shown_turn) break; spot=spot_is_taken(s_list,ev.x,ev.y); if(spot!=-1) { remove_stone_from_list(&s_list,spot); if(color==BLACK) { color=WHITE; } else { color=BLACK; } + struct game_state g=create_game_state(s_list,c_list,color); + history[turn]=g; + turn++; + shown_turn++; break; } s=create_stone(ev.x,ev.y,color); add_stone_to_list(&s_list,s); if(color==BLACK) { color=WHITE; } else { color=BLACK; } + struct game_state g=create_game_state(s_list,c_list,color); + history[turn]=g; + turn++; + shown_turn++; break; case 65511: /*MouseRight*/ - if(ev.x<0 || ev.x>size*2-2 || ev.x <0 || ev.y>size-1 || ev.x%2) break; + if(ev.x<0 || ev.x>size*2-2 || ev.x <0 || ev.y>size-1 || ev.x%2 || turn!=shown_turn) break; spot=spot_is_taken(s_list,ev.x,ev.y); if(spot!=-1) { s=create_stone(ev.x,ev.y,s_list.list[spot].color); @@ -143,6 +188,12 @@ int main(int argc, char **argv) { tb_shutdown(); return 0; break; + case 65515: /*arrow left*/ + shown_turn=max(1,shown_turn-1); + break; + case 65514: /*arrow right*/ + shown_turn=min(turn,shown_turn+1); + break; } } return 0;