Advertisement
Guest User

Minesweeper Game C++ Opensource Code

a guest
Jan 4th, 2021
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3.  
  4. using namespace sf;
  5.  
  6. int main()
  7. {
  8.     srand(time(0));
  9.  
  10.     RenderWindow app(VideoMode(400, 400), "Minesweeper!");
  11.  
  12.     int w = 32;
  13.  
  14.     int gridLogic[12][12];
  15.     int gridView[12][12];
  16.  
  17.     Texture t;
  18.     t.loadFromFile("images/tiles.jpg");
  19.     Sprite s(t);
  20.  
  21.     for (int i = 1; i <= 10; i++)
  22.         for (int j = 1; j <=10; j++)
  23.         {
  24.             gridView[i][j] = 10;
  25.             if (rand() % 5 == 0) gridLogic[i][j] = 9;
  26.             else gridLogic[i][j] = 0;
  27.         }
  28.  
  29.     //Расстановка цифр, отражающих наличие мин в соседних клетках
  30.     for (int i = 1; i <= 10; i++)
  31.         for (int j = 1; j <=10; j++)
  32.         {
  33.             int n = 0;
  34.             if (gridLogic[i][j] == 9) continue;
  35.             if (gridLogic[i + 1][j] == 9) n++;
  36.             if (gridLogic[i][j + 1] == 9) n++;
  37.             if (gridLogic[i - 1][j] == 9) n++;
  38.             if (gridLogic[i][j-1] == 9) n++;
  39.             if (gridLogic[i + 1][j + 1] == 9) n++;
  40.             if (gridLogic[i - 1][j - 1] == 9) n++;
  41.             if (gridLogic[i - 1][j + 1] == 9) n++;
  42.             if (gridLogic[i + 1][j - 1] == 9) n++;
  43.             gridLogic[i][j] = n;
  44.         }
  45.  
  46.     while (app.isOpen())
  47.     {
  48.         Event e;
  49.  
  50.         while (app.waitEvent(e))
  51.         {
  52.             Vector2i pos = Mouse::getPosition(app);
  53.             int x = pos.x / w;
  54.             int y = pos.y / w;
  55.  
  56.             if (e.type == Event::Closed)
  57.                 app.close();
  58.  
  59.             if (e.type == Event::MouseButtonPressed)
  60.             {
  61.                 if (e.key.code == Mouse::Left)
  62.                 {
  63.                     gridView[x][y] = gridLogic[x][y];
  64.                     if (gridLogic[x][y] == 9)
  65.                         for (int i = 1; i <= 10; i++)
  66.                             for (int j = 1; j <=10; j++)
  67.                             {
  68.                                 gridView[i][j] = gridLogic[i][j];
  69.                             }
  70.                 }
  71.                 else if (e.key.code == Mouse::Right) gridView[x][y] = 11;
  72.             }
  73.  
  74.             app.clear(Color::White);
  75.  
  76.             for (int i = 1; i <=10; i++)
  77.                 for (int j = 1; j <=10; j++)
  78.                 {
  79.                     s.setTextureRect(IntRect(gridView[i][j] * w, 0, w, w));
  80.                     s.setPosition(i*w, j*w);
  81.                     app.draw(s);
  82.                 }
  83.             app.display();
  84.         }
  85.     }
  86.  
  87.     return 0;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement