diff --git a/puissance4_GRB/main.c b/puissance4_GRB/main.c index 57c647813a755967808f0d664725fc42372a40cf..bebb23b9da1ccb990e88c39dbabeeb177c74d091 100644 --- a/puissance4_GRB/main.c +++ b/puissance4_GRB/main.c @@ -2,11 +2,11 @@ #include <stdlib.h> #include <stdint.h> #include <stdbool.h> -#include <puissance4.h> +#include "puissance4.h" int main(int argc, char** argv){ - if((argc) > 1 && (argc <= 3)){ + if((argc < 2) || (argc > 3)){ printf("il manque des arguments\n"); return EXIT_FAILURE; } @@ -18,8 +18,10 @@ int main(int argc, char** argv){ width = atoi(argv[2]); } - - + init_puissance4(height, width); + //print_gameCells(); + int win = Launch_puissance4(); + printf("\nle winner c'est ça la : %d\n",win); return EXIT_SUCCESS; } diff --git a/puissance4_GRB/puissance4.c b/puissance4_GRB/puissance4.c index e0a5a958660a9beed585c716c8c8b85a15babe26..d325d2c92790e0d8a7c5449590f8c0d201d583ef 100644 --- a/puissance4_GRB/puissance4.c +++ b/puissance4_GRB/puissance4.c @@ -29,3 +29,207 @@ struct cell **Create_grid2D(){ return cells; } +void print_cells(struct cell **cell){ + for(int i=0; i<game.height; i++){ + for(int j=0; j<game.width; j++){ + printf("%d, ", cell[i][j].symbol); + } + printf("\n"); + } +} +void print_gameCells(){ + print_cells(game.cells); +} + +void init_puissance4(int height, int width){ + game.players[1].check_win = 0; + game.players[1].score = 0; + game.players[1].symbol = CROSS; + + game.players[0].check_win = 0; + game.players[0].score = 0; + game.players[0].symbol = CIRCLE; + + game.curr_player = 0; + game.height = height; + game.width = width; + game.gamePlayed = height*width; + game.cells = Create_grid2D(); +} + +void cell_destroy(struct cell **cells, int height){ + for(int i=0; i<height; i++){ + free(cells[i]); + } + free(cells); +} +int kill_game(){ + cell_destroy(game.cells, game.height); + return EXIT_SUCCESS; +} + +/*void Show_grid(struct cell** cell){ + //return 0; +}*/ + + +int put_free_cell(int j_p, int i, symbol_t symbol){ + if(game.cells[j_p][i].symbol == EMPTY){ + //put the player's symbol + game.cells[j_p][i].symbol = symbol; + }else{ + if(i<=game.height){ + return put_free_cell(j_p, i+1, symbol); + }else{ + return -1; + } + } + return i; +} + +int Launch_puissance4(){ + symbol_t winner=EMPTY; + int chiffre=0; + + while(winner==EMPTY){ + //print_grille(); + print_gameCells(); + printf("au tour de %d de jouer\n", game.curr_player); + printf("balance un chiffre entre 0 et %d\n", game.width); + scanf("%d",&chiffre); + + game.gamePlayed -= 1; + int i = 0; + //verifier que put free cell fonctionne + if((i = put_free_cell(chiffre, 0, game.players[game.curr_player].symbol))<0){ + //la grille est pleine genre... ou y a un bug de zinzin + if(Is_Grid_full()){ + //la grille est pleine sa mere la pute + //print egalite mais y a probablement eu un bug + printf("y a un egalite mais probablement un bug aussi mdr\n"); + //fin du jeu + return EQUAL; //jeu egal + }else{ + //il y a un bug de zinzin + printf("ok la y a un bug de zinzin check ca \n"); + //print la grille pour debug + print_gameCells(); + //fin du jeu + return -1; //erreur + } + } + //check de la win + winner = Find_winner(game.cells, game.cells[chiffre][i]); + if( winner != EMPTY){ + if(winner==CROSS){ + //reset du jeu + mise a jour des scores (scores optionels) + printf("la croix a gagnée cette connasse\n"); + //print joueur croix a gagné + //fin du jeu + return CROSS; + } + if(winner==CIRCLE){ + //reset du jeu + mise a jour des scores (scores optionels) + //print joueur cercle a gagné + printf("le cercle a gagné cet enorme zgeg\n"); + //fin du jeu + return CIRCLE; + } + }else{ + //sois le jeu est plein, sois y a juste pas encore de winner + if(Is_Grid_full()){ + //la grille est pleine + //print egalite + printf("y a un egalite dans les regles de l'art mash'allah\n"); + //winner = EQUAL; + //fin du jeu + return EQUAL; + } + //y a juste pas encore de winner ducoup... + game.curr_player ^= 1; //switch de joueur + printf("au tour de %d de jouer\n", game.curr_player); + } + } + return winner;//boucle sa mere... jusqu'à ce que la grille soit pleine, ou que un winner a été détecté +} + +bool Is_Grid_full(){ + return game.gamePlayed == 0; +} + +/*********************************** + * function : Find_winner + * arguments : grid[][], + * cellule jouée, + * taille de grille (ex: 3 pour 3x3) + * + * return value : symbole -> gagnant ou EMPTY + * Cherche si il y a un gagnant, + * (cherche des suites de cases gagnantes) + * !!! Fonctionne avec CheckWin_in_a_direction !!! + ***********************************/ +symbol_t Find_winner(struct cell **grid, struct cell cellPlayed){ + //int grid_len = game.height*game.width; + int k[4][2]={ + {0,1}, + {1,0}, + {1,-1}, + {1,1} + }; + int k2[4][2]={ + {0,-1}, + {-1,0}, + {-1,1}, + {-1,-1} + }; + symbol_t result; + for(int i=0; i<4; i++){ + //à savoir, il est impossible que le cellPlayed soit empty !! + result = CheckWin_in_a_direction(k[i], grid, grid[cellPlayed.i_pos][cellPlayed.j_pos]); + if(result != EMPTY){return result;} + result = CheckWin_in_a_direction(k2[i], grid, grid[cellPlayed.i_pos][cellPlayed.j_pos]); + game.players[0].check_win = 0; + game.players[1].check_win = 0; + if(result != EMPTY){ + return result; //si il trouve un winner, alors il stoppe la fonction :) + } + } + return EMPTY; // pas de winner pour l'instant +} + +/*********************************** + * function : CheckWin_in_a_direction + * arguments : grid[][], + * cellule jouée, + * taille de grille (ex: 3 pour 3x3) + * + * return value : symbole -> gagnant ou EMPTY si non + * + * Va chercher des suites de cases gagnantes + * recursivement, dans une seule direction + * à l'aide des var globale cross et circle + ***********************************/ +symbol_t CheckWin_in_a_direction(int dir[2], struct cell **grid, struct cell cell){ + int x1 = cell.i_pos+dir[0]; + int x2 = cell.j_pos+dir[1]; + symbol_t result = EMPTY; + + if(x1<game.height && x1>=0 && x2<game.width && x2>=0){ + if(grid[x1][x2].symbol==cell.symbol){ + if(cell.symbol == CIRCLE){ + game.players[0].check_win += 1; + }else{ + game.players[1].check_win += 1; + } + if(game.players[0].check_win>=3){ + return CIRCLE; //circle win + } + else if(game.players[1].check_win>=3){ + return CROSS; //cross win + }else{ + result = CheckWin_in_a_direction(dir, grid, grid[x1][x2]); + } + } + } + return result; +} \ No newline at end of file