diff --git a/Makefile b/Makefile index 5615fc1ce77392118090c446908ae509768e3927..b49a908135da642defb095d984fe00f690d15259 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -puissance4: main.o twoPlayers.o board.o randomAI.o winnerCheck.o +puissance4: main.o twoPlayers.o board.o randomAI.o winnerCheck.o smartAI.o @echo "first rule which must create the puissance4 executable" - gcc main.o twoPlayers.o board.o randomAI.o winnerCheck.o -o puissance4 + gcc main.o twoPlayers.o board.o randomAI.o winnerCheck.o smartAI.o -o puissance4 main.o : src/main.c gcc -Wall -Wextra -c src/main.c @@ -17,6 +17,9 @@ randomAI.o : src/randomAI.c src/randomAI.h winnerCheck.o : src/winnerCheck.c src/winnerCheck.h gcc -Wall -Wextra -c src/winnerCheck.c +smartAI.o : src/smartAI.c src/smartAI.h + gcc -Wall -Wextra -c src/smartAI.c + clean: @echo "this rule must clean everything up (including candidate files in testbed)" $(MAKE) -C testbed clean diff --git a/puissance4 b/puissance4 index b3441d6b1a713cd5c12c285222be1208e96776b9..1084deb680f98e69a3ea65796a3dcb24250ff9aa 100755 Binary files a/puissance4 and b/puissance4 differ diff --git a/src/board.c b/src/board.c index 6e4186c817f30b1b34466cde528813e6f1e5e415..20bad7f78d74aa5f8266aeafd3bfc36c3edfd3e9 100644 --- a/src/board.c +++ b/src/board.c @@ -65,8 +65,8 @@ void print_game(struct board *board){ } int current_line(struct board board, int no_col){ - int no_line = board.line-1; - while(board.data[no_line][no_col] != Vide){ + int no_line = board.line - 1; + while (no_line >= 0 && board.data[no_line][no_col] != Vide) { no_line--; } return no_line; diff --git a/src/main.c b/src/main.c index a24f9b047926822dd9ad550ca9a63d75abca950c..3acfb6d1ff875000186c0f3c0015b468bdc42d2d 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,7 @@ int main(int argc, char **argv){ play_with_randomAI(board); break; case 2: - //play_with_smartAI(board); + play_with_smartAI(board); break; case 3: play_two_players(board); diff --git a/src/smartAI.h b/src/smartAI.h index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..99d569d8b50f567e7c23ae47bb2cae11f7184f71 100644 --- a/src/smartAI.h +++ b/src/smartAI.h @@ -0,0 +1,13 @@ +#include <stdlib.h> +#include <stdio.h> +#include "board.h" +#include "winnerCheck.h" + +#ifndef randomAI_h +#define randomAI_h + +int is_possible_to_win(struct board *board); +int is_possible_to_block_player(struct board *board); +void play_with_smartAI(struct board board); + +#endif \ No newline at end of file diff --git a/src/winnerCheck.c b/src/winnerCheck.c index 50d20950e7606c50b96d7336e8f97ee05161f447..dd06dd4a981b5fcef9ae6686a4951f8f31cde878 100644 --- a/src/winnerCheck.c +++ b/src/winnerCheck.c @@ -1,124 +1,58 @@ #include "winnerCheck.h" bool row_of_four(struct board *board){ - int sum = 1; - int i = 0; - int old_i = -1; - while(i < board->col - 1){ - old_i = i; - i++; - if(board->data[board->last_pos_x][i] == board->last_symbole && board->data[board->last_pos_x][old_i] == board->last_symbole){ - sum++; - } - if(sum == 4){ - return true; + for (int col = 0; col < board->col - 3; col++) { + if (board->data[board->last_pos_x][col] != Vide && + board->data[board->last_pos_x][col] == board->data[board->last_pos_x][col+1] && + board->data[board->last_pos_x][col] == board->data[board->last_pos_x][col+2] && + board->data[board->last_pos_x][col] == board->data[board->last_pos_x][col+3]) { + return true; // Il y a une victoire } } return false; } bool col_of_four(struct board *board){ - int sum = 1; - int i = 0; - int old_i = -1; - while(i < board->line -1){ - old_i = i; - i++; - if(board->data[i][board->last_pos_y] == board->last_symbole && board->data[old_i][board->last_pos_y] == board->last_symbole){ - sum++; - } - - if(sum == 4){ - return true; + for (int row = 0; row < board->line - 3; row++) { + if (board->data[row][board->last_pos_y] != Vide && + board->data[row][board->last_pos_y] == board->data[row+1][board->last_pos_y] && + board->data[row][board->last_pos_y] == board->data[row+2][board->last_pos_y] && + board->data[row][board->last_pos_y] == board->data[row+3][board->last_pos_y]) { + return true; // Il y a une victoire } } return false; } bool diag_of_four_left_right(struct board *board){ - int sum = 1; // Nous commençons à 1 car le dernier symbole est déjà compté - int current_x = board->last_pos_x; - int current_y = board->last_pos_y; - int oldx = -1; - int oldy = -1; - - // Vérification de la diagonale supérieure gauche à la diagonale inférieure droite - while (current_x > 0 && current_y > 0) { - oldx = current_x; - oldy = current_y; - current_x--; - current_y--; - - if (board->data[current_x][current_y] == board->last_symbole && board->data[oldx][oldy] == board->last_symbole) { - sum++; // Increment only if we find consecutive symbols + for (int row = 0; row < board->line - 3; row++) { + for (int col = 0; col < board->col - 3; col++) { + if (board->data[row][col] != Vide && + board->data[row][col] == board->data[row+1][col+1] && + board->data[row][col] == board->data[row+2][col+2] && + board->data[row][col] == board->data[row+3][col+3]) { + return true; // Il y a une victoire + } } } - - // Réinitialisation des coordonnées - current_x = board->last_pos_x; - current_y = board->last_pos_y; - - // Vérification de la diagonale inférieure droite à la diagonale supérieure gauche - while (current_x < board->line - 1 && current_y < board->col - 1) { - oldx = current_x; - oldy = current_y; - current_x++; - current_y++; - - if (board->data[current_x][current_y] == board->last_symbole && board->data[oldx][oldy] == board->last_symbole) { - sum++; // Increment only if we find consecutive symbols - } - } - - // Vérifie si sum est exactement égal à 4 - if (sum == 4) { - return true; // We found 4 consecutive symbols - } return false; // No 4 consecutive symbols found } bool diag_of_four_right_left(struct board *board){ - int sum = 1; // On commence à 1 car le dernier symbole est déjà compté - int current_x = board->last_pos_x; - int current_y = board->last_pos_y; - int oldx = -1; - int oldy = -1; - - // Vérification de la diagonale montante gauche à la diagonale descendante droite - while (current_x > 0 && current_y < board->col - 1) { - oldx = current_x; - oldy = current_y; - current_x--; - current_y++; - - if (board->data[current_x][current_y] == board->last_symbole && board->data[oldx][oldy] == board->last_symbole) { - sum++; // Incrémenter seulement si les symboles sont consécutifs - } - } - - // Réinitialisation des coordonnées - current_x = board->last_pos_x; - current_y = board->last_pos_y; - - // Vérification de la diagonale descendante gauche à la diagonale montante droite - while (current_x < board->line - 1 && current_y >= 0) { - oldx = current_x; - oldy = current_y; - current_x++; - current_y--; - - if (board->data[current_x][current_y] == board->last_symbole && board->data[oldx][oldy] == board->last_symbole) { - sum++; // Incrémenter seulement si les symboles sont consécutifs + for (int row = 0; row < board->line - 3; row++) { + for (int col = 3; col < board->col; col++) { + if (board->data[row][col] != Vide && + board->data[row][col] == board->data[row+1][col-1] && + board->data[row][col] == board->data[row+2][col-2] && + board->data[row][col] == board->data[row+3][col-3]) { + return true; // Il y a une victoire + } } } - - // Vérifie si sum est exactement égal à 4 - if (sum == 4) { - return true; // Si 4 symboles consécutifs sont trouvés, retourner true - } return false; // Sinon, retourner false } bool winner(struct board board){ + //printf("kasjh\n\n\n\n"); return row_of_four(&board) || diag_of_four_left_right(&board) || diag_of_four_right_left(&board) || col_of_four(&board); } \ No newline at end of file