Advertisement
nizamoff

minesweeper.h

Sep 30th, 2022
1,015
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 1 0
  1. #pragma once
  2.  
  3. #include <string>
  4. #include <vector>
  5.  
  6. class Minesweeper {
  7.     size_t width_ = 0;
  8.     size_t height_ = 0;
  9.     size_t open_cells_count_ = 0;
  10.     size_t mines_count_ = 0;
  11.  
  12.     time_t start_ = 0;
  13.     time_t finish_ = 0;
  14.  
  15.     std::vector<int> delta_ = {-1, 0, 1};
  16.  
  17.     struct Cell {
  18.         size_t x = 0;
  19.         size_t y = 0;
  20.         bool have_mine = false;
  21.         size_t neighbors_mines_count_ = 0;
  22.         std::vector<Cell*> neighbors;
  23.  
  24.         enum class Status {
  25.             OPEN,
  26.             MARK,
  27.             NOT_OPEN,
  28.         } status;
  29.  
  30.         bool IsOpen() const;
  31.         bool IsNotOpen() const;
  32.         bool IsMark() const;
  33.     };
  34.  
  35.     std::vector<std::vector<Cell>> field_;
  36.  
  37.     void Initialize(size_t width, size_t height);
  38.     void PrecalcCountOfNeighborsMines();
  39.  
  40.     void StartGame();
  41.     void Victory();
  42.     void GameOver();
  43.     void Open(Cell& cell);
  44.     void Mark(Cell& cell) const;
  45.     bool OutOfRange(size_t x, int dx, size_t width) const;
  46.  
  47. public:
  48.     enum class GameStatus {
  49.         NOT_STARTED,
  50.         IN_PROGRESS,
  51.         VICTORY,
  52.         DEFEAT,
  53.     } game_status_;
  54.  
  55.     using RenderedField = std::vector<std::string>;
  56.  
  57.     Minesweeper(size_t width, size_t height, size_t mines_count);
  58.     Minesweeper(size_t width, size_t height, const std::vector<Cell>& cells_with_mines);
  59.  
  60.     void NewGame(size_t width, size_t height, size_t mines_count);
  61.     void NewGame(size_t width, size_t height, const std::vector<Cell>& cells_with_mines);
  62.  
  63.     void OpenCell(const Cell& cell);
  64.     void MarkCell(const Cell& cell);
  65.  
  66.     GameStatus GetGameStatus() const;
  67.     time_t GetGameTime() const;
  68.  
  69.     RenderedField RenderField() const;
  70. };
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement