Browse Source

On demande un coup au joueur réel tant que le coup fourni n'est pas valide

master
Bastien 8 years ago
committed by Squiz
parent
commit
79d1621ca1
1 changed files with 73 additions and 67 deletions
  1. +73
    -67
      main.c

+ 73
- 67
main.c View File

@ -297,80 +297,86 @@ int main(int argc, char **argv) {
start_pos_first_move = -1;
game_state_ia_copy = game_state_copy;
/* si on a une stratégie */
if( player_state[i].ia_lib_p )
if( player_state[i].ia_lib_p ) {
ia_call_function(player_state[i], ia_next_move, &next_move, &game_state_ia_copy, first_move, &movement);
else
/* on a un joueur réel */
quit += display_usermove_pawn(&gui_res, &game_state_ia_copy, player_state[i].branch, player_state[i].name, &movement, &next_move);
if(!quit) {
printf("(GUI) Moving pawn %d → hole %d\n", movement.start_pos, movement.end_pos);
if(first_move)
start_pos_first_move = movement.start_pos; /* on retient le point de départ pour éviter les coups qui reviendraient là où on est parti */
/* on cherche à valider le coup */
validation_movement = valid_move(start_pos_first_move, movement, previous_movement, player_state[i], game_state_copy, start_position, !next_move);
#ifdef debug
switch(validation_movement) {
case invalid: fputs("validation → invalide\n", stderr); break;
case jump: fputs("validation → jump\n", stderr); break;
case neighbour_valid: fputs("validation → voisin\n", stderr); break;
case jump_valid: fputs("validation → commit jump\n", stderr); break;
}
#endif
/* on effectue le mouvement à l'écran */
if(validation_movement != jump_valid && movement.start_pos != movement.end_pos && movement.start_pos >= 0 && movement.end_pos >= 0 && movement.start_pos < 121 && movement.end_pos < 121 && player_state[i].ia_lib_p)
quit += display_animove_pawn(&gui_res, game_state_copy, player_state[i].branch, player_state[i].name, movement.start_pos, movement.end_pos);
/* on regarde le résultat de la validation */
if(validation_movement == invalid) {
if(!quit) {
printf("(GUI) Moving pawn %d → hole %d\n", movement.start_pos, movement.end_pos);
if(first_move)
start_pos_first_move = movement.start_pos; /* on retient le point de départ pour éviter les coups qui reviendraient là où on est parti */
/* on cherche à valider le coup */
validation_movement = valid_move(start_pos_first_move, movement, previous_movement, player_state[i], game_state_copy, start_position, !next_move);
#ifdef debug
fprintf(stderr, "mouvement (%d,%d) invalide\n",movement.start_pos, movement.end_pos);
#endif
if( (buffer = (char*) malloc( 28 *sizeof(char) ) ) == NULL)
display_animsg(&gui_res, &game_state, player_state[i].branch, player_state[i].name, "Coup non valide", 1000);
else {
snprintf(buffer, 28, "Coup non valide (%d erreur%s)", player_state[i].error+1, (player_state[i].error+1>1)?"s":"");
display_animsg(&gui_res, &game_state, player_state[i].branch, player_state[i].name, buffer, 1500);
free(buffer);
}
if(++(player_state[i].error)==3) {
nb_player_end++;
puts("perdu");
/* on enlève les pions du joueur */
j=0; k=0;
do {
if(game_state.board[j] == player_state[i].branch) {
game_state.board[j] = none;
k++;
}
} while(++j < 121 && k < 10);
if( player_state[i].ia_lib_p )
ia_call_function(player_state[i], ia_end_game, NULL);
switch(validation_movement) {
case invalid: fputs("validation → invalide\n", stderr); break;
case jump: fputs("validation → jump\n", stderr); break;
case neighbour_valid: fputs("validation → voisin\n", stderr); break;
case jump_valid: fputs("validation → commit jump\n", stderr); break;
}
}
if(validation_movement == jump || validation_movement == neighbour_valid) {
/* on effectue le mouvement */
#ifdef debug
fprintf(stderr, "mouvement (%d,%d) valide\n",movement.start_pos, movement.end_pos);
#endif
game_state_copy.board[movement.start_pos]=none;
game_state_copy.board[movement.end_pos]=player_state[i].branch;
previous_movement=movement;
}
if(validation_movement == jump_valid || validation_movement == neighbour_valid) {
/* commit */
#ifdef debug
fputs("commit\n",stderr);
#endif
game_state=game_state_copy; /* commit */
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;
if( player_state[i].ia_lib_p )
ia_call_function(player_state[i], ia_end_game, NULL);
/* on effectue le mouvement à l'écran */
if(validation_movement != jump_valid && movement.start_pos != movement.end_pos && movement.start_pos >= 0 && movement.end_pos >= 0 && movement.start_pos < 121 && movement.end_pos < 121 && player_state[i].ia_lib_p)
quit += display_animove_pawn(&gui_res, game_state_copy, player_state[i].branch, player_state[i].name, movement.start_pos, movement.end_pos);
/* on regarde le résultat de la validation */
if(validation_movement == invalid) {
#ifdef debug
fprintf(stderr, "mouvement (%d,%d) invalide\n",movement.start_pos, movement.end_pos);
#endif
if( (buffer = (char*) malloc( 28 *sizeof(char) ) ) == NULL)
display_animsg(&gui_res, &game_state, player_state[i].branch, player_state[i].name, "Coup non valide", 1000);
else {
snprintf(buffer, 28, "Coup non valide (%d erreur%s)", player_state[i].error+1, (player_state[i].error+1>1)?"s":"");
display_animsg(&gui_res, &game_state, player_state[i].branch, player_state[i].name, buffer, 1500);
free(buffer);
}
if(++(player_state[i].error)==3) {
nb_player_end++;
puts("perdu");
/* on enlève les pions du joueur */
j=0; k=0;
do {
if(game_state.board[j] == player_state[i].branch) {
game_state.board[j] = none;
k++;
}
} while(++j < 121 && k < 10);
if( player_state[i].ia_lib_p )
ia_call_function(player_state[i], ia_end_game, NULL);
}
}
}
first_move=0;
} else {
/* on a un joueur réel, on demande un coup tant qu'il n'est pas valide */
do {
quit += display_usermove_pawn(&gui_res, &game_state_ia_copy, player_state[i].branch, player_state[i].name, &movement, &next_move);
validation_movement = valid_move(start_pos_first_move, movement, previous_movement, player_state[i], game_state_copy, start_position, !next_move);
if(validation_movement == invalid && !quit)
display_animsg(&gui_res, &game_state, player_state[i].branch, player_state[i].name, "Coup non valide, reessayer", 1000);
} while(validation_movement == invalid && !quit);
}
if(validation_movement == jump || validation_movement == neighbour_valid) {
/* on effectue le mouvement */
#ifdef debug
fprintf(stderr, "mouvement (%d,%d) valide\n",movement.start_pos, movement.end_pos);
#endif
game_state_copy.board[movement.start_pos]=none;
game_state_copy.board[movement.end_pos]=player_state[i].branch;
previous_movement=movement;
}
if(validation_movement == jump_valid || validation_movement == neighbour_valid) {
/* commit */
#ifdef debug
fputs("commit\n",stderr);
#endif
game_state=game_state_copy; /* commit */
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;
if( player_state[i].ia_lib_p )
ia_call_function(player_state[i], ia_end_game, NULL);
}
}
first_move=0;
} while(validation_movement == jump && nb_player_end < nb_player-1 && !quit);
/* on sélectionne le joueur suivant */
j=i;


Loading…
Cancel
Save