diff --git a/.gitignore b/.gitignore index 34dde8f90cc751103b17e885ecd6c31040d93bc8..89f8b659e00d92ec9917177118fc759a3fa7b97c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.o .vscode +*.cand diff --git a/Makefile b/Makefile index 57c7cd62f3d1b3b3db4cf8b9d50106740c620218..ce0102a8a007d6716a6bc768063176e028381288 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,15 @@ -puissance4: +puissance4: main.o twoPlayers.o board.o @echo "first rule which must create the puissance4 executable" + gcc main.o twoPlayers.o board.o -o puissance4 + +main.o : src/main.c + gcc -Wall -Wextra -c src/main.c + +twoPlayers.o : src/twoPlayers.c src/twoPlayers.h + gcc -Wall -Wextra -c src/twoPlayers.c + +board.o : src/board.c src/board.h + gcc -Wall -Wextra -c src/board.c clean: @echo "this rule must clean everything up (including candidate files in testbed)" diff --git a/puissance4 b/puissance4 index 5f0533d698e72014369b4c325fb941a9beb63c01..3f2d3a5fdd5ecd874d562eadd24386633c2103c9 100755 Binary files a/puissance4 and b/puissance4 differ diff --git a/src/main.c b/src/main.c index 7e10d898bdacb2448adbb6204fc89d26ba118dea..025ac64e76f9459457678a3c4563abaed48062ff 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ int main(int argc, char **argv){ init_board(&board); printf("Board size is %dx%d (rows x col)\n", board.line, board.col); print_game(&board); + play(board); free_board(&board); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/src/twoPlayers.c b/src/twoPlayers.c index 9d6b462c3bb96ef0051dbb0906d092f48994796a..2cfc862f272ab384d5278a66434da66f886d28fd 100644 --- a/src/twoPlayers.c +++ b/src/twoPlayers.c @@ -1,15 +1,188 @@ -#include <stdio.h> -#include <stdlib.h> -#include <stdbool.h> -#include "board.h" - -#ifndef twoplayers_h -#define twoplayers_h -int current_line(struct board board, int no_col); -bool is_full_board(struct board board); -bool row_of_four(struct board *board); -bool diag_of_four_left_right(struct board *board); -bool diag_of_four_right_left(struct board *board); -bool winner(struct board board); -void play(struct board board); -#endif +#include "twoPlayers.h" + +int current_line(struct board board, int no_col){ + int no_line = board.line-1; + while(board.data[no_line][no_col] != Vide){ + no_line--; + } + return no_line; +} + +bool is_full_board(struct board board){ + for(int i = 0; i < board.line; i++){ + for(int j = 0; j < board.col; j++){ + if(board.data[i][j] == Vide){ + return false; + } + } + } + return true; +} + +bool row_of_four(struct board *board){ + int sum = 0; + int i = 0; + int old_i = -1; + while(i < board->col - 1){ + old_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++; + } + else{ + break; + } + i++; + } + if(sum >= 4){ + return true; + } + return false; +} + +bool col_of_four(struct board *board){ + int sum = 0; + int i = board->line -1; + int old_i = -1; + while(i >= 0){ + old_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++; + } + else{ + break; + } + i--; + } + if(sum >= 4){ + return true; + } + 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 + } + } + + // 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 + } + } + + // 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){ + if(row_of_four(&board) || diag_of_four_left_right(&board) || diag_of_four_right_left(&board) || col_of_four(&board)){ + return true; + } + return false; +} + + +void play(struct board board){ + int no_col = 0; + int no_line = 0; + int no_round = 1; + while(!is_full_board(board)){ + printf("Column number? (starts at 1):"); + scanf("%d", &no_col); + printf("\n"); + no_line = current_line(board,no_col - 1); + if(no_round % 2 != 0){ + board.data[no_line][no_col - 1] = Croix; + board.last_symbole = Croix; + } + else{ + board.data[no_line][no_col - 1] = Cercle; + board.last_symbole = Cercle; + } + board.last_pos_x = no_line; + board.last_pos_y = no_col - 1; + print_game(&board); + if(winner(board)){ + if(board.last_symbole == Croix){ + printf("Player one won!\n"); + } + else if(board.last_symbole == Cercle){ + printf("Player two won!\n"); + } + break; + } + no_round++; + } + if(is_full_board(board)){ + printf("It is a draw.\n"); + } +} diff --git a/src/twoPlayers.h b/src/twoPlayers.h index 881b72163d28c66aef664d28e72e65725d6b16ce..9d6b462c3bb96ef0051dbb0906d092f48994796a 100644 --- a/src/twoPlayers.h +++ b/src/twoPlayers.h @@ -5,6 +5,11 @@ #ifndef twoplayers_h #define twoplayers_h -void play(); -bool winner(); +int current_line(struct board board, int no_col); +bool is_full_board(struct board board); +bool row_of_four(struct board *board); +bool diag_of_four_left_right(struct board *board); +bool diag_of_four_right_left(struct board *board); +bool winner(struct board board); +void play(struct board board); #endif diff --git a/testbed/rand_ai/test1.cand b/testbed/rand_ai/test1.cand deleted file mode 100644 index e28c57bd4e8cffd0bfdedc84a65dc03cd6d93011..0000000000000000000000000000000000000000 --- a/testbed/rand_ai/test1.cand +++ /dev/null @@ -1,118 +0,0 @@ -Board size is 6x7 (rows x col) -┌─┬─┬─┬─┬─┬─┬─┠-│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -└─┴─┴─┴─┴─┴─┴─┘ - 1 2 3 4 5 6 7 -Column number? (starts at 1): -┌─┬─┬─┬─┬─┬─┬─┠-│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -└─┴─┴─┴─┴─┴─┴─┘ - 1 2 3 4 5 6 7 -┌─┬─┬─┬─┬─┬─┬─┠-│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│O│ │ │ │ │ │ -└─┴─┴─┴─┴─┴─┴─┘ - 1 2 3 4 5 6 7 -Column number? (starts at 1): -┌─┬─┬─┬─┬─┬─┬─┠-│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│O│ │ │ │ │ │ -└─┴─┴─┴─┴─┴─┴─┘ - 1 2 3 4 5 6 7 -┌─┬─┬─┬─┬─┬─┬─┠-│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│O│ │ │O│ │ │ -└─┴─┴─┴─┴─┴─┴─┘ - 1 2 3 4 5 6 7 -Column number? (starts at 1): -┌─┬─┬─┬─┬─┬─┬─┠-│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│O│ │ │O│ │ │ -└─┴─┴─┴─┴─┴─┴─┘ - 1 2 3 4 5 6 7 -┌─┬─┬─┬─┬─┬─┬─┠-│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│O│O│ │O│ │ │ -└─┴─┴─┴─┴─┴─┴─┘ - 1 2 3 4 5 6 7 -Column number? (starts at 1): -┌─┬─┬─┬─┬─┬─┬─┠-│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│ │ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│ │ │ │ │ │ │ -├─┼─┼─┼─┼─┼─┼─┤ -│X│O│O│ │O│ │ │ -└─┴─┴─┴─┴─┴─┴─┘ - 1 2 3 4 5 6 7 -Player one won!