Browse Source

Gestion du nom du joueur.

master
Bastien 8 years ago
committed by Squiz
parent
commit
fc1cd51819
2 changed files with 59 additions and 45 deletions
  1. +8
    -14
      dummy.c
  2. +51
    -31
      main.c

+ 8
- 14
dummy.c View File

@ -5,31 +5,25 @@
#include "api.h"
struct stockage {
char *name;
unsigned int nb_joueur;
enum hole_t color;
};
struct stockage storage(unsigned int write, char* name, unsigned int nb_joueur, enum hole_t color) {
struct stockage storage(unsigned int write, unsigned int nb_joueur, enum hole_t color) {
static struct stockage stock={NULL,0};
if(write & 1)
stock.color = color;
if(write & 2)
stock.nb_joueur = nb_joueur;
if(write & 4) {
if(stock.name)
free(stock.name);
stock.name = (char*) malloc((strlen(name)+1)*sizeof(char));
if(stock.name)
strncpy(stock.name, name, strlen(name));
}
return stock;
}
void InitLibrary(char name[50]) {
struct stockage stock;
stock = storage(4,name,0,0);
printf("stratégie init %s\n", stock.name);
struct stockage stock;;
stock = storage(3,0,0);
puts("name ?");
scanf("%s",name);
printf("stratégie init %s\n", name);
}
void StartGame() {
puts("stratégie start game");
@ -37,7 +31,7 @@ void StartGame() {
void StartMatch(const unsigned int nb_joueur, const enum hole_t color) {
srand(time(NULL));
struct stockage stock;
stock = storage(3,NULL,nb_joueur, color);
stock = storage(3,nb_joueur, color);
printf("stratégie start match (%d players, color%d)\n", stock.nb_joueur, stock.color);
}
@ -53,7 +47,7 @@ int NextMove(const SGameState * const gameState, int firstMove, SMove *move) {
struct stockage stock;
size_t i;
int start, end, next;
stock = storage(0, NULL, 0, 0);
stock = storage(0, 0, 0);
printf("joueur %d, pions : [ ", stock.color);
for(i=0;i<121;i++) {
if(gameState->board[i] == stock.color)


+ 51
- 31
main.c View File

@ -22,6 +22,7 @@ enum api_function_t {ia_lib_init, ia_start_match, ia_start_game, ia_end_game , i
struct player_t {
char *name;
enum hole_t branch;
unsigned int error;
void *ia_lib_p; /* NULL si le joueur n'est pas une stratégie */
@ -256,7 +257,7 @@ enum bool winner(struct player_t player, const size_t start_position[6][10], con
}
void save(const char *filename, const int nb_game, const int nb_game_end, const int nb_player, const time_t start_match, const int *winner, const time_t *duration) {
void save(const char *filename, struct player_t *player_state, const int nb_game, const int nb_game_end, const int nb_player, const time_t start_match, const int *winner, const time_t *duration) {
FILE* file;
char *buffer;
int i;
@ -272,8 +273,8 @@ void save(const char *filename, const int nb_game, const int nb_game_end, const
fprintf(file, "game %d (%d minute%s %d second%s) : ", i+1, duration[i]/60, (((duration[i]/60)>1)?"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);
if(winner[i] >= 0 && winner[i] < nb_player)
fprintf(file, "won by player %d (%s)\n", winner[i]+1, player_state[winner[i]].name);
else
fputs("no player has won\n", file);
}
@ -301,7 +302,7 @@ int main(int argc, char** argv) {
struct move_t movement, previous_movement;
int first_move, next_move;
int nb_game, nb_player, nb_game_end, nb_player_end;
int i, j;
int i, j, k;
char *error;
/* heure de début du match et tableaux pour stocker les gagnants et la durée des parties */
@ -336,33 +337,50 @@ int main(int argc, char** argv) {
/* on charge les IA */
j = 3;
for( i = 0; i < nb_player ; i++ ) {
/* on choisit de placer un joueur réel ou une stratégie */
if( (rand()%(nb_player-i)) < (argc-j) ) {
/* on ajoute une stratégie */
/* on prépare pour le nom */
if( (player_state[i].name = (char*) malloc(50*sizeof(char))) == NULL) {
#ifdef debug
fprintf(stderr,"strategy %s (%d)\n", argv[j], i);
fputs("malloc error\n", stderr);
#endif
player_state[i].ia_lib_p = dlopen(argv[j], RTLD_LAZY);
if( (error = dlerror()) != NULL ) {
return 6;
}
/* on choisit de placer un joueur réel ou une stratégie */
if( (rand()%(nb_player-i)) < (argc-j) ) {
/* on ajoute une stratégie */
#ifdef debug
fprintf(stderr,"error while loading %s : %s (%d)\n", argv[j], error, i);
fprintf(stderr,"strategy %s (%d)\n", argv[j], i);
#endif
/* échec du chargement, on décharge toutes les stratégies précédement chargées */
for( j=0 ; j<i ; j++ )
if( player_state[j].ia_lib_p )
dlclose(player_state[j].ia_lib_p);
return 5;
} else
/* on initialise la bibliothèque */
ia_call_function(player_state[i], ia_lib_init, NULL, "toto"); // TODO : what name should be used ?
j++;
} else {
/* on ajoute un joueur réel */
player_state[i].ia_lib_p = dlopen(argv[j], RTLD_LAZY);
if( (error = dlerror()) != NULL ) {
#ifdef debug
fprintf(stderr,"real player (%d)\n", i);
fprintf(stderr,"error while loading %s : %s (%d)\n", argv[j], error, i);
#endif
player_state[i].ia_lib_p = NULL;
}
/* échec du chargement, on décharge toutes les stratégies précédement chargées */
for( j=0 ; j<i ; j++ )
if( player_state[j].ia_lib_p )
dlclose(player_state[j].ia_lib_p);
free(player_state[j].name);
return 5;
} else
/* on initialise la bibliothèque */
ia_call_function(player_state[i], ia_lib_init, NULL, player_state[i].name); /* x = &x puisque c'est un tableau */
j++;
} else {
/* on ajoute un joueur réel */
#ifdef debug
fprintf(stderr,"real player (%d)\n", i);
#endif
player_state[i].ia_lib_p = NULL;
}
/* on vérifie le nom */
for(k = 0; k<50 && (player_state[i].name)[k] != '\0';k++);
if(k==50) {
#ifdef debug
fprintf(stderr,"warning : the length of the player's name %d must be to 50 characters maximum.\n", i);
#endif
(player_state[i].name)[49]='\0';
} else
player_state[i].name = realloc(player_state[i].name, k+1);
}
#ifdef debug
@ -374,14 +392,14 @@ int main(int argc, char** argv) {
if((winner_games = (int*) malloc(nb_game*sizeof(int))) == NULL) {
#ifdef debug
fputs("malloc error\n", stderr);
return 5;
#endif
return 6;
}
if((duration_games = (time_t*) malloc(nb_game*sizeof(time_t))) == NULL) {
#ifdef debug
fputs("malloc error\n", stderr);
return 5;
#endif
return 6;
}
@ -447,7 +465,7 @@ int main(int argc, char** argv) {
while(nb_player_end < nb_player-1 && !quit) {
#ifdef debug
fprintf(stderr, "joueur %d\n", i);
fprintf(stderr, "joueur %d (%s)\n", i, player_state[i].name);
#endif
game_state_copy = game_state;
previous_movement.start_pos=-1; previous_movement.end_pos=-1;
@ -474,7 +492,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;
winner_games[nb_game_end] = i;
ia_call_function(player_state[i], ia_end_game, NULL);
}
}
@ -515,15 +533,17 @@ int main(int argc, char** argv) {
}
/* appel de end_match */
for( i=0 ; i < nb_player ; i++ )
for( i=0 ; i < nb_player ; i++ ) {
if( player_state[i].ia_lib_p ) {
ia_call_function(player_state[i], ia_end_match, NULL);
/* on décharge toutes les stratégies */
dlclose(player_state[i].ia_lib_p);
}
free(player_state[i].name);
}
/* on enregistre le résultat du jeu dans le fichier */
save(score_filename, nb_game, nb_game_end-(quit?1:0), nb_player, time_start_match, winner_games, duration_games);
save(score_filename, player_state, nb_game, nb_game_end-(quit?1:0), nb_player, time_start_match, winner_games, duration_games);
free(winner_games);
free(duration_games);


Loading…
Cancel
Save