diff --git a/src/smartAI.c b/src/smartAI.c index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..411a919250276ff72ebb26f7d44748f2ba720cf3 100644 --- a/src/smartAI.c +++ b/src/smartAI.c @@ -0,0 +1,120 @@ +#include "smartAI.h" + +int is_possible_to_win(struct board *board){ + int no_line = -1; + for(int i = 0; i < board->col; i++){ + //printf("Colonne %d : %d\n", i, no_line); + no_line = current_line(*board, i); + //printf("Colonne %d : %d\n", i, no_line); + + //printf("%d\n", no_line); + if(board->data[no_line][i] == Vide){ + board->data[no_line][i] = Cercle; + board->last_symbole = Cercle; + board->last_pos_x = no_line; + board->last_pos_y = i; + //printf("Case %d, Symbole %d, x %d, y %d\n\n", board.data[no_line][i], board.last_symbole, board.last_pos_x, board.last_pos_y); + if(winner(*board)){ + return 0; + } + board->data[no_line][i] = Vide; + board->last_symbole = Vide; + board->last_pos_x = -1; + board->last_pos_y = -1; + } + } + + return -1; +} + +int is_possible_to_block_player(struct board *board){ + int no_line = -1; + for(int i = 0; i < board->col; i++){ + no_line = current_line(*board, i); + //printf("%d\n", no_line); + if(board->data[no_line][i] == Vide){ + board->data[no_line][i] = Croix; + board->last_symbole = Croix; + board->last_pos_x = no_line; + board->last_pos_y = i; + + if(winner(*board)){ + board->data[no_line][i] = Cercle; + board->last_symbole = Cercle; + board->last_pos_x = no_line; + board->last_pos_y = i; + return 0; + } + board->data[no_line][i] = Vide; + board->last_symbole = Vide; + board->last_pos_x = -1; + board->last_pos_y = -1; + } + } + return -1; +} + + +void play_with_smartAI(struct board board){ + int no_col = 0; + int no_line = 0; + int no_round = 1; + while(!is_full_board(board)){ + if(no_round % 2 != 0){ + // printf("mmmmmmmmmmmmmmmm\n\n\n\n\n\n\n\n\n\n"); + printf("Column number? (starts at 1):"); + scanf("%d", &no_col); + printf("\n"); + no_line = current_line(board,no_col - 1); + board.data[no_line][no_col - 1] = Croix; + board.last_symbole = Croix; + } + else{ + if(is_possible_to_win(&board) != -1){ + print_game(&board); + printf("Computer won!\n"); + break; + } + + if(is_possible_to_block_player(&board) != -1){ + print_game(&board); + no_round++; + + //printf("kasjh\n\n\n\n"); + continue; + } + no_col = rand() % 7; + //printf("Colonne %d : %d\n", no_col, no_line); + //printf("%d\n", no_line); + no_line = current_line(board,no_col); + //printf("Colonne %d : %d\n", no_col, no_line); + //printf("skdjgbnlkd\n\n\n\n\n\n\n\n\n\n"); + + board.data[no_line][no_col] = Cercle; + board.last_symbole = Cercle; + // printf("skdjgbnlkd\n\n\n\n\n\n\n\n\n\n"); + } + board.last_pos_x = no_line; + board.last_pos_y = no_col - 1; + print_game(&board); + // for(int i = 0; i< board.line;i++){ + // for(int j = 0; j<board.col;j++){ + // printf("%d ", board.data[i][j]); + // } + // printf("\n"); + // } + if(winner(board)){ + if(board.last_symbole == Croix){ + printf("Player one won!\n"); + } + else if(board.last_symbole == Cercle){ + printf("Computer won!\n"); + } + break; + } + no_round++; + } + if(is_full_board(board)){ + printf("It is a draw.\n"); + } +} \ No newline at end of file