From e9dca0371f0f65c21ef515fc512d19f969e0009c Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Fri, 15 Oct 2021 15:44:46 +0200 Subject: [PATCH] Add savefile function --- include/model.h | 3 ++- src/model.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/include/model.h b/include/model.h index 21be7e1..8b465a6 100644 --- a/include/model.h +++ b/include/model.h @@ -192,4 +192,5 @@ e_status play_move(s_utictactoe *p_uttt, s_move *p_move); /*Given usefull functions*/ void draw_utictactoe(s_utictactoe *p_uttt); void draw_utictactoe_history(s_utictactoe *p_uttt); -#endif /* MODEL_H */ \ No newline at end of file +void save_a_utictactoe_to_file(FILE *p_f, s_utictactoe *p_uttt); +#endif /* MODEL_H */ diff --git a/src/model.c b/src/model.c index a13bd2b..a46daa4 100644 --- a/src/model.c +++ b/src/model.c @@ -1,5 +1,51 @@ #include "model.h" +// Define shorts positions +char * get_short_position(e_location pos){ + switch(pos) { + case TOPLEFT: + return "TO"; + case TOPCENTER: + return "TC"; + case TOPRIGHT: + return "TR"; + case MIDLEFT: + return "ML"; + case MIDCENTER: + return "MC"; + case MIDRIGHT: + return "MR"; + case BOTTOMLEFT: + return "BL"; + case BOTTOMCENTER: + return "BC"; + case BOTTOMRIGHT: + return "BR"; + case FREE: + return "FR"; + case NONE: + return "NO"; + default: + return "NO"; + } +} + +char get_short_player(e_player player){ + switch(player){ + case PLAYER_O: + return 'O'; + case PLAYER_X: + return 'X'; + case NOBODY: + return '.'; + case BOTH: + return '#'; + default: + return NO; + } +} + +#define SHORT_VERSION(code) SHORT_##code /* * Create empty move @@ -236,10 +282,28 @@ e_status play_move(s_utictactoe *p_uttt, s_move *p_move) { //check if we have a winner in inner_ttt then set player to outer p_uttt->outer_tictactoe->content[p_move->outer_position] = p_uttt->inner_tictactoes[p_move->outer_position]->winner; - } - p_uttt->outer_tictactoe->content[p_move->outer_position] = p_move->player; + else { + p_uttt->outer_tictactoe->content[p_move->outer_position] = p_move->player; + } set_tictactoe_winner(p_uttt->outer_tictactoe); return YES; } +void save_a_utictactoe_to_file(FILE *p_f, s_utictactoe *p_uttt){ + assert(p_f); + assert(p_uttt); + fprintf(p_f, "%d\n", p_uttt->inception_level); + if(p_uttt->history){ + list_element_s_move * c_hist = p_uttt->history; + while (c_hist != NULL){ + if(c_hist->last_move){ + fprintf(p_f, "%s %s %c\n", + get_short_position(c_hist->last_move->inner_position), + get_short_position(c_hist->last_move->outer_position), + get_short_player(c_hist->last_move->player)); + } + c_hist = c_hist->next; + } + } +}