Browse Source

Merge branch 'master' into ai

master
Stéphane 8 years ago
parent
commit
2fc4e5f0a5
1 changed files with 34 additions and 21 deletions
  1. +34
    -21
      main.c

+ 34
- 21
main.c View File

@ -38,8 +38,8 @@ enum bool ia_call_function(const struct player_t player, const enum api_function
enum bool res;
char* function_name;
void (*ia_function_pf)();
int a; enum hole_t b;
void *c, *d;
unsigned int a; int b; enum hole_t c;
void *d, *e;
if(!player.ia_lib_p) {
#ifdef debug
fputs("the player is not a strategy.\n",stderr);
@ -75,15 +75,15 @@ enum bool ia_call_function(const struct player_t player, const enum api_function
(*ia_function_pf)(va_arg (ap, char*));
break;
case ia_start_match:
a = va_arg(ap, int);
b = va_arg(ap, enum hole_t);
(*ia_function_pf)(a, b);
a = va_arg(ap, unsigned int);
c = va_arg(ap, enum hole_t);
(*ia_function_pf)(a, c);
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 */
d = va_arg(ap, struct game_state_t*);
b = va_arg(ap, int);
e = va_arg(ap, struct move_t*);
*((int*) result) = (*(int(*)())ia_function_pf)(d, b, e); /* berk */
break;
default: /* par défaut la fonction est de type void sans paramètres */
(*ia_function_pf)();
@ -92,7 +92,7 @@ enum bool ia_call_function(const struct player_t player, const enum api_function
res = true;
} else {
#ifdef debug
fprintf(stderr,"function \"%s\" not found in strategy.\n", function_name);
fprintf(stderr,"function \"%s\" not found in strategy. (%s)\n", function_name, error);
#endif
res = false;
}
@ -164,7 +164,7 @@ enum bool search(const size_t *tab, const size_t length, const size_t value) {
first = 0;
last = length-1;
while(first <= last) {
middle = (last-first)/2;
middle = (last+first)/2;
if(tab[middle] == value)
return true;
if(tab[middle] < value)
@ -178,20 +178,34 @@ enum bool search(const size_t *tab, const size_t length, const size_t value) {
enum bool valid_move(const struct move_t move, const struct move_t previous_move, const struct player_t player, const struct game_state_t game, const size_t start_position[6][10], const enum bool last_move) {
size_t j;
ssize_t dest, dest2;
#ifdef debug
puts("=== validation ===");
if(last_move)
puts("last_move");
if(previous_move.start_pos == -1 && previous_move.end_pos == -1)
puts("first_move");
#endif
if(move.start_pos < 0 || move.end_pos < 0 || move.start_pos > 120 || move.end_pos > 120)
return false;
/* on considère que c'est le premier saut (pas de vérification du collage) si le previous_move = (-1,-1) */
if(previous_move.start_pos == -1 && previous_move.end_pos == -1 && game.board[move.start_pos] != player.branch ) /* on vérifie que la case appartient au joueur */
{
printf("la case %d n'appartient pas au joueur\n",move.start_pos);
printf("%d %d\n", game.board[move.start_pos], player.branch);
return false;
}
if(move.start_pos != previous_move.end_pos) /* on vérifie le collage */
if(previous_move.start_pos != -1 && previous_move.end_pos != -1 && move.start_pos != previous_move.end_pos) /* on vérifie le collage */
return false;
if(last_move) /* si c'est le dernier mouvement, on ne stationne pas sur une branche */
if(last_move) { /* si c'est le dernier mouvement, on ne stationne pas sur une branche */
for(j = (((player.branch-1)%3)+1)%6; j!=((player.branch-1)%3) ; j=(j+(j==((player.branch-1)%3)+2?2:1))%6 ) /* ne pas faire les branches de départ et d'arrivée */
if(search(start_position[j],10,move.end_pos))
return false;
}
/* on regarde autour */
j = 0;
@ -290,7 +304,7 @@ int main(int argc, char** argv) {
return 5;
} else
/* on initialise la bibliothèque */
ia_call_function(player_state[i], ia_lib_init, NULL, "toto");
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 */
@ -310,20 +324,19 @@ int main(int argc, char** argv) {
puts("(GUI) Opening window");
struct gui_resource_t gui_res = display_start(gui_res);
/* appel de start_match */
for( i=0 ; i < nb_player ; i++ )
for( i=0 ; i < nb_player ; i++ ) {
/* on associe une branche à chaque joueur */
player_state[i].branch = star_branch(nb_player, i);
/* appel de start_match pour les joueurs présents */
if( player_state[i].ia_lib_p )
ia_call_function(player_state[i], ia_start_match, NULL, nb_player, i+1);
ia_call_function(player_state[i], ia_start_match, NULL, nb_player, player_state[i].branch);
}
while( nb_game-- > 0 ) {
#ifdef debug
fprintf(stderr,"%d %s left\n", nb_game, nb_game>1?"games":"game");
#endif
/* on associe une branche à chaque joueur */
for( i=0 ; i < nb_player ; i++ )
player_state[i].branch = star_branch(nb_player, i);
/* appel de start_game */
for( i=0 ; i < nb_player ; i++ )
if( player_state[i].ia_lib_p )


Loading…
Cancel
Save