Advertisement
marwanpro

demin bastien

Nov 3rd, 2016
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #define CHMAX 100
  5.  
  6. using namespace std;
  7.  
  8. const int DIMX = 6;
  9. const int DIMY = 6;
  10. const int BOMB = 2;
  11. bool cheat = true;
  12. int randInt(int min, int max) { return min + (rand() % (int)(max - min + 1)); }
  13. int glX = 0;
  14. int glY = 0;
  15.  
  16. bool game = true;
  17.  
  18.  
  19. struct mCase
  20. {
  21.     int x;
  22.     int y;
  23.     int bombesProches = 0;
  24.     bool bombe = false;
  25.     bool visible = true;
  26.     bool revele = false;
  27.     bool drapeau = false;
  28.  
  29.     void init()
  30.     {
  31.         x = glX;
  32.         y = glY;
  33.         bombesProches = 0;
  34.         if (x == 0 || y == 0 || x == DIMX || y == DIMY) visible = false;
  35.         drapeau = false;
  36.         bombe = false;
  37.     }
  38. };
  39.  
  40.  
  41. void init(mCase table[DIMX][DIMY])
  42. {
  43.     for (glX = 0; glX < DIMX; glX++)
  44.     {
  45.         for (glY = 0; glY < DIMY; glY++)
  46.         {
  47.             table[glX][glY].init();
  48.         }
  49.     }
  50.  
  51.     for (int i = 0; i < BOMB; i++)
  52.     {
  53.         int x = randInt(1, DIMX - 2);
  54.         int y = randInt(1, DIMY - 2);
  55.         if (table[x][y].bombe == false && table[x][y].visible == true) table[x][y].bombe = true;
  56.         else i--;
  57.     }
  58.  
  59.     for (glX = 1; glX < DIMX - 1; glX++)
  60.     {
  61.         for (glY = 1; glY < DIMY - 1; glY++)
  62.         {
  63.             for (int x = glX - 1; x <= glX + 1; x++)
  64.             {
  65.                 for (int y = glY - 1; y <= glY + 1; y++)
  66.                 {
  67.                     if (table[x][y].bombe == true)
  68.                     {
  69.                         table[glX][glY].bombesProches++;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }
  76.  
  77.  
  78. void triche(mCase table[DIMX][DIMY])
  79. {
  80.  
  81.     for (glX = 1; glX < DIMX - 1; glX++)
  82.     {
  83.         for (glY = 1; glY < DIMY - 1; glY++)
  84.         {
  85.             if (table[glX][glY].bombe) cout << 'B';
  86.             else cout << table[glX][glY].bombesProches;
  87.             cout << " ";
  88.         }
  89.         cout << endl;
  90.     }
  91.     cout << endl;
  92. }
  93.  
  94.  
  95. void affiche(mCase table[DIMX][DIMY])
  96. {
  97.  
  98.     for (int x = 1; x < DIMX - 1; x++)
  99.     {
  100.         for (int y = 1; y < DIMY - 1; y++)
  101.         {
  102.             if (table[x][y].drapeau) cout << "D";
  103.             else if (table[x][y].revele)
  104.             {
  105.                 if (table[x][y].bombe) cout << "B";
  106.                 else cout << table[x][y].bombesProches;
  107.             }
  108.             else cout << "?";
  109.             cout << " ";
  110.         }
  111.         cout << endl;
  112.     }
  113.     cout << endl;
  114. }
  115.  
  116.  
  117. void entree(mCase table[DIMX][DIMY])
  118. {
  119.     int x, y, action;
  120.     cout << "X: ";
  121.     do cin >> y;
  122.     while (y < 1 || y > DIMX - 1);
  123.     cout << "Y: ";
  124.     do cin >> x;
  125.     while (x < 1 || x > DIMY - 1);
  126.     cout << "1: Clic | 2: Drapeau: ";
  127.     do cin >> action;
  128.     while (action < 1 || action > 2);
  129.     cout << endl;
  130.  
  131.     if (action == 2)
  132.     {
  133.         if (table[x][y].revele)
  134.         {
  135.             cout << "Case deja affiche" << endl;
  136.             return;
  137.         }
  138.         if (table[x][y].drapeau)
  139.         {
  140.             table[x][y].drapeau = false;
  141.             cout << "Drapeau retire" << endl;
  142.         }
  143.         else
  144.         {
  145.             table[x][y].drapeau = true;
  146.             cout << "Drapeau pose" << endl;
  147.         }
  148.     }
  149.  
  150.     if (action == 1)
  151.     {
  152.         if (table[x][y].drapeau)
  153.         {
  154.             cout << "Il y'a un drapeau, enlevez le avant de demine" << endl;
  155.             return;
  156.         }
  157.         table[x][y].revele = true;
  158.         if (table[x][y].bombe)
  159.         {
  160.             cout << "Perdu !" << endl;
  161.             game = false;
  162.         }
  163.     }
  164. }
  165.  
  166. void testGagne(mCase table[DIMX][DIMY])
  167. {
  168.     int nombreCase = (DIMX - 2)*(DIMY - 2) - BOMB;
  169.     for (int x = 1; x < DIMX - 1; x++)
  170.     {
  171.         for (int y = 1; y < DIMY - 1; y++)
  172.         {
  173.             if (table[x][y].revele) nombreCase--;
  174.         }
  175.     }
  176.     if (nombreCase == 0)
  177.     {
  178.         cout << "Vous avez gagne !";
  179.         game = false;
  180.     }
  181.  
  182. }
  183.  
  184.  
  185. int main(void)
  186. {
  187.     srand(time(NULL));
  188.     mCase table[DIMX][DIMY];
  189.     init(table);
  190.     if (cheat) triche(table);
  191.     while (game)
  192.     {
  193.         affiche(table);
  194.         entree(table);
  195.         testGagne(table);
  196.     }
  197.     system("pause");
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement