Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using std::vector;
  5.  
  6. class Minesweeper {
  7.  private:
  8.     size_t M, N;
  9.     vector<vector<int>> Table;
  10.  
  11.     void CheckForMinesAround(size_t i, size_t j) {
  12.         int counter = 0;
  13.         for (int dx = -1; dx <= 1; ++dx) {
  14.             for (int dy = -1; dy <= 1; ++dy) {
  15.                 if ((-dx <= static_cast<int>(i)) && (i + dx < M) &&
  16.                    (-dy <= static_cast<int>(j)) && (j + dy < N) &&
  17.                    Table[i + dx][j + dy] == -1) {
  18.                     ++counter;
  19.                 }
  20.             }
  21.         }
  22.         Table[i][j] = counter;
  23.     }
  24.  
  25.  public:
  26.     Minesweeper(size_t m, size_t n): M(m), N(n) {
  27.         Table = vector<vector<int>> (M, vector<int> (N, 0));
  28.     }
  29.  
  30.     const size_t Rows() const {
  31.         return M;
  32.     }
  33.  
  34.     const size_t Columns() const {
  35.         return N;
  36.     }
  37.  
  38.     void SetMine(size_t i, size_t j) {
  39.         Table[i][j] = -1;
  40.     }
  41.  
  42.     int operator () (size_t i, size_t j) const {
  43.         return Table[i][j];
  44.     }
  45.  
  46.     void CheckForMinesAround() {
  47.         for (size_t i = 0; i != M; ++i)
  48.             for (size_t j = 0; j != N; ++j)
  49.                 if (Table[i][j] != -1)
  50.                     CheckForMinesAround(i, j);
  51.     }
  52. };
  53.  
  54. std::ostream& operator << (std::ostream& out, const Minesweeper& ms) {
  55.     for (size_t i = 0; i != ms.Rows(); ++i) {
  56.         for (size_t j = 0; j != ms.Columns(); ++j) {
  57.             if (ms(i, j) == -1)
  58.                 out << '*';
  59.             else
  60.                 out << ms(i, j);
  61.         }
  62.         out << "\n";
  63.     }
  64.     return out;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement