Advertisement
Guest User

Untitled

a guest
May 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. class Point {
  9. bool bomb_Planted;
  10. bool is_Search;
  11. int number_Bombs;
  12. public:
  13. Point(bool FlagBomb) {
  14. bomb_Planted = FlagBomb;
  15. number_Bombs = 0;
  16. is_Search = false;
  17. }
  18. void count_Bombs(Point Arr[3][3]) {
  19. for (int i = 0;i < 3;i++) {
  20. for (int j = 0;j < 3;j++) {
  21. if (i == 1 && j == 1)
  22. continue;
  23. if (Arr[i][j].bomb_Planted)
  24. number_Bombs++;
  25. }
  26. }
  27. }
  28.  
  29. bool do_Bomb() {
  30. if (bomb_Planted == true)
  31. return false;
  32. bomb_Planted = true;
  33. return true;
  34. }
  35.  
  36. bool is_Planted() {
  37. return bomb_Planted;
  38. }
  39.  
  40. bool _is_Search() {
  41. return is_Search;
  42. }
  43.  
  44. void do_Search() {
  45. is_Search = true;
  46. }
  47.  
  48. int bombs_Around() {
  49. return number_Bombs;
  50. }
  51. };
  52.  
  53. class Square {
  54. private:
  55. int bomb_Pointer;
  56. int clear_Pointer;
  57. vector<vector<Point> > battleground;
  58. int Random(int min_Value, int max_Value)
  59. {
  60. int u = (int)(((double)rand() / (RAND_MAX + 1) * (max_Value - min_Value) + min_Value) + 0.5f);
  61. return(u);
  62. }
  63.  
  64. public:
  65. Square(int m, int n) {
  66. bomb_Pointer = 0;
  67. clear_Pointer = 0;
  68. for (int i = 0;i < m + 2;i++) {
  69. vector<Point> str(n + 2, 0);
  70. battleground.push_back(str);
  71. }
  72. for (int i = 1;i < m + 1;i++) {
  73. for (int j = 1;j < n + 1;j++) {
  74. battleground[i][j];
  75. }
  76. }
  77. for (int i = 0;i < (int)sqrt(m*n);i++) {
  78. bomb_Pointer+=battleground[Random(1, m)][Random(1, n)].do_Bomb();
  79. }
  80. for (int i = 1;i < m + 1;i++) {
  81. for (int j = 1;j < n + 1;j++) {
  82. Point Arr[3][3] = { battleground[i - 1][j - 1], battleground[i - 1][j], battleground[i - 1][j + 1],
  83. battleground[i][j - 1], battleground[i][j], battleground[i][j + 1],
  84. battleground[i + 1][j - 1], battleground[i + 1][j], battleground[i + 1][j + 1] };
  85. battleground[i][j].count_Bombs(Arr);
  86. }
  87. }
  88. }
  89.  
  90. void check_Point(int x, int y) {
  91. if (x <= 0 || x >= battleground[0].size() - 1 || y <= 0 || y >= battleground.size() - 1) {
  92. cout << "This point is not exist!" << endl;
  93. cout << "Enter new coordinates" << endl;
  94. int a, b;
  95. cin >> a >> b;
  96. check_Point(b, a);
  97. return;
  98. }
  99. if (battleground[y][x]._is_Search()) {
  100. cout << "This point has been searched" << endl;
  101. cout << "Enter new coordinates" << endl;
  102. int a, b;
  103. cin >> a >> b;
  104. check_Point(b, a);
  105. return;
  106. }
  107. if (battleground[y][x].is_Planted()) {
  108. print_Battleground(true);
  109. return;
  110. }
  111. battleground[y][x].do_Search();
  112. clear_Pointer++;
  113. if ((bomb_Pointer + clear_Pointer) == (battleground.size() - 2)*(battleground[0].size() - 2)) {
  114. cout << "You win" << endl;
  115. return;
  116. }
  117. if (battleground[y][x].bombs_Around() != 0) {
  118. print_Battleground(false);
  119. cout << "Enter new coordinates" << endl;
  120. int a, b;
  121. cin >> a >> b;
  122. check_Point(b, a);
  123. return;
  124. }
  125. calculate_Environment(y - 1, x - 1);
  126. calculate_Environment(y- 1, x);
  127. calculate_Environment(y - 1, x + 1);
  128. calculate_Environment(y, x - 1);
  129. calculate_Environment(y, x + 1);
  130. calculate_Environment(y + 1, x - 1);
  131. calculate_Environment(y + 1, x);
  132. calculate_Environment(y + 1, x + 1);
  133. print_Battleground(false);
  134. int a, b;
  135. cin >> a >> b;
  136. check_Point(b, a);
  137. return;
  138. }
  139.  
  140. void calculate_Environment(int i, int j) {
  141. if (j == 0 || j == battleground[0].size() - 1 || i == 0 || i == battleground.size() - 1)
  142. return;
  143. if (battleground[i][j]._is_Search())
  144. return;
  145. if (battleground[i][j].is_Planted())
  146. return;
  147. battleground[i][j].do_Search();
  148. clear_Pointer++;
  149. if (battleground[i][j].bombs_Around() == 0) {
  150. calculate_Environment(i - 1, j - 1);
  151. calculate_Environment(i - 1, j);
  152. calculate_Environment(i - 1, j + 1);
  153. calculate_Environment(i, j - 1);
  154. calculate_Environment(i, j + 1);
  155. calculate_Environment(i + 1, j - 1);
  156. calculate_Environment(i + 1, j);
  157. calculate_Environment(i + 1, j + 1);
  158. }
  159. }
  160.  
  161. void print_Battleground(bool flag_loser) {
  162. system("cls");
  163. if (flag_loser == true) {
  164. cout << "You lose!" << endl;
  165. for (int i = 1;i < battleground.size() - 1;i++) {
  166. for (int j = 1;j < battleground[i].size()-1;j++) {
  167. if (battleground[i][j].is_Planted()) {
  168. cout << "*";
  169. }
  170. else cout << "#";
  171. }
  172. cout << endl;
  173. }
  174. return;
  175. }
  176. for (int i = 1;i < battleground.size() - 1;i++) {
  177. for (int j = 1;j < battleground[i].size()-1;j++) {
  178. if (battleground[i][j]._is_Search()) {
  179. cout << battleground[i][j].bombs_Around();
  180. }
  181. else cout << "@";
  182. }
  183. cout << endl;
  184. }
  185. }
  186. };
  187.  
  188.  
  189. int main() {
  190. srand((unsigned)time(NULL));
  191. cout << "Enter size of ground" << endl;
  192. int a, b;
  193. cin >> a >> b;
  194. Square battleground(a, b);
  195. cout << "Enter coordinates" << endl;
  196. cin >> a >> b;
  197. battleground.check_Point(b, a);
  198. system("Pause");
  199. return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement