Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <ctime>
  4. #include <random>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8. int x,y,r,t;
  9. int mine;
  10. int** arraymine;
  11. char** arrayboard;
  12. int counter;
  13.  
  14. void adjminethingy()
  15. {
  16. counter=48;
  17. if (arraymine[x-1][y-2] == 0 && y>=1)//north
  18. {
  19. counter++;
  20. arrayboard[x-1][y-1]=counter;
  21. }
  22. if (arraymine[x-1][y] == 0 && y<r)//south
  23. {
  24. counter++;
  25. arrayboard[x-1][y-1]=counter;
  26. }
  27. if (arraymine[x][y-1] == 0 && x<r)//east
  28. {
  29. counter++;
  30. arrayboard[x-1][y-1]=counter;
  31. }
  32. if (arraymine[x-2][y-1] == 0 && x>1)//west
  33. {
  34. counter++;
  35. arrayboard[x-1][y-1]=counter;
  36. }
  37. if (arraymine[x][y-2] == 0 && x>r && y>1)//north-east
  38. {
  39. counter++;
  40. arrayboard[x-1][y-1]=counter;
  41. }
  42. if (arraymine[x][y] == 0 && x<r && y<r)//south-east
  43. {
  44. counter++;
  45. arrayboard[x-1][y-1]=counter;
  46. }
  47. if (arraymine[x-2][y] == 0 && x>1 && y<r)//south-west
  48. {
  49. counter++;
  50. arrayboard[x-1][y-1]=counter;
  51. }
  52. if (arraymine[x-2][y-2] == 0 && x>1 && y>1)//north-west
  53. {
  54. counter++;
  55. arrayboard[x-1][y-1]=counter;
  56. }
  57. }
  58.  
  59. void boarddraw()
  60. {
  61. for(int i=0;i<r;i++)
  62. {
  63. for(int j=0;j<t;j++)
  64. {
  65. cout<<"["<<arrayboard[i][j]<<"]";
  66. }
  67. cout<<endl;
  68. }
  69. }
  70. void makemove()
  71. {
  72. boarddraw();
  73. cout<<"enter x:";
  74. cin>>x;
  75. cout<<"enter y:";
  76. cin>>y;
  77. if(arraymine[x-1][y-1]==0)
  78. {
  79. cout<<"you have hit a mine";
  80. }
  81. else
  82. cout<<"mined your own business \n";
  83.  
  84. adjminethingy();
  85. cout<<endl<<counter<<endl;
  86. makemove();
  87.  
  88. }
  89. //This function randomises mines in the board
  90. void randomise()
  91. {
  92. srand(time(0));
  93. for (int i=0; i<r; i++)
  94. {
  95. for (int j=0; j<r; j++)
  96. {
  97. int index1=rand()%r;
  98. int index2=rand()%r;
  99. int temp=arraymine[i][j];
  100. arraymine[i][j]=arraymine[index1][index2];
  101. arraymine[index1][index2]=temp;
  102. }
  103. }
  104. }
  105. //This function sets up the board
  106. void mine_board() // Creating mines
  107. {
  108. for(int i=0;i<r;i++)
  109. {
  110. for(int j=0;j<t;j++)
  111. {
  112. if(mine>0)
  113. {
  114. arraymine[i][j]=0;
  115. mine--;
  116. }
  117. else
  118. arraymine[i][j]=1;
  119. }
  120. }
  121.  
  122. // Randomises mines
  123. randomise();
  124.  
  125. for(int i=0;i<r;i++)
  126. {
  127. for(int j=0;j<t;j++)
  128. {
  129. arrayboard[i][j]=2;
  130. }
  131. }
  132.  
  133. for(int i=0;i<r;i++)
  134. {
  135. for(int j=0;j<t;j++)
  136. {
  137. cout << "[" << arraymine[i][j] << "]";
  138. }
  139. cout << endl;
  140. }
  141.  
  142. }
  143. /*void mine_board()
  144. {
  145. for(int i=0;i<r;i++)
  146. {
  147. for(int j=0;j<t;j++)
  148. {
  149. if(mine>0)
  150. {
  151. arraymine[i][j]=0;
  152. mine--;
  153. }
  154. else
  155. arraymine[i][j]=1;
  156. }
  157. }
  158. //this randomises the mines
  159. randomise();
  160.  
  161. for(int i=0;i<r;i++)
  162. {
  163. for(int j=0;j<t;j++)
  164. {
  165. cout<<"["<<arrayboard[i][j]<<"]";
  166. }
  167. cout<<endl;
  168. }
  169. //this draws a physical board made up of ASCII Smiley Faces
  170. for(int i=0;i<r;i++)
  171. {
  172. for(int j=0;j<t;j++)
  173. {
  174. arrayboard[i][j]=2;
  175. }
  176. }
  177. for(int i=0;i<r;i++)
  178. {
  179. for(int j=0;j<t;j++)
  180. {
  181. cout<<arrayboard[i][j];
  182. }
  183. cout<<endl;
  184. }
  185. }*/
  186. //void menu draws the board and allows the user to input the game they wish to play
  187. //based on this it then sets the values of r & t (board size) and mine (number of mines)
  188. void menu()
  189. {
  190. int selection;
  191. cout<<setw(60)<<"MINESWEEPER\n\n";
  192. cout << "1. Play Beginner Game\n" << "2. Play Intermediate Game\n" << "3. Play Advanced Game\n" << "4. Show Score Board\n" << "5. Quit Game\n\n\n";
  193. cout << "Please enter your selection: ";
  194. cin >> selection;
  195. cout << endl;
  196.  
  197. switch (selection)
  198. {
  199. case 1:
  200. cout << "Play Beginner Game\n";
  201. cout << "\n";
  202. system("cls"); //hides above text and leaves only the board
  203. r=8;
  204. t=8;
  205. mine=10;
  206. break;
  207.  
  208. case 2:
  209. cout << "Play Intermediate Game\n";
  210. cout << "\n";
  211. system("cls"); //hides above text and leaves only the board
  212. r=16;
  213. t=16;
  214. mine=40;
  215. break;
  216. case 3:
  217. cout << "Play Advanced Game\n";
  218. cout << "\n";
  219. system("cls"); //hides above text and leaves only the board
  220. r=24;
  221. t=24;
  222. mine=99;
  223. break;
  224.  
  225. case 4:
  226. cout << "Show Score Board\n";
  227. cout << "\n";
  228. break;
  229.  
  230. case 5:
  231. cout << "Goodbye.\n";
  232. break;
  233. //if not 1-5, this brings up an error and restarts the menu
  234. default: ;
  235. system("cls");
  236. cout<< selection << " is not a valid menu item, please try again.\n";
  237. menu();
  238.  
  239. cout << endl;
  240. }
  241.  
  242. return;
  243. }
  244. //based on the input in menu, this generates the board and mines
  245. void array_board()
  246. {
  247. arraymine = new int*[r-1];
  248. for(int k=0;k<r;k++)
  249. arraymine[k] = new int[t-1];
  250.  
  251. arrayboard = new char*[r-1];
  252. for(int k=0;k<r;k++)
  253. arrayboard[k] = new char[t-1];
  254. }
  255. int main()
  256. {
  257. menu();
  258. array_board();
  259. mine_board();
  260. makemove();
  261.  
  262. system("pause");
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement