Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. /* IDE-Eclipse Neon
  2. * Toolchain-MinGW32-->GCC
  3. * Minesweeper.cpp
  4. * Format-C++11
  5. * Created on: Sep 23, 2016
  6. * Author: Kartikey
  7. */
  8. #include<iostream>
  9. using namespace std;
  10. //class definition begins...
  11. class box
  12. {
  13. int neighbor; // keeps count of the number of mines in the 8 adjacent boxes to a selected box.
  14. bool mine, disp; //mine will be true if the box in question contains a mine. disp will be true, if the contents of a box are to be printed during the next time the grid is displayed.
  15. public:
  16. box() //Constructor
  17. {
  18. mine = false;
  19. disp = false;
  20. neighbor = 0;
  21. }
  22. void inc_neighbor() //On detecting a mine in vicinity, inspect() increments the value of neighbor by 1.
  23. {
  24. neighbor++;
  25. }
  26. bool sweep(void) //Checks whether a mine is present or not
  27. {
  28. if(mine == true)
  29. {
  30. return true;
  31. }
  32. else
  33. {
  34. return false;
  35. }
  36. }
  37. void set_disp() //Sets disp variable to "true", so that when Displaying() is called, that box in the grid displays it's contents (mines or neighbor)
  38. {
  39. disp = true;
  40. }
  41. bool check_disp() //Check whether the content of a particular box is to be displayed...
  42. {
  43. if(disp==false)
  44. {
  45. return false;
  46. }
  47. else
  48. {
  49. return true;
  50. }
  51. }
  52. void generate() //Puts a mine in the box
  53. {
  54. mine = true;
  55. }
  56. void display() //Displays the value of the neighbor
  57. {
  58. if(sweep()==false)
  59. {
  60. cout << neighbor;
  61. }
  62. else
  63. {
  64. cout << "*";
  65. }
  66. }
  67. };
  68. //class definition ends.
  69. void inspect(box grid[][11], int x, int y)//Counts of the number of mines in the 8 adjacent boxes to a selected box.
  70. {
  71. for(int i=x-1; i<=i+1; i++)
  72. {
  73. for(int j=y-1; j<=j+1; j++) //Logic to eliminate the checking of certain boxes, when dealing with the boxes that are at the edge of the grid.
  74. {
  75. if((j==0)||(j==11)||(i==0)||(j==11)||((i==x)&&(j==y)))
  76. {
  77. continue;
  78. }
  79. else
  80. {
  81. if(grid[i][j].sweep()==true)
  82. {
  83. grid[x][y].inc_neighbor();
  84. }
  85. }
  86. }
  87. }
  88. }
  89. void Displaying(box grid[][11]) //Function for displaying the grid.
  90. {
  91. for(int i=0; i<11; i++)
  92. {
  93. for(int j=0; j<11; j++)
  94. {
  95. if((i == 0)&&( j != 0))
  96. {
  97. cout << j << " ";
  98. continue;
  99. }
  100. else if(j == 0)
  101. {
  102. cout << i << " ";
  103. continue;
  104. }
  105. else
  106. {
  107. if(grid[i][j].check_disp()==false)
  108. {
  109. cout << " " << " ";
  110. }
  111. else
  112. {
  113. grid[i][j].display();
  114. cout << " ";
  115. }
  116. }
  117. }
  118. cout << endl << endl;
  119. }
  120. }
  121. void Generate_Field(box grid[][11]) //Mines are placed.
  122. {
  123. int values[10][2] = {{1, 3}, {3, 6}, {5, 5}, {7, 9}, {2, 3}, {8, 5}, {2, 1}, {6, 7}, {9, 1}, {8, 4}}; //Using pre-defined co-ordinates till the time I'm testing the logic of the program. Will use srand() to generate random coordinates in the final stage.
  124. for(int i=0; i<10; i++)
  125. {
  126. grid[(values[i][0])][(values[i][1])].generate();
  127. }
  128. }
  129. void Disp_Mine(box grid[][11]) //Displays the location of mines in the grid.
  130. {
  131. for(int i=0; i<11; i++)
  132. {
  133. for(int j=0; j<11; j++)
  134. {
  135. if((i == 0)&&( j != 0))
  136. {
  137. cout << j << " ";
  138. continue;
  139. }
  140. else if(j == 0)
  141. {
  142. cout << i << " ";
  143. continue;
  144. }
  145. else
  146. {
  147. if(grid[i][j].sweep()==false)
  148. {
  149. cout << " " << " ";
  150. }
  151. else
  152. {
  153. grid[i][j].display();
  154. cout << " ";
  155. }
  156. }
  157. }
  158. cout << endl << endl;
  159. }
  160. }
  161. int main()
  162. {
  163. box grid[11][11];
  164. //int chances = 0, x, y;
  165. //bool game_over = false;
  166. Displaying(grid);
  167. cout << endl;
  168. Generate_Field(grid);
  169. Disp_Mine(grid);
  170. cout << endl;
  171. for(int i=0; i<=10; i++)
  172. {
  173. for(int j=0; j<=10; j++)
  174. {
  175. grid[i][j].set_disp();
  176. }
  177. }
  178. //inspect(grid, 1, 1);
  179. Displaying(grid);
  180. /*while(game_over==false)
  181. {
  182. Displaying(grid);
  183. cout << "Enter the x coordinate : ";
  184. cin >> x;
  185. cout << "Enter the y coordinate : ";
  186. cin >> y;
  187. grid[x][y].set_disp();
  188. if(grid[x][y].sweep()==true)
  189. {
  190. cout << "BOOM! You're dead.";
  191. chances++;
  192. break;
  193. }
  194. else
  195. {
  196.  
  197. }
  198. }*/
  199. return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement