Advertisement
alsagone

Untitled

Dec 14th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. using namespace std;
  7. const int NB_MINES=10;
  8. const int TAILLE_X=9;
  9. const int TAILLE_Y=9;
  10.  
  11. struct case_grille
  12. {
  13.     int valeur;
  14.     bool decouverte;
  15. };
  16.  
  17. void init (struct case_grille g[TAILLE_X+2][TAILLE_Y+2])
  18. {
  19.     int i, j;
  20.     for (i=0;i<TAILLE_X+2;i++)
  21.     {
  22.         for (j=0;j<TAILLE_Y+2;j++)
  23.         {
  24.             g[i][j].valeur=0;
  25.             g[i][j].decouverte=false;
  26.         }
  27.     }
  28. }
  29.  
  30. void remp(struct case_grille g[TAILLE_X+2][TAILLE_Y+2])
  31. {
  32.     int i, j, k;
  33.     for (k=0;k<NB_MINES;k++)
  34.     {
  35.        i=rand()%TAILLE_X+1;
  36.        j=rand()%TAILLE_Y+1;
  37.     }
  38.    
  39.     while (g[i][j].valeur!=0)
  40.     {
  41.         g[i][j].valeur=(-1);
  42.     }
  43. }
  44.  
  45. void bombe (struct case_grille g[TAILLE_X+2][TAILLE_Y+2])
  46. {
  47.     int i, j, k, l;
  48.     for (i=1; i<TAILLE_X+1;i++)
  49.     {
  50.         for (j=1;j<TAILLE_Y+1;j++)
  51.         {
  52.             if (g[i][j].valeur!=1)
  53.             {
  54.                 for (k=i-1;j<=i+1;k++)
  55.                 {
  56.                     for (l=j-1;l<=j+1;l++)
  57.                     {
  58.                         if (g[k][l].valeur==(-1)) {g[i][j].valeur++;}
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66.  
  67. void affiche (struct case_grille g[TAILLE_X+2][TAILLE_Y+2])
  68.     {
  69.         int i, j;
  70.         for (i=1;i<TAILLE_X+1;i++)
  71.         {
  72.             for (j=1;j<TAILLE_Y;j++)
  73.             {
  74.                 if (g[i][j].decouverte)
  75.                 {
  76.                     if (g[i][j].valeur==-1)
  77.                     {
  78.                         putchar(207);
  79.                     }
  80.                         else
  81.                             cout <<g[i][j].valeur;
  82.                     }
  83.                     cout <<endl;
  84.                 }
  85.             }
  86.         }
  87.  
  88.  
  89.     int decouvrir (struct case_grille g[TAILLE_X+2][TAILLE_Y+2], int x, int y)
  90.     {
  91.        // int i, j;
  92.         int code_retour;
  93.         do
  94.         {
  95.             cout<<"Donnez les coordonnees de la case"<<endl;
  96.             cin>>x>>y;
  97.         }
  98.         while (g[x][y].decouverte==true);
  99.         if (g[x][y].valeur==-1)
  100.             code_retour = 0;
  101.         else
  102.             code_retour = -1;
  103.  
  104.     return code_retour;
  105.     }
  106.  
  107.     int jouer (struct case_grille g[TAILLE_X+2][TAILLE_Y+2], int reste)
  108.     {
  109.         int x,y;
  110.         int retour;
  111.         bool bombe=false;
  112.         while((reste!=0)&&(bombe=false))
  113.             retour=decouvrir(g,x,y);
  114.         if (retour==0)
  115.         {
  116.             bombe=true;
  117.             g[x][y].decouverte=true;
  118.         }
  119.         else
  120.             if (g[x][y].decouverte==false)
  121.         {
  122.             g[x][y].decouverte=true;
  123.         reste--;
  124.         }
  125.         else
  126.         {
  127.             cout <<"Case jouer"<<endl;
  128.             affiche(g);
  129.             cout<<"Nombre de case restante"<<reste<<endl;
  130.         }
  131.         if (reste==0)
  132.             return 0;
  133.         else
  134.             return 1;
  135.     }
  136.  
  137. int main (void)
  138.     {
  139.         struct case_grille jeu[TAILLE_X+2][TAILLE_Y+2];
  140.         int nb_case=((TAILLE_X*TAILLE_Y)-NB_MINES);
  141.         int fin_partie;
  142.         srand(time(NULL));
  143.         init(jeu);
  144.         affiche (jeu);
  145.         remp(jeu);
  146.         bombe(jeu);
  147.         fin_partie=jouer(jeu, nb_case);
  148.         if (fin_partie==0)
  149.             cout<<"BRAVO"<<endl;
  150.         else
  151.                 cout<<"Vous avez perdu"<<endl;
  152.         system("PAUSE");
  153.         return 0;
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement