Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "function.h"
  4.  
  5. void jouerOrdinateur (char *possibilite, int choixUtilisateur, int nombreDeFois, int *fin);
  6. int modifierTTT (int choixUtilisateur, char *possibilite, int *intPossibilite);
  7. void generateTTT (char *possibilite);
  8.  
  9. int main()
  10. {
  11.     int intPossibilite[9] = {1,2,3,4,5,6,7,8,9};
  12.     char possibilite[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
  13.     int nombreDeFois = 0;
  14.     int fin = 0;
  15.  
  16.     int choixUtilisateur = 0;
  17.     int gagnant = 0, i = 0, j=0,k=0, l=0;
  18.     int partieGagnant[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
  19.     int signeAregarder[2] = {'.', '+'};
  20.     int totalPoint = 0;
  21.     int recommencer = 0;
  22.  
  23.     //Affichage de la version du logiciel
  24.     printf("\n\t\t----------------------------------\n");
  25.     printf("\t\t\t Tic-Tac-To 1.0\n");
  26.     printf("\t\t----------------------------------\n\n");
  27.  
  28.     //CrÈation du tableau qui va afficher les X et O
  29.     generateTTT(possibilite);
  30.  
  31.     do{
  32.  
  33.         printf("\n\n");
  34.  
  35.         do{
  36.             printf("Veuillez entrer votre choix : ");
  37.             scanf("%d", &choixUtilisateur);
  38.         }while(modifierTTT(choixUtilisateur, possibilite, intPossibilite) != 1);
  39.  
  40.         nombreDeFois++;
  41.  
  42.         jouerOrdinateur(possibilite, choixUtilisateur, nombreDeFois, &fin);
  43.  
  44.         for(i=0;i<2;i++){
  45.             for(j=0;j<8;j++){
  46.                 for(k=0;k<3;k++){
  47.                     if(possibilite[partieGagnant[j][k]] == signeAregarder[i]){
  48.                             totalPoint++;
  49.                     }
  50.                 }
  51.                 if(totalPoint == 3 && signeAregarder[i] == '.'){
  52.                     gagnant = 1;
  53.                     break;
  54.                 }else if(totalPoint == 3 && signeAregarder[i] == '+'){
  55.                     gagnant = 2;
  56.                     break;
  57.                 }else{
  58.                     totalPoint=0;
  59.                 }
  60.             }
  61.             if(gagnant > 0)
  62.                 break;
  63.         }
  64.  
  65.         generateTTT(possibilite);
  66.  
  67.     }while(gagnant < 1 && fin != 1);
  68.  
  69.     if(gagnant == 1){
  70.         printf("\n\t\t----------------------------------\n");
  71.         printf("\n\t\t\t Vous avez perdu ! \n\n");
  72.         printf("\t\t----------------------------------\n\n");
  73.     }else if(gagnant == 2){
  74.         printf("\n\t\t----------------------------------\n");
  75.         printf("\n\t\t     Bravo, vous avez gagne ! \n\n");
  76.         printf("\t\t----------------------------------\n\n");
  77.     }else{
  78.         printf("\n\t\t----------------------------------\n");
  79.         printf("\n\t\t\t Partie neutre ! \n\n");
  80.         printf("\t\t----------------------------------\n\n");
  81.     }
  82.  
  83.     printf("Appuyez sur une touche pour quitter le programme...");
  84.     scanf("%d", &recommencer);
  85.  
  86.     return 0;
  87. }
  88.  
  89. int modifierTTT (int choixUtilisateur, char *possibilite, int *intPossibilite){
  90.     int i = 0;
  91.     if(choixUtilisateur <= 9 && choixUtilisateur >= 1){
  92.         for(i = 0; i < 9; i++){
  93.             if(choixUtilisateur == intPossibilite[i]){
  94.                 if(possibilite[i] != '+' && possibilite[i] != '.'){
  95.                     possibilite[i] = '+';
  96.                     return 1;
  97.                     break;
  98.                 }else{
  99.                     printf("-- Deja prit !\n");
  100.                 }
  101.             }
  102.         }
  103.     }else{
  104.         printf("-- Ce choix n'existe pas !\n");
  105.         return 0;
  106.     }
  107. }
  108.  
  109. void jouerOrdinateur (char *possibilite, int choixUtilisateur, int nombreDeFois, int *fin){
  110.     int positionDepart[3][4] = {{1,3,7,9},{2,4,6,8},{5,0,0,0}};
  111.     int i = 0, j = 0, k = 0;
  112.     if(nombreDeFois == 1){
  113.         for(i=0;i<3;i++){
  114.             for(j=0;j<4;j++){
  115.                 if(choixUtilisateur == positionDepart[i][j]){
  116.                     if(positionDepart[i][j] <= 7){
  117.                         possibilite[positionDepart[i][j]] = '.';
  118.                         break;
  119.                     }else{
  120.                         possibilite[positionDepart[i][j]-2] = '.';
  121.                         break;
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.     }else{
  127.         //Nous testons les trois lignes horizontal de gauche ‡ droite
  128.         int check1=0;
  129.         int check2=0;
  130.         int coupJouer=0;
  131.         for(i=0;i<3;i++){
  132.             check1 = 0+i*3;
  133.             check2 = 1+i*3;
  134.             if(possibilite[check1] == possibilite[check2] && possibilite[check2+1] != '+' && possibilite[check2+1] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
  135.                 possibilite[check2+1] = '.';
  136.                 coupJouer++;
  137.                 break;
  138.             }else if(possibilite[check1] == possibilite[check2+1] && possibilite[check1+1] != '+' && possibilite[check1+1] != '.' && possibilite[check2+1] != '.' && possibilite[check1] != '.' && coupJouer==0){
  139.                 possibilite[check1+1] = '.';
  140.                 coupJouer++;
  141.                 break;
  142.             }
  143.         }
  144.         //Nous testons les trois lignes horizontal de droite ‡ gauche
  145.         check1=0;
  146.         check2=0;
  147.         for(i=0;i<3;i++){
  148.             check1 = 1+i*3;
  149.             check2 = 2+i*3;
  150.             if(possibilite[check1] == possibilite[check2] && possibilite[check1-1] != '+' && possibilite[check1-1] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
  151.                 possibilite[check1-1] = '.';
  152.                 coupJouer++;
  153.                 break;
  154.             }
  155.         }
  156.         //Nous testons les trois lignes vertical de haut en bas
  157.         check1=0;
  158.         check2=0;
  159.         for(i=0;i<3;i++){
  160.             check1 = i;
  161.             check2 = i+3;
  162.             if(possibilite[check1] == possibilite[check2] && possibilite[check2+3] != '+' && possibilite[check2+3] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
  163.                 possibilite[check2+3] = '.';
  164.                 coupJouer++;
  165.                 break;
  166.             }else if(possibilite[check1] == possibilite[check2+3] && possibilite[check2] != '+' && possibilite[check2] != '.' && possibilite[check2+3] != '.' && possibilite[check1] != '.' && coupJouer==0){
  167.                 possibilite[check2] = '.';
  168.                 coupJouer++;
  169.                 break;
  170.             }
  171.         }
  172.         //Nous testons les trois lignes vertical de bas en haut
  173.         check1=0;
  174.         check2=0;
  175.         for(i=0;i<3;i++){
  176.             check1 = i+3;
  177.             check2 = check1+3;
  178.             if(possibilite[check1] == possibilite[check2] && possibilite[check1-3] != '+' && possibilite[check1-3] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
  179.                 possibilite[check1-3] = '.';
  180.                 coupJouer++;
  181.                 break;
  182.             }
  183.         }
  184.         //Nous testons la diagonal de haut gauche ‡ bas droit de gauche ‡ droite
  185.         check1=0;
  186.         check2=0;
  187.         for(i=0;i<2;i++){
  188.             check1 = i*2;
  189.             check2 = 4;
  190.             if(possibilite[check1] == possibilite[check2] && possibilite[8-check1] != '+' && possibilite[8-check1] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
  191.                 possibilite[8-check1] = '.';
  192.                 coupJouer++;
  193.                 break;
  194.             }else if(possibilite[check1] == possibilite[check2+check2-check1] && possibilite[8-check1] != '+' && possibilite[8-check1] != '.' && possibilite[check2+check2-check1] != '.' && possibilite[check1] != '.' && coupJouer==0){
  195.                 possibilite[4] = '.';
  196.                 coupJouer++;
  197.                 break;
  198.             }
  199.         }
  200.         //Nous testons la diagonal de haut gauche ‡ bas droit de droite ‡ gauche
  201.         check1=0;
  202.         check2=0;
  203.         for(i=0;i<2;i++){
  204.             check1 = 6+i*2;
  205.             check2 = 4;
  206.             if(possibilite[check1] == possibilite[check2] && possibilite[8-check1] != '+' && possibilite[8-check1] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
  207.                 possibilite[8-check1] = '.';
  208.                 coupJouer++;
  209.                 break;
  210.             }
  211.         }
  212.  
  213.         if(coupJouer==0){
  214.             if(possibilite[4] != '.' && possibilite[4] != '+'){
  215.                 possibilite[4] = '.';
  216.                 coupJouer++;
  217.             }else{
  218.                 for(i=0;i<9;i++){
  219.                     if(possibilite[i] != '.' && possibilite[i] != '+' && coupJouer == 0){
  220.                         possibilite[i] = '.';
  221.                         coupJouer++;
  222.                     }
  223.                 }
  224.             }
  225.         }
  226.         if(coupJouer==0){
  227.             *fin = 1;
  228.         }
  229.     }
  230. }
  231.  
  232. void generateTTT(char *possibilite){
  233.  
  234.     printf("\t\t\t ___   ___   ___\n");
  235.     printf("\t\t\t|   | |   | |   |\n");
  236.     printf("\t\t\t| %c | | %c | | %c |\n", possibilite[0], possibilite[1], possibilite[2]);
  237.     printf("\t\t\t|___| |___| |___|\n");
  238.     printf("\t\t\t|   | |   | |   |\n");
  239.     printf("\t\t\t| %c | | %c | | %c |\n", possibilite[3], possibilite[4], possibilite[5]);
  240.     printf("\t\t\t|___| |___| |___|\n");
  241.     printf("\t\t\t|   | |   | |   |\n");
  242.     printf("\t\t\t| %c | | %c | | %c |\n", possibilite[6], possibilite[7], possibilite[8]);
  243.     printf("\t\t\t|___| |___| |___|\n");
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement