Advertisement
Guest User

minesweeper.cpp

a guest
Mar 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. //
  2. // Created by domin on 09.03.2019.
  3. //
  4. #include <iostream>
  5. #include "MinesweeperBoard.h"
  6. #include <time.h>
  7. #include <stdlib.h>
  8.  
  9.  
  10. using namespace std;
  11.  
  12.  
  13.  
  14.  
  15. MinesweeperBoard::MinesweeperBoard()
  16.  
  17. {
  18. width = 10;
  19. height = 10;
  20. set_board();
  21.  
  22.  
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. void MinesweeperBoard::set_board()
  38. {
  39. for (int y = 0; y <height; y++)
  40. {
  41. for (int x = 0; x <width; x++)
  42. {
  43. board[x][y].hasFlag = 0;
  44. board[x][y].hasMine = 0;
  45. board[x][y].isRevealed = 0;
  46.  
  47. }
  48. }
  49. }
  50.  
  51. void MinesweeperBoard::gamemode(GameMode mode)
  52. {
  53.  
  54. srand(time(NULL));
  55. if (mode == GameMode::EASY)
  56. {
  57. for (int x = 0; x < 10;)
  58. {
  59. int iks = (rand() % 10) + 0; // losowanie współrzędnych
  60. int igrek = (rand() % 10) + 0;
  61.  
  62. if (board[iks][igrek].hasMine == 1)
  63. {
  64. continue;
  65. }
  66. else
  67. {
  68. board[iks][igrek].hasMine = 1;
  69. x++;
  70. }
  71. board[iks][igrek].hasMine = 1;
  72. }
  73.  
  74. }
  75.  
  76. if(mode == GameMode::NORMAL)
  77. {
  78.  
  79.  
  80. for (int x = 0; x < 20;)
  81. {
  82. int iks = (rand() % 10) + 0; // losowanie współrzędnych
  83. int igrek = (rand() % 10) + 0;
  84. if (board[iks][igrek].hasMine == 1)
  85. {
  86. continue;
  87. } else
  88. {
  89. board[iks][igrek].hasMine = 1;
  90. x++;
  91. }
  92. board[iks][igrek].hasMine = 1;
  93.  
  94. }
  95. }
  96. if (mode == GameMode::HARD)
  97. {
  98. for (int x = 0; x < 30;)
  99. {
  100. int iks = (rand() % 10) + 0; // losowanie współrzędnych
  101. int igrek = (rand() % 10) + 0;
  102. if (board[iks][igrek].hasMine == 1)
  103. {
  104. continue;
  105. } else
  106. {
  107. board[iks][igrek].hasMine = 1;
  108. x++;
  109. }
  110. board[iks][igrek].hasMine = 1;
  111.  
  112. }
  113. }
  114.  
  115. }
  116.  
  117. void MinesweeperBoard::debug_display()
  118. {
  119. for (int y = 0;y<height;y++)
  120. {
  121. for (int x = 0;x<width;x++)
  122. {
  123.  
  124. cout << '[';
  125. if (board[x][y].hasMine == true)
  126. {
  127. cout << 'M';
  128. }
  129. else
  130. {
  131. cout << '.';
  132. }
  133. if (board[x][y].
  134. isRevealed == true
  135. )
  136. {
  137. cout << 'o';
  138. }
  139. else
  140. {
  141. cout << '.';
  142. }
  143. if (board[x][y].
  144. hasFlag == true
  145. )
  146. {
  147. cout << 'f';
  148. }
  149. else
  150. {
  151. cout << '.';
  152. }
  153. cout << ']';
  154.  
  155. }
  156. cout <<endl;
  157. }
  158. }
  159. void MinesweeperBoard::countMines(int x, int y)
  160. {
  161. Minecount=0;
  162.  
  163. if (board[x][y + 1].hasMine == true)
  164. Minecount++;
  165. if (board[x][y - 1].hasMine == true)
  166. Minecount++;
  167. if (board[x + 1][y].hasMine == true)
  168. Minecount++;
  169. if (board[x - 1][y].hasMine == true)
  170. Minecount++;
  171. if (board[x - 1][y - 1].hasMine == true)
  172. Minecount++;
  173. if (board[x + 1][y + 1].hasMine == true)
  174. Minecount++;
  175. if (board[x + 1][y - 1].hasMine == true)
  176. Minecount++;
  177. if (board[x - 1][y + 1].hasMine == true)
  178. Minecount++;
  179. cout << Minecount;
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement