Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int W = 20;
  7. const int H = 20;
  8. const int MINES = 60;
  9.  
  10. char map[W][H];
  11. char opened_map[W][H];
  12.  
  13. void create() {
  14.     for (int i = 0; i < W; i++)
  15.         for (int j = 0; j < H; j++) {
  16.             map[i][j] = '.';
  17.             opened_map[i][j] = '#';
  18.         }
  19.     for (int i = 0; i < MINES; i++) {
  20.         int x, y;
  21.         do {
  22.             x = rand() % W;
  23.             y = rand() % H;
  24.         } while (map[x][y] != '.');
  25.         map[x][y] = '*';
  26.     }
  27. }
  28.  
  29. bool is_available(int x, int y) {
  30.     if (x < 0 || y < 0) return false;
  31.     if (x >= W || y >= H) return false;
  32.     return true;
  33. }
  34.  
  35. bool is_bomb(int x, int y) {
  36.     if (map[x][y] == '*')
  37.         return true;
  38.     return false;
  39. }
  40.  
  41. int count_bombs(int i, int j) {
  42.     int result = 0;
  43.     for (int i1 = -1; i1 <= 1; i1++) {
  44.         for (int j1 = -1; j1 <= 1; j1++) {
  45.             if (is_available(i + i1, j + j1)) {
  46.                 if (is_bomb(i + i1, j + j1)) {
  47.                     result++;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     return result;
  53. }
  54.  
  55.  
  56. void recursive_open(short i, short j) {
  57.     if (opened_map[i][j] != '#') return;
  58.     int bombs = count_bombs(i, j);
  59.     opened_map[i][j] = '0' + bombs;
  60.     if (bombs == 0) {
  61.         for (short i1 = -1; i1 <= 1; i1++) {
  62.             for (short j1 = -1; j1 <= 1; j1++) {
  63.                 if (is_available(i + i1, j + j1)) {
  64.                     recursive_open(i + i1, j + j1);
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. bool toggle(int i, int j) {
  72.     if (!is_available(i, j)) return true;
  73.     if (map[i][j] == '*') {
  74.         opened_map[i][j] = '*';
  75.         return false;
  76.     } else {
  77.         recursive_open(i, j);
  78.     }
  79.     return true;
  80. }
  81.  
  82. void draw_opened() {
  83.     for (int i = 0; i < W; i++) {
  84.         for (int j = 0; j < H; j++)
  85.             cout << opened_map[i][j] << " ";
  86.         cout << endl;
  87.     }
  88. }
  89.  
  90. void draw_map() {
  91.     for (int i = 0; i < W; i++) {
  92.         for (int j = 0; j < H; j++)
  93.             cout << map[i][j] << " ";
  94.         cout << endl;
  95.     }
  96. }
  97.  
  98. int main() {
  99.     create();
  100.     int x, y;
  101.     do {
  102.         draw_opened();
  103.         cout << endl;
  104.         draw_map();
  105.         cout << endl;
  106.         cin >> x >> y;
  107.     } while (toggle(x, y));
  108.     cout << "end";
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement