Add savefile function

This commit is contained in:
Yorick Barbanneau 2021-10-15 15:44:46 +02:00
parent 3a020073f9
commit e9dca0371f
2 changed files with 68 additions and 3 deletions

View file

@ -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 */
void save_a_utictactoe_to_file(FILE *p_f, s_utictactoe *p_uttt);
#endif /* MODEL_H */

View file

@ -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;
}
}
}