Browse Source

Change calls to library

master
Bastien 8 years ago
committed by Squiz
parent
commit
a1583f6e64
2 changed files with 105 additions and 98 deletions
  1. +13
    -8
      dameschinoises.patch
  2. +92
    -90
      main.c

+ 13
- 8
dameschinoises.patch View File

@ -1,5 +1,5 @@
--- ../dameschinoises.h 2012-12-08 15:04:30.000000000 +0100
+++ dameschinoises.h 2012-12-16 22:43:40.000000000 +0100
--- dameschinoises.h 2012-12-16 22:46:55.000000000 +0100
+++ api.h 2012-12-22 08:46:12.000000000 +0100
@@ -1,80 +1,65 @@
-/////////// Strutures à utiliser
+/* Strutures à utiliser */
@ -61,9 +61,10 @@
* @param char name[50]
- * nom associé à la librairie
- */
-//void InitLibrary(char name[50]);
+ * nom associé la bibliothèque
+*/
//void InitLibrary(char name[50]);
+/* void InitLibrary(char name[50]); */
typedef void (*pfInitLibrary)(char[50]);
-/**
@ -76,28 +77,31 @@
*/
-//void StartMatch(const unsigned int nbJoueur, const EPlayer couleur);
-typedef void (*pfStartMatch)(const unsigned int, const EPlayer);
+//void StartMatch(const unsigned int nbJoueur, const enum player_t couleur);
+/* void StartMatch(const unsigned int nbJoueur, const enum player_t couleur); */
+typedef void (*pfStartMatch)(const unsigned int, const enum player_t);
-/**
- * Initialiser l'IA pour une manche (d'un match)
- */
-//void StartGame();
+/* Initialiser l'IA pour une manche (d'un match) */
//void StartGame();
+/* void StartGame(); */
typedef void (*pfStartGame)();
-/**
- * Fin d'une manche (d'un match)
- */
-//void EndGame();
+/* Fin d'une manche (d'un match) */
//void EndGame();
+/* void EndGame(); */
typedef void (*pfEndGame)();
-/**
- * Fin d'un match
- */
-//void EndMatch();
+/* Fin d'un match */
//void EndMatch();
+/* void EndMatch(); */
typedef void (*pfEndMatch)();
-/**
@ -116,6 +120,7 @@
* @return int
* booleen : 0=plus de mouvement ; 1=prendre en compte le mouvement
*/
//int NextMove(const SGameState * const gameState, int firstMove, SMove *move);
-//int NextMove(const SGameState * const gameState, int firstMove, SMove *move);
-typedef int (*pfNextMove)(const SGameState * const, int, SMove *);
+/* int NextMove(const SGameState * const gameState, int firstMove, SMove *move); */
+typedef int (*pfNextMove)(const struct game_state_t * const, int, struct move_t *);

+ 92
- 90
main.c View File

@ -1,5 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <time.h>
#include "api.h"
@ -7,9 +8,12 @@
#define debug
enum bool {false, true};
enum api_function_t {ia_lib_init, ia_start_match, ia_start_game, ia_end_game , ia_end_match, ia_next_move};
struct branch_t {
void *lib_strategy_p;
struct player_state_t {
enum bool play;
void *ia_lib_p;
size_t pawn[10];
};
@ -20,26 +24,67 @@ enum bool char_to_int(char *argv, int *nombre) {
return (error!=argv);
}
/* not used */
size_t star_branch(int i) {
return i%2?((i-1)/2)+3:i/2;
}
/* not used */
int star_branch2(unsigned int nb_player, size_t index) {
return (nb_player>=2 && nb_player<=4 && index >= nb_player/2)?index-(nb_player>3?2:1)+3:index;
void ia_call_function(const struct player_state_t player_state, enum api_function_t api_function, ...) {
va_list ap;
char* error;
char* function_name;
void (*ia_function_pf)();
int a; enum player_t b;
va_start (ap, api_function);
switch(api_function) {
case ia_lib_init:
function_name = "InitLibrary";
break;
case ia_start_match:
function_name = "StartMatch";
break;
case ia_start_game:
function_name = "StartGame";
break;
case ia_end_game:
function_name = "EndGame";
break;
case ia_end_match:
function_name = "EndMatch";
break;
case ia_next_move:
function_name = "NextMove";
break;
}
*(void **) (&ia_function_pf) = dlsym(player_state.ia_lib_p, function_name);
if( (error=dlerror()) == NULL ) {
switch(api_function) {
case ia_lib_init:
(*ia_function_pf)(va_arg (ap, char*));
break;
case ia_start_match:
a = va_arg(ap, int);
b = va_arg(ap, enum player_t);
(*ia_function_pf)(a, b);
break;
default:
(*ia_function_pf)();
break;
}
}
#ifdef debug
else
fprintf(stderr,"function \"%s\" not found in strategy.\n", function_name);
#endif
va_end(ap);
}
int main(int argc, char** argv) {
struct branch_t player_branch[6];
int i, j, k, nb_game, nb_player;
const size_t start_position[6][10] = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{111, 112, 113, 114, 115, 116, 117, 118, 119, 120},
{19, 20, 21, 22, 32, 33, 34, 44, 45, 55},
{65, 75, 76, 86, 87, 88, 98, 99, 100, 101},
{74, 84, 85, 95, 96, 97, 107, 108, 109, 110},
{10, 11, 12, 13, 23, 24, 25, 35, 36, 46}};
struct player_state_t player_state[6];
struct game_state_t game_state;
const size_t start[6][10] = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{111, 112, 113, 114, 115, 116, 117, 118, 119, 120},
{19, 20, 21, 22, 32, 33, 34, 44, 45, 55},
{65, 75, 76, 86, 87, 88, 98, 99, 100, 101},
{74, 84, 85, 95, 96, 97, 107, 108, 109, 110},
{10, 11, 12, 13, 23, 24, 25, 35, 36, 46}};
int nb_game, nb_player;
int i, j;
if (argc < 3 || argc > 9) {
@ -76,85 +121,54 @@ int main(int argc, char** argv) {
#ifdef debug
fprintf(stderr,"strategy %s (%d)\n", argv[j], i);
#endif
if( (player_branch[i].lib_strategy_p = dlopen(argv[j], RTLD_LAZY)) == NULL ) {
if( (player_state[i].ia_lib_p = dlopen(argv[j], RTLD_LAZY)) == NULL ) {
/* échec du chargement, on décharge toutes les stratégies précédement chargées */
for( j=0 ; j<i ; j++ ) {
if(player_branch[j].lib_strategy_p != NULL)
dlclose(player_branch[j].lib_strategy_p);
}
for( j=0 ; j<i ; j++ )
if( player_state[j].ia_lib_p )
dlclose(player_state[j].ia_lib_p);
return 5;
} else {
} else
/* on initialise la bibliothèque */
char* error;
void (*library_init_pf)();
*(void **) (&library_init_pf) = dlsym(player_branch[i].lib_strategy_p, "InitLibrary");
if((error=dlerror()) == NULL) {
(*library_init_pf)("toto"); // TODO : passer le nom de la stratégie en paramètre
}
#ifdef debug
else
fprintf(stderr,"function InitLibrary not found in stratégie (%d)\n", i);
#endif
}
ia_call_function(player_state[i], ia_lib_init, "toto");
j++;
} else {
/* on ajoute un joueur réel */
#ifdef debug
fprintf(stderr,"real player (%d)\n", i);
#endif
player_branch[i].lib_strategy_p = NULL;
player_state[i].ia_lib_p = NULL;
}
}
#ifdef debug
for( i=0 ; i<nb_player ; printf("%d\n", player_branch[i].lib_strategy_p), i++ );
for( i=0 ; i<nb_player ; printf("%d\n", player_state[i++].ia_lib_p) );
puts("");
#endif
/* appel de start_match */
for( i=0 ; i < nb_player ; i++ ) {
if( player_branch[i].lib_strategy_p != NULL ) {
char* error;
void (*library_start_match_pf)(const unsigned int, const enum player_t);
*(void **) (&library_start_match_pf) = dlsym(player_branch[i].lib_strategy_p, "StartMatch");
if( (error=dlerror()) == NULL ) {
(*library_start_match_pf)(nb_player, i+1);
}
#ifdef debug
else
fprintf(stderr,"function StartMatch not found in stratégie (%d)\n", i);
#endif
}
}
for( i=0 ; i < nb_player ; i++ )
if( player_state[i].ia_lib_p )
ia_call_function(player_state[i], ia_start_match, nb_player, i+1);
for( i=0; i < nb_game ; i++ ) {
while( nb_game-- > 0 ) {
#ifdef debug
fprintf(stderr,"game %d\n", i);
fprintf(stderr,"game %d left\n", nb_game);
#endif
/* appel de start_game */
for( j=0 ; j < nb_player ; j++ ) {
if( player_branch[j].lib_strategy_p != NULL ) {
char* error;
void (*library_start_game_pf)();
*(void **) (&library_start_game_pf) = dlsym(player_branch[j].lib_strategy_p, "StartGame");
if( (error=dlerror()) == NULL ) {
(*library_start_game_pf)();
}
#ifdef debug
else
fprintf(stderr,"function EndMatch not found in stratégie (%d)\n", j);
#endif
for( i=0 ; i < nb_player ; i++ )
if( player_state[i].ia_lib_p )
ia_call_function(player_state[i], ia_start_game);
/* initialisation : on place les pions */
for( i=0 ; i < nb_player ; i++ ) {
player_state[i].play = true;
for( j=0 ; j < 10 ; j++ ) {
player_state[i].pawn[j] = start_position[i][j];
game_state.board[start_position[i][j]] = i+1;
}
}
/* on place les pions */
for( j=0 ; j < nb_player ; j++ )
for( k=0 ; k < 10 ; k++ ) {
player_branch[j].pawn[k] = start[j][k];
game_state.board[start[j][k]] = j+1;
}
/* chaque joueur joue */
@ -163,26 +177,14 @@ int main(int argc, char** argv) {
/* appel de end_match */
for( i=0 ; i < nb_player ; i++ ) {
if( player_branch[i].lib_strategy_p != NULL ) {
char* error;
void (*library_end_match_pf)();
*(void **) (&library_end_match_pf) = dlsym(player_branch[i].lib_strategy_p, "EndMatch");
if( (error=dlerror()) == NULL ) {
(*library_end_match_pf)();
}
#ifdef debug
else
fprintf(stderr,"function EndMatch not found in stratégie (%d)\n", i);
#endif
for( i=0 ; i < nb_player ; i++ )
if( player_state[i].ia_lib_p ) {
ia_call_function(player_state[i], ia_end_match);
/* on décharge toutes les stratégies */
dlclose(player_state[i].ia_lib_p);
}
}
/* on décharge toutes les stratégies */
for(i=0; i < nb_player ; i++)
if(player_branch[i].lib_strategy_p)
dlclose(player_branch[i].lib_strategy_p);
return 0;
}

Loading…
Cancel
Save