Advertisement
marwanpro

minesweeper cli &q

Oct 29th, 2016
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. const int DIMX = 10;
  8. const int DIMY = 10;
  9. const int BOMB = 12;
  10. int glX = 0;
  11. int glY = 0;
  12. int randInt(int min, int max) { return min + (rand() % (int)(max - min + 1)); }
  13. /*struct mCase;
  14. void init(mCase table[DIMX][DIMY]);
  15. void print(mCase table[DIMX][DIMY]);
  16. struct table;*/
  17.  
  18.  
  19. struct mCase
  20. {
  21.     int x;
  22.     int y;
  23.     int nearBomb = 0;
  24.     bool isBomb = false;
  25.     bool isVisible = true;
  26.     bool isDisplayed = false;
  27.     bool isFlagged = false;
  28.  
  29.     void init()
  30.     {
  31.         x = glX;
  32.         y = glY;
  33.         nearBomb = 0;
  34.         if (x == 0 || y == 0 || x == DIMX || y == DIMY) isVisible = false;
  35.         isFlagged = false;
  36.         isBomb = false;
  37.     }
  38. };
  39.  
  40.  
  41. void init(mCase table[DIMX][DIMY])
  42. {
  43.     // Just calling init
  44.     for (glX = 0; glX < DIMX; glX++)
  45.     {
  46.         for (glY = 0; glY < DIMY; glY++)
  47.         {
  48.             table[glX][glY].init();
  49.         }
  50.     }
  51.  
  52.     // Placing bombs
  53.     for (int i = 0; i < BOMB; i++)
  54.     {
  55.         int x = randInt(1, DIMX - 2);
  56.         int y = randInt(1, DIMY - 2);
  57.         if (table[x][y].isBomb == false && table[x][y].isVisible == true) table[x][y].isBomb = true;
  58.         else i--;
  59.     }
  60.  
  61.     // Count near bombs
  62.     for (glX = 1; glX < DIMX - 1; glX++)
  63.     {
  64.         //cout << "glX: " << glX;
  65.         for (glY = 1; glY < DIMY - 1; glY++)
  66.         {
  67.             //cout << ", glY: " << glY << endl;
  68.             for (int x = glX - 1; x <= glX + 1; x++)
  69.             {
  70.                 for (int y = glY - 1; y <= glY + 1; y++)
  71.                 {
  72.                     //cout << "Case [" << glX << "][" << glY << "] : Checking (" << x << "," << y << ")" << endl;
  73.                     if (table[x][y].isBomb == true)
  74.                     {
  75.                         table[glX][glY].nearBomb++;
  76.                         //cout << "BOMB" << endl;
  77.                     }
  78.  
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85.  
  86. void print(mCase table[DIMX][DIMY])
  87. {
  88.  
  89.     for (glX = 1; glX < DIMX - 1; glX++)
  90.     {
  91.         for (glY = 1; glY < DIMY - 1; glY++)
  92.         {
  93.             if (table[glX][glY].isBomb) cout << 'B';
  94.             else cout << table[glX][glY].nearBomb;
  95.             cout << " ";
  96.         }
  97.         cout << endl;
  98.     }
  99. }
  100.  
  101.  
  102. int main(void)
  103. {
  104.     srand(time(NULL));
  105.     mCase table[DIMX][DIMY];
  106.     init(table);
  107.     print(table);
  108.     system("pause");
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement