Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <time.h>
- using namespace std;
- class Point {
- bool bomb_Planted;
- bool is_Search;
- int number_Bombs;
- public:
- Point(bool FlagBomb) {
- bomb_Planted = FlagBomb;
- number_Bombs = 0;
- is_Search = false;
- }
- void count_Bombs(Point Arr[3][3]) {
- for (int i = 0;i < 3;i++) {
- for (int j = 0;j < 3;j++) {
- if (i == 1 && j == 1)
- continue;
- if (Arr[i][j].bomb_Planted)
- number_Bombs++;
- }
- }
- }
- bool do_Bomb() {
- if (bomb_Planted == true)
- return false;
- bomb_Planted = true;
- return true;
- }
- bool is_Planted() {
- return bomb_Planted;
- }
- bool _is_Search() {
- return is_Search;
- }
- void do_Search() {
- is_Search = true;
- }
- int bombs_Around() {
- return number_Bombs;
- }
- };
- class Square {
- private:
- int bomb_Pointer;
- int clear_Pointer;
- vector<vector<Point> > battleground;
- int Random(int min_Value, int max_Value)
- {
- int u = (int)(((double)rand() / (RAND_MAX + 1) * (max_Value - min_Value) + min_Value) + 0.5f);
- return(u);
- }
- public:
- Square(int m, int n) {
- bomb_Pointer = 0;
- clear_Pointer = 0;
- for (int i = 0;i < m + 2;i++) {
- vector<Point> str(n + 2, 0);
- battleground.push_back(str);
- }
- for (int i = 1;i < m + 1;i++) {
- for (int j = 1;j < n + 1;j++) {
- battleground[i][j];
- }
- }
- for (int i = 0;i < (int)sqrt(m*n);i++) {
- bomb_Pointer+=battleground[Random(1, m)][Random(1, n)].do_Bomb();
- }
- for (int i = 1;i < m + 1;i++) {
- for (int j = 1;j < n + 1;j++) {
- Point Arr[3][3] = { battleground[i - 1][j - 1], battleground[i - 1][j], battleground[i - 1][j + 1],
- battleground[i][j - 1], battleground[i][j], battleground[i][j + 1],
- battleground[i + 1][j - 1], battleground[i + 1][j], battleground[i + 1][j + 1] };
- battleground[i][j].count_Bombs(Arr);
- }
- }
- }
- void check_Point(int x, int y) {
- if (x <= 0 || x >= battleground[0].size() - 1 || y <= 0 || y >= battleground.size() - 1) {
- cout << "This point is not exist!" << endl;
- cout << "Enter new coordinates" << endl;
- int a, b;
- cin >> a >> b;
- check_Point(b, a);
- return;
- }
- if (battleground[y][x]._is_Search()) {
- cout << "This point has been searched" << endl;
- cout << "Enter new coordinates" << endl;
- int a, b;
- cin >> a >> b;
- check_Point(b, a);
- return;
- }
- if (battleground[y][x].is_Planted()) {
- print_Battleground(true);
- return;
- }
- battleground[y][x].do_Search();
- clear_Pointer++;
- if ((bomb_Pointer + clear_Pointer) == (battleground.size() - 2)*(battleground[0].size() - 2)) {
- cout << "You win" << endl;
- return;
- }
- if (battleground[y][x].bombs_Around() != 0) {
- print_Battleground(false);
- cout << "Enter new coordinates" << endl;
- int a, b;
- cin >> a >> b;
- check_Point(b, a);
- return;
- }
- calculate_Environment(y - 1, x - 1);
- calculate_Environment(y- 1, x);
- calculate_Environment(y - 1, x + 1);
- calculate_Environment(y, x - 1);
- calculate_Environment(y, x + 1);
- calculate_Environment(y + 1, x - 1);
- calculate_Environment(y + 1, x);
- calculate_Environment(y + 1, x + 1);
- print_Battleground(false);
- int a, b;
- cin >> a >> b;
- check_Point(b, a);
- return;
- }
- void calculate_Environment(int i, int j) {
- if (j == 0 || j == battleground[0].size() - 1 || i == 0 || i == battleground.size() - 1)
- return;
- if (battleground[i][j]._is_Search())
- return;
- if (battleground[i][j].is_Planted())
- return;
- battleground[i][j].do_Search();
- clear_Pointer++;
- if (battleground[i][j].bombs_Around() == 0) {
- calculate_Environment(i - 1, j - 1);
- calculate_Environment(i - 1, j);
- calculate_Environment(i - 1, j + 1);
- calculate_Environment(i, j - 1);
- calculate_Environment(i, j + 1);
- calculate_Environment(i + 1, j - 1);
- calculate_Environment(i + 1, j);
- calculate_Environment(i + 1, j + 1);
- }
- }
- void print_Battleground(bool flag_loser) {
- system("cls");
- if (flag_loser == true) {
- cout << "You lose!" << endl;
- for (int i = 1;i < battleground.size() - 1;i++) {
- for (int j = 1;j < battleground[i].size()-1;j++) {
- if (battleground[i][j].is_Planted()) {
- cout << "*";
- }
- else cout << "#";
- }
- cout << endl;
- }
- return;
- }
- for (int i = 1;i < battleground.size() - 1;i++) {
- for (int j = 1;j < battleground[i].size()-1;j++) {
- if (battleground[i][j]._is_Search()) {
- cout << battleground[i][j].bombs_Around();
- }
- else cout << "@";
- }
- cout << endl;
- }
- }
- };
- int main() {
- srand((unsigned)time(NULL));
- cout << "Enter size of ground" << endl;
- int a, b;
- cin >> a >> b;
- Square battleground(a, b);
- cout << "Enter coordinates" << endl;
- cin >> a >> b;
- battleground.check_Point(b, a);
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement