Advertisement
Guest User

morpion

a guest
Dec 5th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define clear_linux() printf("\033[H\033[J")
  5. #define clear_windows() system("cls")
  6. #define clear() clear_windows()
  7.  
  8.  
  9. #define TAILLE_GRILLE 11
  10. #define TAILLE_VALIDATION 5
  11. #define SAVEFILE "save.txt"
  12.  
  13. #define EQUIPE_1 'X'
  14. #define EQUIPE_2 'O'
  15.  
  16. #define FILE_EXISTS 1
  17.  
  18.  
  19. int score1;
  20. int score2;
  21. int fin_partie = 0;
  22. char grille [TAILLE_GRILLE][TAILLE_GRILLE];
  23. void intro();
  24. void creationTab();
  25. int player_move(int joueur);
  26.  
  27. int _winner(char c);
  28. int winner();
  29.  
  30. int init_grille() {
  31.     int i;
  32.     for (i = 0; i < TAILLE_GRILLE; i++) {
  33.     int j;
  34.         for (j = 0; j < TAILLE_GRILLE; j++)
  35.             grille[i][j] = '*';
  36.     }
  37. }
  38.  
  39. void save() {
  40.     if (fin_partie) {
  41.         return;
  42.     }
  43.  
  44.     FILE* f = fopen(SAVEFILE, "w");
  45.  
  46.     for (int i = 0; i < TAILLE_GRILLE; i++) {
  47.         int j;
  48.         for (j = 0; j < TAILLE_GRILLE; j++) {
  49.             char to_write = grille[i][j];
  50.             fwrite(&to_write, 1, 1, f);
  51.         }
  52.     }
  53.  
  54.     fclose(f);
  55. }
  56.  
  57. void load() {
  58.     FILE* f = fopen(SAVEFILE, "r");
  59.  
  60.     for (int i = 0; i < TAILLE_GRILLE; i++) {
  61.         int j;
  62.         for (j = 0; j < TAILLE_GRILLE; j++) {
  63.             if (feof(f)) {
  64.                 printf("invalid save file !\n");
  65.                 exit(1);
  66.             }
  67.  
  68.             char rd;
  69.             fread(&rd, 1, 1, f);
  70.             grille[i][j] = rd;
  71.         }
  72.     }
  73.  
  74.     fclose(f);
  75. }
  76.  
  77. int test_save_file() {
  78.     char valid = 0;
  79.  
  80.     FILE* f = fopen(SAVEFILE, "r");
  81.  
  82.     if (f != NULL) {
  83.         fclose(f);
  84.         valid = FILE_EXISTS;
  85.     }
  86.  
  87.     return valid;
  88. }
  89.  
  90. int main(void){
  91.     init_grille();
  92.  
  93.     if (test_save_file() == FILE_EXISTS) {
  94.         load();
  95.         remove(SAVEFILE);
  96.     }
  97.  
  98.     intro();
  99. }
  100.  
  101. void intro(){
  102.     int reponse  = 0;
  103.     printf("Un Morpion ça te dit ( 1=oui 0=non)? ");
  104.     scanf("%d",&reponse);
  105.  
  106.     if (reponse == 0)
  107.         return;
  108.  
  109.     int tour = 0;
  110.     int match_nul = 0;
  111.     int winneris = 0;
  112.  
  113.     // boucle de jeu
  114.     do {
  115.  
  116.         if (tour >= TAILLE_GRILLE*TAILLE_GRILLE) {
  117.             match_nul = 1;
  118.             break;
  119.         }
  120.  
  121.         clear();
  122.         creationTab();
  123.         player_move((tour % 4) + 1);
  124.         tour++;
  125.  
  126.     } while ((winneris = winner()) == 0);
  127.  
  128.     // afficher la grille finale
  129.     clear();
  130.     creationTab();
  131.  
  132.     if (match_nul) {
  133.         printf("Match nul\n");
  134.     } else {
  135.         printf("Partie finie, l'equipe %d a gagne\n", winneris); // récuperer le joueur qui a gagné
  136.     }
  137. }
  138.  
  139. void creationTab(){
  140.   int i=0;
  141.   int j=0;
  142.  
  143.   printf("--------Le Morpion-------- \n\n");
  144.   for(i=0; i<TAILLE_GRILLE; i++)
  145.   {
  146.     printf("\n");
  147.     for(j=0; j<TAILLE_GRILLE; j++)
  148.     {
  149.         if (j == 0)
  150.             printf("|");
  151.  
  152.         printf(" %c |",grille[j][i]);
  153.     }
  154.  
  155.   }
  156.   printf(" Equipe 1: %d - Equipe 2: %d",score1,score2);
  157.   printf("\n");
  158. }
  159.  
  160. void in(int* x) {
  161.     int succes = scanf("%d", x);
  162.  
  163.     if (succes == 0) {
  164.         char c;
  165.         scanf("%c", &c);
  166.  
  167.         if (c == 's') {
  168.             save();
  169.             printf("partie sauveguardee !\n");
  170.             exit(0);
  171.         }
  172.     }
  173.     fseek( stdin, 0, SEEK_END );
  174. }
  175.  
  176. void get_coordinates(int* x, int* y, int joueur) {
  177.     int mauvaise_coordonnee = 0;
  178.  
  179.     do {
  180.  
  181.         if (mauvaise_coordonnee) {
  182.             printf("Coordonnees invalides, veuillez recommencer\n");
  183.         }
  184.  
  185.         printf("Joueur %d :déplacement X: \n", joueur);
  186.         in(x);
  187.  
  188.         printf("Joueur %d :déplacement Y: \n", joueur);
  189.         in(y);
  190.  
  191.         mauvaise_coordonnee = 1;
  192.     } while ((*x < 0 || *x >= TAILLE_GRILLE) || (*y < 0 || *y >= TAILLE_GRILLE));
  193. }
  194.  
  195. int player_move(int joueur){
  196.   int x,y;
  197.  
  198.   do {
  199.     get_coordinates(&x, &y, joueur);
  200.  
  201.     if (grille[x][y] != '*')
  202.         printf("Case déjà occupée,recommence autre part \n");
  203.  
  204.   } while (grille[x][y] != '*');
  205.  
  206.   grille[x][y]= (joueur % 2) == 1 ? EQUIPE_1 : EQUIPE_2;
  207. }
  208.  
  209. int winner() {
  210.     if (_winner(EQUIPE_1)==3) {
  211.         return 1;
  212.     } else if (_winner(EQUIPE_2)==3) {
  213.         return 2;
  214.     }
  215.     if (_winner(EQUIPE_1)<=3)
  216.         score1 = _winner(EQUIPE_1);
  217.     if (_winner(EQUIPE_2)<=3)
  218.         score2 = _winner(EQUIPE_2);
  219.  
  220.  
  221.     return 0;
  222. }
  223.  
  224. int _winner(char c) {
  225.     // diagonale droite
  226.     int score =0;
  227.     int x1;
  228.     int x2;
  229.     int x3;
  230.     int x4;
  231.     for (x1 = 0; x1 < TAILLE_GRILLE - TAILLE_VALIDATION + 1; x1++) {
  232.       int y;
  233.       for (y = 0; y < TAILLE_GRILLE - TAILLE_VALIDATION + 1; y++) {
  234.  
  235.         int sum = 0;
  236.         int i;
  237.         for (i = 0; i < TAILLE_VALIDATION; i++) {
  238.           if (grille[x1 + i][y + i] == c) {
  239.             sum++;
  240.           } else
  241.             break;
  242.         }
  243.  
  244.         if (sum == TAILLE_VALIDATION) {
  245.           score++;
  246.         }
  247.       }
  248.     }
  249.  
  250.     // diagonale gauche
  251.     for (x2 = TAILLE_VALIDATION - 1; x2 < TAILLE_GRILLE; x2++) {
  252.       int y;
  253.       for (y = 0; y < TAILLE_GRILLE - TAILLE_VALIDATION + 1; y++) {
  254.  
  255.         int sum = 0;
  256.         int i;
  257.         for (i = 0; i < TAILLE_VALIDATION; i++) {
  258.           if (grille[x2 - i][y + i] == c) {
  259.             sum++;
  260.           } else
  261.             break;
  262.         }
  263.         if (sum == TAILLE_VALIDATION) {
  264.           score++;
  265.         }
  266.       }
  267.     }
  268.  
  269.     //horizontal
  270.     for (x3 = 0; x3 < TAILLE_GRILLE - TAILLE_VALIDATION + 1; x3++) {
  271.       int y;
  272.       for (y = 0; y < TAILLE_GRILLE; y++) {
  273.  
  274.         int sum = 0;
  275.         int i;
  276.         for (i = 0; i < TAILLE_VALIDATION; i++) {
  277.           if (grille[x3 + i][y] == c) {
  278.             sum++;
  279.           } else
  280.             break;
  281.         }
  282.  
  283.         if (sum == TAILLE_VALIDATION) {
  284.           score++;
  285.         }
  286.       }
  287.     }
  288.  
  289.     //vertical
  290.     for (x4 = 0; x4 < TAILLE_GRILLE; x4++) {
  291.       int y;
  292.       for (y = 0; y < TAILLE_GRILLE - TAILLE_VALIDATION + 1; y++) {
  293.  
  294.         int sum = 0;
  295.         int i;
  296.         for (i = 0; i < TAILLE_VALIDATION; i++) {
  297.           if (grille[x4][y + i] == c) {
  298.             sum++;
  299.           } else
  300.             break;
  301.         }
  302.  
  303.         if (sum == TAILLE_VALIDATION) {
  304.           score++;
  305.  
  306.         }
  307.       }
  308.     }
  309.  
  310.  
  311.     return score;
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement