-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.h
81 lines (70 loc) · 2.67 KB
/
minesweeper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include <string>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
#include <random>
class Minesweeper {
public:
struct Cell {
int64_t x = 0;
int64_t y = 0;
};
enum class GameStatus {
NOT_STARTED,
IN_PROGRESS,
VICTORY,
DEFEAT,
};
using RenderedField = std::vector<std::string>;
using Game = std::map<std::pair<int64_t, int64_t>, std::string>;
Minesweeper(size_t width, size_t height, size_t mines_count);
Minesweeper(size_t width, size_t height, const std::vector<Cell>& cells_with_mines);
void NewGame(size_t width, size_t height, size_t mines_count);
void NewGame(size_t width, size_t height, const std::vector<Cell>& cells_with_mines);
void OpenCell(const Cell& cell);
void MarkCell(const Cell& cell);
GameStatus GetGameStatus() const;
clock_t GetGameTime() const;
RenderedField RenderField();
Game game_;
private:
GameStatus game_status_ = GameStatus::NOT_STARTED;
size_t h_;
size_t w_;
bool start_ = false;
clock_t time_start_ = 0;
clock_t time_end_ = 0;
std::set<std::pair<int64_t, int64_t>> opened_;
Game bombs_;
int NeighboursMines(int64_t x, int64_t y) {
int ans = 0;
std::vector<std::pair<int64_t, int64_t>> coord = {{x + 1, y + 1}, {x + 1, y - 1}, {x - 1, y + 1},
{x - 1, y - 1}, {x, y + 1}, {x + 1, y},
{x, y - 1}, {x - 1, y}};
for (const auto& x_y : coord) {
if (x_y.first < h_ && x_y.first >= 0 && x_y.second < w_ && x_y.second >= 0) {
if (bombs_.find(x_y) != bombs_.end() && bombs_.at(x_y) == "bomb") {
++ans;
}
}
}
return ans;
}
std::deque<std::pair<int64_t, int64_t>> Neighbours(int64_t x, int64_t y) {
std::deque<std::pair<int64_t, int64_t>> ans;
std::deque<std::pair<size_t, size_t>> coord = {{x + 1, y + 1}, {x + 1, y - 1}, {x - 1, y + 1}, {x - 1, y - 1},
{x, y + 1}, {x + 1, y}, {x, y - 1}, {x - 1, y}};
for (const auto& x_y : coord) {
if (x_y.first < h_ && x_y.first >= 0 && x_y.second < w_ && x_y.second >= 0 &&
opened_.find(x_y) == opened_.end() &&
((game_.find({x_y.first, x_y.second}) != game_.end() && game_.at({x_y.first, x_y.second}) != "flag") ||
game_.find({x_y.first, x_y.second}) == game_.end())) {
ans.push_back(x_y);
}
}
return ans;
}
};