MasterGun

Mines

May 30th, 2021 (edited)
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. using namespace std;
  4. using namespace sf;
  5. void hmminesroundtiles(int gridLogic[12][12], int size)
  6. {
  7.     for (int i = 1; i <= 10; i++) {
  8.         for (int j = 1; j <= 10; j++) {
  9.             int n = 0;
  10.             if (gridLogic[i][j] == 9) continue;
  11.             if (gridLogic[i][j+1] == 9) n++;
  12.             if (gridLogic[i+1][j] == 9) n++;
  13.             if (gridLogic[i][j - 1] == 9) n++;
  14.             if (gridLogic[i-1][j] == 9) n++;
  15.             if (gridLogic[i+1][j + 1] == 9) n++;
  16.             if (gridLogic[i + 1][j - 1] == 9) n++;
  17.             if (gridLogic[i - 1][j + 1] == 9) n++;
  18.             if (gridLogic[i - 1][j - 1] == 9) n++;
  19.             gridLogic[i][j] = n;
  20.         }
  21.     }
  22. }
  23. int main()
  24. {
  25.     srand(time(0));
  26.     RenderWindow window(VideoMode(400, 400), "Mines");
  27.     int fieldwidth = 32;
  28.     int gridview[12][12];
  29.     int gridLogic[12][12];
  30.     Texture texture;
  31.     texture.loadFromFile("C:\\Users\\limb547\\Documents\\tiles.jpg");
  32.     Sprite sp(texture);
  33.     for (int i = 1; i <= 10; i++) {
  34.         for (int j = 1; j <= 10; j++) {
  35.             gridview[i][j] = 10;
  36.             if (rand() % 5 == 0) {
  37.                 gridLogic[i][j] = 9;
  38.             }
  39.             else {
  40.                 gridLogic[i][j] = 0;
  41.  
  42.             }
  43.         }
  44.     }
  45.     hmminesroundtiles(gridLogic, 10);
  46.  
  47.     while (window.isOpen())
  48.     {
  49.         Vector2i cursorPosition = Mouse::getPosition(window);
  50.         int x = cursorPosition.x / fieldwidth;
  51.         int y = cursorPosition.y / fieldwidth;
  52.         Event event;
  53.         while (window.pollEvent(event))
  54.         {
  55.             if (event.type == Event::Closed) {
  56.                 // тогда закрываем его
  57.                 window.close();
  58.             }
  59.             if (event.type == Event::MouseButtonPressed) {
  60.                 if (event.key.code == Mouse::Left) {
  61.                     gridview[x][y] = gridLogic[x][y];
  62.                 }
  63.                 else if (event.key.code == Mouse::Right) {
  64.                     gridview[x][y] = 11;
  65.                 }
  66.             }  
  67.             for (int i = 1; i <= 10; i++) {
  68.                 for (int j = 1; j <= 10; j++) {
  69.                     sp.setTextureRect(IntRect(gridview[i][j] * fieldwidth, 0, fieldwidth, fieldwidth));
  70.                     sp.setPosition(i * fieldwidth, j * fieldwidth);
  71.                     window.draw(sp);
  72.                 }
  73.             }
  74.             window.display();
  75.         }
  76.     }
  77.     return 0;
  78. }
Add Comment
Please, Sign In to add comment