Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define clear_linux() printf("\033[H\033[J")
- #define clear_windows() system("cls")
- #define clear() clear_windows()
- #define TAILLE_GRILLE 11
- #define TAILLE_VALIDATION 5
- #define SAVEFILE "save.txt"
- #define EQUIPE_1 'X'
- #define EQUIPE_2 'O'
- #define FILE_EXISTS 1
- int score1;
- int score2;
- int fin_partie = 0;
- char grille [TAILLE_GRILLE][TAILLE_GRILLE];
- void intro();
- void creationTab();
- int player_move(int joueur);
- int _winner(char c);
- int winner();
- int init_grille() {
- int i;
- for (i = 0; i < TAILLE_GRILLE; i++) {
- int j;
- for (j = 0; j < TAILLE_GRILLE; j++)
- grille[i][j] = '*';
- }
- }
- void save() {
- if (fin_partie) {
- return;
- }
- FILE* f = fopen(SAVEFILE, "w");
- for (int i = 0; i < TAILLE_GRILLE; i++) {
- int j;
- for (j = 0; j < TAILLE_GRILLE; j++) {
- char to_write = grille[i][j];
- fwrite(&to_write, 1, 1, f);
- }
- }
- fclose(f);
- }
- void load() {
- FILE* f = fopen(SAVEFILE, "r");
- for (int i = 0; i < TAILLE_GRILLE; i++) {
- int j;
- for (j = 0; j < TAILLE_GRILLE; j++) {
- if (feof(f)) {
- printf("invalid save file !\n");
- exit(1);
- }
- char rd;
- fread(&rd, 1, 1, f);
- grille[i][j] = rd;
- }
- }
- fclose(f);
- }
- int test_save_file() {
- char valid = 0;
- FILE* f = fopen(SAVEFILE, "r");
- if (f != NULL) {
- fclose(f);
- valid = FILE_EXISTS;
- }
- return valid;
- }
- int main(void){
- init_grille();
- if (test_save_file() == FILE_EXISTS) {
- load();
- remove(SAVEFILE);
- }
- intro();
- }
- void intro(){
- int reponse = 0;
- printf("Un Morpion ça te dit ( 1=oui 0=non)? ");
- scanf("%d",&reponse);
- if (reponse == 0)
- return;
- int tour = 0;
- int match_nul = 0;
- int winneris = 0;
- // boucle de jeu
- do {
- if (tour >= TAILLE_GRILLE*TAILLE_GRILLE) {
- match_nul = 1;
- break;
- }
- clear();
- creationTab();
- player_move((tour % 4) + 1);
- tour++;
- } while ((winneris = winner()) == 0);
- // afficher la grille finale
- clear();
- creationTab();
- if (match_nul) {
- printf("Match nul\n");
- } else {
- printf("Partie finie, l'equipe %d a gagne\n", winneris); // récuperer le joueur qui a gagné
- }
- }
- void creationTab(){
- int i=0;
- int j=0;
- printf("--------Le Morpion-------- \n\n");
- for(i=0; i<TAILLE_GRILLE; i++)
- {
- printf("\n");
- for(j=0; j<TAILLE_GRILLE; j++)
- {
- if (j == 0)
- printf("|");
- printf(" %c |",grille[j][i]);
- }
- }
- printf(" Equipe 1: %d - Equipe 2: %d",score1,score2);
- printf("\n");
- }
- void in(int* x) {
- int succes = scanf("%d", x);
- if (succes == 0) {
- char c;
- scanf("%c", &c);
- if (c == 's') {
- save();
- printf("partie sauveguardee !\n");
- exit(0);
- }
- }
- fseek( stdin, 0, SEEK_END );
- }
- void get_coordinates(int* x, int* y, int joueur) {
- int mauvaise_coordonnee = 0;
- do {
- if (mauvaise_coordonnee) {
- printf("Coordonnees invalides, veuillez recommencer\n");
- }
- printf("Joueur %d :déplacement X: \n", joueur);
- in(x);
- printf("Joueur %d :déplacement Y: \n", joueur);
- in(y);
- mauvaise_coordonnee = 1;
- } while ((*x < 0 || *x >= TAILLE_GRILLE) || (*y < 0 || *y >= TAILLE_GRILLE));
- }
- int player_move(int joueur){
- int x,y;
- do {
- get_coordinates(&x, &y, joueur);
- if (grille[x][y] != '*')
- printf("Case déjà occupée,recommence autre part \n");
- } while (grille[x][y] != '*');
- grille[x][y]= (joueur % 2) == 1 ? EQUIPE_1 : EQUIPE_2;
- }
- int winner() {
- if (_winner(EQUIPE_1)==3) {
- return 1;
- } else if (_winner(EQUIPE_2)==3) {
- return 2;
- }
- if (_winner(EQUIPE_1)<=3)
- score1 = _winner(EQUIPE_1);
- if (_winner(EQUIPE_2)<=3)
- score2 = _winner(EQUIPE_2);
- return 0;
- }
- int _winner(char c) {
- // diagonale droite
- int score =0;
- int x1;
- int x2;
- int x3;
- int x4;
- for (x1 = 0; x1 < TAILLE_GRILLE - TAILLE_VALIDATION + 1; x1++) {
- int y;
- for (y = 0; y < TAILLE_GRILLE - TAILLE_VALIDATION + 1; y++) {
- int sum = 0;
- int i;
- for (i = 0; i < TAILLE_VALIDATION; i++) {
- if (grille[x1 + i][y + i] == c) {
- sum++;
- } else
- break;
- }
- if (sum == TAILLE_VALIDATION) {
- score++;
- }
- }
- }
- // diagonale gauche
- for (x2 = TAILLE_VALIDATION - 1; x2 < TAILLE_GRILLE; x2++) {
- int y;
- for (y = 0; y < TAILLE_GRILLE - TAILLE_VALIDATION + 1; y++) {
- int sum = 0;
- int i;
- for (i = 0; i < TAILLE_VALIDATION; i++) {
- if (grille[x2 - i][y + i] == c) {
- sum++;
- } else
- break;
- }
- if (sum == TAILLE_VALIDATION) {
- score++;
- }
- }
- }
- //horizontal
- for (x3 = 0; x3 < TAILLE_GRILLE - TAILLE_VALIDATION + 1; x3++) {
- int y;
- for (y = 0; y < TAILLE_GRILLE; y++) {
- int sum = 0;
- int i;
- for (i = 0; i < TAILLE_VALIDATION; i++) {
- if (grille[x3 + i][y] == c) {
- sum++;
- } else
- break;
- }
- if (sum == TAILLE_VALIDATION) {
- score++;
- }
- }
- }
- //vertical
- for (x4 = 0; x4 < TAILLE_GRILLE; x4++) {
- int y;
- for (y = 0; y < TAILLE_GRILLE - TAILLE_VALIDATION + 1; y++) {
- int sum = 0;
- int i;
- for (i = 0; i < TAILLE_VALIDATION; i++) {
- if (grille[x4][y + i] == c) {
- sum++;
- } else
- break;
- }
- if (sum == TAILLE_VALIDATION) {
- score++;
- }
- }
- }
- return score;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement