Skip to content
Snippets Groups Projects
Commit d9a21531 authored by Sabrina's avatar Sabrina
Browse files

Mode smartAI fonctionnel

parent 0f1c6061
No related branches found
No related tags found
No related merge requests found
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
......
No preview for this file type
......@@ -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;
......
......@@ -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);
......
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment