Browse Source

modification de « ia_call_functions » pour gérer les valeurs de retour.

master
Bastien 8 years ago
committed by Squiz
parent
commit
ddabefa41a
2 changed files with 31 additions and 21 deletions
  1. +6
    -6
      dameschinoises.patch
  2. +25
    -15
      main.c

+ 6
- 6
dameschinoises.patch View File

@ -1,5 +1,5 @@
--- dameschinoises.h 2012-12-16 22:46:55.000000000 +0100
+++ api.h 2012-12-22 08:46:12.000000000 +0100
+++ api.h 2012-12-28 09:21:38.000000000 +0100
@@ -1,80 +1,65 @@
-/////////// Strutures à utiliser
+/* Strutures à utiliser */
@ -33,13 +33,13 @@
-// Les prototypes en commentaires sont à implémenter dans la librairie
-// Les types pointeur de fonction dans l'application
+/* État d'une case du jeu */
+enum player_t { none, color1, color2, color3, color4, color5, color6 };
+enum hole_t { none, color1, color2, color3, color4, color5, color6 };
-/**
- * Initialiser la librairie
+/* Structure de représentation du jeu avec son plateau */
+struct game_state_t {
+ enum player_t board[121]; /* Tableau contenants les zones où les pions peuvent se trouver */
+ enum hole_t board[121]; /* Tableau contenants les zones où les pions peuvent se trouver */
+};
+
+/* Structure de représentation d'un mouvement */
@ -49,7 +49,7 @@
+};
+
+/* Compatibilité */
+typedef enum player_t EPlayer;
+typedef enum hole_t EPlayer;
+typedef struct game_state_t SGameState;
+typedef struct move_t SMove;
+
@ -77,8 +77,8 @@
*/
-//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); */
+typedef void (*pfStartMatch)(const unsigned int, const enum player_t);
+/* void StartMatch(const unsigned int nbJoueur, const enum hole_t couleur); */
+typedef void (*pfStartMatch)(const unsigned int, const enum hole_t);
-/**
- * Initialiser l'IA pour une manche (d'un match)


+ 25
- 15
main.c View File

@ -16,7 +16,7 @@ enum direction_t { left, right, up_left, up_right, down_left, down_right };
enum api_function_t {ia_lib_init, ia_start_match, ia_start_game, ia_end_game , ia_end_match, ia_next_move};
struct player_state_t {
struct player_t {
size_t branch;
unsigned int error;
void *ia_lib_p;
@ -29,13 +29,15 @@ enum bool char_to_int(char *argv, int *nombre) {
return (error!=argv);
}
void ia_call_function(const struct player_state_t player_state, enum api_function_t api_function, ...) {
enum bool ia_call_function(const struct player_t player, enum api_function_t api_function, void *result, ...) {
va_list ap;
char* error;
enum bool res;
char* function_name;
void (*ia_function_pf)();
int a; enum player_t b;
va_start (ap, api_function);
int a; enum hole_t b;
void *c, *d;
va_start(ap, result);
switch(api_function) {
case ia_lib_init:
function_name = "InitLibrary";
@ -56,7 +58,7 @@ void ia_call_function(const struct player_state_t player_state, enum api_functio
function_name = "NextMove";
break;
}
*(void **) (&ia_function_pf) = dlsym(player_state.ia_lib_p, function_name);
*(void **) (&ia_function_pf) = dlsym(player.ia_lib_p, function_name);
if( (error=dlerror()) == NULL ) {
switch(api_function) {
case ia_lib_init:
@ -64,19 +66,27 @@ void ia_call_function(const struct player_state_t player_state, enum api_functio
break;
case ia_start_match:
a = va_arg(ap, int);
b = va_arg(ap, enum player_t);
b = va_arg(ap, enum hole_t);
(*ia_function_pf)(a, b);
break;
case ia_next_move:
c = va_arg(ap, struct game_state_t*);
a = va_arg(ap, int);
d = va_arg(ap, struct move_t*);
*((int*) result) = (*(int(*)())ia_function_pf)(c, a, d); /* berk */
default:
(*ia_function_pf)();
break;
}
}
res = true;
} else {
#ifdef debug
else
fprintf(stderr,"function \"%s\" not found in strategy.\n", function_name);
#endif
res = false;
}
va_end(ap);
return res;
}
/* pour associer une branche de l'étoile à un joueur */
@ -138,7 +148,7 @@ ssize_t move_calculation(size_t index, enum direction_t move) {
}
}
enum bool valid_move(const struct move_t move, const struct player_state_t player, const struct game_state_t game) {
enum bool valid_move(const struct move_t move, const struct player_t player, const struct game_state_t game) {
size_t j;
ssize_t dest, dest2;
if(game.board[move.start_pos] != (player.branch)+1)
@ -170,7 +180,7 @@ enum bool valid_move(const struct move_t move, const struct player_state_t playe
return false;
}
enum bool winner(struct player_state_t player, const size_t start_position[6][10], const struct game_state_t game) {
enum bool winner(struct player_t player, const size_t start_position[6][10], const struct game_state_t game) {
size_t final_branch, j;
final_branch = player.branch+(player.branch>2?-3:3);
j = 0;
@ -188,7 +198,7 @@ int main(int argc, char** argv) {
{111, 112, 113, 114, 115, 116, 117, 118, 119, 120},
{65, 75, 76, 86, 87, 88, 98, 99, 100, 101},
{10, 11, 12, 13, 23, 24, 25, 35, 36, 46}};
struct player_state_t player_state[6];
struct player_t player_state[6];
struct game_state_t game_state;
int nb_game, nb_player;
enum bool win;
@ -237,7 +247,7 @@ int main(int argc, char** argv) {
return 5;
} else
/* on initialise la bibliothèque */
ia_call_function(player_state[i], ia_lib_init, "toto");
ia_call_function(player_state[i], ia_lib_init, NULL, "toto");
j++;
} else {
/* on ajoute un joueur réel */
@ -256,7 +266,7 @@ int main(int argc, char** argv) {
/* appel de start_match */
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);
ia_call_function(player_state[i], ia_start_match, NULL, nb_player, i+1);
while( nb_game-- > 0 ) {
@ -267,7 +277,7 @@ int main(int argc, char** argv) {
/* appel de start_game */
for( i=0 ; i < nb_player ; i++ )
if( player_state[i].ia_lib_p )
ia_call_function(player_state[i], ia_start_game);
ia_call_function(player_state[i], ia_start_game, NULL);
/* initialisation du plateau */
for(i=0; i < 121; game_state.board[i++] = none);
@ -304,7 +314,7 @@ int main(int argc, char** argv) {
/* appel de end_match */
for( i=0 ; i < nb_player ; i++ )
if( player_state[i].ia_lib_p ) {
ia_call_function(player_state[i], ia_end_match);
ia_call_function(player_state[i], ia_end_match, NULL);
/* on décharge toutes les stratégies */
dlclose(player_state[i].ia_lib_p);
}


Loading…
Cancel
Save