Browse Source

Enregistrement du résultat de la partie dans un fichier.

master
Bastien 8 years ago
committed by Squiz
parent
commit
6031da211c
2 changed files with 75 additions and 7 deletions
  1. +73
    -7
      main.c
  2. +2
    -0
      readme

+ 73
- 7
main.c View File

@ -1,3 +1,4 @@
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
@ -12,6 +13,7 @@
#define end_pos endPos
#define debug
#define score_filename "scores"
enum bool {false, true};
@ -253,6 +255,37 @@ enum bool winner(struct player_t player, const size_t start_position[6][10], con
return true;
}
void save(const char *filename, const int nb_game, const int nb_player, const time_t start_match, const int *winner, const time_t *duration) {
FILE* file;
char *buffer;
int i;
if( (file=fopen(filename, "at")) != NULL) {
/* remove '\n' from ctime */
buffer = ctime(&start_match);
buffer[strlen(buffer)-1] = '\0';
fprintf(file, "=== %s (%d game%s - %d players) ===\n", buffer, nb_game, (nb_game>1)?"s":"", nb_player);
for(i=0; i < nb_game ; i++) {
if(duration[i] > 3600)
fprintf(file, "game %d (%d hour%s %d minute%s %d second%s) : ", i+1, duration[i]/3600, ((duration[i]/3600)>1?"s":""), (duration[i]%3600)/60, (((duration[i]%3600)/60)?"s":""), (duration[i]%3600)%60, (((duration[i]%3600)%60)>1?"s":""));
else if(duration[i] > 60)
fprintf(file, "game %d (%d minute%s %d second%s) : ", i+1, duration[i]/60, ((duration[i]/60)?"s":""), duration[i]%60, ((duration[i]%60)>1?"s":""));
else
fprintf(file, "game %d (%d second%s) : ", i+1, duration[i], ((duration[i]%60)>1?"s":""));
if(winner[i] >=0 && winner[i] < nb_player)
fprintf(file, "won by player %d\n", i+1);
else
fputs("no player has won\n", file);
}
fputs("\n", file);
fclose(file);
}
#ifdef debug
else
fprintf(stderr, "cannot open file \"%s\"\n", filename);
#endif
}
int main(int argc, char** argv) {
const size_t start_position[6][10] = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{19, 20, 21, 22, 32, 33, 34, 44, 45, 55},
@ -264,9 +297,12 @@ int main(int argc, char** argv) {
struct game_state_t game_state, game_state_copy, game_state_ia_copy;
struct move_t movement, previous_movement;
int first_move, next_move;
int nb_game, nb_player, nb_player_end;
int nb_game, nb_player, nb_game_end, nb_player_end;
int i, j;
/* heure de début du match et tableaux pour stocker les gagnants et la durée des parties */
time_t time_start_match, *duration_games;
int *winner_games;
if (argc < 3 || argc > 9) {
fprintf(stderr, "%s nb_game nb_player [[ia]…].\n", argv[0]);
@ -329,6 +365,22 @@ int main(int argc, char** argv) {
puts("");
#endif
/* on initialise les tableaux pour stocker les gagnants et la durée de chaque partie */
if((winner_games = (int*) malloc(nb_game*sizeof(int))) == NULL) {
#ifdef debug
fputs("malloc error\n", stderr);
return 5;
#endif
}
if((duration_games = (time_t*) malloc(nb_game*sizeof(time_t))) == NULL) {
#ifdef debug
fputs("malloc error\n", stderr);
return 5;
#endif
}
/* ouverture de la fenêtre graphique */
puts("(GUI) Opening window");
struct gui_resource_t gui_res = display_start(gui_res);
@ -344,10 +396,17 @@ int main(int argc, char** argv) {
ia_call_function(player_state[i], ia_start_match, NULL, nb_player, player_state[i].branch);
}
while( nb_game-- > 0 ) {
/* on note l'heure du début du jeu */
time_start_match = time(NULL);
for( nb_game_end = 0 ; nb_game_end < nb_game ; nb_game_end++ ) {
#ifdef debug
fprintf(stderr,"%d %s left\n", nb_game, nb_game>1?"games":"game");
#endif
duration_games[nb_game_end] = time(NULL);
winner_games[nb_game_end] = -1;
/* appel de start_game */
for( i=0 ; i < nb_player ; i++ )
@ -409,6 +468,7 @@ int main(int argc, char** argv) {
if(winner(player_state[i], start_position, game_state)) {
nb_player_end=nb_player /* la partie se termine */;
puts("le joueur a gagné");
winner_games[nb_game_end] = i+1;
ia_call_function(player_state[i], ia_end_game, NULL);
}
}
@ -433,8 +493,10 @@ int main(int argc, char** argv) {
i%=nb_player;
} while(player_state[i].error >= 3);
/* le joueur a joué, rotation du plateau à l'écran vers le suivant */
printf("(GUI) Rotating board, branch %d → %d\n", player_state[j].branch, player_state[i].branch);
display_anirotate_board(gui_res, game_state, player_state[j].branch, player_state[i].branch + (j+1!=i)*6 /*places des joueurs actuel et suivant, ∈[1-6]*/);
if(nb_player_end < nb_player-1) { // TODO condition identique au while, c'est pour quand on arrive à la fin de la partie, ne pas tourner le plateau
printf("(GUI) Rotating board, branch %d → %d\n", player_state[j].branch, player_state[i].branch);
display_anirotate_board(gui_res, game_state, player_state[j].branch, player_state[i].branch + (j+1!=i)*6 /*places des joueurs actuel et suivant, ∈[1-6]*/);
}
}
puts("fin de la partie");
/* display_animsg(gui_res, game_state, player_state[i].branch, "Manche terminée", 1000); TODO*/
@ -443,11 +505,9 @@ int main(int argc, char** argv) {
if(player_state[i].error < 3)
ia_call_function(player_state[i], ia_end_game, NULL);
duration_games[nb_game_end] = time(NULL) - duration_games[nb_game_end];
}
/* appel de end_match */
for( i=0 ; i < nb_player ; i++ )
if( player_state[i].ia_lib_p ) {
@ -456,6 +516,12 @@ int main(int argc, char** argv) {
dlclose(player_state[i].ia_lib_p);
}
/* on enregistre le résultat du jeu dans le fichier */
save(score_filename, nb_game, nb_player, time_start_match, winner_games, duration_games);
free(winner_games);
free(duration_games);
/* fermeture de la fenêtre graphique */
display_close(gui_res);


+ 2
- 0
readme View File

@ -175,3 +175,5 @@ tous les pions du joueur « player » sont situés sur la branche opposée à sa
fonction « main »
=================
La partie s'arrete si un seul joueur gagne ou si tous les joueurs sauf un ont perdus (un joueur ne va pas jouer tout seul)

Loading…
Cancel
Save